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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
|
//------------------------------------------------------------------------------
// <copyright file="OleDbParameter.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.OleDb {
using System;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.ProviderBase;
using System.Diagnostics;
using System.Globalization;
[
System.ComponentModel.TypeConverterAttribute(typeof(System.Data.OleDb.OleDbParameter.OleDbParameterConverter))
]
public sealed partial class OleDbParameter : DbParameter, ICloneable, IDbDataParameter {
private NativeDBType _metaType;
private int _changeID;
private string _parameterName;
private byte _precision;
private byte _scale;
private bool _hasScale;
private NativeDBType _coerceMetaType;
public OleDbParameter() : base() { // V1.0 nothing
}
public OleDbParameter(string name, object value) : this() { // MDAC 59521
Debug.Assert(!(value is OleDbType), "use OleDbParameter(string, OleDbType)");
Debug.Assert(!(value is SqlDbType), "use OleDbParameter(string, OleDbType)");
ParameterName = name;
Value = value;
}
public OleDbParameter(string name, OleDbType dataType) : this() {
ParameterName = name;
OleDbType = dataType;
}
public OleDbParameter(string name, OleDbType dataType, int size) : this() {
ParameterName = name;
OleDbType = dataType;
Size = size;
}
public OleDbParameter(string name, OleDbType dataType, int size, string srcColumn) : this() {
ParameterName = name;
OleDbType = dataType;
Size = size;
SourceColumn = srcColumn;
}
[ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
public OleDbParameter(string parameterName,
OleDbType dbType, int size,
ParameterDirection direction, Boolean isNullable,
Byte precision, Byte scale,
string srcColumn, DataRowVersion srcVersion,
object value) : this() { // V1.0 everything
ParameterName = parameterName;
OleDbType = dbType;
Size = size;
Direction = direction;
IsNullable = isNullable;
PrecisionInternal = precision;
ScaleInternal = scale;
SourceColumn = srcColumn;
SourceVersion = srcVersion;
Value = value;
}
[ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
public OleDbParameter(string parameterName,
OleDbType dbType, int size,
ParameterDirection direction,
Byte precision, Byte scale,
string sourceColumn, DataRowVersion sourceVersion, bool sourceColumnNullMapping,
object value) : this() { // V2.0 everything - round trip all browsable properties + precision/scale
ParameterName = parameterName;
OleDbType = dbType;
Size = size;
Direction = direction;
PrecisionInternal = precision;
ScaleInternal = scale;
SourceColumn = sourceColumn;
SourceVersion = sourceVersion;
SourceColumnNullMapping = sourceColumnNullMapping;
Value = value;
}
internal int ChangeID {
get {
return _changeID;
}
}
override public DbType DbType {
get {
return GetBindType(Value).enumDbType;
}
set {
NativeDBType dbtype = _metaType;
if ((null == dbtype) || (dbtype.enumDbType != value)) { // MDAC 63571
PropertyTypeChanging();
_metaType = NativeDBType.FromDbType(value);
}
}
}
public override void ResetDbType() {
ResetOleDbType();
}
[
RefreshProperties(RefreshProperties.All),
ResCategoryAttribute(Res.DataCategory_Data),
ResDescriptionAttribute(Res.OleDbParameter_OleDbType),
System.Data.Common.DbProviderSpecificTypePropertyAttribute(true),
]
public OleDbType OleDbType {
get {
return GetBindType(Value).enumOleDbType;
}
set {
NativeDBType dbtype = _metaType;
if ((null == dbtype) || (dbtype.enumOleDbType != value)) { // MDAC 63571
PropertyTypeChanging();
_metaType = NativeDBType.FromDataType(value);
}
}
}
private bool ShouldSerializeOleDbType() {
return (null != _metaType);
}
public void ResetOleDbType() {
if (null != _metaType) {
PropertyTypeChanging();
_metaType = null;
}
}
[
ResCategoryAttribute(Res.DataCategory_Data),
ResDescriptionAttribute(Res.DbParameter_ParameterName),
]
override public string ParameterName { // V1.2.3300, XXXParameter V1.0.3300
get {
string parameterName = _parameterName;
return ((null != parameterName) ? parameterName : ADP.StrEmpty);
}
set {
if (_parameterName != value) {
PropertyChanging();
_parameterName = value;
}
}
}
[DefaultValue((Byte)0)] // MDAC 65862
[ResCategoryAttribute(Res.DataCategory_Data)]
[ResDescriptionAttribute(Res.DbDataParameter_Precision)]
public new Byte Precision {
get {
return PrecisionInternal;
}
set {
PrecisionInternal = value;
}
}
internal byte PrecisionInternal {
get {
byte precision = _precision;
if (0 == precision) {
precision = ValuePrecision(Value);
}
return precision;
}
set {
if (_precision != value) {
PropertyChanging();
_precision = value;
}
}
}
private bool ShouldSerializePrecision() {
return (0 != _precision);
}
[DefaultValue((Byte)0)] // MDAC 65862
[ResCategoryAttribute(Res.DataCategory_Data)]
[ResDescriptionAttribute(Res.DbDataParameter_Scale)]
public new Byte Scale {
get {
return ScaleInternal;
}
set {
ScaleInternal = value;
}
}
internal byte ScaleInternal {
get {
byte scale = _scale;
if (!ShouldSerializeScale(scale)) { // WebData 94688
scale = ValueScale(Value);
}
return scale;
}
set {
if (_scale != value || !_hasScale) {
PropertyChanging();
_scale = value;
_hasScale = true;
}
}
}
private bool ShouldSerializeScale() {
return ShouldSerializeScale(_scale);
}
private bool ShouldSerializeScale(byte scale) {
return _hasScale && ((0 != scale) || ShouldSerializePrecision());
}
object ICloneable.Clone() {
return new OleDbParameter(this);
}
private void CloneHelper(OleDbParameter destination) {
CloneHelperCore(destination);
destination._metaType = _metaType;
destination._parameterName = _parameterName;
destination._precision = _precision;
destination._scale = _scale;
destination._hasScale = _hasScale;
}
private void PropertyChanging() {
unchecked { _changeID++; }
}
private void PropertyTypeChanging() {
PropertyChanging();
_coerceMetaType = null;
CoercedValue = null;
}
// goal: call virtual property getters only once per parameter
internal bool BindParameter(int index, Bindings bindings) {
int changeID = _changeID;
object value = Value;
NativeDBType dbtype = GetBindType(value);
if (OleDbType.Empty == dbtype.enumOleDbType) {
throw ODB.UninitializedParameters(index, dbtype.enumOleDbType);
}
_coerceMetaType = dbtype;
value = CoerceValue(value, dbtype);
CoercedValue = value;
ParameterDirection direction = Direction;
byte precision;
if (ShouldSerializePrecision()) {
precision = PrecisionInternal;
}
else {
precision = ValuePrecision(value);
}
if (0 == precision) {
precision = dbtype.maxpre;
}
byte scale;
if (ShouldSerializeScale()) {
scale = ScaleInternal;
}
else {
scale = ValueScale(value);
}
int wtype = dbtype.wType;
int bytecount, size;
if (dbtype.islong) { // long data (image, text, ntext)
bytecount = ADP.PtrSize;
if (ShouldSerializeSize()) {
size = Size;
}
else {
if (NativeDBType.STR == dbtype.dbType) {
size = Int32.MaxValue; // WebData 98940
}
else if (NativeDBType.WSTR == dbtype.dbType) {
size = Int32.MaxValue/2;
}
else {
size = Int32.MaxValue;
}
}
wtype |= NativeDBType.BYREF;
}
else if (dbtype.IsVariableLength) { // variable length data (varbinary, varchar, nvarchar)
if (!ShouldSerializeSize() && ADP.IsDirection(this, ParameterDirection.Output)) {
throw ADP.UninitializedParameterSize(index, _coerceMetaType.dataType);
}
bool computedSize;
if (ShouldSerializeSize()) {
size = Size;
computedSize = false;
}
else {
size = ValueSize(value);
computedSize = true;
}
if (0 < size) {
if (NativeDBType.WSTR == dbtype.wType) {
// maximum 0x3FFFFFFE characters, computed this way to avoid overflow exception
bytecount = Math.Min(size, 0x3FFFFFFE) * 2 + 2;
}
else {
Debug.Assert(NativeDBType.STR != dbtype.wType, "should have ANSI binding, describing is okay");
bytecount = size;
}
if (computedSize) {
if (NativeDBType.STR == dbtype.dbType) { // WebData 98140
// maximum 0x7ffffffe characters, computed this way to avoid overflow exception
size = Math.Min(size, 0x3FFFFFFE) * 2;
}
}
if (ODB.LargeDataSize < bytecount) {
bytecount = ADP.PtrSize;
wtype |= NativeDBType.BYREF;
}
}
else if (0 == size) {
if (NativeDBType.WSTR == wtype) { // allow space for null termination character
bytecount = 2;
// 0 == size, okay for (STR == dbType)
}
else {
Debug.Assert(NativeDBType.STR != dbtype.wType, "should have ANSI binding, describing is okay");
bytecount = 0;
}
}
else if (-1 == size) {
bytecount = ADP.PtrSize;
wtype |= NativeDBType.BYREF;
}
else {
throw ADP.InvalidSizeValue(size);
}
}
else { // fixed length data
bytecount = dbtype.fixlen;
size = bytecount;
}
bindings.CurrentIndex = index;
// tagDBPARAMBINDINFO info for SetParameterInfo
bindings.DataSourceType = dbtype.dbString.DangerousGetHandle(); // NOTE: This is a constant and isn't exposed publicly, so there really isn't a potential for Handle Recycling.
bindings.Name = ADP.PtrZero;
bindings.ParamSize = new IntPtr(size);
bindings.Flags = GetBindFlags(direction);
//bindings.Precision = precision;
//bindings.Scale = scale;
// tagDBBINDING info for CreateAccessor
bindings.Ordinal = (IntPtr)(index+1);
bindings.Part = dbtype.dbPart;
bindings.ParamIO = GetBindDirection(direction);
bindings.Precision = precision;
bindings.Scale = scale;
bindings.DbType = wtype;
bindings.MaxLen = bytecount; // also increments databuffer size (uses DbType)
//bindings.ValueOffset = bindings.DataBufferSize; // set via MaxLen
//bindings.LengthOffset = i * sizeof_int64;
//bindings.StatusOffset = i * sizeof_int64 + sizeof_int32;
//bindings.TypeInfoPtr = 0;
//bindings.ObjectPtr = 0;
//bindings.BindExtPtr = 0;
//bindings.MemOwner = /*DBMEMOWNER_CLIENTOWNED*/0;
//bindings.Flags = 0;
//bindings.ParameterChangeID = changeID; // bind until something changes
Debug.Assert(_changeID == changeID, "parameter has unexpectedly changed");
if (Bid.AdvancedOn) {
Bid.Trace("<oledb.struct.tagDBPARAMBINDINFO|INFO|ADV> index=%d, parameterName='%ls'\n", index, ParameterName);//, bindings.BindInfo[index]);
Bid.Trace("<oledb.struct.tagDBBINDING|INFO|ADV>\n");//, bindings.DBBinding[index]);
}
return IsParameterComputed();
}
private static object CoerceValue(object value, NativeDBType destinationType) {
Debug.Assert(null != destinationType, "null destinationType");
if ((null != value) && (DBNull.Value != value) && (typeof(object) != destinationType.dataType)) {
Type currentType = value.GetType();
if (currentType != destinationType.dataType) {
try {
if ((typeof(string) == destinationType.dataType) && (typeof(char[]) == currentType)) {
}
else if ((NativeDBType.CY == destinationType.dbType) && (typeof(string) == currentType)) {
value = Decimal.Parse((string)value, NumberStyles.Currency, (IFormatProvider)null); // WebData 99376
}
else {
value = Convert.ChangeType(value, destinationType.dataType, (IFormatProvider)null);
}
}
catch (Exception e) {
//
if (!ADP.IsCatchableExceptionType(e)) {
throw;
}
throw ADP.ParameterConversionFailed(value, destinationType.dataType, e); // WebData 75433
}
}
}
return value;
}
private NativeDBType GetBindType(object value) {
NativeDBType dbtype = _metaType;
if (null == dbtype) {
if (ADP.IsNull(value)) {
dbtype = OleDb.NativeDBType.Default;
}
else {
dbtype = NativeDBType.FromSystemType(value);
}
}
return dbtype;
}
internal object GetCoercedValue() {
object value = CoercedValue; // will also be set during binding, will rebind everytime if _metaType not set
if (null == value) {
value = CoerceValue(Value, _coerceMetaType);
CoercedValue = value;
}
return value;
}
internal bool IsParameterComputed() {
NativeDBType metaType = _metaType;
return ((null == metaType)
|| (!ShouldSerializeSize() && metaType.IsVariableLength)
|| ((NativeDBType.DECIMAL == metaType.dbType) || (NativeDBType.NUMERIC == metaType.dbType)
&& (!ShouldSerializeScale() || !ShouldSerializePrecision())
)
); // MDAC 69299
}
// @devnote: use IsParameterComputed which is called in the normal case
// only to call Prepare to throw the specialized error message
// reducing the overall number of methods to actually jit
internal void Prepare(OleDbCommand cmd) { // MDAC 70232
Debug.Assert(IsParameterComputed(), "Prepare computed parameter");
if (null == _metaType) {
throw ADP.PrepareParameterType(cmd);
}
else if (!ShouldSerializeSize() && _metaType.IsVariableLength) {
throw ADP.PrepareParameterSize(cmd);
}
else if (!ShouldSerializePrecision() && !ShouldSerializeScale() && ((NativeDBType.DECIMAL == _metaType.wType) || (NativeDBType.NUMERIC == _metaType.wType))) { // MDAC 71441
throw ADP.PrepareParameterScale(cmd, _metaType.wType.ToString("G", CultureInfo.InvariantCulture));
}
// Disabling the assert, see Dev11 775862 for details: http://vstfdevdiv.redmond.corp.microsoft.com:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=775862
// Debug.Assert(false, "OleDbParameter.Prepare didn't throw");
}
[
RefreshProperties(RefreshProperties.All),
ResCategoryAttribute(Res.DataCategory_Data),
ResDescriptionAttribute(Res.DbParameter_Value),
TypeConverterAttribute(typeof(StringConverter)),
]
override public object Value { // V1.2.3300, XXXParameter V1.0.3300
get {
return _value;
}
set {
_coercedValue = null;
_value = value;
}
}
private byte ValuePrecision(object value) {
return ValuePrecisionCore(value);
}
private byte ValueScale(object value) {
return ValueScaleCore(value);
}
private int ValueSize(object value) {
return ValueSizeCore(value);
}
static private int GetBindDirection(ParameterDirection direction) {
return (ODB.ParameterDirectionFlag & (int)direction);
/*switch(Direction) {
default:
case ParameterDirection.Input:
return ODB.DBPARAMIO_INPUT;
case ParameterDirection.Output:
case ParameterDirection.ReturnValue:
return ODB.DBPARAMIO_OUTPUT;
case ParameterDirection.InputOutput:
return (ODB.DBPARAMIO_INPUT | ODB.DBPARAMIO_OUTPUT);
}*/
}
static private int GetBindFlags(ParameterDirection direction) {
return (ODB.ParameterDirectionFlag & (int)direction);
/*switch(Direction) {
default:
case ParameterDirection.Input:
return ODB.DBPARAMFLAGS_ISINPUT;
case ParameterDirection.Output:
case ParameterDirection.ReturnValue:
return ODB.DBPARAMFLAGS_ISOUTPUT;
case ParameterDirection.InputOutput:
return (ODB.DBPARAMFLAGS_ISINPUT | ODB.DBPARAMFLAGS_ISOUTPUT);
}*/
}
// implemented as nested class to take advantage of the private/protected ShouldSerializeXXX methods
sealed internal class OleDbParameterConverter : System.ComponentModel.ExpandableObjectConverter {
// converter classes should have public ctor
public OleDbParameterConverter() {
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) {
if (typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType) {
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (null == destinationType) {
throw ADP.ArgumentNull("destinationType");
}
if ((typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) == destinationType) && (value is OleDbParameter)) {
return ConvertToInstanceDescriptor(value as OleDbParameter);
}
return base.ConvertTo(context, culture, value, destinationType);
}
private System.ComponentModel.Design.Serialization.InstanceDescriptor ConvertToInstanceDescriptor(OleDbParameter p) {
int flags = 0;
if (p.ShouldSerializeOleDbType()) {
flags |= 1;
}
if (p.ShouldSerializeSize()) {
flags |= 2;
}
if (!ADP.IsEmpty(p.SourceColumn)) {
flags |= 4;
}
if (null != p.Value) {
flags |= 8;
}
if ((ParameterDirection.Input != p.Direction) || p.IsNullable
|| p.ShouldSerializePrecision() || p.ShouldSerializeScale()
|| (DataRowVersion.Current != p.SourceVersion)) {
flags |= 16; // V1.0 everything
}
if (p.SourceColumnNullMapping) {
flags |= 32; // v2.0 everything
}
Type[] ctorParams;
object[] ctorValues;
switch(flags) {
case 0: // ParameterName
case 1: // OleDbType
ctorParams = new Type[] { typeof(string), typeof(OleDbType) };
ctorValues = new object[] { p.ParameterName, p.OleDbType };
break;
case 2: // Size
case 3: // Size, OleDbType
ctorParams = new Type[] { typeof(string), typeof(OleDbType), typeof(int) };
ctorValues = new object[] { p.ParameterName, p.OleDbType, p.Size };
break;
case 4: // SourceColumn
case 5: // SourceColumn, OleDbType
case 6: // SourceColumn, Size
case 7: // SourceColumn, Size, OleDbType
ctorParams = new Type[] { typeof(string), typeof(OleDbType), typeof(int), typeof(string) };
ctorValues = new object[] { p.ParameterName, p.OleDbType, p.Size, p.SourceColumn };
break;
case 8: // Value
ctorParams = new Type[] { typeof(string), typeof(object) };
ctorValues = new object[] { p.ParameterName, p.Value };
break;
default: // everything else
if (0 == (32 & flags)) { // V1.0 everything
ctorParams = new Type[] {
typeof(string), typeof(OleDbType), typeof(int), typeof(ParameterDirection),
typeof(bool), typeof(byte), typeof(byte), typeof(string),
typeof(DataRowVersion), typeof(object) };
ctorValues = new object[] {
p.ParameterName, p.OleDbType, p.Size, p.Direction,
p.IsNullable, p.PrecisionInternal, p.ScaleInternal, p.SourceColumn,
p.SourceVersion, p.Value };
}
else { // v2.0 everything - round trip all browsable properties + precision/scale
ctorParams = new Type[] {
typeof(string), typeof(OleDbType), typeof(int), typeof(ParameterDirection),
typeof(byte), typeof(byte),
typeof(string), typeof(DataRowVersion), typeof(bool),
typeof(object) };
ctorValues = new object[] {
p.ParameterName, p.OleDbType, p.Size, p.Direction,
p.PrecisionInternal, p.ScaleInternal,
p.SourceColumn, p.SourceVersion, p.SourceColumnNullMapping,
p.Value };
}
break;
}
System.Reflection.ConstructorInfo ctor = typeof(OleDbParameter).GetConstructor(ctorParams);
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(ctor, ctorValues);
}
}
}
}
|