6d34f8a1a4b5fc4410bb9c5bd7ddd34b7acdc956
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / EntityClient / DbParameterHelper.cs
1 //---------------------------------------------------------------------
2 // <copyright file="DbParameterHelper.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner  Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10
11 namespace System.Data.EntityClient {
12
13     using System;
14     using System.ComponentModel;
15     using System.Data;
16     using System.Data.Common;
17     using System.Data.Entity;
18
19     public sealed partial class EntityParameter : DbParameter { 
20         private object _value;
21
22         private object _parent;
23
24         private ParameterDirection _direction;
25         private int? _size;
26
27
28         private string _sourceColumn;
29         private DataRowVersion _sourceVersion;
30         private bool _sourceColumnNullMapping;
31
32         private bool? _isNullable;
33
34         private object _coercedValue;
35
36         private EntityParameter(EntityParameter source) : this() { 
37             EntityUtil.CheckArgumentNull(source, "source");
38
39             source.CloneHelper(this);
40
41             ICloneable cloneable = (_value as ICloneable);
42             if (null != cloneable) { 
43                 _value = cloneable.Clone();
44             }
45         }
46
47         private object CoercedValue { 
48             get {
49                 return _coercedValue;
50             }
51             set {
52                 _coercedValue = value;
53             }
54         }
55
56         [
57         RefreshProperties(RefreshProperties.All),
58         EntityResCategoryAttribute(EntityRes.DataCategory_Data),
59         EntityResDescriptionAttribute(EntityRes.DbParameter_Direction),
60         ]
61         override public ParameterDirection Direction { 
62             get {
63                 ParameterDirection direction = _direction;
64                 return ((0 != direction) ? direction : ParameterDirection.Input);
65             }
66             set {
67                 if (_direction != value) {
68                     switch (value) { 
69                     case ParameterDirection.Input:
70                     case ParameterDirection.Output:
71                     case ParameterDirection.InputOutput:
72                     case ParameterDirection.ReturnValue:
73                         PropertyChanging();
74                         _direction = value;
75                         break;
76                     default:
77                         throw EntityUtil.InvalidParameterDirection(value);
78                     }
79                 }
80             }
81         }
82
83         override public bool IsNullable { 
84             get {
85                 bool result = this._isNullable.HasValue ? this._isNullable.Value : true;
86                 return result;
87             }
88             set {
89                 _isNullable = value;
90             }
91         }
92
93         internal int Offset {
94             get {
95                 return 0;
96             }
97         }
98
99         [
100         EntityResCategoryAttribute(EntityRes.DataCategory_Data),
101         EntityResDescriptionAttribute(EntityRes.DbParameter_Size),
102         ]
103         override public int Size { 
104             get {
105                 int size = _size.HasValue ? _size.Value : 0;
106                 if (0 == size) {
107                     size = ValueSize(Value);
108                 }
109                 return size;
110             }
111             set {
112                 if (!_size.HasValue || _size.Value != value) {
113                     if (value < -1) {
114                         throw EntityUtil.InvalidSizeValue(value);
115                     }
116                     PropertyChanging();
117                     if (0 == value) {
118                         _size = null;
119                     }
120                     else {
121                         _size = value;
122                     }
123                 }
124             }
125         }
126
127         private void ResetSize() {
128             if (_size.HasValue) {
129                 PropertyChanging();
130                 _size = null;
131             }
132         }
133
134         private bool ShouldSerializeSize() { 
135             return (_size.HasValue && _size.Value != 0);
136         }
137
138         [
139         EntityResCategoryAttribute(EntityRes.DataCategory_Update),
140         EntityResDescriptionAttribute(EntityRes.DbParameter_SourceColumn),
141         ]
142         override public string SourceColumn { 
143             get {
144                 string sourceColumn = _sourceColumn;
145                 return ((null != sourceColumn) ? sourceColumn : string.Empty);
146             }
147             set {
148                 _sourceColumn = value;
149             }
150         }
151
152         public override bool SourceColumnNullMapping {
153             get {
154                 return _sourceColumnNullMapping;
155             }
156             set {
157                 _sourceColumnNullMapping = value;
158             }
159         }
160
161         [
162         EntityResCategoryAttribute(EntityRes.DataCategory_Update),
163         EntityResDescriptionAttribute(EntityRes.DbParameter_SourceVersion),
164         ]
165         override public DataRowVersion SourceVersion { 
166             get {
167                 DataRowVersion sourceVersion = _sourceVersion;
168                 return ((0 != sourceVersion) ? sourceVersion : DataRowVersion.Current);
169             }
170             set {
171                 switch(value) { 
172                 case DataRowVersion.Original:
173                 case DataRowVersion.Current:
174                 case DataRowVersion.Proposed:
175                 case DataRowVersion.Default:
176                     _sourceVersion = value;
177                     break;
178                 default:
179                     throw EntityUtil.InvalidDataRowVersion(value);
180                 }
181             }
182         }
183
184         private void CloneHelperCore(EntityParameter destination) {
185             destination._value                     = _value;
186             
187             destination._direction                 = _direction;
188             destination._size                      = _size;
189
190             destination._sourceColumn              = _sourceColumn;
191             destination._sourceVersion             = _sourceVersion;
192             destination._sourceColumnNullMapping   = _sourceColumnNullMapping;
193             destination._isNullable                = _isNullable;
194         }
195         
196         internal void CopyTo(DbParameter destination) {
197             EntityUtil.CheckArgumentNull(destination, "destination");
198             CloneHelper((EntityParameter)destination);
199         }
200
201         internal object CompareExchangeParent(object value, object comparand) {
202             
203             
204             
205             
206             object parent = _parent;
207             if (comparand == parent) {
208                 _parent = value;
209             }
210             return parent;
211         }
212
213         internal void ResetParent() {
214             _parent = null;
215         }
216
217         override public string ToString() { 
218             return ParameterName;
219         }
220
221         private byte ValuePrecisionCore(object value) { 
222             if (value is Decimal) {
223                 return ((System.Data.SqlTypes.SqlDecimal)(Decimal) value).Precision; 
224             }
225             return 0;
226         }
227
228         private  byte ValueScaleCore(object value) { 
229             if (value is Decimal) {
230                 return (byte)((Decimal.GetBits((Decimal)value)[3] & 0x00ff0000) >> 0x10);
231             }
232             return 0;
233         }
234
235         private  int ValueSizeCore(object value) { 
236             if (!EntityUtil.IsNull(value)) {
237                 string svalue = (value as string);
238                 if (null != svalue) {
239                     return svalue.Length;
240                 }
241                 byte[] bvalue = (value as byte[]);
242                 if (null != bvalue) {
243                     return bvalue.Length;
244                 }
245                 char[] cvalue = (value as char[]);
246                 if (null != cvalue) {
247                     return cvalue.Length;
248                 }
249                 if ((value is byte) || (value is char)) {
250                     return 1;
251                 }
252             }
253             return 0;
254         }
255     }
256 }