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
|
#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;
using System.Runtime.InteropServices;
namespace DB2ClientCS
{
/// <summary>
/// Summary description for DB2ClientCommand.
/// </summary>
public class DB2ClientCommand : IDbCommand
{
#region Private data members
private string commandText;
private DB2ClientConnection db2Conn;
private DB2ClientTransaction db2Trans;
private int commandTimeout;
private bool prepared = false;
private IntPtr hwndStmt; //Our statement handle
private DB2ClientParameterCollection parameters = new DB2ClientParameterCollection();
#endregion
#region Constructors
/// <summary>
/// Default constructor. Since I'm using CLI functions to do this stuff, we're stuck
/// until we get the database environment handle.
/// </summary>
public DB2ClientCommand()
{
hwndStmt = IntPtr.Zero;
}
public DB2ClientCommand(string commandStr)
{
commandText = commandStr;
hwndStmt = IntPtr.Zero;
}
public DB2ClientCommand(string commandStr, DB2ClientConnection con)
{
commandText = commandStr;
db2Conn = con;
AllocateStatement("Constructor 3");
}
public DB2ClientCommand (string commandStr, DB2ClientConnection con, DB2ClientTransaction trans)
{
commandText = commandStr;
db2Conn = con;
db2Trans = trans;
AllocateStatement("Constructor 4");
}
///DB2 Specific constructors
///
public DB2ClientCommand (IntPtr hwndSt)
{
hwndStmt = hwndSt;
}
public void Dispose()
{
}
#endregion
#region SelfDescribe property
///
/// Property dictates whether or not any paramter markers will get their describe info
/// from the database, or if the user will supply the information
///
bool selfDescribe = false;
public bool SelfDescribe
{
get
{
return selfDescribe;
}
set
{
selfDescribe = value;
}
}
#endregion
#region CommandText property
///
///The query; If it gets set, reset the prepared property
///
public string CommandText
{
get
{
return commandText;
}
set
{
prepared = false;
commandText = value;
}
}
#endregion
#region CommandTimeout property
///
/// The Timeout property states how long we wait for results to return
///
public int CommandTimeout
{
get
{
return commandTimeout;
}
set
{
commandTimeout = value;
}
}
#endregion
#region CommandType property
///
/// I believe this one is left as text all of the time for DB2, but I have to check that...
///
public CommandType CommandType
{
get
{
return CommandType.Text;
}
set
{
///Do nothing
}
}
#endregion
#region Connection property
///
/// The connection we'll be executing on.
///
public IDbConnection Connection
{
get
{
return db2Conn;
}
set
{
db2Conn = (DB2ClientConnection)value;
}
}
#endregion
#region Parameters property
///
/// Parameter list, Not yet implemented
///
public DB2ClientParameterCollection Parameters
{
get
{
return parameters;
}
}
IDataParameterCollection IDbCommand.Parameters
{
get
{
return parameters;
}
}
#endregion
#region Transaction property
///
/// The transaction this command is associated with
///
public IDbTransaction Transaction
{
get
{
return db2Trans;
}
set
{
db2Trans = (DB2ClientTransaction)value;
}
}
#endregion
#region UpdatedRowSource property
///
/// Need to see how this works with DB2...
///
public UpdateRowSource UpdatedRowSource
{
get
{
throw new DB2ClientException ("TBD");
}
set
{
throw new DB2ClientException ("TBD");
}
}
#endregion
#region Statement Handle
///
/// returns the DB2Client statement handle
///
public IntPtr statementHandle
{
get
{
return hwndStmt;
}
}
#endregion
#region AllocateStatement function
///
/// Allocate a statement handle, internal. Pass in the name of the caller for exception info.
/// I think I'll make the handle a property and add a constructor with the handle argument so that
/// statements can be executed on the same handle if need be, though you could accomplish the same by
/// just keeping the command object open.
///
internal void AllocateStatement(string location)
{
short sqlRet;
sqlRet = DB2ClientPrototypes.SQLAllocHandle(DB2ClientConstants.SQL_HANDLE_STMT, db2Conn.DBHandle, ref hwndStmt);
if (sqlRet == DB2ClientConstants.SQL_ERROR)
throw new DB2ClientException(DB2ClientConstants.SQL_HANDLE_DBC, db2Conn.DBHandle, location +": Unable to allocate statement handle.");
}
#endregion
#region Cancel
/// <summary>
/// Attempt to cancel an executing command
/// </summary>
public void Cancel()
{
DB2ClientPrototypes.SQLCancel(hwndStmt);
}
#endregion
#region CreateParameter
///
///Returns a parameter
///
public IDbDataParameter CreateParameter()
{
throw new DB2ClientException("TBD");
}
#endregion
#region ExecuteNonQuery
///
/// ExecuteNonQuery Executes an SQL statement without returning a DataSet
///
public int ExecuteNonQuery()
{
short sqlRet;
if (prepared)
sqlRet = DB2ClientPrototypes.SQLExecute(hwndStmt);
else
sqlRet = DB2ClientPrototypes.SQLExecDirect(hwndStmt, commandText, commandText.Length);
int numRows = 0;
sqlRet = DB2ClientPrototypes.SQLRowCount(hwndStmt, ref numRows); //How many rows affected. numRows will be -1 if we aren't dealing with an Insert, Delete or Update, or if the statement did not execute successfully
///At this point, I think we need to save any results, but not return them
///For now, we will go execute and return the number of rows affected
return numRows;
}
#endregion
#region ExecuteReader calls
///
///ExecuteReader
///
public IDataReader ExecuteReader()
{
DB2ClientDataReader reader;
if (!prepared)
{
ExecuteNonQuery();
reader = new DB2ClientDataReader(db2Conn, this);
}
else
reader = new DB2ClientDataReader(db2Conn, this, true);
return reader;
}
public IDataReader ExecuteReader(CommandBehavior behavior)
{
throw new DB2ClientException("TBD");
}
#endregion
#region ExecuteScalar
///
/// ExecuteScalar
///
public object ExecuteScalar()
{
throw new DB2ClientException("TBD");
}
#endregion
#region Prepare ()
///
/// Prepare a statement against the database
///
public void Prepare ()
{
DB2ClientUtils util = new DB2ClientUtils();
short sqlRet = 0;
IntPtr numParams = IntPtr.Zero;
sqlRet = DB2ClientPrototypes.SQLPrepare(hwndStmt, commandText, commandText.Length);
util.DB2CheckReturn(sqlRet, DB2ClientConstants.SQL_HANDLE_STMT, hwndStmt, "SQLPrepare error.");
short i=1;
foreach ( DB2ClientParameter param in parameters)
{
if (selfDescribe)
{
sqlRet = param.Describe(this.hwndStmt, i);
util.DB2CheckReturn(sqlRet, DB2ClientConstants.SQL_HANDLE_STMT, hwndStmt, "Error binding parameter in DB2ClientCommand: ");
}
sqlRet = param.Bind(this.hwndStmt, i);
util.DB2CheckReturn(sqlRet, DB2ClientConstants.SQL_HANDLE_STMT, hwndStmt, "Error binding parameter in DB2ClientCommand: ");
i++;
}
prepared=true;
}
#endregion
}
}
|