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:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
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.
23 /// DB2DriverCS - A DB2 driver for .Net
\r
24 /// Copyright 2003 By Christopher Bockner
\r
25 /// Released under the terms of the MIT/X11 Licence
\r
26 /// Please refer to the Licence.txt file that should be distributed with this package
\r
27 /// This software requires that DB2 client software be installed correctly on the machine
\r
28 /// (or instance) on which the driver is running.
\r
33 namespace DB2ClientCS
\r
36 /// Parameter object for DB2 client
\r
38 public sealed class DB2ClientParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
\r
40 private DbType dbType;
\r
41 private IntPtr db2DataType;
\r
42 private ParameterDirection direction;
\r
43 private bool nullable;
\r
44 private string parameterName;
\r
45 private string sourceColumn;
\r
46 private IntPtr paramSize;
\r
47 private IntPtr decimalDigits;
\r
48 private DataRowVersion sourceVersion;
\r
51 private int iDataVal;
\r
52 private byte scale, precision;
\r
54 bool selfDescribe = false;
\r
56 #region Contructors and destructors
\r
57 public DB2ClientParameter()
\r
60 public DB2ClientParameter (string name)
\r
62 parameterName = name;
\r
65 public DB2ClientParameter(string name, DbType type)
\r
67 parameterName = name;
\r
70 public DB2ClientParameter(string name, object data)
\r
72 parameterName = name;
\r
75 public DB2ClientParameter(string name, DbType type, string columnName)
\r
77 parameterName = name;
\r
79 sourceColumn = columnName;
\r
81 public DB2ClientParameter(string name, int db2Type)
\r
83 parameterName = name;
\r
84 dbType = inferTypeFromDB2Type(db2Type);
\r
85 db2DataType = new IntPtr(db2Type);
\r
89 #region DbType Property
\r
91 /// Parameter data type
\r
93 public DbType DbType
\r
107 /// In or out parameter, or both
\r
109 public ParameterDirection Direction
\r
123 /// Does this parameter support a null value
\r
125 public bool IsNullable
\r
137 #region ParameterName
\r
138 public string ParameterName
\r
142 return parameterName;
\r
146 parameterName = value;
\r
150 #region SourceColumn
\r
152 /// Gets or sets the name of the source column that is mapped to the DataSet
\r
154 public string SourceColumn
\r
158 return sourceColumn;
\r
162 sourceColumn = value;
\r
166 #region SourceVersion
\r
168 /// DataRowVersion property
\r
170 public DataRowVersion SourceVersion
\r
174 return sourceVersion;
\r
178 sourceVersion = value;
\r
182 #region IDbDataParameter properties
\r
183 public byte Precision
\r
191 precision = value;
\r
221 /// The actual parameter data
\r
223 public object Value
\r
231 this.dataVal = value;
\r
232 DbType = inferType(dataVal);
\r
233 // Load buffer with new value
\r
234 if (dbType==DbType.Int32)
\r
235 iDataVal=(int) value;
\r
238 // Treat everything else as a string
\r
239 // Init string buffer
\r
240 if (bDataVal==null || bDataVal.Length< (((int)paramSize>20)?(int)paramSize:20) )
\r
241 bDataVal=new byte[((int)paramSize>20)?(int)paramSize:20];
\r
243 bDataVal.Initialize();
\r
244 // Convert value into string and store into buffer
\r
245 byte[] strValueBuffer=System.Text.Encoding.ASCII.GetBytes(dataVal.ToString());
\r
246 strValueBuffer.CopyTo(bDataVal,0);
\r
252 #region inferType Method
\r
254 /// Determine the data type based on the value
\r
256 private DbType inferType (object Data)
\r
258 switch (Type.GetTypeCode(Data.GetType()))
\r
260 case TypeCode.Empty:
\r
261 throw new SystemException("Invalid data type");
\r
263 case TypeCode.Object:
\r
264 return DbType.Object;
\r
266 case TypeCode.DBNull:
\r
267 case TypeCode.Char:
\r
268 case TypeCode.SByte:
\r
269 case TypeCode.UInt16:
\r
270 case TypeCode.UInt32:
\r
271 case TypeCode.UInt64:
\r
272 // Throw a SystemException for unsupported data types.
\r
273 throw new SystemException("Invalid data type");
\r
275 case TypeCode.Boolean:
\r
276 return DbType.Boolean;
\r
278 case TypeCode.Byte:
\r
279 return DbType.Byte;
\r
281 case TypeCode.Int16:
\r
282 return DbType.Int16;
\r
284 case TypeCode.Int32:
\r
285 return DbType.Int32;
\r
287 case TypeCode.Int64:
\r
288 return DbType.Int64;
\r
290 case TypeCode.Single:
\r
291 return DbType.Single;
\r
293 case TypeCode.Double:
\r
294 return DbType.Double;
\r
296 case TypeCode.Decimal:
\r
297 return DbType.Decimal;
\r
299 case TypeCode.DateTime:
\r
300 return DbType.DateTime;
\r
302 case TypeCode.String:
\r
303 return DbType.String;
\r
306 throw new SystemException("Value is of unknown data type");
\r
310 #region inferTypeFromDB2Type
\r
312 /// Determine the DbType from the SQL type returned by SQLDescribeParam
\r
314 private DbType inferTypeFromDB2Type(int db2Type)
\r
318 case DB2ClientConstants.SQL_CHAR:
\r
319 return DbType.AnsiString;
\r
320 case DB2ClientConstants.SQL_NUMERIC:
\r
321 case DB2ClientConstants.SQL_DECIMAL:
\r
322 return DbType.Decimal;
\r
323 case DB2ClientConstants.SQL_DATETIME:
\r
324 return DbType.DateTime;
\r
325 case DB2ClientConstants.SQL_FLOAT:
\r
326 case DB2ClientConstants.SQL_DOUBLE:
\r
327 return DbType.Double;
\r
328 case DB2ClientConstants.SQL_INTEGER:
\r
329 return DbType.Int32;
\r
330 case DB2ClientConstants.SQL_SMALLINT:
\r
331 return DbType.Int16;
\r
332 case DB2ClientConstants.SQL_VARCHAR:
\r
333 return DbType.String;
\r
334 case DB2ClientConstants.SQL_USER_DEFINED_TYPE:
\r
335 return DbType.Object;
\r
337 throw new SystemException("DB2 Data type is unknown.");
\r
343 /// Describe the parameter. Use at the caller's discretion
\r
345 public short Describe(IntPtr hwndStmt, short paramNum)
\r
347 IntPtr nullable = IntPtr.Zero;
\r
348 paramSize = IntPtr.Zero;
\r
349 decimalDigits = IntPtr.Zero;
\r
352 sqlRet = DB2ClientPrototypes.SQLDescribeParam(hwndStmt, paramNum, ref db2DataType, ref paramSize, ref decimalDigits, ref nullable);
\r
358 /// Bind this parameter
\r
360 public short Bind(IntPtr hwndStmt, short paramNum)
\r
364 switch ((int)db2DataType)
\r
366 case DB2ClientConstants.SQL_DECIMAL: //These types are treated as SQL_C_CHAR for binding purposes
\r
367 case DB2ClientConstants.SQL_TYPE_DATE:
\r
368 case DB2ClientConstants.SQL_TYPE_TIME:
\r
369 case DB2ClientConstants.SQL_TYPE_TIMESTAMP:
\r
370 case DB2ClientConstants.SQL_VARCHAR:
\r
371 case DB2ClientConstants.SQL_CHAR:
\r
372 sqlRet = DB2ClientPrototypes.SQLBindParameter(hwndStmt, (ushort)paramNum, DB2ClientConstants.SQL_PARAM_INPUT, DB2ClientConstants.SQL_C_DEFAULT, (short)db2DataType, Convert.ToUInt32((int)paramSize) , (short) decimalDigits, bDataVal, 0, 0);
\r
375 sqlRet = DB2ClientPrototypes.SQLBindParameter(hwndStmt, (ushort)paramNum, DB2ClientConstants.SQL_PARAM_INPUT, DB2ClientConstants.SQL_C_DEFAULT, (short)db2DataType, Convert.ToUInt32((int)paramSize) , 0, ref iDataVal, 0, 0);
\r
381 object ICloneable.Clone ()
\r
383 throw new NotImplementedException ();
\r