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
24 /// Copyright 2003 By Christopher Bockner
25 /// Released under the terms of the MIT/X11 Licence
26 /// Please refer to the Licence.txt file that should be distributed with this package
27 /// This software requires that DB2 client software be installed correctly on the machine
28 /// (or instance) on which the driver is running.
32 using System
.Runtime
.InteropServices
;
38 /// Summary description for DB2ClientCommand.
40 public class DB2ClientCommand
: IDbCommand
42 #region Private data members
43 private string commandText
;
44 private DB2ClientConnection db2Conn
;
45 private DB2ClientTransaction db2Trans
;
46 private int commandTimeout
;
47 private bool prepared
= false;
48 private IntPtr hwndStmt
; //Our statement handle
49 private DB2ClientParameterCollection parameters
= new DB2ClientParameterCollection();
55 /// Default constructor. Since I'm using CLI functions to do this stuff, we're stuck
56 /// until we get the database environment handle.
58 public DB2ClientCommand()
60 hwndStmt
= IntPtr
.Zero
;
62 public DB2ClientCommand(string commandStr
)
64 commandText
= commandStr
;
65 hwndStmt
= IntPtr
.Zero
;
67 public DB2ClientCommand(string commandStr
, DB2ClientConnection con
)
69 commandText
= commandStr
;
71 AllocateStatement("Constructor 3");
73 public DB2ClientCommand (string commandStr
, DB2ClientConnection con
, DB2ClientTransaction trans
)
75 commandText
= commandStr
;
78 AllocateStatement("Constructor 4");
80 ///DB2 Specific constructors
82 public DB2ClientCommand (IntPtr hwndSt
)
90 #region SelfDescribe property
92 /// Property dictates whether or not any paramter markers will get their describe info
93 /// from the database, or if the user will supply the information
95 bool selfDescribe
= false;
96 public bool SelfDescribe
104 selfDescribe
= value;
108 #region CommandText property
110 ///The query; If it gets set, reset the prepared property
112 public string CommandText
125 #region CommandTimeout property
127 /// The Timeout property states how long we wait for results to return
129 public int CommandTimeout
133 return commandTimeout
;
137 commandTimeout
= value;
141 #region CommandType property
143 /// I believe this one is left as text all of the time for DB2, but I have to check that...
145 public CommandType CommandType
149 return CommandType
.Text
;
157 #region Connection property
159 /// The connection we'll be executing on.
161 public IDbConnection Connection
169 db2Conn
= (DB2ClientConnection
)value;
173 #region Parameters property
175 /// Parameter list, Not yet implemented
177 public DB2ClientParameterCollection Parameters
184 IDataParameterCollection IDbCommand
.Parameters
193 #region Transaction property
195 /// The transaction this command is associated with
197 public IDbTransaction Transaction
205 db2Trans
= (DB2ClientTransaction
)value;
209 #region UpdatedRowSource property
211 /// Need to see how this works with DB2...
213 public UpdateRowSource UpdatedRowSource
217 throw new DB2ClientException ("TBD");
221 throw new DB2ClientException ("TBD");
225 #region Statement Handle
227 /// returns the DB2Client statement handle
229 public IntPtr statementHandle
237 #region AllocateStatement function
239 /// Allocate a statement handle, internal. Pass in the name of the caller for exception info.
240 /// I think I'll make the handle a property and add a constructor with the handle argument so that
241 /// statements can be executed on the same handle if need be, though you could accomplish the same by
242 /// just keeping the command object open.
244 internal void AllocateStatement(string location
)
247 sqlRet
= DB2ClientPrototypes
.SQLAllocHandle(DB2ClientConstants
.SQL_HANDLE_STMT
, db2Conn
.DBHandle
, ref hwndStmt
);
248 if (sqlRet
== DB2ClientConstants
.SQL_ERROR
)
249 throw new DB2ClientException(DB2ClientConstants
.SQL_HANDLE_DBC
, db2Conn
.DBHandle
, location
+": Unable to allocate statement handle.");
254 /// Attempt to cancel an executing command
258 DB2ClientPrototypes
.SQLCancel(hwndStmt
);
261 #region CreateParameter
263 ///Returns a parameter
265 public IDbDataParameter
CreateParameter()
267 throw new DB2ClientException("TBD");
270 #region ExecuteNonQuery
272 /// ExecuteNonQuery Executes an SQL statement without returning a DataSet
274 public int ExecuteNonQuery()
278 sqlRet
= DB2ClientPrototypes
.SQLExecute(hwndStmt
);
280 sqlRet
= DB2ClientPrototypes
.SQLExecDirect(hwndStmt
, commandText
, commandText
.Length
);
283 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
284 ///At this point, I think we need to save any results, but not return them
285 ///For now, we will go execute and return the number of rows affected
289 #region ExecuteReader calls
293 public IDataReader
ExecuteReader()
295 DB2ClientDataReader reader
;
300 reader
= new DB2ClientDataReader(db2Conn
, this);
303 reader
= new DB2ClientDataReader(db2Conn
, this, true);
308 public IDataReader
ExecuteReader(CommandBehavior behavior
)
310 throw new DB2ClientException("TBD");
313 #region ExecuteScalar
317 public object ExecuteScalar()
319 throw new DB2ClientException("TBD");
325 /// Prepare a statement against the database
327 public void Prepare ()
329 DB2ClientUtils util
= new DB2ClientUtils();
332 IntPtr numParams
= IntPtr
.Zero
;
333 sqlRet
= DB2ClientPrototypes
.SQLPrepare(hwndStmt
, commandText
, commandText
.Length
);
334 util
.DB2CheckReturn(sqlRet
, DB2ClientConstants
.SQL_HANDLE_STMT
, hwndStmt
, "SQLPrepare error.");
336 foreach ( DB2ClientParameter param
in parameters
)
340 sqlRet
= param
.Describe(this.hwndStmt
, i
);
341 util
.DB2CheckReturn(sqlRet
, DB2ClientConstants
.SQL_HANDLE_STMT
, hwndStmt
, "Error binding parameter in DB2ClientCommand: ");
343 sqlRet
= param
.Bind(this.hwndStmt
, i
);
344 util
.DB2CheckReturn(sqlRet
, DB2ClientConstants
.SQL_HANDLE_STMT
, hwndStmt
, "Error binding parameter in DB2ClientCommand: ");