OdbcCommand.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //
  2. // System.Data.Odbc.OdbcCommand
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System.ComponentModel;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.Collections;
  35. using System.Runtime.InteropServices;
  36. namespace System.Data.Odbc
  37. {
  38. /// <summary>
  39. /// Represents an SQL statement or stored procedure to execute against a data source.
  40. /// </summary>
  41. [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OdbcCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
  42. [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
  43. public sealed class OdbcCommand : Component, ICloneable, IDbCommand
  44. {
  45. #region Fields
  46. string commandText;
  47. int timeout;
  48. CommandType commandType;
  49. OdbcConnection connection;
  50. OdbcParameterCollection parameters;
  51. OdbcTransaction transaction;
  52. bool designTimeVisible;
  53. bool prepared=false;
  54. OdbcDataReader dataReader;
  55. IntPtr hstmt;
  56. #endregion // Fields
  57. #region Constructors
  58. public OdbcCommand ()
  59. {
  60. commandText = String.Empty;
  61. timeout = 30; // default timeout
  62. commandType = CommandType.Text;
  63. connection = null;
  64. parameters = new OdbcParameterCollection ();
  65. transaction = null;
  66. designTimeVisible = false;
  67. dataReader = null;
  68. }
  69. public OdbcCommand (string cmdText) : this ()
  70. {
  71. CommandText = cmdText;
  72. }
  73. public OdbcCommand (string cmdText, OdbcConnection connection)
  74. : this (cmdText)
  75. {
  76. Connection = connection;
  77. }
  78. public OdbcCommand (string cmdText,
  79. OdbcConnection connection,
  80. OdbcTransaction transaction) : this (cmdText, connection)
  81. {
  82. this.transaction = transaction;
  83. }
  84. #endregion // Constructors
  85. #region Properties
  86. internal IntPtr hStmt
  87. {
  88. get { return hstmt; }
  89. }
  90. [OdbcCategory ("Data")]
  91. [DefaultValue ("")]
  92. [OdbcDescriptionAttribute ("Command text to execute")]
  93. [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  94. [RefreshPropertiesAttribute (RefreshProperties.All)]
  95. public string CommandText
  96. {
  97. get {
  98. return commandText;
  99. }
  100. set {
  101. prepared=false;
  102. commandText = value;
  103. }
  104. }
  105. [OdbcDescriptionAttribute ("Time to wait for command to execute")]
  106. [DefaultValue (30)]
  107. public int CommandTimeout {
  108. get {
  109. return timeout;
  110. }
  111. set {
  112. timeout = value;
  113. }
  114. }
  115. [OdbcCategory ("Data")]
  116. [DefaultValue ("Text")]
  117. [OdbcDescriptionAttribute ("How to interpret the CommandText")]
  118. [RefreshPropertiesAttribute (RefreshProperties.All)]
  119. public CommandType CommandType {
  120. get {
  121. return commandType;
  122. }
  123. set {
  124. commandType = value;
  125. }
  126. }
  127. [OdbcCategory ("Behavior")]
  128. [OdbcDescriptionAttribute ("Connection used by the command")]
  129. [DefaultValue (null)]
  130. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  131. public OdbcConnection Connection {
  132. get {
  133. return connection;
  134. }
  135. set {
  136. connection = value;
  137. }
  138. }
  139. [BrowsableAttribute (false)]
  140. [DesignOnlyAttribute (true)]
  141. [DefaultValue (true)]
  142. public bool DesignTimeVisible {
  143. get {
  144. return designTimeVisible;
  145. }
  146. set {
  147. designTimeVisible = value;
  148. }
  149. }
  150. [OdbcCategory ("Data")]
  151. [OdbcDescriptionAttribute ("The parameters collection")]
  152. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  153. public OdbcParameterCollection Parameters {
  154. get {
  155. return parameters;
  156. }
  157. }
  158. [BrowsableAttribute (false)]
  159. [OdbcDescriptionAttribute ("The transaction used by the command")]
  160. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  161. public OdbcTransaction Transaction {
  162. get {
  163. return transaction;
  164. }
  165. set {
  166. transaction = value;
  167. }
  168. }
  169. [OdbcCategory ("Behavior")]
  170. [DefaultValue (UpdateRowSource.Both)]
  171. [OdbcDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
  172. public UpdateRowSource UpdatedRowSource {
  173. [MonoTODO]
  174. get {
  175. throw new NotImplementedException ();
  176. }
  177. [MonoTODO]
  178. set {
  179. throw new NotImplementedException ();
  180. }
  181. }
  182. IDbConnection IDbCommand.Connection {
  183. get {
  184. return Connection;
  185. }
  186. set {
  187. Connection = (OdbcConnection) value;
  188. }
  189. }
  190. IDataParameterCollection IDbCommand.Parameters {
  191. get {
  192. return Parameters;
  193. }
  194. }
  195. IDbTransaction IDbCommand.Transaction {
  196. get {
  197. return (IDbTransaction) Transaction;
  198. }
  199. set {
  200. if (value is OdbcTransaction)
  201. {
  202. Transaction = (OdbcTransaction)value;
  203. }
  204. else
  205. {
  206. throw new ArgumentException ();
  207. }
  208. }
  209. }
  210. #endregion // Properties
  211. #region Methods
  212. public void Cancel ()
  213. {
  214. if (hstmt!=IntPtr.Zero)
  215. {
  216. OdbcReturn Ret=libodbc.SQLCancel(hstmt);
  217. if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
  218. throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
  219. }
  220. else
  221. throw new InvalidOperationException();
  222. }
  223. public OdbcParameter CreateParameter ()
  224. {
  225. return new OdbcParameter ();
  226. }
  227. IDbDataParameter IDbCommand.CreateParameter ()
  228. {
  229. return CreateParameter ();
  230. }
  231. [MonoTODO]
  232. protected override void Dispose (bool disposing)
  233. {
  234. }
  235. private void ExecSQL(string sql)
  236. {
  237. OdbcReturn ret;
  238. if ((parameters.Count>0) && !prepared)
  239. Prepare();
  240. if (prepared)
  241. {
  242. ret=libodbc.SQLExecute(hstmt);
  243. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  244. throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
  245. }
  246. else
  247. {
  248. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  249. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  250. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  251. ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
  252. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  253. throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
  254. }
  255. }
  256. public int ExecuteNonQuery ()
  257. {
  258. return ExecuteNonQuery (true);
  259. }
  260. private int ExecuteNonQuery (bool freeHandle)
  261. {
  262. int records = 0;
  263. if (connection == null)
  264. throw new InvalidOperationException ();
  265. if (connection.State == ConnectionState.Closed)
  266. throw new InvalidOperationException ();
  267. // FIXME: a third check is mentioned in .NET docs
  268. ExecSQL(CommandText);
  269. // .NET documentation says that except for INSERT, UPDATE and
  270. // DELETE where the return value is the number of rows affected
  271. // for the rest of the commands the return value is -1.
  272. if ((CommandText.ToUpper().IndexOf("UPDATE")!=-1) ||
  273. (CommandText.ToUpper().IndexOf("INSERT")!=-1) ||
  274. (CommandText.ToUpper().IndexOf("DELETE")!=-1)) {
  275. int numrows = 0;
  276. OdbcReturn ret = libodbc.SQLRowCount(hstmt,ref numrows);
  277. records = numrows;
  278. }
  279. else
  280. records = -1;
  281. if (freeHandle && !prepared) {
  282. OdbcReturn ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  283. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  284. throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,hstmt));
  285. }
  286. return records;
  287. }
  288. public void Prepare()
  289. {
  290. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  291. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  292. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  293. ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
  294. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  295. throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
  296. int i=1;
  297. foreach (OdbcParameter p in parameters)
  298. {
  299. p.Bind(hstmt, i);
  300. i++;
  301. }
  302. prepared=true;
  303. }
  304. public OdbcDataReader ExecuteReader ()
  305. {
  306. return ExecuteReader (CommandBehavior.Default);
  307. }
  308. IDataReader IDbCommand.ExecuteReader ()
  309. {
  310. return ExecuteReader ();
  311. }
  312. public OdbcDataReader ExecuteReader (CommandBehavior behavior)
  313. {
  314. ExecuteNonQuery(false);
  315. dataReader=new OdbcDataReader(this,behavior);
  316. return dataReader;
  317. }
  318. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  319. {
  320. return ExecuteReader (behavior);
  321. }
  322. public object ExecuteScalar ()
  323. {
  324. object val = null;
  325. OdbcDataReader reader=ExecuteReader();
  326. try
  327. {
  328. if (reader.Read ())
  329. val=reader[0];
  330. }
  331. finally
  332. {
  333. reader.Close();
  334. }
  335. return val;
  336. }
  337. [MonoTODO]
  338. object ICloneable.Clone ()
  339. {
  340. throw new NotImplementedException ();
  341. }
  342. public void ResetCommandTimeout ()
  343. {
  344. timeout = 30;
  345. }
  346. #endregion
  347. }
  348. }