Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SqlCommandBuilder.cs « SqlClient « Data « System « System.Data « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7563b0c082c5fc00459745a2eac2cb7b336d00bb (plain)
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
//------------------------------------------------------------------------------
// <copyright file="SqlCommandBuilder.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.SqlClient {

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.Common;
    using System.Data.Sql;
    using System.Data.SqlTypes;
    using System.Diagnostics;
    using System.Globalization;
    using System.Text;
    using System.Runtime.CompilerServices;
    using System.Threading;

    public sealed class SqlCommandBuilder : DbCommandBuilder {

        public SqlCommandBuilder() : base() {
            GC.SuppressFinalize(this);
            base.QuotePrefix = "["; // initialize base with defaults
            base.QuoteSuffix = "]";
        }

        public SqlCommandBuilder(SqlDataAdapter adapter) : this() {
            DataAdapter = adapter;
        }

        /// <devnote>SqlServer only supports CatalogLocation.Start</devnote>
        [
        Browsable(false),
        EditorBrowsableAttribute(EditorBrowsableState.Never) ,
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override CatalogLocation CatalogLocation {
            get {
                return CatalogLocation.Start;
            }
            set {
                if (CatalogLocation.Start != value) {
                    throw ADP.SingleValuedProperty("CatalogLocation", "Start");
                }
            }
        }

        /// <devnote>SqlServer only supports '.'</devnote>
        [
        Browsable(false),
        EditorBrowsableAttribute(EditorBrowsableState.Never),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override string CatalogSeparator {
            get {
                return ".";
            }
            set {
                if ("." != value) {
                    throw ADP.SingleValuedProperty("CatalogSeparator", ".");
                }
            }
        }

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

        /// <devnote>SqlServer only supports '.'</devnote>
        [
        Browsable(false),
        EditorBrowsableAttribute(EditorBrowsableState.Never),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override string QuotePrefix {
            get {
                return base.QuotePrefix;
            }
            set {
                if (("[" != value) && ("\"" != value)){
                    throw ADP.DoubleValuedProperty("QuotePrefix", "[", "\"");
                }
                base.QuotePrefix = value;
            }
        }

        [
        Browsable(false),
        EditorBrowsableAttribute(EditorBrowsableState.Never),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override string QuoteSuffix {
            get {
                return base.QuoteSuffix;
            }
            set {
                if (("]" != value) && ("\"" != value)) {
                    throw ADP.DoubleValuedProperty("QuoteSuffix", "]", "\"");
                }
                base.QuoteSuffix = value;
            }
        }

        [
        Browsable(false),
        EditorBrowsableAttribute(EditorBrowsableState.Never),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
        ]
        public override string SchemaSeparator {
            get {
                return ".";
            }
            set {
                if ("." != value) {
                    throw ADP.SingleValuedProperty("SchemaSeparator",".");
                }
            }
        }

        private void SqlRowUpdatingHandler(object sender, SqlRowUpdatingEventArgs ruevent) {
            base.RowUpdatingHandler(ruevent);
        }

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

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

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

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

            if ((p.SqlDbType == SqlDbType.Udt) && !p.SourceColumnNullMapping) {
                p.UdtTypeName = datarow["DataTypeName"] as string;
            }
            else {
                p.UdtTypeName = String.Empty;
            }

            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);
            }
        }

        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 "@p" + parameterOrdinal.ToString(System.Globalization.CultureInfo.InvariantCulture);
        }

        private void ConsistentQuoteDelimiters(string quotePrefix, string quoteSuffix){

            Debug.Assert(quotePrefix == "\"" || quotePrefix == "[");
            if ((("\"" == quotePrefix) && ("\"" != quoteSuffix)) ||
                (("[" == quotePrefix) && ("]" != quoteSuffix))) {
                throw ADP.InvalidPrefixSuffix();
            }

        }
        static public void DeriveParameters(SqlCommand command) { // MDAC 65927\
            SqlConnection.ExecutePermission.Demand();

            if (null == command) {
                throw ADP.ArgumentNull("command");
            }
            TdsParser bestEffortCleanupTarget = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
#if DEBUG
                TdsParser.ReliabilitySection tdsReliabilitySection = new TdsParser.ReliabilitySection();

                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                    tdsReliabilitySection.Start();
#else
                {
#endif
                    bestEffortCleanupTarget = SqlInternalConnection.GetBestEffortCleanupTarget(command.Connection);
                    command.DeriveParameters();
                }
#if DEBUG
                finally {
                    tdsReliabilitySection.Stop();
                }
#endif
            }
            catch (System.OutOfMemoryException e) {
                if (null != command && null != command.Connection) {
                    command.Connection.Abort(e);
                }
                throw;
            }
            catch (System.StackOverflowException e) {             
                if (null != command && null != command.Connection) {
                    command.Connection.Abort(e);
                }
                throw;
            }
            catch (System.Threading.ThreadAbortException e)  {            
                if (null != command && null != command.Connection) {
                    command.Connection.Abort(e);
                }
                SqlInternalConnection.BestEffortCleanup(bestEffortCleanupTarget);
                throw;
            }
        }


/*        private static void GetLiteralInfo (DataRow dataTypeRow, out string literalPrefix, out string literalSuffix) {

            Object tempValue = dataTypeRow[DbMetaDataColumnNames.LiteralPrefix];
            if (tempValue == DBNull.Value) {
                literalPrefix = "";
            }
            else {
                literalPrefix = (string)dataTypeRow[DbMetaDataColumnNames.LiteralPrefix];
            }
            tempValue = dataTypeRow[DbMetaDataColumnNames.LiteralSuffix];
            if (tempValue == DBNull.Value) {
                literalSuffix = "";
            }
            else {
                literalSuffix = (string)dataTypeRow[DbMetaDataColumnNames.LiteralSuffix];
            }
        }
*/

        protected override DataTable GetSchemaTable (DbCommand srcCommand) {
            SqlCommand sqlCommand = srcCommand as SqlCommand;
            SqlNotificationRequest  notificationRequest     = sqlCommand.Notification;
            bool                    notificationAutoEnlist  = sqlCommand.NotificationAutoEnlist;

            sqlCommand.Notification             = null;
            sqlCommand.NotificationAutoEnlist   = false;

            try {
                using (SqlDataReader dataReader = sqlCommand.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo)){
                    return dataReader.GetSchemaTable();
                }
            }
            finally {
                sqlCommand.Notification             = notificationRequest;
                sqlCommand.NotificationAutoEnlist   = notificationAutoEnlist;
            }

        }

        protected override DbCommand InitializeCommand(DbCommand command) {
            SqlCommand cmd = (SqlCommand) base.InitializeCommand(command);
            cmd.NotificationAutoEnlist = false;
            return cmd;
        }

        public override string QuoteIdentifier(string unquotedIdentifier){
            ADP.CheckArgumentNull(unquotedIdentifier, "unquotedIdentifier");
            string quoteSuffixLocal = QuoteSuffix;
            string quotePrefixLocal = QuotePrefix;
            ConsistentQuoteDelimiters(quotePrefixLocal, quoteSuffixLocal);
            return ADP.BuildQuotedString(quotePrefixLocal,quoteSuffixLocal,unquotedIdentifier);;
        }

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

        public override string UnquoteIdentifier(string quotedIdentifier){

            ADP.CheckArgumentNull(quotedIdentifier, "quotedIdentifier");
            String unquotedIdentifier;
            string quoteSuffixLocal = QuoteSuffix;
            string quotePrefixLocal = QuotePrefix;
            ConsistentQuoteDelimiters(quotePrefixLocal, quoteSuffixLocal);
            // ignoring the return value becasue an unquoted source string is OK here
            ADP.RemoveStringQuotes(quotePrefixLocal, quoteSuffixLocal, quotedIdentifier, out unquotedIdentifier);
            return unquotedIdentifier;
       }
    }
}