File: DbCommandDefinition.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (269 lines) | stat: -rw-r--r-- 11,212 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
//------------------------------------------------------------------------------
// <copyright file="DbCommandDefinition.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner  Microsoft
// @backupOwner Microsoft
//------------------------------------------------------------------------------

using System.Diagnostics;
using System.Data.Metadata.Edm;

namespace System.Data.Common {

    /// <summary>
    /// A prepared command definition, can be cached and reused to avoid 
    /// repreparing a command.
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "Db")]
    public class DbCommandDefinition {

        private readonly ICloneable _prototype;

        /// <summary>
        /// Internal factory method to create the default Command Definition object
        /// based on a prototype command. The prototype command is cloned 
        /// before the protected constructor is invoked
        /// </summary>
        /// <param name="prototype">prototype DbCommand</param>
        /// <returns>the DbCommandDefinition</returns>
        internal static DbCommandDefinition CreateCommandDefinition(DbCommand prototype) {
            EntityUtil.CheckArgumentNull(prototype, "prototype");
            ICloneable cloneablePrototype = prototype as ICloneable;
            if (null == cloneablePrototype) {
                throw EntityUtil.CannotCloneStoreProvider();
            }
            DbCommand clonedPrototype = (DbCommand)(cloneablePrototype.Clone());
            return new DbCommandDefinition(clonedPrototype);
        }

        /// <summary>
        /// Protected constructor; the command is assumed to be a prototype
        /// that will be cloned on CreateCommand, and the cloned command will be executed.
        /// </summary>
        protected DbCommandDefinition(DbCommand prototype) {
            EntityUtil.CheckArgumentNull(prototype, "prototype");
            _prototype = prototype as ICloneable;
            if (null == _prototype) {
                throw EntityUtil.CannotCloneStoreProvider();
            }
        }

        /// <summary>
        /// Constructor overload for subclasses to use
        /// </summary>
        protected DbCommandDefinition() {
        }

        /// <summary>
        /// Create a DbCommand object from the definition, that can be executed.
        /// </summary>
        /// <returns></returns>
        public virtual DbCommand CreateCommand() {
            return (DbCommand)(_prototype.Clone());
        }

        internal static void PopulateParameterFromTypeUsage(DbParameter parameter, TypeUsage type, bool isOutParam)
        {
            EntityUtil.CheckArgumentNull(parameter, "parameter");
            EntityUtil.CheckArgumentNull(type, "type");
            
            // parameter.IsNullable - from the NullableConstraintAttribute value
            parameter.IsNullable = TypeSemantics.IsNullable(type);
            
            // parameter.ParameterName - set by the caller;
            // parameter.SourceColumn - not applicable until we have a data adapter;
            // parameter.SourceColumnNullMapping - not applicable until we have a data adapter;
            // parameter.SourceVersion - not applicable until we have a data adapter;
            // parameter.Value - left unset;
            // parameter.DbType - determined by the TypeMapping;
            // parameter.Precision - from the TypeMapping;
            // parameter.Scale - from the TypeMapping;
            // parameter.Size - from the TypeMapping;


            // type.EdmType may not be a primitive type here - e.g. the user specified
            // a complex or entity type when creating an ObjectParameter instance. To keep 
            // the same behavior we had in previous versions we let it through here. We will 
            // throw an exception later when actually invoking the stored procedure where we
            // don't allow parameters that are non-primitive.
            if(Helper.IsPrimitiveType(type.EdmType))
            {
                DbType dbType;
                if (TryGetDbTypeFromPrimitiveType((PrimitiveType)type.EdmType, out dbType))
                {
                    switch (dbType)
                    {
                        case DbType.Binary:
                            PopulateBinaryParameter(parameter, type, dbType, isOutParam);
                            break;
                        case DbType.DateTime:
                        case DbType.Time:
                        case DbType.DateTimeOffset:
                            PopulateDateTimeParameter(parameter, type, dbType);
                            break;
                        case DbType.Decimal:
                            PopulateDecimalParameter(parameter, type, dbType);
                            break;
                        case DbType.String:
                            PopulateStringParameter(parameter, type, isOutParam);
                            break;
                        default:
                            parameter.DbType = dbType;
                            break;
                    }
                }
            }
        }

        internal static bool TryGetDbTypeFromPrimitiveType(PrimitiveType type, out DbType dbType)
        {
            switch (type.PrimitiveTypeKind)
            {
                case PrimitiveTypeKind.Binary:
                    dbType = DbType.Binary; 
                    return true;
                case PrimitiveTypeKind.Boolean:
                    dbType = DbType.Boolean; 
                    return true;
                case PrimitiveTypeKind.Byte:
                    dbType = DbType.Byte; 
                    return true;
                case PrimitiveTypeKind.DateTime:
                    dbType = DbType.DateTime; 
                    return true;
                case PrimitiveTypeKind.Time:
                    dbType = DbType.Time; 
                    return true;
                case PrimitiveTypeKind.DateTimeOffset:
                    dbType = DbType.DateTimeOffset; 
                    return true;
                case PrimitiveTypeKind.Decimal:
                    dbType = DbType.Decimal; 
                    return true;
                case PrimitiveTypeKind.Double:
                    dbType = DbType.Double; 
                    return true;
                case PrimitiveTypeKind.Guid:
                    dbType = DbType.Guid; 
                    return true;
                case PrimitiveTypeKind.Single:
                    dbType = DbType.Single; 
                    return true;
                case PrimitiveTypeKind.SByte:
                    dbType = DbType.SByte; 
                    return true;
                case PrimitiveTypeKind.Int16:
                    dbType = DbType.Int16; 
                    return true;
                case PrimitiveTypeKind.Int32:
                    dbType = DbType.Int32; 
                    return true;
                case PrimitiveTypeKind.Int64:
                    dbType = DbType.Int64; 
                    return true;
                case PrimitiveTypeKind.String:
                    dbType = DbType.String; 
                    return true;
                default:
                    dbType = default(DbType);
                    return  false;
            }
        }

        private static void PopulateBinaryParameter(DbParameter parameter, TypeUsage type, DbType dbType, bool isOutParam)
        {
            parameter.DbType = dbType;

            // For each facet, set the facet value only if we have it, note that it's possible to not have
            // it in the case the facet value is null
            SetParameterSize(parameter, type, isOutParam);
        }

        private static void PopulateDecimalParameter (DbParameter parameter, TypeUsage type, DbType dbType)
        {
            parameter.DbType = dbType;
            IDbDataParameter dataParameter = (IDbDataParameter)parameter;

            // For each facet, set the facet value only if we have it, note that it's possible to not have
            // it in the case the facet value is null
            byte precision;
            byte scale;
            if (TypeHelpers.TryGetPrecision(type, out precision))
            {
                dataParameter.Precision = precision;
            }

            if (TypeHelpers.TryGetScale(type, out scale))
            {
                dataParameter.Scale = scale;
            }
        }

        private static void PopulateDateTimeParameter(DbParameter parameter, TypeUsage type, DbType dbType)
        {
            parameter.DbType = dbType;
            IDbDataParameter dataParameter = (IDbDataParameter)parameter;

            // For each facet, set the facet value only if we have it, note that it's possible to not have
            // it in the case the facet value is null
            byte precision;
            if (TypeHelpers.TryGetPrecision(type, out precision))
            {
                dataParameter.Precision = precision;
            }
        }


        private static void PopulateStringParameter(DbParameter parameter, TypeUsage type, bool isOutParam)
        {
            // For each facet, set the facet value only if we have it, note that it's possible to not have
            // it in the case the facet value is null
            bool unicode = true;
            bool fixedLength = false;

            if (!TypeHelpers.TryGetIsFixedLength(type, out fixedLength))
            {
                // If we can't get the fixed length facet value, then default to fixed length = false
                fixedLength = false;
            }

            if (!TypeHelpers.TryGetIsUnicode(type, out unicode))
            {
                // If we can't get the unicode facet value, then default to unicode = true
                unicode = true;
            }

            if (fixedLength)
            {
                parameter.DbType = (unicode ? DbType.StringFixedLength : DbType.AnsiStringFixedLength);
            }
            else
            {
                parameter.DbType = (unicode ? DbType.String : DbType.AnsiString);
            }

            SetParameterSize(parameter, type, isOutParam);
        }

        private static void SetParameterSize(DbParameter parameter, TypeUsage type, bool isOutParam)
        {
            // only set the size if the parameter has a specific size value.
            Facet maxLengthFacet;
            if (type.Facets.TryGetValue(DbProviderManifest.MaxLengthFacetName, true, out maxLengthFacet) && maxLengthFacet.Value != null)
            {
                // only set size if there is a specific size
                if (!Helper.IsUnboundedFacetValue(maxLengthFacet))
                {
                    parameter.Size = (int)maxLengthFacet.Value;
                }
                else if (isOutParam)
                {
                    // if it is store procedure parameter and it is unbounded set the size to max
                    parameter.Size = Int32.MaxValue;
                }
            }
        }
    }
}