File: ObjectParameter.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (337 lines) | stat: -rw-r--r-- 11,225 bytes parent folder | download | duplicates (8)
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
//---------------------------------------------------------------------
// <copyright file="ObjectParameter.cs" company="Microsoft">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupowner Microsoft
//---------------------------------------------------------------------

namespace System.Data.Objects
{
    using System;
    using System.Data;
    using System.Data.Common.CommandTrees;
    using System.Data.Metadata.Edm;
    using System.Diagnostics;

    /// <summary> 
    ///   This class represents a query parameter at the object layer, which consists
    ///   of a Name, a Type and a Value.
    /// </summary>
    public sealed class ObjectParameter
    {
        #region Static Methods

        // --------------
        // Static Methods
        // --------------

        #region ValidateParameterName

        /// <summary>
        ///   This internal method uses regular expression matching to ensure that the
        ///   specified parameter name is valid. Parameter names must start with a letter,
        ///   and may only contain letters (A-Z, a-z), numbers (0-9) and underscores (_).
        /// </summary>
        internal static bool ValidateParameterName (string name)
        {
            // Note: Parameter names must begin with a letter, and may contain only
            // letters, numbers and underscores.
            return DbCommandTree.IsValidParameterName(name);
        }

        #endregion

        #endregion

        #region Public Constructors

        // -------------------
        // Public Constructors
        // -------------------

        #region ObjectParameter (string, Type)

        /// <summary>
        ///   This constructor creates an unbound (i.e., value-less) parameter from the
        ///   specified name and type. The value can be set at any time through the 
        ///   public 'Value' property.
        /// </summary>
        /// <param name="name">
        ///   The parameter name.
        /// </param>
        /// <param name="type">
        ///   The CLR type of the parameter.
        /// </param>
        /// <returns>
        ///   A new unbound ObjectParameter instance.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   If the value of either argument is null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///   If the value of the name argument is invalid. Parameter names must start
        ///   with a letter and may only contain letters (A-Z, a-z), numbers (0-9) and 
        ///   underscores (_).
        /// </exception>
        public ObjectParameter (string name, Type type)
        {
            EntityUtil.CheckArgumentNull(name, "name");
            EntityUtil.CheckArgumentNull(type, "type");

            if (!ObjectParameter.ValidateParameterName(name))
            {
                throw EntityUtil.Argument(System.Data.Entity.Strings.ObjectParameter_InvalidParameterName(name), "name");
            }

            this._name  = name;
            this._type  = type;

            // If the parameter type is Nullable<>, we need to extract out the underlying
            // Nullable<> type argument.
            this._mappableType = System.Data.Objects.ELinq.TypeSystem.GetNonNullableType(this._type);
        }

        #endregion

        #region ObjectParameter (string, object)

        /// <summary>
        ///   This constructor creates a fully-bound (i.e., valued) parameter from the
        ///   specified name and value. The type is inferred from the initial value, but
        ///   the value can be changed at any time through the public 'Value' property.
        /// </summary>
        /// <param name="name">
        ///   The parameter name.
        /// </param>
        /// <param name="value">
        ///   The initial value (and inherently, type) of the parameter.
        /// </param>
        /// <returns>
        ///   A new fully-bound ObjectParameter instance.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///   If the value of either argument is null.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException">
        ///   If the value of the name argument is invalid. Parameter names must start
        ///   with a letter and may only contain letters (A-Z, a-z), numbers (0-9) and 
        ///   underscores (_).
        /// </exception>
        public ObjectParameter (string name, object value)
        {
            EntityUtil.CheckArgumentNull(name, "name");
            EntityUtil.CheckArgumentNull(value, "value");

            if (!ObjectParameter.ValidateParameterName(name))
            {
                throw EntityUtil.Argument(System.Data.Entity.Strings.ObjectParameter_InvalidParameterName(name), "name");
            }

            this._name  = name;
            this._type  = value.GetType();
            this._value = value;

            // If the parameter type is Nullable<>, we need to extract out the underlying
            // Nullable<> type argument.
            this._mappableType = System.Data.Objects.ELinq.TypeSystem.GetNonNullableType(this._type);
        }

        #endregion

        #endregion

        #region Private Constructors

        // -------------------
        // Copy Constructor
        // -------------------

        /// <summary>
        ///   This constructor is used by <see cref="ShallowCopy"/> to create a new ObjectParameter
        ///   with field values taken from the field values of an existing ObjectParameter.
        /// </summary>
        /// <param name="template">
        ///   The existing ObjectParameter instance from which field values should be taken.
        /// </param>
        /// <returns>
        ///   A new ObjectParameter instance with the same field values as the specified ObjectParameter
        /// </returns>
        private ObjectParameter(ObjectParameter template)
        {
            Debug.Assert(template != null, "Template ObjectParameter cannot be null");

            this._name = template._name;
            this._type = template._type;
            this._mappableType = template._mappableType;
            this._effectiveType = template._effectiveType;
            this._value = template._value;
        }
                
        #endregion

        #region Private Fields

        // --------------
        // Private Fields
        // --------------

        /// <summary>
        ///   The name of the parameter. Cannot be null and is immutable.
        /// </summary>
        private string _name;

        /// <summary>
        ///   The CLR type of the parameter. Cannot be null and is immutable.
        /// </summary>
        private Type _type;

        /// <summary>
        ///   The mappable CLR type of the parameter. Unless the parameter type is
        ///   Nullable, this type is equal to the parameter type. In the case of
        ///   Nullable parameters, this type is the underlying Nullable argument
        ///   type. Cannot be null and is immutable.
        /// </summary>
        private Type _mappableType;

        /// <summary>
        ///     Used to specify the exact metadata type of this parameter.
        ///     Typically null, can only be set using the internal <see cref="TypeUsage"/> property.
        /// </summary>
        private TypeUsage _effectiveType;

        /// <summary>
        ///   The value of the parameter. Does not need to be bound until execution
        ///   time and can be modified at any time.
        /// </summary>
        private object _value;

        #endregion

        #region Public Properties

        // -----------------
        // Public Properties
        // -----------------

        /// <summary>
        ///   The parameter name, which can only be set through a constructor.
        /// </summary>
        public string Name
        {
            get
            {
                return this._name;
            }
        }

        /// <summary>
        ///   The parameter type, which can only be set through a constructor.
        /// </summary>
        public Type ParameterType
        {
            get
            {
                return this._type;
            }
        }

        /// <summary>
        ///   The parameter value, which can be set at any time (and subsequently 
        ///   changed) before query execution. Note that type-checking is not 
        ///   enforced between the declared parameter type and the type of the 
        ///   specified value; such validation is left up to the underlying 
        ///   provider(s) at execution time.
        /// </summary>
        public object Value
        {
            get
            {
                return this._value;
            }

            set
            {
                this._value = value;
            }
        }

        #endregion

        #region Internal Properties

        // -------------------
        // Internal Properties
        // -------------------

        /// <summary>
        ///     Gets or sets a <see cref="TypeUsage"/> that specifies the exact
        ///     type of which the parameter value is considered an instance.
        /// </summary>
        internal TypeUsage TypeUsage
        {
            get
            {
                return _effectiveType;
            }

            set
            {
                Debug.Assert(null == _effectiveType, "Effective type should only be set once");
                _effectiveType = value;
            }
        }

        /// <summary>
        ///   The mappable parameter type; this is primarily used to handle the case of 
        ///   Nullable parameter types. For example, metadata knows nothing about 'int?', 
        ///   only 'Int32'. For internal use only.
        /// </summary>
        internal Type MappableType
        {
            get
            {
                return this._mappableType;
            }
        }
        
        #endregion

        #region Internal Methods

        // ----------------
        // Internal Methods
        // ----------------

        /// <summary>
        /// Creates a new ObjectParameter instance with identical field values to this instance.
        /// </summary>
        /// <returns>The new ObjectParameter instance</returns>
        internal ObjectParameter ShallowCopy()
        {
            return new ObjectParameter(this);
        }
                
        /// <summary>
        ///   This internal method ensures that the specified type is a scalar
        ///   type supported by the underlying provider by ensuring that scalar 
        ///   metadata for this type is retrievable.
        /// </summary>
        internal bool ValidateParameterType (ClrPerspective perspective)
        {
            TypeUsage type = null;

            // The parameter type metadata is only valid if it's scalar or enumeration type metadata.
            if ((perspective.TryGetType(this._mappableType, out type)) && 
                (TypeSemantics.IsScalarType(type)))
            {
                return true;
            }

            return false;
        }

        #endregion
    }
}