-using System;
-using System.Data;
-using System.Runtime.InteropServices;
-namespace IBM.Data.DB2
-{
-
- public sealed class DB2Parameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
- {
- private DbType dbType = DbType.Object;
- private DB2Type db2Type = DB2Type.Invalid;
- private short db2DataType = DB2Constants.SQL_UNKNOWN_TYPE;
- private short db2LastUsedDataType = DB2Constants.SQL_UNKNOWN_TYPE;
-
- private ParameterDirection direction;
- private short db2Direction = DB2Constants.SQL_PARAM_INPUT;
- private bool nullable = true;
- private string parameterName;
- private string sourceColumn;
- private DataRowVersion sourceVersion;
- private object dataVal;
- private byte scale, precision;
- private int size;
- internal IntPtr internalBuffer;
- internal IntPtr internalLengthBuffer;
- internal int requiredMemory;
-
- #region Contructors and destructors
- public DB2Parameter()
- {
- direction = ParameterDirection.Input;
- sourceVersion = DataRowVersion.Current;
- }
-
- public DB2Parameter(string name, DB2Type type)
- {
- direction = ParameterDirection.Input;
- sourceVersion = DataRowVersion.Current;
- this.ParameterName = name;
- this.DB2Type = type;
- }
-
- public DB2Parameter(string name, DB2Type type, int size)
- {
- direction = ParameterDirection.Input;
- sourceVersion = DataRowVersion.Current;
- this.ParameterName = name;
- this.DB2Type = type;
- this.Size = size;
- }
-
- public DB2Parameter(string name, DB2Type type, int size, string sourceColumn)
- {
- direction = ParameterDirection.Input;
- sourceVersion = DataRowVersion.Current;
- this.ParameterName = name;
- this.DB2Type = type;
- this.Size = size;
- this.SourceColumn = sourceColumn;
- }
-
- //only for testing purposes!!!
- public DB2Parameter(string name, short type, int size, string sourceColumn)
- {
- direction = ParameterDirection.Input;
- sourceVersion = DataRowVersion.Current;
- this.ParameterName = name;
- this.db2DataType = type;
- this.Size = size;
- this.SourceColumn = sourceColumn;
- }
-
- public DB2Parameter(string parameterName, object value)
- {
- direction = ParameterDirection.Input;
- sourceVersion = DataRowVersion.Current;
- this.ParameterName = parameterName;
- this.Value = value;
- }
-
- public DB2Parameter(string parameterName, DB2Type db2Type, int size, ParameterDirection parameterDirection, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)
- {
- this.ParameterName = parameterName;
- this.DB2Type = db2Type;
- this.Size = size;
- this.Direction = parameterDirection;
- this.IsNullable = isNullable;
- this.Precision = precision;
- this.Scale = scale;
- this.SourceColumn = srcColumn;
- this.SourceVersion = srcVersion;
- this.Value = value;
- }
-
- #endregion
- #region Properties
- #region DbType Property
- public DB2Type DB2Type
- {
- get
- {
- return db2Type;
- }
- set
- {
- db2Type = value;
- switch(db2Type)
- {
- case DB2Type.Invalid: dbType = DbType.Object; db2DataType = DB2Constants.SQL_UNKNOWN_TYPE; break;
- case DB2Type.SmallInt: dbType = DbType.Int16; db2DataType = DB2Constants.SQL_SMALLINT; break;
- case DB2Type.Integer: dbType = DbType.Int32; db2DataType = DB2Constants.SQL_INTEGER; break;
- case DB2Type.BigInt: dbType = DbType.Int64; db2DataType = DB2Constants.SQL_BIGINT; break;
- case DB2Type.Real: dbType = DbType.Single; db2DataType = DB2Constants.SQL_REAL; break;
- case DB2Type.Double: dbType = DbType.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;
- case DB2Type.Float: dbType = DbType.Single; db2DataType = DB2Constants.SQL_REAL; break;
- case DB2Type.Decimal: dbType = DbType.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
- case DB2Type.Numeric: dbType = DbType.VarNumeric; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.Date: dbType = DbType.Date; db2DataType = DB2Constants.SQL_TYPE_DATE; break;
- case DB2Type.Time: dbType = DbType.Time; db2DataType = DB2Constants.SQL_TYPE_TIME; break;
- case DB2Type.Timestamp: dbType = DbType.DateTime; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;
- case DB2Type.Char: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.VarChar: dbType = DbType.StringFixedLength; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.LongVarChar: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.Binary: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
- case DB2Type.VarBinary: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
- case DB2Type.LongVarBinary: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.Graphic: dbType = DbType.StringFixedLength; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.VarGraphic: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.LongVarGraphic: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.Clob: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.Blob: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
- case DB2Type.DbClob: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DB2Type.Datalink: dbType = DbType.Byte; db2DataType = DB2Constants.SQL_VARBINARY; break;
- case DB2Type.RowId: dbType = DbType.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
- case DB2Type.XmlReader: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;
- default:
- throw new NotSupportedException("Value is of unknown data type");
- }
- }
- }
- ///
- /// Parameter data type
- ///
- public DbType DbType
- {
- get
- {
- return dbType;
- }
- set
- {
- dbType = value;
- switch(dbType)
- {
- case DbType.AnsiString: db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DbType.AnsiStringFixedLength: db2Type = DB2Type.Char; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DbType.Binary: db2Type = DB2Type.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;
- case DbType.Boolean: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_BIT; break;
- case DbType.Byte: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;
- case DbType.Currency: db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
- case DbType.Date: db2Type = DB2Type.Date; db2DataType = DB2Constants.SQL_TYPE_DATE; break;
- case DbType.DateTime: db2Type = DB2Type.Timestamp; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;
- case DbType.Decimal: db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
- case DbType.Double: db2Type = DB2Type.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;
- case DbType.Guid: db2Type = DB2Type.Binary; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DbType.Int16: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;
- case DbType.Int32: db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;
- case DbType.Int64: db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;
- case DbType.Object: db2Type = DB2Type.Invalid; db2DataType = DB2Constants.SQL_UNKNOWN_TYPE; break;
- case DbType.SByte: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;
- case DbType.Single: db2Type = DB2Type.Float; db2DataType = DB2Constants.SQL_REAL; break;
- case DbType.String: db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DbType.StringFixedLength: db2Type = DB2Type.Char; db2DataType = DB2Constants.SQL_WCHAR; break;
- case DbType.Time: db2Type = DB2Type.Time; db2DataType = DB2Constants.SQL_TYPE_TIME; break;
- case DbType.UInt16: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;
- case DbType.UInt32: db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;
- case DbType.UInt64: db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;
- case DbType.VarNumeric: db2Type = DB2Type.Numeric; db2DataType = DB2Constants.SQL_WCHAR; break;
- default:
- throw new NotSupportedException("Value is of unknown data type");
- }
- }
- }
-
- #endregion
- #region Direction
- ///
- /// In or out parameter, or both
- ///
- public ParameterDirection Direction
- {
- get
- {
- return direction;
- }
- set
- {
- direction = value;
- switch(direction)
- {
- default:
- case ParameterDirection.Input: db2Direction = DB2Constants.SQL_PARAM_INPUT; break;
- case ParameterDirection.Output: db2Direction = DB2Constants.SQL_PARAM_OUTPUT; break;
- case ParameterDirection.InputOutput: db2Direction = DB2Constants.SQL_PARAM_INPUT_OUTPUT; break;
- case ParameterDirection.ReturnValue: db2Direction = DB2Constants.SQL_RETURN_VALUE; break;
- }
- }
- }
- #endregion
- #region IsNullable
- ///
- /// Does this parameter support a null value
- ///
- public bool IsNullable
- {
- get
- {
- return nullable;
- }
- set
- {
- nullable = value;
- }
- }
- #endregion
- #region ParameterName
- public string ParameterName
- {
- get
- {
- return parameterName;
- }
- set
- {
- parameterName = value;
- }
- }
- #endregion
- #region SourceColumn
- ///
- /// Gets or sets the name of the source column that is mapped to the DataSet
- ///
- public string SourceColumn
- {
- get
- {
- return sourceColumn;
- }
- set
- {
- sourceColumn = value;
- }
- }
- #endregion
- #region SourceVersion
- ///
- /// DataRowVersion property
- ///
- public DataRowVersion SourceVersion
- {
- get
- {
- return sourceVersion;
- }
- set
- {
- sourceVersion = value;
- }
- }
- #endregion
- #region IDbDataParameter properties
- public byte Precision
- {
- get
- {
- return precision;
- }
- set
- {
- precision = value;
- }
- }
-
- public byte Scale
- {
- get
- {
- return scale;
- }
- set
- {
- scale = value;
- }
- }
-
- public int Size
- {
- get
- {
- return size;
- }
- set
- {
- size = value;
- }
- }
- #endregion
- #region Value
- ///
- /// The actual parameter data
- ///
- public object Value
- {
- get
- {
- return dataVal;
- }
- set
- {
- this.dataVal = value;
- }
- }
- #endregion
- #endregion
- #region inferType Method
- /// <summary>
- /// Determine the data type based on the value
- /// </summary>
- private void InferType()
- {
- if(Value == null)
- throw new ArgumentException("No DB2Parameter.Value found");
-
- if(Value is IConvertible)
- {
- switch(((IConvertible)Value).GetTypeCode())
- {
- case TypeCode.Char: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_WCHAR; break;
- case TypeCode.Boolean: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_BIT; break;
- case TypeCode.SByte:
- case TypeCode.Byte: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;
- case TypeCode.UInt16:
- case TypeCode.Int16: dbType = DbType.Int16; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;
- case TypeCode.UInt32:
- case TypeCode.Int32: dbType = DbType.Int32; db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;
- case TypeCode.UInt64:
- case TypeCode.Int64: dbType = DbType.Int64; db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;
- case TypeCode.Single: dbType = DbType.Single; db2Type = DB2Type.Float; db2DataType = DB2Constants.SQL_REAL; break;
- case TypeCode.Double: dbType = DbType.Double; db2Type = DB2Type.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;
- case TypeCode.Decimal: dbType = DbType.Decimal; db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;
- case TypeCode.DateTime: dbType = DbType.DateTime; db2Type = DB2Type.Timestamp; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;
- case TypeCode.String: dbType = DbType.String; db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;
-
- case TypeCode.Object:
- case TypeCode.DBNull:
- case TypeCode.Empty:
- throw new SystemException("Unknown data type");
- default:
- throw new SystemException("Value is of unknown data type");
- }
- }
- else if(Value is byte[])
- {
- dbType = DbType.Binary;
- db2Type = DB2Type.VarBinary;
- db2DataType = DB2Constants.SQL_VARBINARY;
- }
- else if(Value is TimeSpan)
- {
- dbType = DbType.Time;
- db2Type = DB2Type.Time;
- db2DataType = DB2Constants.SQL_TYPE_TIME;
- }
- else
- {
- throw new NotSupportedException("Value is of unsupported data type");
- }
- }
- #endregion
-
- internal void CalculateRequiredmemory()
- {
- if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
- {
- if(Value == null)
- throw new ArgumentException("Value missing");
- }
- if(dbType == DbType.Object)
- {
- if((direction == ParameterDirection.Output) || (direction == ParameterDirection.ReturnValue))
- throw new ArgumentException("Unknown type");
-
- if((direction != ParameterDirection.Input) || !Convert.IsDBNull(Value))
- {
- InferType();
- }
- }
-
- if((db2DataType == DB2Constants.SQL_VARBINARY) ||
- (db2DataType == DB2Constants.SQL_WCHAR))
- {
- if(Size <= 0)
- {
- if(direction != ParameterDirection.Input)
- throw new ArgumentException("Size not specified");
- if(Value == DBNull.Value)
- requiredMemory = 0;
- else if(Value is string)
- requiredMemory = ((string)Value).Length;
- else if(Value is byte[])
- requiredMemory = ((byte[])Value).Length;
- else
- throw new ArgumentException("wrong type!?");
- }
- else
- {
- requiredMemory = Size;
- }
- if(db2DataType == DB2Constants.SQL_WCHAR)
- requiredMemory = (requiredMemory * 2) + 2;
- requiredMemory = (requiredMemory | 0xf) + 1;
- }
- requiredMemory = Math.Max(128, requiredMemory);
- }
-
- #region Bind
- ///
- /// Bind this parameter
- ///
- internal short Bind(IntPtr hwndStmt, short paramNum)
- {
- int inLength = requiredMemory;
- db2LastUsedDataType = db2DataType;
- short db2CType = DB2Constants.SQL_C_DEFAULT;
- if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
- {
- if(Convert.IsDBNull(Value))
- {
- inLength = DB2Constants.SQL_NULL_DATA;
- if((db2DataType == DB2Constants.SQL_UNKNOWN_TYPE) ||
- (db2DataType == DB2Constants.SQL_DECIMAL))
- {
- db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
- db2CType = DB2Constants.SQL_C_WCHAR;
- }
- }
- }
- if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))
- {
-
- switch (db2DataType)
- {
-
- case DB2Constants.SQL_WCHAR:
- string tmpString = Convert.ToString(Value);
- inLength = tmpString.Length;
- if((Size > 0) && (inLength > Size))
- inLength = Size;
-
-
- Marshal.Copy(tmpString.ToCharArray(), 0, internalBuffer, inLength);
- inLength *= 2;
-
- db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
- db2CType = DB2Constants.SQL_C_WCHAR;
- break;
- case DB2Constants.SQL_VARBINARY:
- byte[] tmpBytes = (byte[])Value;
- inLength = tmpBytes.Length;
- if((Size > 0) && (inLength > Size))
- inLength = Size;
-
- Marshal.Copy(tmpBytes, 0, internalBuffer, inLength);
- db2CType = DB2Constants.SQL_TYPE_BINARY;
- break;
- case DB2Constants.SQL_BIT:
- case DB2Constants.SQL_UTINYINT:
- case DB2Constants.SQL_SMALLINT:
-
- Marshal.WriteInt16(internalBuffer, Convert.ToInt16(Value));
- db2CType = DB2Constants.SQL_C_SSHORT;
- break;
- case DB2Constants.SQL_INTEGER:
-
- Marshal.WriteInt32(internalBuffer, Convert.ToInt32(Value));
- db2CType = DB2Constants.SQL_C_SLONG;
- break;
- case DB2Constants.SQL_BIGINT:
-
- Marshal.WriteInt64(internalBuffer, Convert.ToInt64(Value));
- db2CType = DB2Constants.SQL_C_SBIGINT;
- break;
- case DB2Constants.SQL_REAL:
-
- Marshal.StructureToPtr((float)Convert.ToDouble(Value), internalBuffer, false);
- db2CType = DB2Constants.SQL_C_TYPE_REAL;
- break;
- case DB2Constants.SQL_DOUBLE:
-
- Marshal.StructureToPtr(Convert.ToDouble(Value), internalBuffer, false);
- db2CType = DB2Constants.SQL_C_DOUBLE;
- break;
- case DB2Constants.SQL_DECIMAL:
- byte[] tmpDecimalData = System.Text.Encoding.UTF8.GetBytes(
- Convert.ToDecimal(Value).ToString(System.Globalization.CultureInfo.InvariantCulture));
- inLength = Math.Min(tmpDecimalData.Length, requiredMemory);
-
- Marshal.Copy(tmpDecimalData, 0, internalBuffer, inLength);
- db2LastUsedDataType = DB2Constants.SQL_VARCHAR;
- db2CType = DB2Constants.SQL_C_CHAR;
- break;
- case DB2Constants.SQL_TYPE_DATE:
- DateTime tmpDate = Convert.ToDateTime(Value);
- Marshal.WriteInt16(internalBuffer, 0, (short)tmpDate.Year);
- Marshal.WriteInt16(internalBuffer, 2, (short)tmpDate.Month);
- Marshal.WriteInt16(internalBuffer, 4, (short)tmpDate.Day);
- db2CType = DB2Constants.SQL_C_TYPE_DATE;
- break;
- case DB2Constants.SQL_TYPE_TIMESTAMP:
- DateTime tmpDateTime = Convert.ToDateTime(Value);
- Marshal.WriteInt16(internalBuffer, 0, (short)tmpDateTime.Year);
- Marshal.WriteInt16(internalBuffer, 2, (short)tmpDateTime.Month);
- Marshal.WriteInt16(internalBuffer, 4, (short)tmpDateTime.Day);
- Marshal.WriteInt16(internalBuffer, 6, (short)tmpDateTime.Hour);
- Marshal.WriteInt16(internalBuffer, 8, (short)tmpDateTime.Minute);
- Marshal.WriteInt16(internalBuffer, 10, (short)tmpDateTime.Second);
- Marshal.WriteInt32(internalBuffer, 12, (int)((tmpDateTime.Ticks % 10000000) * 100));
- db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;
- break;
- case DB2Constants.SQL_TYPE_TIME:
- TimeSpan tmpTime = (TimeSpan)Value;
- Marshal.WriteInt16(internalBuffer, 0, (short)tmpTime.Hours);
- Marshal.WriteInt16(internalBuffer, 2, (short)tmpTime.Minutes);
- Marshal.WriteInt16(internalBuffer, 4, (short)tmpTime.Seconds);
- db2CType = DB2Constants.SQL_C_TYPE_TIME;
- break;
- }
- }
- else
- {
- switch (db2DataType)
- {
- case DB2Constants.SQL_WCHAR:
- db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;
- db2CType = DB2Constants.SQL_C_WCHAR;
- break;
- case DB2Constants.SQL_VARBINARY:
- db2CType = DB2Constants.SQL_TYPE_BINARY;
- break;
- case DB2Constants.SQL_BIT:
- case DB2Constants.SQL_UTINYINT:
- case DB2Constants.SQL_SMALLINT:
- db2CType = DB2Constants.SQL_C_SSHORT;
- break;
- case DB2Constants.SQL_INTEGER:
- db2CType = DB2Constants.SQL_C_SLONG;
- break;
- case DB2Constants.SQL_BIGINT:
- db2CType = DB2Constants.SQL_C_SBIGINT;
- break;
- case DB2Constants.SQL_REAL:
- db2CType = DB2Constants.SQL_C_TYPE_REAL;
- break;
- case DB2Constants.SQL_DOUBLE:
- db2CType = DB2Constants.SQL_C_DOUBLE;
- break;
- case DB2Constants.SQL_DECIMAL:
- db2LastUsedDataType = DB2Constants.SQL_VARCHAR;
- db2CType = DB2Constants.SQL_C_CHAR;
- break;
- case DB2Constants.SQL_TYPE_DATE:
- db2CType = DB2Constants.SQL_C_TYPE_DATE;
- break;
- case DB2Constants.SQL_TYPE_TIMESTAMP:
- db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;
- break;
- case DB2Constants.SQL_TYPE_TIME:
- db2CType = DB2Constants.SQL_C_TYPE_TIME;
- break;
- }
- }
- Marshal.WriteInt32(internalLengthBuffer, inLength);
- short sqlRet = DB2CLIWrapper.SQLBindParameter(hwndStmt, paramNum, db2Direction,
- db2CType, db2LastUsedDataType, Size, Scale,
- internalBuffer, requiredMemory, internalLengthBuffer);
-
- return sqlRet;
- }
- #endregion
- object ICloneable.Clone ()
- {
- DB2Parameter clone = new DB2Parameter();
- clone.dbType = dbType;
- clone.db2Type = db2Type;
- clone.db2DataType = db2DataType;
- clone.db2LastUsedDataType = db2LastUsedDataType;
- clone.direction = direction;
- clone.db2Direction = db2Direction;
- clone.nullable = nullable;
- clone.parameterName = parameterName;
- clone.sourceColumn = sourceColumn;
- clone.sourceVersion = sourceVersion;
- clone.dataVal = dataVal;
- clone.scale = scale;
- clone.precision = precision;
- clone.size = size;
- if(dataVal is ICloneable)
- {
- clone.dataVal = ((ICloneable)dataVal).Clone();
- }
- return clone;
- }
-
- internal void GetOutValue()
- {
- int length = Marshal.ReadInt32(internalLengthBuffer);
- if(length == DB2Constants.SQL_NULL_DATA)
- {
- dataVal = DBNull.Value;
- return;
- }
- switch(DB2Type)
- {
- case DB2Type.SmallInt:
- dataVal = Marshal.ReadInt16(internalBuffer);
- break;
- case DB2Type.Integer:
- dataVal = Marshal.ReadInt32(internalBuffer);
- break;
- case DB2Type.BigInt:
- dataVal = Marshal.ReadInt64(internalBuffer);
- break;
- case DB2Type.Double:
- dataVal = Marshal.PtrToStructure(internalBuffer, typeof(Double));
- break;
- case DB2Type.Float:
- dataVal = Marshal.PtrToStructure(internalBuffer, typeof(Single));
- break;
- case DB2Type.Char:
- case DB2Type.VarChar:
- case DB2Type.LongVarChar:
- case DB2Type.Graphic:
- case DB2Type.VarGraphic:
- case DB2Type.LongVarGraphic:
- case DB2Type.Clob:
- case DB2Type.DbClob:
- dataVal = Marshal.PtrToStringUni(internalBuffer, Math.Min(Size, length / 2));
- break;
- case DB2Type.Binary:
- case DB2Type.VarBinary:
- case DB2Type.LongVarBinary:
- case DB2Type.Blob:
- case DB2Type.Datalink:
- length = Math.Min(Size, length);
- dataVal = new byte[length];
- Marshal.Copy(internalBuffer, (byte[])dataVal, 0, length);
- break;
- case DB2Type.Decimal:
- dataVal = decimal.Parse(Marshal.PtrToStringAnsi(internalBuffer, length),
- System.Globalization.CultureInfo.InvariantCulture);
- break;
- case DB2Type.Timestamp:
- DateTime dtTmp = new DateTime(
- Marshal.ReadInt16(internalBuffer, 0), // year
- Marshal.ReadInt16(internalBuffer, 2), // month
- Marshal.ReadInt16(internalBuffer, 4), // day
- Marshal.ReadInt16(internalBuffer, 6), // hour
- Marshal.ReadInt16(internalBuffer, 8), // minute
- Marshal.ReadInt16(internalBuffer, 10));// second
- dataVal = dtTmp.AddTicks(Marshal.ReadInt32(internalBuffer, 12) / 100); // nanoseconds
- break;
- case DB2Type.Date:
- dataVal = new DateTime(
- Marshal.ReadInt16(internalBuffer, 0),
- Marshal.ReadInt16(internalBuffer, 2),
- Marshal.ReadInt16(internalBuffer, 4));
- break;
- case DB2Type.Time:
- dataVal = new TimeSpan(
- Marshal.ReadInt16(internalBuffer, 0), // Hour
- Marshal.ReadInt16(internalBuffer, 2), // Minute
- Marshal.ReadInt16(internalBuffer, 4)); // Second
- break;
-
- case DB2Type.Invalid:
- case DB2Type.Real:
- case DB2Type.Numeric:
- case DB2Type.RowId:
- case DB2Type.XmlReader:
- throw new NotImplementedException();
- default:
- throw new NotSupportedException("unknown data type");
- }
- }
- }
-}
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+using System;\r
+using System.Data;\r
+using System.Runtime.InteropServices;\r
+\r
+namespace IBM.Data.DB2\r
+{\r
+\r
+ public sealed class DB2Parameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable\r
+ {\r
+ private DbType dbType = DbType.Object;\r
+ private DB2Type db2Type = DB2Type.Invalid;\r
+ private short db2DataType = DB2Constants.SQL_UNKNOWN_TYPE;\r
+ private short db2LastUsedDataType = DB2Constants.SQL_UNKNOWN_TYPE;\r
+\r
+ private ParameterDirection direction;\r
+ private short db2Direction = DB2Constants.SQL_PARAM_INPUT;\r
+ private bool nullable = true;\r
+ private string parameterName;\r
+ private string sourceColumn;\r
+ private DataRowVersion sourceVersion;\r
+ private object dataVal;\r
+ private byte scale, precision;\r
+ private int size;\r
+ internal IntPtr internalBuffer;\r
+ internal IntPtr internalLengthBuffer;\r
+ internal int requiredMemory;\r
+\r
+ #region Contructors and destructors\r
+ public DB2Parameter()\r
+ {\r
+ direction = ParameterDirection.Input;\r
+ sourceVersion = DataRowVersion.Current;\r
+ } \r
+\r
+ public DB2Parameter(string name, DB2Type type)\r
+ {\r
+ direction = ParameterDirection.Input;\r
+ sourceVersion = DataRowVersion.Current;\r
+ this.ParameterName = name;\r
+ this.DB2Type = type;\r
+ } \r
+\r
+ public DB2Parameter(string name, DB2Type type, int size)\r
+ {\r
+ direction = ParameterDirection.Input;\r
+ sourceVersion = DataRowVersion.Current;\r
+ this.ParameterName = name;\r
+ this.DB2Type = type;\r
+ this.Size = size;\r
+ }\r
+\r
+ public DB2Parameter(string name, DB2Type type, int size, string sourceColumn)\r
+ {\r
+ direction = ParameterDirection.Input;\r
+ sourceVersion = DataRowVersion.Current;\r
+ this.ParameterName = name;\r
+ this.DB2Type = type;\r
+ this.Size = size;\r
+ this.SourceColumn = sourceColumn;\r
+ }\r
+\r
+ public DB2Parameter(string parameterName, object value)\r
+ {\r
+ direction = ParameterDirection.Input;\r
+ sourceVersion = DataRowVersion.Current;\r
+ this.ParameterName = parameterName;\r
+ this.Value = value;\r
+ }\r
+\r
+ public DB2Parameter(string parameterName, DB2Type db2Type, int size, ParameterDirection parameterDirection, bool isNullable, byte precision, byte scale, string srcColumn, DataRowVersion srcVersion, object value)\r
+ {\r
+ this.ParameterName = parameterName;\r
+ this.DB2Type = db2Type;\r
+ this.Size = size;\r
+ this.Direction = parameterDirection;\r
+ this.IsNullable = isNullable;\r
+ this.Precision = precision;\r
+ this.Scale = scale;\r
+ this.SourceColumn = srcColumn;\r
+ this.SourceVersion = srcVersion;\r
+ this.Value = value;\r
+ }\r
+\r
+ #endregion\r
+\r
+ #region Properties\r
+ #region DbType Property\r
+ public DB2Type DB2Type\r
+ {\r
+ get \r
+ {\r
+ return db2Type;\r
+ }\r
+ set \r
+ {\r
+ db2Type = value;\r
+ switch(db2Type)\r
+ {\r
+ case DB2Type.Invalid: dbType = DbType.Object; db2DataType = DB2Constants.SQL_UNKNOWN_TYPE; break;\r
+ case DB2Type.SmallInt: dbType = DbType.Int16; db2DataType = DB2Constants.SQL_SMALLINT; break;\r
+ case DB2Type.Integer: dbType = DbType.Int32; db2DataType = DB2Constants.SQL_INTEGER; break;\r
+ case DB2Type.BigInt: dbType = DbType.Int64; db2DataType = DB2Constants.SQL_BIGINT; break;\r
+ case DB2Type.Real: dbType = DbType.Single; db2DataType = DB2Constants.SQL_REAL; break;\r
+ case DB2Type.Double: dbType = DbType.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;\r
+ case DB2Type.Float: dbType = DbType.Single; db2DataType = DB2Constants.SQL_REAL; break;\r
+ case DB2Type.Decimal: dbType = DbType.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;\r
+ case DB2Type.Numeric: dbType = DbType.VarNumeric; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.Date: dbType = DbType.Date; db2DataType = DB2Constants.SQL_TYPE_DATE; break;\r
+ case DB2Type.Time: dbType = DbType.Time; db2DataType = DB2Constants.SQL_TYPE_TIME; break;\r
+ case DB2Type.Timestamp: dbType = DbType.DateTime; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;\r
+ case DB2Type.Char: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.VarChar: dbType = DbType.StringFixedLength; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.LongVarChar: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.Binary: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;\r
+ case DB2Type.VarBinary: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;\r
+ case DB2Type.LongVarBinary: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.Graphic: dbType = DbType.StringFixedLength; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.VarGraphic: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.LongVarGraphic: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.Clob: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.Blob: dbType = DbType.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;\r
+ case DB2Type.DbClob: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DB2Type.Datalink: dbType = DbType.Byte; db2DataType = DB2Constants.SQL_VARBINARY; break;\r
+ case DB2Type.RowId: dbType = DbType.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;\r
+ case DB2Type.XmlReader: dbType = DbType.String; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ default:\r
+ throw new NotSupportedException("Value is of unknown data type");\r
+ }\r
+ }\r
+ }\r
+ ///\r
+ /// Parameter data type\r
+ /// \r
+ public DbType DbType\r
+ {\r
+ get \r
+ {\r
+ return dbType;\r
+ }\r
+ set \r
+ {\r
+ dbType = value;\r
+ switch(dbType)\r
+ {\r
+ case DbType.AnsiString: db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DbType.AnsiStringFixedLength: db2Type = DB2Type.Char; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DbType.Binary: db2Type = DB2Type.Binary; db2DataType = DB2Constants.SQL_VARBINARY; break;\r
+ case DbType.Boolean: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_BIT; break;\r
+ case DbType.Byte: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;\r
+ case DbType.Currency: db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;\r
+ case DbType.Date: db2Type = DB2Type.Date; db2DataType = DB2Constants.SQL_TYPE_DATE; break;\r
+ case DbType.DateTime: db2Type = DB2Type.Timestamp; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;\r
+ case DbType.Decimal: db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;\r
+ case DbType.Double: db2Type = DB2Type.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;\r
+ case DbType.Guid: db2Type = DB2Type.Binary; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DbType.Int16: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;\r
+ case DbType.Int32: db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;\r
+ case DbType.Int64: db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;\r
+ case DbType.Object: db2Type = DB2Type.Invalid; db2DataType = DB2Constants.SQL_UNKNOWN_TYPE; break;\r
+ case DbType.SByte: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;\r
+ case DbType.Single: db2Type = DB2Type.Float; db2DataType = DB2Constants.SQL_REAL; break;\r
+ case DbType.String: db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DbType.StringFixedLength: db2Type = DB2Type.Char; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case DbType.Time: db2Type = DB2Type.Time; db2DataType = DB2Constants.SQL_TYPE_TIME; break;\r
+ case DbType.UInt16: db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;\r
+ case DbType.UInt32: db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;\r
+ case DbType.UInt64: db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;\r
+ case DbType.VarNumeric: db2Type = DB2Type.Numeric; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ default:\r
+ throw new NotSupportedException("Value is of unknown data type");\r
+ }\r
+ }\r
+ }\r
+\r
+ #endregion\r
+ #region Direction\r
+ ///\r
+ /// In or out parameter, or both\r
+ /// \r
+ public ParameterDirection Direction\r
+ {\r
+ get \r
+ {\r
+ return direction;\r
+ }\r
+ set \r
+ {\r
+ direction = value;\r
+ switch(direction)\r
+ {\r
+ default:\r
+ case ParameterDirection.Input: db2Direction = DB2Constants.SQL_PARAM_INPUT; break;\r
+ case ParameterDirection.Output: db2Direction = DB2Constants.SQL_PARAM_OUTPUT; break;\r
+ case ParameterDirection.InputOutput: db2Direction = DB2Constants.SQL_PARAM_INPUT_OUTPUT; break;\r
+ case ParameterDirection.ReturnValue: db2Direction = DB2Constants.SQL_RETURN_VALUE; break;\r
+ }\r
+ }\r
+ }\r
+ #endregion\r
+ #region IsNullable\r
+ ///\r
+ /// Does this parameter support a null value\r
+ /// \r
+ public bool IsNullable \r
+ {\r
+ get \r
+ {\r
+ return nullable;\r
+ }\r
+ set \r
+ {\r
+ nullable = value;\r
+ }\r
+ }\r
+ #endregion\r
+ #region ParameterName\r
+ public string ParameterName\r
+ {\r
+ get \r
+ {\r
+ return parameterName;\r
+ }\r
+ set \r
+ {\r
+ parameterName = value;\r
+ }\r
+ }\r
+ #endregion\r
+ #region SourceColumn\r
+ ///\r
+ /// Gets or sets the name of the source column that is mapped to the DataSet\r
+ /// \r
+ public string SourceColumn\r
+ {\r
+ get \r
+ {\r
+ return sourceColumn;\r
+ }\r
+ set \r
+ {\r
+ sourceColumn = value;\r
+ }\r
+ }\r
+ #endregion\r
+ #region SourceVersion\r
+ ///\r
+ /// DataRowVersion property\r
+ /// \r
+ public DataRowVersion SourceVersion \r
+ {\r
+ get \r
+ {\r
+ return sourceVersion;\r
+ }\r
+ set \r
+ {\r
+ sourceVersion = value;\r
+ }\r
+ }\r
+ #endregion\r
+ #region IDbDataParameter properties\r
+ public byte Precision \r
+ {\r
+ get \r
+ { \r
+ return precision;\r
+ }\r
+ set \r
+ { \r
+ precision = value; \r
+ }\r
+ }\r
+ \r
+ public byte Scale \r
+ {\r
+ get \r
+ { \r
+ return scale;\r
+ }\r
+ set \r
+ { \r
+ scale = value; \r
+ }\r
+ }\r
+ \r
+ public int Size \r
+ {\r
+ get \r
+ {\r
+ return size;\r
+ }\r
+ set \r
+ { \r
+ size = value;\r
+ }\r
+ }\r
+ #endregion\r
+ #region Value\r
+ ///\r
+ /// The actual parameter data\r
+ /// \r
+ public object Value \r
+ {\r
+ get\r
+ {\r
+ return dataVal;\r
+ }\r
+ set \r
+ {\r
+ this.dataVal = value;\r
+ }\r
+ }\r
+ #endregion\r
+ #endregion\r
+ \r
+ #region inferType Method\r
+ /// <summary>\r
+ /// Determine the data type based on the value\r
+ /// </summary>\r
+ private void InferType()\r
+ {\r
+ if(Value == null)\r
+ throw new ArgumentException("No DB2Parameter.Value found");\r
+\r
+ if(Value is IConvertible)\r
+ {\r
+ switch(((IConvertible)Value).GetTypeCode())\r
+ {\r
+ case TypeCode.Char: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+ case TypeCode.Boolean: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_BIT; break;\r
+ case TypeCode.SByte:\r
+ case TypeCode.Byte: dbType = DbType.Byte; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_UTINYINT; break;\r
+ case TypeCode.UInt16:\r
+ case TypeCode.Int16: dbType = DbType.Int16; db2Type = DB2Type.SmallInt; db2DataType = DB2Constants.SQL_SMALLINT; break;\r
+ case TypeCode.UInt32:\r
+ case TypeCode.Int32: dbType = DbType.Int32; db2Type = DB2Type.Integer; db2DataType = DB2Constants.SQL_INTEGER; break;\r
+ case TypeCode.UInt64:\r
+ case TypeCode.Int64: dbType = DbType.Int64; db2Type = DB2Type.BigInt; db2DataType = DB2Constants.SQL_BIGINT; break;\r
+ case TypeCode.Single: dbType = DbType.Single; db2Type = DB2Type.Float; db2DataType = DB2Constants.SQL_REAL; break;\r
+ case TypeCode.Double: dbType = DbType.Double; db2Type = DB2Type.Double; db2DataType = DB2Constants.SQL_DOUBLE; break;\r
+ case TypeCode.Decimal: dbType = DbType.Decimal; db2Type = DB2Type.Decimal; db2DataType = DB2Constants.SQL_DECIMAL; break;\r
+ case TypeCode.DateTime: dbType = DbType.DateTime; db2Type = DB2Type.Timestamp; db2DataType = DB2Constants.SQL_TYPE_TIMESTAMP; break;\r
+ case TypeCode.String: dbType = DbType.String; db2Type = DB2Type.VarChar; db2DataType = DB2Constants.SQL_WCHAR; break;\r
+\r
+ case TypeCode.Object:\r
+ case TypeCode.DBNull:\r
+ case TypeCode.Empty:\r
+ throw new SystemException("Unknown data type");\r
+ default:\r
+ throw new SystemException("Value is of unknown data type");\r
+ }\r
+ }\r
+ else if(Value is byte[])\r
+ {\r
+ dbType = DbType.Binary;\r
+ db2Type = DB2Type.VarBinary;\r
+ db2DataType = DB2Constants.SQL_VARBINARY;\r
+ }\r
+ else if(Value is TimeSpan)\r
+ {\r
+ dbType = DbType.Time;\r
+ db2Type = DB2Type.Time;\r
+ db2DataType = DB2Constants.SQL_TYPE_TIME;\r
+ }\r
+ else\r
+ {\r
+ throw new NotSupportedException("Value is of unsupported data type");\r
+ }\r
+ }\r
+ #endregion\r
+ \r
+ internal void CalculateRequiredmemory()\r
+ {\r
+ //if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))\r
+ //{\r
+ // if(Value == null)\r
+ // throw new ArgumentException("Value missing");\r
+ //}\r
+ if(dbType == DbType.Object)\r
+ {\r
+ if((direction == ParameterDirection.Output) || (direction == ParameterDirection.ReturnValue))\r
+ throw new ArgumentException("Unknown type");\r
+\r
+ if((direction != ParameterDirection.Input) || !Convert.IsDBNull(Value))\r
+ {\r
+ InferType();\r
+ }\r
+ }\r
+ if (db2DataType == DB2Constants.SQL_INTEGER)\r
+ {\r
+ requiredMemory = 4;\r
+ }\r
+ if((db2DataType == DB2Constants.SQL_VARBINARY) ||\r
+ (db2DataType == DB2Constants.SQL_WCHAR))\r
+ {\r
+ if(Size <= 0)\r
+ {\r
+ if(direction != ParameterDirection.Input)\r
+ throw new ArgumentException("Size not specified");\r
+ if(Value == DBNull.Value)\r
+ requiredMemory = 0;\r
+ else if(Value is string)\r
+ requiredMemory = ((string)Value).Length;\r
+ else if(Value is byte[])\r
+ requiredMemory = ((byte[])Value).Length;\r
+ else\r
+ throw new ArgumentException("wrong type!?");\r
+ }\r
+ else\r
+ {\r
+ requiredMemory = Size;\r
+ }\r
+ if(db2DataType == DB2Constants.SQL_WCHAR)\r
+ requiredMemory = (requiredMemory * 2) + 2;\r
+ requiredMemory = (requiredMemory | 0xf) + 1;\r
+ }\r
+ requiredMemory = Math.Max(128, requiredMemory);\r
+ }\r
+\r
+ #region Bind \r
+ ///\r
+ /// Bind this parameter\r
+ /// \r
+ internal short Bind(IntPtr hwndStmt, short paramNum)\r
+ {\r
+ int inLength = requiredMemory;\r
+ db2LastUsedDataType = db2DataType;\r
+ short db2CType = DB2Constants.SQL_C_DEFAULT;\r
+ if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))\r
+ {\r
+ if(Convert.IsDBNull(Value))\r
+ {\r
+ inLength = DB2Constants.SQL_NULL_DATA;\r
+ if((db2DataType == DB2Constants.SQL_UNKNOWN_TYPE) || \r
+ (db2DataType == DB2Constants.SQL_DECIMAL))\r
+ {\r
+ db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;\r
+ db2CType = DB2Constants.SQL_C_WCHAR;\r
+ }\r
+ }\r
+ }\r
+ if((direction == ParameterDirection.Input) || (direction == ParameterDirection.InputOutput))\r
+ {\r
+ switch (db2DataType) \r
+ {\r
+ case DB2Constants.SQL_WCHAR:\r
+ string tmpString = Convert.ToString(Value);\r
+ inLength = tmpString.Length;\r
+ if((Size > 0) && (inLength > Size))\r
+ inLength = Size;\r
+ Marshal.Copy(tmpString.ToCharArray(), 0, internalBuffer, inLength);\r
+ inLength *= 2;\r
+ db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;\r
+ db2CType = DB2Constants.SQL_C_WCHAR;\r
+ if(inLength > 32000)\r
+ {\r
+ db2LastUsedDataType = DB2Constants.SQL_TYPE_BLOB;\r
+ }\r
+ break;\r
+ case DB2Constants.SQL_VARBINARY:\r
+ byte[] tmpBytes = (byte[])Value;\r
+ inLength = tmpBytes.Length;\r
+ if((Size > 0) && (inLength > Size))\r
+ inLength = Size;\r
+ Marshal.Copy(tmpBytes, 0, internalBuffer, inLength);\r
+ db2CType = DB2Constants.SQL_TYPE_BINARY;\r
+ break;\r
+ case DB2Constants.SQL_BIT:\r
+ case DB2Constants.SQL_UTINYINT:\r
+ case DB2Constants.SQL_SMALLINT:\r
+ Marshal.WriteInt16(internalBuffer, Convert.ToInt16(Value));\r
+ db2CType = DB2Constants.SQL_C_SSHORT;\r
+ break;\r
+ case DB2Constants.SQL_INTEGER:\r
+ Marshal.WriteInt32(internalBuffer, Convert.ToInt32(Value));\r
+ db2CType = DB2Constants.SQL_C_SLONG;\r
+ break;\r
+ case DB2Constants.SQL_BIGINT:\r
+ Marshal.WriteInt64(internalBuffer, Convert.ToInt64(Value));\r
+ db2CType = DB2Constants.SQL_C_SBIGINT;\r
+ break;\r
+ case DB2Constants.SQL_REAL:\r
+ Marshal.StructureToPtr((float)Convert.ToDouble(Value), internalBuffer, false);\r
+ db2CType = DB2Constants.SQL_C_TYPE_REAL;\r
+ break;\r
+ case DB2Constants.SQL_DOUBLE:\r
+ Marshal.StructureToPtr(Convert.ToDouble(Value), internalBuffer, false);\r
+ db2CType = DB2Constants.SQL_C_DOUBLE;\r
+ break;\r
+ case DB2Constants.SQL_DECIMAL:\r
+ byte[] tmpDecimalData = System.Text.Encoding.UTF8.GetBytes(\r
+ Convert.ToDecimal(Value).ToString(System.Globalization.CultureInfo.InvariantCulture));\r
+ inLength = Math.Min(tmpDecimalData.Length, requiredMemory);\r
+ Marshal.Copy(tmpDecimalData, 0, internalBuffer, inLength);\r
+ db2LastUsedDataType = DB2Constants.SQL_VARCHAR;\r
+ db2CType = DB2Constants.SQL_C_CHAR;\r
+ break;\r
+ case DB2Constants.SQL_TYPE_DATE:\r
+ DateTime tmpDate = Convert.ToDateTime(Value);\r
+ Marshal.WriteInt16(internalBuffer, 0, (short)tmpDate.Year);\r
+ Marshal.WriteInt16(internalBuffer, 2, (short)tmpDate.Month);\r
+ Marshal.WriteInt16(internalBuffer, 4, (short)tmpDate.Day);\r
+ db2CType = DB2Constants.SQL_C_TYPE_DATE;\r
+ break;\r
+ case DB2Constants.SQL_TYPE_TIMESTAMP:\r
+ DateTime tmpDateTime = Convert.ToDateTime(Value);\r
+ Marshal.WriteInt16(internalBuffer, 0, (short)tmpDateTime.Year);\r
+ Marshal.WriteInt16(internalBuffer, 2, (short)tmpDateTime.Month);\r
+ Marshal.WriteInt16(internalBuffer, 4, (short)tmpDateTime.Day);\r
+ Marshal.WriteInt16(internalBuffer, 6, (short)tmpDateTime.Hour);\r
+ Marshal.WriteInt16(internalBuffer, 8, (short)tmpDateTime.Minute);\r
+ Marshal.WriteInt16(internalBuffer, 10, (short)tmpDateTime.Second);\r
+ Marshal.WriteInt32(internalBuffer, 12, (int)((tmpDateTime.Ticks % 10000000) * 100));\r
+ db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;\r
+ break;\r
+ case DB2Constants.SQL_TYPE_TIME:\r
+ TimeSpan tmpTime = (TimeSpan)Value;\r
+ Marshal.WriteInt16(internalBuffer, 0, (short)tmpTime.Hours);\r
+ Marshal.WriteInt16(internalBuffer, 2, (short)tmpTime.Minutes);\r
+ Marshal.WriteInt16(internalBuffer, 4, (short)tmpTime.Seconds);\r
+ db2CType = DB2Constants.SQL_C_TYPE_TIME;\r
+ break;\r
+ }\r
+ }\r
+ else\r
+ {\r
+ switch (db2DataType) \r
+ {\r
+ case DB2Constants.SQL_WCHAR:\r
+ db2LastUsedDataType = DB2Constants.SQL_VARGRAPHIC;\r
+ db2CType = DB2Constants.SQL_C_WCHAR;\r
+ break;\r
+ case DB2Constants.SQL_VARBINARY:\r
+ db2CType = DB2Constants.SQL_TYPE_BINARY;\r
+ break;\r
+ case DB2Constants.SQL_BIT:\r
+ case DB2Constants.SQL_UTINYINT:\r
+ case DB2Constants.SQL_SMALLINT:\r
+ db2CType = DB2Constants.SQL_C_SSHORT;\r
+ break;\r
+ case DB2Constants.SQL_INTEGER:\r
+ db2CType = DB2Constants.SQL_C_SLONG;\r
+ break;\r
+ case DB2Constants.SQL_BIGINT:\r
+ db2CType = DB2Constants.SQL_C_SBIGINT;\r
+ break;\r
+ case DB2Constants.SQL_REAL:\r
+ db2CType = DB2Constants.SQL_C_TYPE_REAL;\r
+ break;\r
+ case DB2Constants.SQL_DOUBLE:\r
+ db2CType = DB2Constants.SQL_C_DOUBLE;\r
+ break;\r
+ case DB2Constants.SQL_DECIMAL:\r
+ db2LastUsedDataType = DB2Constants.SQL_VARCHAR;\r
+ db2CType = DB2Constants.SQL_C_CHAR;\r
+ break;\r
+ case DB2Constants.SQL_TYPE_DATE:\r
+ db2CType = DB2Constants.SQL_C_TYPE_DATE;\r
+ break;\r
+ case DB2Constants.SQL_TYPE_TIMESTAMP:\r
+ db2CType = DB2Constants.SQL_C_TYPE_TIMESTAMP;\r
+ break;\r
+ case DB2Constants.SQL_TYPE_TIME:\r
+ db2CType = DB2Constants.SQL_C_TYPE_TIME;\r
+ break;\r
+ }\r
+ }\r
+ Marshal.WriteInt32(internalLengthBuffer, inLength);\r
+ short sqlRet = DB2CLIWrapper.SQLBindParameter(hwndStmt, paramNum, db2Direction, \r
+ db2CType, db2LastUsedDataType, Size, Scale,\r
+ internalBuffer, requiredMemory, internalLengthBuffer);\r
+\r
+ return sqlRet;\r
+ }\r
+ #endregion\r
+ object ICloneable.Clone ()\r
+ {\r
+ DB2Parameter clone = new DB2Parameter();\r
+ clone.dbType = dbType;\r
+ clone.db2Type = db2Type;\r
+ clone.db2DataType = db2DataType;\r
+ clone.db2LastUsedDataType = db2LastUsedDataType;\r
+ clone.direction = direction;\r
+ clone.db2Direction = db2Direction;\r
+ clone.nullable = nullable;\r
+ clone.parameterName = parameterName;\r
+ clone.sourceColumn = sourceColumn;\r
+ clone.sourceVersion = sourceVersion;\r
+ clone.dataVal = dataVal;\r
+ clone.scale = scale;\r
+ clone.precision = precision;\r
+ clone.size = size;\r
+ if(dataVal is ICloneable)\r
+ {\r
+ clone.dataVal = ((ICloneable)dataVal).Clone();\r
+ }\r
+ return clone;\r
+ }\r
+ \r
+ internal void GetOutValue()\r
+ {\r
+ int length = Marshal.ReadInt32(internalLengthBuffer);\r
+ if(length == DB2Constants.SQL_NULL_DATA)\r
+ {\r
+ dataVal = DBNull.Value;\r
+ return;\r
+ }\r
+ switch(DB2Type) \r
+ {\r
+ case DB2Type.SmallInt:\r
+ dataVal = Marshal.ReadInt16(internalBuffer);\r
+ break;\r
+ case DB2Type.Integer:\r
+ dataVal = Marshal.ReadInt32(internalBuffer);\r
+ break;\r
+ case DB2Type.BigInt:\r
+ dataVal = Marshal.ReadInt64(internalBuffer);\r
+ break;\r
+ case DB2Type.Double:\r
+ dataVal = Marshal.PtrToStructure(internalBuffer, typeof(Double));\r
+ break;\r
+ case DB2Type.Float:\r
+ dataVal = Marshal.PtrToStructure(internalBuffer, typeof(Single));\r
+ break;\r
+ case DB2Type.Char:\r
+ case DB2Type.VarChar:\r
+ case DB2Type.LongVarChar:\r
+ case DB2Type.Graphic:\r
+ case DB2Type.VarGraphic:\r
+ case DB2Type.LongVarGraphic:\r
+ case DB2Type.Clob:\r
+ case DB2Type.DbClob:\r
+ dataVal = Marshal.PtrToStringUni(internalBuffer, Math.Min(Size, length / 2));\r
+ break;\r
+ case DB2Type.Binary:\r
+ case DB2Type.VarBinary:\r
+ case DB2Type.LongVarBinary:\r
+ case DB2Type.Blob:\r
+ case DB2Type.Datalink:\r
+ length = Math.Min(Size, length);\r
+ dataVal = new byte[length];\r
+ Marshal.Copy(internalBuffer, (byte[])dataVal, 0, length);\r
+ break;\r
+ case DB2Type.Decimal:\r
+ dataVal = decimal.Parse(Marshal.PtrToStringAnsi(internalBuffer, length), \r
+ System.Globalization.CultureInfo.InvariantCulture);\r
+ break;\r
+ case DB2Type.Timestamp:\r
+ DateTime dtTmp = new DateTime(\r
+ Marshal.ReadInt16(internalBuffer, 0), // year\r
+ Marshal.ReadInt16(internalBuffer, 2), // month\r
+ Marshal.ReadInt16(internalBuffer, 4), // day\r
+ Marshal.ReadInt16(internalBuffer, 6), // hour\r
+ Marshal.ReadInt16(internalBuffer, 8), // minute\r
+ Marshal.ReadInt16(internalBuffer, 10));// second\r
+ dataVal = dtTmp.AddTicks(Marshal.ReadInt32(internalBuffer, 12) / 100); // nanoseconds \r
+ break;\r
+ case DB2Type.Date:\r
+ dataVal = new DateTime(\r
+ Marshal.ReadInt16(internalBuffer, 0),\r
+ Marshal.ReadInt16(internalBuffer, 2),\r
+ Marshal.ReadInt16(internalBuffer, 4));\r
+ break;\r
+ case DB2Type.Time:\r
+ dataVal = new TimeSpan(\r
+ Marshal.ReadInt16(internalBuffer, 0), // Hour\r
+ Marshal.ReadInt16(internalBuffer, 2), // Minute\r
+ Marshal.ReadInt16(internalBuffer, 4)); // Second\r
+ break;\r
+\r
+ case DB2Type.Invalid:\r
+ case DB2Type.Real:\r
+ case DB2Type.Numeric:\r
+ case DB2Type.RowId:\r
+ case DB2Type.XmlReader:\r
+ throw new NotImplementedException();\r
+ default:\r
+ throw new NotSupportedException("unknown data type");\r
+ }\r
+ }\r
+ }\r
+}\r