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
|
//
// 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.
//
#region Licence
/// DB2DriverCS - A DB2 driver for .Net
/// Copyright 2003 By Christopher Bockner
/// Released under the terms of the MIT/X11 Licence
/// Please refer to the Licence.txt file that should be distributed with this package
/// This software requires that DB2 client software be installed correctly on the machine
/// (or instance) on which the driver is running.
#endregion
using System;
using System.Data;
namespace DB2ClientCS
{
/// <summary>
/// Parameter object for DB2 client
/// </summary>
public sealed class DB2ClientParameter : MarshalByRefObject, IDbDataParameter, IDataParameter, ICloneable
{
private DbType dbType;
private IntPtr db2DataType;
private ParameterDirection direction;
private bool nullable;
private string parameterName;
private string sourceColumn;
private IntPtr paramSize;
private IntPtr decimalDigits;
private DataRowVersion sourceVersion;
object dataVal;
byte[] bDataVal;
private int iDataVal;
private byte scale, precision;
private int size;
bool selfDescribe = false;
#region Contructors and destructors
public DB2ClientParameter()
{
}
public DB2ClientParameter (string name)
{
parameterName = name;
}
public DB2ClientParameter(string name, DbType type)
{
parameterName = name;
dbType = type;
}
public DB2ClientParameter(string name, object data)
{
parameterName = name;
this.Value = data;
}
public DB2ClientParameter(string name, DbType type, string columnName)
{
parameterName = name;
dbType = type;
sourceColumn = columnName;
}
public DB2ClientParameter(string name, int db2Type)
{
parameterName = name;
dbType = inferTypeFromDB2Type(db2Type);
db2DataType = new IntPtr(db2Type);
}
#endregion
#region Properties
#region DbType Property
///
/// Parameter data type
///
public DbType DbType
{
get
{
return dbType;
}
set
{
dbType = value;
}
}
#endregion
#region Direction
///
/// In or out parameter, or both
///
public ParameterDirection Direction
{
get
{
return direction;
}
set
{
direction = value;
}
}
#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;
DbType = inferType(dataVal);
// Load buffer with new value
if (dbType==DbType.Int32)
iDataVal=(int) value;
else
{
// Treat everything else as a string
// Init string buffer
if (bDataVal==null || bDataVal.Length< (((int)paramSize>20)?(int)paramSize:20) )
bDataVal=new byte[((int)paramSize>20)?(int)paramSize:20];
else
bDataVal.Initialize();
// Convert value into string and store into buffer
byte[] strValueBuffer=System.Text.Encoding.ASCII.GetBytes(dataVal.ToString());
strValueBuffer.CopyTo(bDataVal,0);
}
}
}
#endregion
#endregion
#region inferType Method
/// <summary>
/// Determine the data type based on the value
/// </summary>
private DbType inferType (object Data)
{
switch (Type.GetTypeCode(Data.GetType()))
{
case TypeCode.Empty:
throw new SystemException("Invalid data type");
case TypeCode.Object:
return DbType.Object;
case TypeCode.DBNull:
case TypeCode.Char:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
// Throw a SystemException for unsupported data types.
throw new SystemException("Invalid data type");
case TypeCode.Boolean:
return DbType.Boolean;
case TypeCode.Byte:
return DbType.Byte;
case TypeCode.Int16:
return DbType.Int16;
case TypeCode.Int32:
return DbType.Int32;
case TypeCode.Int64:
return DbType.Int64;
case TypeCode.Single:
return DbType.Single;
case TypeCode.Double:
return DbType.Double;
case TypeCode.Decimal:
return DbType.Decimal;
case TypeCode.DateTime:
return DbType.DateTime;
case TypeCode.String:
return DbType.String;
default:
throw new SystemException("Value is of unknown data type");
}
}
#endregion
#region inferTypeFromDB2Type
///
/// Determine the DbType from the SQL type returned by SQLDescribeParam
///
private DbType inferTypeFromDB2Type(int db2Type)
{
switch (db2Type)
{
case DB2ClientConstants.SQL_CHAR:
return DbType.AnsiString;
case DB2ClientConstants.SQL_NUMERIC:
case DB2ClientConstants.SQL_DECIMAL:
return DbType.Decimal;
case DB2ClientConstants.SQL_DATETIME:
return DbType.DateTime;
case DB2ClientConstants.SQL_FLOAT:
case DB2ClientConstants.SQL_DOUBLE:
return DbType.Double;
case DB2ClientConstants.SQL_INTEGER:
return DbType.Int32;
case DB2ClientConstants.SQL_SMALLINT:
return DbType.Int16;
case DB2ClientConstants.SQL_VARCHAR:
return DbType.String;
case DB2ClientConstants.SQL_USER_DEFINED_TYPE:
return DbType.Object;
default:
throw new SystemException("DB2 Data type is unknown.");
}
}
#endregion
#region Describe
///
/// Describe the parameter. Use at the caller's discretion
///
public short Describe(IntPtr hwndStmt, short paramNum)
{
IntPtr nullable = IntPtr.Zero;
paramSize = IntPtr.Zero;
decimalDigits = IntPtr.Zero;
short sqlRet = 0;
sqlRet = DB2ClientPrototypes.SQLDescribeParam(hwndStmt, paramNum, ref db2DataType, ref paramSize, ref decimalDigits, ref nullable);
return sqlRet;
}
#endregion
#region Bind
///
/// Bind this parameter
///
public short Bind(IntPtr hwndStmt, short paramNum)
{
short sqlRet = 0;
switch ((int)db2DataType)
{
case DB2ClientConstants.SQL_DECIMAL: //These types are treated as SQL_C_CHAR for binding purposes
case DB2ClientConstants.SQL_TYPE_DATE:
case DB2ClientConstants.SQL_TYPE_TIME:
case DB2ClientConstants.SQL_TYPE_TIMESTAMP:
case DB2ClientConstants.SQL_VARCHAR:
case DB2ClientConstants.SQL_CHAR:
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);
break;
default:
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);
break;
}
return sqlRet;
}
#endregion
object ICloneable.Clone ()
{
throw new NotImplementedException ();
}
}
}
|