1 //---------------------------------------------------------------------
2 // <copyright file="DbParameterHelper.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 namespace System.Data.EntityClient {
14 using System.ComponentModel;
16 using System.Data.Common;
17 using System.Data.Entity;
19 public sealed partial class EntityParameter : DbParameter {
20 private object _value;
22 private object _parent;
24 private ParameterDirection _direction;
28 private string _sourceColumn;
29 private DataRowVersion _sourceVersion;
30 private bool _sourceColumnNullMapping;
32 private bool? _isNullable;
34 private object _coercedValue;
36 private EntityParameter(EntityParameter source) : this() {
37 EntityUtil.CheckArgumentNull(source, "source");
39 source.CloneHelper(this);
41 ICloneable cloneable = (_value as ICloneable);
42 if (null != cloneable) {
43 _value = cloneable.Clone();
47 private object CoercedValue {
52 _coercedValue = value;
57 RefreshProperties(RefreshProperties.All),
58 EntityResCategoryAttribute(EntityRes.DataCategory_Data),
59 EntityResDescriptionAttribute(EntityRes.DbParameter_Direction),
61 override public ParameterDirection Direction {
63 ParameterDirection direction = _direction;
64 return ((0 != direction) ? direction : ParameterDirection.Input);
67 if (_direction != value) {
69 case ParameterDirection.Input:
70 case ParameterDirection.Output:
71 case ParameterDirection.InputOutput:
72 case ParameterDirection.ReturnValue:
77 throw EntityUtil.InvalidParameterDirection(value);
83 override public bool IsNullable {
85 bool result = this._isNullable.HasValue ? this._isNullable.Value : true;
100 EntityResCategoryAttribute(EntityRes.DataCategory_Data),
101 EntityResDescriptionAttribute(EntityRes.DbParameter_Size),
103 override public int Size {
105 int size = _size.HasValue ? _size.Value : 0;
107 size = ValueSize(Value);
112 if (!_size.HasValue || _size.Value != value) {
114 throw EntityUtil.InvalidSizeValue(value);
127 private void ResetSize() {
128 if (_size.HasValue) {
134 private bool ShouldSerializeSize() {
135 return (_size.HasValue && _size.Value != 0);
139 EntityResCategoryAttribute(EntityRes.DataCategory_Update),
140 EntityResDescriptionAttribute(EntityRes.DbParameter_SourceColumn),
142 override public string SourceColumn {
144 string sourceColumn = _sourceColumn;
145 return ((null != sourceColumn) ? sourceColumn : string.Empty);
148 _sourceColumn = value;
152 public override bool SourceColumnNullMapping {
154 return _sourceColumnNullMapping;
157 _sourceColumnNullMapping = value;
162 EntityResCategoryAttribute(EntityRes.DataCategory_Update),
163 EntityResDescriptionAttribute(EntityRes.DbParameter_SourceVersion),
165 override public DataRowVersion SourceVersion {
167 DataRowVersion sourceVersion = _sourceVersion;
168 return ((0 != sourceVersion) ? sourceVersion : DataRowVersion.Current);
172 case DataRowVersion.Original:
173 case DataRowVersion.Current:
174 case DataRowVersion.Proposed:
175 case DataRowVersion.Default:
176 _sourceVersion = value;
179 throw EntityUtil.InvalidDataRowVersion(value);
184 private void CloneHelperCore(EntityParameter destination) {
185 destination._value = _value;
187 destination._direction = _direction;
188 destination._size = _size;
190 destination._sourceColumn = _sourceColumn;
191 destination._sourceVersion = _sourceVersion;
192 destination._sourceColumnNullMapping = _sourceColumnNullMapping;
193 destination._isNullable = _isNullable;
196 internal void CopyTo(DbParameter destination) {
197 EntityUtil.CheckArgumentNull(destination, "destination");
198 CloneHelper((EntityParameter)destination);
201 internal object CompareExchangeParent(object value, object comparand) {
206 object parent = _parent;
207 if (comparand == parent) {
213 internal void ResetParent() {
217 override public string ToString() {
218 return ParameterName;
221 private byte ValuePrecisionCore(object value) {
222 if (value is Decimal) {
223 return ((System.Data.SqlTypes.SqlDecimal)(Decimal) value).Precision;
228 private byte ValueScaleCore(object value) {
229 if (value is Decimal) {
230 return (byte)((Decimal.GetBits((Decimal)value)[3] & 0x00ff0000) >> 0x10);
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;
241 byte[] bvalue = (value as byte[]);
242 if (null != bvalue) {
243 return bvalue.Length;
245 char[] cvalue = (value as char[]);
246 if (null != cvalue) {
247 return cvalue.Length;
249 if ((value is byte) || (value is char)) {