disable broken tests on net_4_0
[mcs.git] / class / IBM.Data.DB2 / IBM.Data.DB2 / DB2Parameter.cs
blob42bd73cb2261c2630a8a6dcb981d82077aff0192
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 using System;
23 using System.Data;
24 using System.Runtime.InteropServices;
26 namespace IBM.Data.DB2
29 public sealed class DB2Parameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
31 private DbType dbType = DbType.Object;
32 private DB2Type db2Type = DB2Type.Invalid;
33 private short db2DataType = DB2Constants.SQL_UNKNOWN_TYPE;
34 private short db2LastUsedDataType = DB2Constants.SQL_UNKNOWN_TYPE;
36 private ParameterDirection direction;
37 private short db2Direction = DB2Constants.SQL_PARAM_INPUT;
38 private bool nullable = true;
39 private string parameterName;
40 private string sourceColumn;
41 private DataRowVersion sourceVersion;
42 private object dataVal;
43 private byte scale, precision;
44 private int size;
45 internal IntPtr internalBuffer;
46 internal IntPtr internalLengthBuffer;
47 internal int requiredMemory;
49 #region Contructors and destructors
50 public DB2Parameter()
52 direction = ParameterDirection.Input;
53 sourceVersion = DataRowVersion.Current;
56 public DB2Parameter(string name, DB2Type type)
58 direction = ParameterDirection.Input;
59 sourceVersion = DataRowVersion.Current;
60 this.ParameterName = name;
61 this.DB2Type = type;
64 public DB2Parameter(string name, DB2Type type, int size)
66 direction = ParameterDirection.Input;
67 sourceVersion = DataRowVersion.Current;
68 this.ParameterName = name;
69 this.DB2Type = type;
70 this.Size = size;
73 public DB2Parameter(string name, DB2Type type, int size, string sourceColumn)
75 direction = ParameterDirection.Input;
76 sourceVersion = DataRowVersion.Current;
77 this.ParameterName = name;
78 this.DB2Type = type;
79 this.Size = size;
80 this.SourceColumn = sourceColumn;
83 public DB2Parameter(string parameterName, object value)
85 direction = ParameterDirection.Input;
86 sourceVersion = DataRowVersion.Current;
87 this.ParameterName = parameterName;
88 this.Value = value;
91 public DB2Parameter(string parameterName, DB2Type db2Type, int size, ParameterDirection parameterDirection, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
93 this.ParameterName = parameterName;
94 this.DB2Type = db2Type;
95 this.Size = size;
96 this.Direction = parameterDirection;
97 this.IsNullable = isNullable;
98 this.Precision = precision;
99 this.Scale = scale;
100 this.SourceColumn = srcColumn;
101 this.SourceVersion = srcVersion;
102 this.Value = value;
105 #endregion
107 #region Properties
108 #region DbType Property
109 public DB2Type DB2Type
111 get
113 return db2Type;
115 set
117 db2Type = value;
118 switch(db2Type)
120 case DB2Type.Invalid: dbType = DbType.Object; db2DataType = DB2Constants.SQL_UNKNOWN_TYPE; break;
121 case DB2Type.SmallInt: dbType = DbType.Int16; db2DataType = DB2Constants.SQL_SMALLINT; break;
122 case DB2Type.Integer: dbType = DbType.Int32; db2DataType = DB2Constants.SQL_INTEGER; break;
123 case DB2Type.BigInt: dbType = DbType.Int64; db2DataType = DB2Constants.SQL_BIGINT; break;
124 case DB2Type.Real: dbType = DbType.Single; db2DataType = DB2Constants.SQL_REAL; break;
125 case DB2Type.Double: dbType = DbType.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;
126 case DB2Type.Float: dbType = DbType.Single; db2DataType = DB2Constants.SQL_REAL; break;
127 case DB2Type.Decimal: dbType = DbType.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
128 case DB2Type.Numeric: dbType = DbType.VarNumeric; db2DataType = DB2Constants.SQL_WCHAR; break;
129 case DB2Type.Date: dbType = DbType.Date; db2DataType = DB2Constants.SQL_TYPE_DATE; break;
130 case DB2Type.Time: dbType = DbType.Time; db2DataType = DB2Constants.SQL_TYPE_TIME; break;
131 case DB2Type.Timestamp: dbType = DbType.DateTime; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;
132 case DB2Type.Char: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
133 case DB2Type.VarChar: dbType = DbType.StringFixedLength; db2DataType = DB2Constants.SQL_WCHAR; break;
134 case DB2Type.LongVarChar: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
135 case DB2Type.Binary: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
136 case DB2Type.VarBinary: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
137 case DB2Type.LongVarBinary: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
138 case DB2Type.Graphic: dbType = DbType.StringFixedLength; db2DataType = DB2Constants.SQL_WCHAR; break;
139 case DB2Type.VarGraphic: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
140 case DB2Type.LongVarGraphic: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
141 case DB2Type.Clob: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
142 case DB2Type.Blob: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
143 case DB2Type.DbClob: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
144 case DB2Type.Datalink: dbType = DbType.Byte; db2DataType = DB2Constants.SQL_VARBINARY; break;
145 case DB2Type.RowId: dbType = DbType.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
146 case DB2Type.XmlReader: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
147 default:
148 throw new NotSupportedException("Value is of unknown data type");
153 /// Parameter data type
154 ///
155 public DbType DbType
157 get
159 return dbType;
161 set
163 dbType = value;
164 switch(dbType)
166 case DbType.AnsiString: db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;
167 case DbType.AnsiStringFixedLength: db2Type = DB2Type.Char; db2DataType = DB2Constants.SQL_WCHAR; break;
168 case DbType.Binary: db2Type = DB2Type.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
169 case DbType.Boolean: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_BIT; break;
170 case DbType.Byte: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;
171 case DbType.Currency: db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
172 case DbType.Date: db2Type = DB2Type.Date; db2DataType = DB2Constants.SQL_TYPE_DATE; break;
173 case DbType.DateTime: db2Type = DB2Type.Timestamp; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;
174 case DbType.Decimal: db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
175 case DbType.Double: db2Type = DB2Type.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;
176 case DbType.Guid: db2Type = DB2Type.Binary; db2DataType = DB2Constants.SQL_WCHAR; break;
177 case DbType.Int16: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;
178 case DbType.Int32: db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;
179 case DbType.Int64: db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;
180 case DbType.Object: db2Type = DB2Type.Invalid; db2DataType = DB2Constants.SQL_UNKNOWN_TYPE; break;
181 case DbType.SByte: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;
182 case DbType.Single: db2Type = DB2Type.Float; db2DataType = DB2Constants.SQL_REAL; break;
183 case DbType.String: db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;
184 case DbType.StringFixedLength: db2Type = DB2Type.Char; db2DataType = DB2Constants.SQL_WCHAR; break;
185 case DbType.Time: db2Type = DB2Type.Time; db2DataType = DB2Constants.SQL_TYPE_TIME; break;
186 case DbType.UInt16: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;
187 case DbType.UInt32: db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;
188 case DbType.UInt64: db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;
189 case DbType.VarNumeric: db2Type = DB2Type.Numeric; db2DataType = DB2Constants.SQL_WCHAR; break;
190 default:
191 throw new NotSupportedException("Value is of unknown data type");
196 #endregion
197 #region Direction
199 /// In or out parameter, or both
200 ///
201 public ParameterDirection Direction
203 get
205 return direction;
207 set
209 direction = value;
210 switch(direction)
212 default:
213 case ParameterDirection.Input: db2Direction = DB2Constants.SQL_PARAM_INPUT; break;
214 case ParameterDirection.Output: db2Direction = DB2Constants.SQL_PARAM_OUTPUT; break;
215 case ParameterDirection.InputOutput: db2Direction = DB2Constants.SQL_PARAM_INPUT_OUTPUT; break;
216 case ParameterDirection.ReturnValue: db2Direction = DB2Constants.SQL_RETURN_VALUE; break;
220 #endregion
221 #region IsNullable
223 /// Does this parameter support a null value
224 ///
225 public bool IsNullable
227 get
229 return nullable;
231 set
233 nullable = value;
236 #endregion
237 #region ParameterName
238 public string ParameterName
240 get
242 return parameterName;
244 set
246 parameterName = value;
249 #endregion
250 #region SourceColumn
252 /// Gets or sets the name of the source column that is mapped to the DataSet
253 ///
254 public string SourceColumn
256 get
258 return sourceColumn;
260 set
262 sourceColumn = value;
265 #endregion
266 #region SourceVersion
268 /// DataRowVersion property
269 ///
270 public DataRowVersion SourceVersion
272 get
274 return sourceVersion;
276 set
278 sourceVersion = value;
281 #endregion
282 #region IDbDataParameter properties
283 public byte Precision
285 get
287 return precision;
289 set
291 precision = value;
295 public byte Scale
297 get
299 return scale;
301 set
303 scale = value;
307 public int Size
309 get
311 return size;
313 set
315 size = value;
318 #endregion
319 #region Value
321 /// The actual parameter data
322 ///
323 public object Value
327 return dataVal;
329 set
331 this.dataVal = value;
334 #endregion
335 #endregion
337 #region inferType Method
338 /// <summary>
339 /// Determine the data type based on the value
340 /// </summary>
341 private void InferType()
343 if(Value == null)
344 throw new ArgumentException("No DB2Parameter.Value found");
346 if(Value is IConvertible)
348 switch(((IConvertible)Value).GetTypeCode())
350 case TypeCode.Char: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_WCHAR; break;
351 case TypeCode.Boolean: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_BIT; break;
352 case TypeCode.SByte:
353 case TypeCode.Byte: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;
354 case TypeCode.UInt16:
355 case TypeCode.Int16: dbType = DbType.Int16; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;
356 case TypeCode.UInt32:
357 case TypeCode.Int32: dbType = DbType.Int32; db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;
358 case TypeCode.UInt64:
359 case TypeCode.Int64: dbType = DbType.Int64; db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;
360 case TypeCode.Single: dbType = DbType.Single; db2Type = DB2Type.Float; db2DataType = DB2Constants.SQL_REAL; break;
361 case TypeCode.Double: dbType = DbType.Double; db2Type = DB2Type.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;
362 case TypeCode.Decimal: dbType = DbType.Decimal; db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
363 case TypeCode.DateTime: dbType = DbType.DateTime; db2Type = DB2Type.Timestamp; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;
364 case TypeCode.String: dbType = DbType.String; db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;
366 case TypeCode.Object:
367 case TypeCode.DBNull:
368 case TypeCode.Empty:
369 throw new SystemException("Unknown data type");
370 default:
371 throw new SystemException("Value is of unknown data type");
374 else if(Value is byte[])
376 dbType = DbType.Binary;
377 db2Type = DB2Type.VarBinary;
378 db2DataType = DB2Constants.SQL_VARBINARY;
380 else if(Value is TimeSpan)
382 dbType = DbType.Time;
383 db2Type = DB2Type.Time;
384 db2DataType = DB2Constants.SQL_TYPE_TIME;
386 else
388 throw new NotSupportedException("Value is of unsupported data type");
391 #endregion
393 internal void CalculateRequiredmemory()
395 //if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
397 // if(Value == null)
398 // throw new ArgumentException("Value missing");
400 if(dbType == DbType.Object)
402 if((direction == ParameterDirection.Output) || (direction == ParameterDirection.ReturnValue))
403 throw new ArgumentException("Unknown type");
405 if((direction != ParameterDirection.Input) || !Convert.IsDBNull(Value))
407 InferType();
410 if (db2DataType == DB2Constants.SQL_INTEGER)
412 requiredMemory = 4;
414 if((db2DataType == DB2Constants.SQL_VARBINARY) ||
415 (db2DataType == DB2Constants.SQL_WCHAR))
417 if(Size <= 0)
419 if(direction != ParameterDirection.Input)
420 throw new ArgumentException("Size not specified");
421 if(Value == DBNull.Value)
422 requiredMemory = 0;
423 else if(Value is string)
424 requiredMemory = ((string)Value).Length;
425 else if(Value is byte[])
426 requiredMemory = ((byte[])Value).Length;
427 else
428 throw new ArgumentException("wrong type!?");
430 else
432 requiredMemory = Size;
434 if(db2DataType == DB2Constants.SQL_WCHAR)
435 requiredMemory = (requiredMemory * 2) + 2;
436 requiredMemory = (requiredMemory | 0xf) + 1;
438 requiredMemory = Math.Max(128, requiredMemory);
441 #region Bind
443 /// Bind this parameter
444 ///
445 internal short Bind(IntPtr hwndStmt, short paramNum)
447 int inLength = requiredMemory;
448 db2LastUsedDataType = db2DataType;
449 short db2CType = DB2Constants.SQL_C_DEFAULT;
450 if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
452 if(Convert.IsDBNull(Value))
454 inLength = DB2Constants.SQL_NULL_DATA;
455 if((db2DataType == DB2Constants.SQL_UNKNOWN_TYPE) ||
456 (db2DataType == DB2Constants.SQL_DECIMAL))
458 db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
459 db2CType = DB2Constants.SQL_C_WCHAR;
463 if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
465 switch (db2DataType)
467 case DB2Constants.SQL_WCHAR:
468 string tmpString = Convert.ToString(Value);
469 inLength = tmpString.Length;
470 if((Size > 0) && (inLength > Size))
471 inLength = Size;
472 Marshal.Copy(tmpString.ToCharArray(), 0, internalBuffer, inLength);
473 inLength *= 2;
474 db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
475 db2CType = DB2Constants.SQL_C_WCHAR;
476 if(inLength > 32000)
478 db2LastUsedDataType = DB2Constants.SQL_TYPE_BLOB;
480 break;
481 case DB2Constants.SQL_VARBINARY:
482 byte[] tmpBytes = (byte[])Value;
483 inLength = tmpBytes.Length;
484 if((Size > 0) && (inLength > Size))
485 inLength = Size;
486 Marshal.Copy(tmpBytes, 0, internalBuffer, inLength);
487 db2CType = DB2Constants.SQL_TYPE_BINARY;
488 break;
489 case DB2Constants.SQL_BIT:
490 case DB2Constants.SQL_UTINYINT:
491 case DB2Constants.SQL_SMALLINT:
492 Marshal.WriteInt16(internalBuffer, Convert.ToInt16(Value));
493 db2CType = DB2Constants.SQL_C_SSHORT;
494 break;
495 case DB2Constants.SQL_INTEGER:
496 Marshal.WriteInt32(internalBuffer, Convert.ToInt32(Value));
497 db2CType = DB2Constants.SQL_C_SLONG;
498 break;
499 case DB2Constants.SQL_BIGINT:
500 Marshal.WriteInt64(internalBuffer, Convert.ToInt64(Value));
501 db2CType = DB2Constants.SQL_C_SBIGINT;
502 break;
503 case DB2Constants.SQL_REAL:
504 Marshal.StructureToPtr((float)Convert.ToDouble(Value), internalBuffer, false);
505 db2CType = DB2Constants.SQL_C_TYPE_REAL;
506 break;
507 case DB2Constants.SQL_DOUBLE:
508 Marshal.StructureToPtr(Convert.ToDouble(Value), internalBuffer, false);
509 db2CType = DB2Constants.SQL_C_DOUBLE;
510 break;
511 case DB2Constants.SQL_DECIMAL:
512 byte[] tmpDecimalData = System.Text.Encoding.UTF8.GetBytes(
513 Convert.ToDecimal(Value).ToString(System.Globalization.CultureInfo.InvariantCulture));
514 inLength = Math.Min(tmpDecimalData.Length, requiredMemory);
515 Marshal.Copy(tmpDecimalData, 0, internalBuffer, inLength);
516 db2LastUsedDataType = DB2Constants.SQL_VARCHAR;
517 db2CType = DB2Constants.SQL_C_CHAR;
518 break;
519 case DB2Constants.SQL_TYPE_DATE:
520 DateTime tmpDate = Convert.ToDateTime(Value);
521 Marshal.WriteInt16(internalBuffer, 0, (short)tmpDate.Year);
522 Marshal.WriteInt16(internalBuffer, 2, (short)tmpDate.Month);
523 Marshal.WriteInt16(internalBuffer, 4, (short)tmpDate.Day);
524 db2CType = DB2Constants.SQL_C_TYPE_DATE;
525 break;
526 case DB2Constants.SQL_TYPE_TIMESTAMP:
527 DateTime tmpDateTime = Convert.ToDateTime(Value);
528 Marshal.WriteInt16(internalBuffer, 0, (short)tmpDateTime.Year);
529 Marshal.WriteInt16(internalBuffer, 2, (short)tmpDateTime.Month);
530 Marshal.WriteInt16(internalBuffer, 4, (short)tmpDateTime.Day);
531 Marshal.WriteInt16(internalBuffer, 6, (short)tmpDateTime.Hour);
532 Marshal.WriteInt16(internalBuffer, 8, (short)tmpDateTime.Minute);
533 Marshal.WriteInt16(internalBuffer, 10, (short)tmpDateTime.Second);
534 Marshal.WriteInt32(internalBuffer, 12, (int)((tmpDateTime.Ticks % 10000000) * 100));
535 db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;
536 break;
537 case DB2Constants.SQL_TYPE_TIME:
538 TimeSpan tmpTime = (TimeSpan)Value;
539 Marshal.WriteInt16(internalBuffer, 0, (short)tmpTime.Hours);
540 Marshal.WriteInt16(internalBuffer, 2, (short)tmpTime.Minutes);
541 Marshal.WriteInt16(internalBuffer, 4, (short)tmpTime.Seconds);
542 db2CType = DB2Constants.SQL_C_TYPE_TIME;
543 break;
546 else
548 switch (db2DataType)
550 case DB2Constants.SQL_WCHAR:
551 db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
552 db2CType = DB2Constants.SQL_C_WCHAR;
553 break;
554 case DB2Constants.SQL_VARBINARY:
555 db2CType = DB2Constants.SQL_TYPE_BINARY;
556 break;
557 case DB2Constants.SQL_BIT:
558 case DB2Constants.SQL_UTINYINT:
559 case DB2Constants.SQL_SMALLINT:
560 db2CType = DB2Constants.SQL_C_SSHORT;
561 break;
562 case DB2Constants.SQL_INTEGER:
563 db2CType = DB2Constants.SQL_C_SLONG;
564 break;
565 case DB2Constants.SQL_BIGINT:
566 db2CType = DB2Constants.SQL_C_SBIGINT;
567 break;
568 case DB2Constants.SQL_REAL:
569 db2CType = DB2Constants.SQL_C_TYPE_REAL;
570 break;
571 case DB2Constants.SQL_DOUBLE:
572 db2CType = DB2Constants.SQL_C_DOUBLE;
573 break;
574 case DB2Constants.SQL_DECIMAL:
575 db2LastUsedDataType = DB2Constants.SQL_VARCHAR;
576 db2CType = DB2Constants.SQL_C_CHAR;
577 break;
578 case DB2Constants.SQL_TYPE_DATE:
579 db2CType = DB2Constants.SQL_C_TYPE_DATE;
580 break;
581 case DB2Constants.SQL_TYPE_TIMESTAMP:
582 db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;
583 break;
584 case DB2Constants.SQL_TYPE_TIME:
585 db2CType = DB2Constants.SQL_C_TYPE_TIME;
586 break;
589 Marshal.WriteInt32(internalLengthBuffer, inLength);
590 short sqlRet = DB2CLIWrapper.SQLBindParameter(hwndStmt, paramNum, db2Direction,
591 db2CType, db2LastUsedDataType, Size, Scale,
592 internalBuffer, requiredMemory, internalLengthBuffer);
594 return sqlRet;
596 #endregion
597 object ICloneable.Clone ()
599 DB2Parameter clone = new DB2Parameter();
600 clone.dbType = dbType;
601 clone.db2Type = db2Type;
602 clone.db2DataType = db2DataType;
603 clone.db2LastUsedDataType = db2LastUsedDataType;
604 clone.direction = direction;
605 clone.db2Direction = db2Direction;
606 clone.nullable = nullable;
607 clone.parameterName = parameterName;
608 clone.sourceColumn = sourceColumn;
609 clone.sourceVersion = sourceVersion;
610 clone.dataVal = dataVal;
611 clone.scale = scale;
612 clone.precision = precision;
613 clone.size = size;
614 if(dataVal is ICloneable)
616 clone.dataVal = ((ICloneable)dataVal).Clone();
618 return clone;
621 internal void GetOutValue()
623 int length = Marshal.ReadInt32(internalLengthBuffer);
624 if(length == DB2Constants.SQL_NULL_DATA)
626 dataVal = DBNull.Value;
627 return;
629 switch(DB2Type)
631 case DB2Type.SmallInt:
632 dataVal = Marshal.ReadInt16(internalBuffer);
633 break;
634 case DB2Type.Integer:
635 dataVal = Marshal.ReadInt32(internalBuffer);
636 break;
637 case DB2Type.BigInt:
638 dataVal = Marshal.ReadInt64(internalBuffer);
639 break;
640 case DB2Type.Double:
641 dataVal = Marshal.PtrToStructure(internalBuffer, typeof(Double));
642 break;
643 case DB2Type.Float:
644 dataVal = Marshal.PtrToStructure(internalBuffer, typeof(Single));
645 break;
646 case DB2Type.Char:
647 case DB2Type.VarChar:
648 case DB2Type.LongVarChar:
649 case DB2Type.Graphic:
650 case DB2Type.VarGraphic:
651 case DB2Type.LongVarGraphic:
652 case DB2Type.Clob:
653 case DB2Type.DbClob:
654 dataVal = Marshal.PtrToStringUni(internalBuffer, Math.Min(Size, length / 2));
655 break;
656 case DB2Type.Binary:
657 case DB2Type.VarBinary:
658 case DB2Type.LongVarBinary:
659 case DB2Type.Blob:
660 case DB2Type.Datalink:
661 length = Math.Min(Size, length);
662 dataVal = new byte[length];
663 Marshal.Copy(internalBuffer, (byte[])dataVal, 0, length);
664 break;
665 case DB2Type.Decimal:
666 dataVal = decimal.Parse(Marshal.PtrToStringAnsi(internalBuffer, length),
667 System.Globalization.CultureInfo.InvariantCulture);
668 break;
669 case DB2Type.Timestamp:
670 DateTime dtTmp = new DateTime(
671 Marshal.ReadInt16(internalBuffer, 0), // year
672 Marshal.ReadInt16(internalBuffer, 2), // month
673 Marshal.ReadInt16(internalBuffer, 4), // day
674 Marshal.ReadInt16(internalBuffer, 6), // hour
675 Marshal.ReadInt16(internalBuffer, 8), // minute
676 Marshal.ReadInt16(internalBuffer, 10));// second
677 dataVal = dtTmp.AddTicks(Marshal.ReadInt32(internalBuffer, 12) / 100); // nanoseconds
678 break;
679 case DB2Type.Date:
680 dataVal = new DateTime(
681 Marshal.ReadInt16(internalBuffer, 0),
682 Marshal.ReadInt16(internalBuffer, 2),
683 Marshal.ReadInt16(internalBuffer, 4));
684 break;
685 case DB2Type.Time:
686 dataVal = new TimeSpan(
687 Marshal.ReadInt16(internalBuffer, 0), // Hour
688 Marshal.ReadInt16(internalBuffer, 2), // Minute
689 Marshal.ReadInt16(internalBuffer, 4)); // Second
690 break;
692 case DB2Type.Invalid:
693 case DB2Type.Real:
694 case DB2Type.Numeric:
695 case DB2Type.RowId:
696 case DB2Type.XmlReader:
697 throw new NotImplementedException();
698 default:
699 throw new NotSupportedException("unknown data type");