File: OleDbCommandBuilder.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (403 lines) | stat: -rw-r--r-- 20,145 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//------------------------------------------------------------------------------
// <copyright file="OleDbCommandBuilder.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <owner current="true" primary="false">Microsoft</owner>
//------------------------------------------------------------------------------

namespace System.Data.OleDb {

    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Common;
    using System.Data.ProviderBase;
    using System.Diagnostics;
    using System.Globalization;
    using System.Text;

    public sealed class OleDbCommandBuilder : DbCommandBuilder {

        public OleDbCommandBuilder() : base() {
            GC.SuppressFinalize(this);
        }

        public OleDbCommandBuilder(OleDbDataAdapter adapter) : this() {
            DataAdapter = adapter;
        }

        [
        DefaultValue(null),
        ResCategoryAttribute(Res.DataCategory_Update),
        ResDescriptionAttribute(Res.OleDbCommandBuilder_DataAdapter), // MDAC 60524
        ]
        new public OleDbDataAdapter DataAdapter {
            get {
                return (base.DataAdapter as OleDbDataAdapter);
            }
            set {
                base.DataAdapter = value;
            }
        }

        private void OleDbRowUpdatingHandler(object sender, OleDbRowUpdatingEventArgs ruevent) {
            RowUpdatingHandler(ruevent);
        }

        new public OleDbCommand GetInsertCommand() {
            return (OleDbCommand) base.GetInsertCommand();
        }
        new public OleDbCommand GetInsertCommand(bool useColumnsForParameterNames) {
            return (OleDbCommand) base.GetInsertCommand(useColumnsForParameterNames);
        }

        new public OleDbCommand GetUpdateCommand() {
            return (OleDbCommand) base.GetUpdateCommand();
        }
        new public OleDbCommand GetUpdateCommand(bool useColumnsForParameterNames) {
            return (OleDbCommand) base.GetUpdateCommand(useColumnsForParameterNames);
        }

        new public OleDbCommand GetDeleteCommand() {
            return (OleDbCommand) base.GetDeleteCommand();
        }
        new public OleDbCommand GetDeleteCommand(bool useColumnsForParameterNames) {
            return (OleDbCommand) base.GetDeleteCommand(useColumnsForParameterNames);
        }

        override protected string GetParameterName(int parameterOrdinal) {
            return "p" + parameterOrdinal.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }
        override protected string GetParameterName(string parameterName) {
            return parameterName;
        }

        override protected string GetParameterPlaceholder(int parameterOrdinal) {
            return "?";
        }

        override protected void ApplyParameterInfo(DbParameter parameter, DataRow datarow, StatementType statementType, bool whereClause) {
            OleDbParameter p = (OleDbParameter) parameter;
            object valueType = datarow[SchemaTableColumn.ProviderType];
            p.OleDbType = (OleDbType) valueType;

            object bvalue = datarow[SchemaTableColumn.NumericPrecision];
            if (DBNull.Value != bvalue) {
                byte bval = (byte)(short)bvalue;
                p.PrecisionInternal = ((0xff != bval) ? bval : (byte)0);
            }

            bvalue = datarow[SchemaTableColumn.NumericScale];
            if (DBNull.Value != bvalue) {
                byte bval = (byte)(short)bvalue;
                p.ScaleInternal = ((0xff != bval) ? bval : (byte)0);
            }
        }

        static public void DeriveParameters(OleDbCommand command) { // MDAC 65927
            OleDbConnection.ExecutePermission.Demand();

            if (null == command) {
                throw ADP.ArgumentNull("command");
            }
            switch (command.CommandType) {
            case System.Data.CommandType.Text:
                throw ADP.DeriveParametersNotSupported(command);
            case System.Data.CommandType.StoredProcedure:
                break;
            case System.Data.CommandType.TableDirect:
                // CommandType.TableDirect - do nothing, parameters are not supported
                throw ADP.DeriveParametersNotSupported(command);
            default:
                throw ADP.InvalidCommandType(command.CommandType);
            }
            if (ADP.IsEmpty(command.CommandText)) {
                throw ADP.CommandTextRequired(ADP.DeriveParameters);
            }
            OleDbConnection connection = command.Connection;
            if (null == connection) {
                throw ADP.ConnectionRequired(ADP.DeriveParameters);
            }
            ConnectionState state = connection.State;
            if (ConnectionState.Open != state) {
                throw ADP.OpenConnectionRequired(ADP.DeriveParameters, state);
            }
            OleDbParameter[] list = DeriveParametersFromStoredProcedure(connection, command);

            OleDbParameterCollection parameters = command.Parameters;
            parameters.Clear();

            for(int i = 0; i < list.Length; ++i) {
                parameters.Add(list[i]);
            }
        }

        // known difference: when getting the parameters for a sproc, the
        //   return value gets marked as a return value but for a sql stmt
        //   the return value gets marked as an output parameter.
        static private OleDbParameter[] DeriveParametersFromStoredProcedure(OleDbConnection connection, OleDbCommand command) {
            OleDbParameter[] plist = new OleDbParameter[0];

            if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedure_Parameters)) {
                string quotePrefix, quoteSuffix;
                connection.GetLiteralQuotes(ADP.DeriveParameters, out quotePrefix, out quoteSuffix);

                Object[] parsed = MultipartIdentifier.ParseMultipartIdentifier(command.CommandText, quotePrefix, quoteSuffix, '.', 4, true, Res.OLEDB_OLEDBCommandText, false); // MDAC 70930
                if (null == parsed[3]) {
                    throw ADP.NoStoredProcedureExists(command.CommandText);             
                }
                
                Object[] restrictions = new object[4];
                object value;

                // Parse returns an enforced 4 part array
                // 0) Server - ignored but removal would be a run-time breaking change from V1.0
                // 1) Catalog
                // 2) Schema
                // 3) ProcedureName

                // Restrictions array which is passed to OleDb API expects:
                // 0) Catalog
                // 1) Schema
                // 2) ProcedureName
                // 3) ParameterName (leave null)

                // Copy from Parse format to OleDb API format
                Array.Copy(parsed, 1, restrictions, 0, 3);

                //if (cmdConnection.IsServer_msdaora) {
                //    restrictions[1] = Convert.ToString(cmdConnection.UserId).ToUpper();
                //}
                DataTable table = connection.GetSchemaRowset(OleDbSchemaGuid.Procedure_Parameters, restrictions);

                if (null != table) {
                    DataColumnCollection columns = table.Columns;

                    DataColumn parameterName = null;
                    DataColumn parameterDirection = null;
                    DataColumn dataType = null;
                    DataColumn maxLen = null;
                    DataColumn numericPrecision = null;
                    DataColumn numericScale = null;
                    DataColumn backendtype = null;

                    int index = columns.IndexOf(ODB.PARAMETER_NAME);
                    if (-1 != index) parameterName = columns[index];

                    index = columns.IndexOf(ODB.PARAMETER_TYPE);
                    if (-1 != index) parameterDirection = columns[index];

                    index = columns.IndexOf(ODB.DATA_TYPE);
                    if (-1 != index) dataType = columns[index];

                    index = columns.IndexOf(ODB.CHARACTER_MAXIMUM_LENGTH);
                    if (-1 != index) maxLen = columns[index];

                    index = columns.IndexOf(ODB.NUMERIC_PRECISION);
                    if (-1 != index) numericPrecision = columns[index];

                    index = columns.IndexOf(ODB.NUMERIC_SCALE);
                    if (-1 != index) numericScale = columns[index];

                    index = columns.IndexOf(ODB.TYPE_NAME); // MDAC 72315
                    if (-1 != index) backendtype = columns[index];

                    DataRow[] dataRows = table.Select(null, ODB.ORDINAL_POSITION_ASC, DataViewRowState.CurrentRows); // MDAC 70928
                    plist = new OleDbParameter[dataRows.Length];
                    for(index = 0; index < dataRows.Length; ++index) {
                        DataRow dataRow = dataRows[index];

                        OleDbParameter parameter = new OleDbParameter();

                        if ((null != parameterName) && !dataRow.IsNull(parameterName, DataRowVersion.Default)) {
                            // $
                            parameter.ParameterName = Convert.ToString(dataRow[parameterName, DataRowVersion.Default], CultureInfo.InvariantCulture).TrimStart(new char[] { '@', ' ', ':'});
                        }
                        if ((null != parameterDirection) && !dataRow.IsNull(parameterDirection, DataRowVersion.Default)) {
                            short direction = Convert.ToInt16(dataRow[parameterDirection, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.Direction = ConvertToParameterDirection(direction);
                        }
                        if ((null != dataType) && !dataRow.IsNull(dataType, DataRowVersion.Default)) {
                            // need to ping FromDBType, otherwise WChar->WChar when the user really wants VarWChar
                            short wType = Convert.ToInt16(dataRow[dataType, DataRowVersion.Default], CultureInfo.InvariantCulture);
                            parameter.OleDbType = NativeDBType.FromDBType(wType, false, false).enumOleDbType;
                        }
                        if ((null != maxLen) && !dataRow.IsNull(maxLen, DataRowVersion.Default)) {
                            parameter.Size = Convert.ToInt32(dataRow[maxLen, DataRowVersion.Default], CultureInfo.InvariantCulture);
                        }
                        switch(parameter.OleDbType) {
                            case OleDbType.Decimal:
                            case OleDbType.Numeric:
                            case OleDbType.VarNumeric:
                                if ((null != numericPrecision) && !dataRow.IsNull(numericPrecision, DataRowVersion.Default)) {
                                    // @devnote: unguarded cast from Int16 to Byte
                                    parameter.PrecisionInternal = (Byte) Convert.ToInt16(dataRow[numericPrecision], CultureInfo.InvariantCulture);
                                }
                                if ((null != numericScale) && !dataRow.IsNull(numericScale, DataRowVersion.Default)) {
                                    // @devnote: unguarded cast from Int16 to Byte
                                    parameter.ScaleInternal = (Byte) Convert.ToInt16(dataRow[numericScale], CultureInfo.InvariantCulture);
                                }
                                break;
                            case OleDbType.VarBinary: // MDAC 72315
                            case OleDbType.VarChar:
                            case OleDbType.VarWChar:
                                value = dataRow[backendtype, DataRowVersion.Default];
                                if (value is string) {
                                    string backendtypename = ((string) value).ToLower(CultureInfo.InvariantCulture);
                                    switch(backendtypename) {
                                    case "binary":
                                        parameter.OleDbType = OleDbType.Binary;
                                        break;
                                    //case "varbinary":
                                    //    parameter.OleDbType = OleDbType.VarBinary;
                                    //    break;
                                    case "image":
                                        parameter.OleDbType = OleDbType.LongVarBinary;
                                        break;
                                    case "char":
                                        parameter.OleDbType = OleDbType.Char;
                                        break;
                                    //case "varchar":
                                    //case "varchar2":
                                    //    parameter.OleDbType = OleDbType.VarChar;
                                    //    break;
                                    case "text":
                                        parameter.OleDbType = OleDbType.LongVarChar;
                                        break;
                                    case "nchar":
                                        parameter.OleDbType = OleDbType.WChar;
                                        break;
                                    //case "nvarchar":
                                    //    parameter.OleDbType = OleDbType.VarWChar;
                                    case "ntext":
                                        parameter.OleDbType = OleDbType.LongVarWChar;
                                        break;
                                    }
                                }
                                break;
                        }
                        //if (AdapterSwitches.OleDbSql.TraceVerbose) {
                        //    ADP.Trace_Parameter("StoredProcedure", parameter);
                        //}
                        plist[index] = parameter;
                    }
                }
                if ((0 == plist.Length) && connection.SupportSchemaRowset(OleDbSchemaGuid.Procedures)) {
                    restrictions = new Object[4] { null, null, command.CommandText, null};
                    table = connection.GetSchemaRowset(OleDbSchemaGuid.Procedures, restrictions);
                    if (0 == table.Rows.Count) {
                        throw ADP.NoStoredProcedureExists(command.CommandText);
                    }
                }
            }
            else if (connection.SupportSchemaRowset(OleDbSchemaGuid.Procedures)) {
                Object[] restrictions = new Object[4] { null, null, command.CommandText, null};
                DataTable table = connection.GetSchemaRowset(OleDbSchemaGuid.Procedures, restrictions);
                if (0 == table.Rows.Count) {
                    throw ADP.NoStoredProcedureExists(command.CommandText);
                }
                // we don't ever expect a procedure with 0 parameters, they should have at least a return value
                throw ODB.NoProviderSupportForSProcResetParameters(connection.Provider); // MDAC 71968
            }
            else {
                throw ODB.NoProviderSupportForSProcResetParameters(connection.Provider); // MDAC 70918
            }
            return plist;
        }

        static private ParameterDirection ConvertToParameterDirection(int value) {
            switch (value) {
                case ODB.DBPARAMTYPE_INPUT:
                    return System.Data.ParameterDirection.Input;
                case ODB.DBPARAMTYPE_INPUTOUTPUT:
                    return System.Data.ParameterDirection.InputOutput;
                case ODB.DBPARAMTYPE_OUTPUT:
                    return System.Data.ParameterDirection.Output;
                case ODB.DBPARAMTYPE_RETURNVALUE:
                    return System.Data.ParameterDirection.ReturnValue;
                default:
                    return System.Data.ParameterDirection.Input;
            }
        }

        public override string QuoteIdentifier(string unquotedIdentifier){
            return QuoteIdentifier(unquotedIdentifier, null /* use DataAdapter.SelectCommand.Connection if available */);
        }
        public string QuoteIdentifier( string unquotedIdentifier, OleDbConnection connection){
            ADP.CheckArgumentNull(unquotedIdentifier, "unquotedIdentifier");

            // if the user has specificed a prefix use the user specified  prefix and suffix
            // otherwise get them from the provider
            string quotePrefix = QuotePrefix;
            string quoteSuffix = QuoteSuffix;
            if (ADP.IsEmpty(quotePrefix) == true) {
                if (connection == null) {
                    // VSTFDEVDIV 479567: use the adapter's connection if QuoteIdentifier was called from 
                    // DbCommandBuilder instance (which does not have an overload that gets connection object)
                    connection = base.GetConnection() as OleDbConnection;
                    if (connection == null) {
                        throw ADP.QuotePrefixNotSet(ADP.QuoteIdentifier);
                    }
                }
                connection.GetLiteralQuotes(ADP.QuoteIdentifier, out quotePrefix, out quoteSuffix);
                // if the quote suffix is null assume that it is the same as the prefix (See OLEDB spec
                // IDBInfo::GetLiteralInfo DBLITERAL_QUOTE_SUFFIX.)
                if (quoteSuffix == null) {
                    quoteSuffix = quotePrefix;
                }
            }

            return ADP.BuildQuotedString(quotePrefix,quoteSuffix,unquotedIdentifier);
        }

        override protected void SetRowUpdatingHandler(DbDataAdapter adapter) {
            Debug.Assert(adapter is OleDbDataAdapter, "!OleDbDataAdapter");
            if (adapter == base.DataAdapter) { // removal case
                ((OleDbDataAdapter)adapter).RowUpdating -= OleDbRowUpdatingHandler;
            }
            else { // adding case
                ((OleDbDataAdapter)adapter).RowUpdating += OleDbRowUpdatingHandler;
            }
        }

        public override string UnquoteIdentifier( string quotedIdentifier){
            return UnquoteIdentifier(quotedIdentifier, null /* use DataAdapter.SelectCommand.Connection if available */);
        }

        public string UnquoteIdentifier(string quotedIdentifier, OleDbConnection connection){

            ADP.CheckArgumentNull(quotedIdentifier, "quotedIdentifier");


            // if the user has specificed a prefix use the user specified  prefix and suffix
            // otherwise get them from the provider
            string quotePrefix = QuotePrefix;
            string quoteSuffix = QuoteSuffix;
            if (ADP.IsEmpty(quotePrefix) == true) {
                if (connection == null) {
                    // VSTFDEVDIV 479567: use the adapter's connection if UnquoteIdentifier was called from 
                    // DbCommandBuilder instance (which does not have an overload that gets connection object)
                    connection = base.GetConnection() as OleDbConnection;
                    if (connection == null) {
                        throw ADP.QuotePrefixNotSet(ADP.UnquoteIdentifier);
                    }
                }
                connection.GetLiteralQuotes(ADP.UnquoteIdentifier, out quotePrefix, out quoteSuffix);
                // if the quote suffix is null assume that it is the same as the prefix (See OLEDB spec
                // IDBInfo::GetLiteralInfo DBLITERAL_QUOTE_SUFFIX.)
                if (quoteSuffix == null) {
                    quoteSuffix = quotePrefix;
                }
            }

            String unquotedIdentifier;
            // ignoring the return value because it is acceptable for the quotedString to not be quoted in this
            // context.
            ADP.RemoveStringQuotes(quotePrefix, quoteSuffix, quotedIdentifier, out unquotedIdentifier);
            return unquotedIdentifier;

        }

    }
}