(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Data.DB2Client / Mono.Data.Db2Client / DB2ClientCommand.cs
blob5e723aea2d13267262e8e5cffb69f07660a6e0ac
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
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.
22 #region Licence
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.
29 #endregion
30 using System;
31 using System.Data;
32 using System.Runtime.InteropServices;
35 namespace DB2ClientCS
37 /// <summary>
38 /// Summary description for DB2ClientCommand.
39 /// </summary>
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();
51 #endregion
53 #region Constructors
54 /// <summary>
55 /// Default constructor. Since I'm using CLI functions to do this stuff, we're stuck
56 /// until we get the database environment handle.
57 /// </summary>
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;
70 db2Conn = con;
71 AllocateStatement("Constructor 3");
73 public DB2ClientCommand (string commandStr, DB2ClientConnection con, DB2ClientTransaction trans)
75 commandText = commandStr;
76 db2Conn = con;
77 db2Trans = trans;
78 AllocateStatement("Constructor 4");
80 ///DB2 Specific constructors
81 ///
82 public DB2ClientCommand (IntPtr hwndSt)
84 hwndStmt = hwndSt;
86 public void Dispose()
89 #endregion
90 #region SelfDescribe property
91 ///
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
94 ///
95 bool selfDescribe = false;
96 public bool SelfDescribe
98 get
100 return selfDescribe;
102 set
104 selfDescribe = value;
107 #endregion
108 #region CommandText property
110 ///The query; If it gets set, reset the prepared property
112 public string CommandText
116 return commandText;
120 prepared = false;
121 commandText = value;
124 #endregion
125 #region CommandTimeout property
127 /// The Timeout property states how long we wait for results to return
128 ///
129 public int CommandTimeout
133 return commandTimeout;
135 set
137 commandTimeout = value;
140 #endregion
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...
144 ///
145 public CommandType CommandType
149 return CommandType.Text;
153 ///Do nothing
156 #endregion
157 #region Connection property
159 /// The connection we'll be executing on.
160 ///
161 public IDbConnection Connection
165 return db2Conn;
169 db2Conn = (DB2ClientConnection)value;
172 #endregion
173 #region Parameters property
175 /// Parameter list, Not yet implemented
176 ///
177 public DB2ClientParameterCollection Parameters
181 return parameters;
184 IDataParameterCollection IDbCommand.Parameters
188 return parameters;
191 #endregion
193 #region Transaction property
195 /// The transaction this command is associated with
196 ///
197 public IDbTransaction Transaction
201 return db2Trans;
205 db2Trans = (DB2ClientTransaction)value;
208 #endregion
209 #region UpdatedRowSource property
211 /// Need to see how this works with DB2...
212 ///
213 public UpdateRowSource UpdatedRowSource
217 throw new DB2ClientException ("TBD");
221 throw new DB2ClientException ("TBD");
224 #endregion
225 #region Statement Handle
227 /// returns the DB2Client statement handle
228 ///
229 public IntPtr statementHandle
233 return hwndStmt;
236 #endregion
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.
243 ///
244 internal void AllocateStatement(string location)
246 short sqlRet;
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.");
251 #endregion
252 #region Cancel
253 /// <summary>
254 /// Attempt to cancel an executing command
255 /// </summary>
256 public void Cancel()
258 DB2ClientPrototypes.SQLCancel(hwndStmt);
260 #endregion
261 #region CreateParameter
263 ///Returns a parameter
265 public IDbDataParameter CreateParameter()
267 throw new DB2ClientException("TBD");
269 #endregion
270 #region ExecuteNonQuery
272 /// ExecuteNonQuery Executes an SQL statement without returning a DataSet
273 ///
274 public int ExecuteNonQuery()
276 short sqlRet;
277 if (prepared)
278 sqlRet = DB2ClientPrototypes.SQLExecute(hwndStmt);
279 else
280 sqlRet = DB2ClientPrototypes.SQLExecDirect(hwndStmt, commandText, commandText.Length);
282 int numRows = 0;
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
286 return numRows;
288 #endregion
289 #region ExecuteReader calls
291 ///ExecuteReader
293 public IDataReader ExecuteReader()
295 DB2ClientDataReader reader;
297 if (!prepared)
299 ExecuteNonQuery();
300 reader = new DB2ClientDataReader(db2Conn, this);
302 else
303 reader = new DB2ClientDataReader(db2Conn, this, true);
305 return reader;
308 public IDataReader ExecuteReader(CommandBehavior behavior)
310 throw new DB2ClientException("TBD");
312 #endregion
313 #region ExecuteScalar
315 /// ExecuteScalar
316 ///
317 public object ExecuteScalar()
319 throw new DB2ClientException("TBD");
321 #endregion
323 #region Prepare ()
325 /// Prepare a statement against the database
326 ///
327 public void Prepare ()
329 DB2ClientUtils util = new DB2ClientUtils();
330 short sqlRet = 0;
332 IntPtr numParams = IntPtr.Zero;
333 sqlRet = DB2ClientPrototypes.SQLPrepare(hwndStmt, commandText, commandText.Length);
334 util.DB2CheckReturn(sqlRet, DB2ClientConstants.SQL_HANDLE_STMT, hwndStmt, "SQLPrepare error.");
335 short i=1;
336 foreach ( DB2ClientParameter param in parameters)
338 if (selfDescribe)
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: ");
345 i++;
347 prepared=true;
349 #endregion