OdbcCommand.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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;
  32. using System.ComponentModel;
  33. using System.Data;
  34. using System.Data.Common;
  35. #if NET_2_0
  36. using System.Data.ProviderBase;
  37. #endif // NET_2_0
  38. using System.Collections;
  39. using System.Runtime.InteropServices;
  40. namespace System.Data.Odbc
  41. {
  42. /// <summary>
  43. /// Represents an SQL statement or stored procedure to execute against a data source.
  44. /// </summary>
  45. [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OdbcCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
  46. [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
  47. #if NET_2_0
  48. public sealed class OdbcCommand : DbCommandBase, ICloneable
  49. #else
  50. public sealed class OdbcCommand : Component, ICloneable, IDbCommand
  51. #endif //NET_2_0
  52. {
  53. #region Fields
  54. #if ONLY_1_1
  55. string commandText;
  56. int timeout;
  57. CommandType commandType;
  58. #endif // ONLY_1_1
  59. OdbcConnection connection;
  60. OdbcTransaction transaction;
  61. OdbcParameterCollection _parameters;
  62. bool designTimeVisible;
  63. bool prepared=false;
  64. OdbcDataReader dataReader;
  65. IntPtr hstmt;
  66. #endregion // Fields
  67. #region Constructors
  68. public OdbcCommand ()
  69. {
  70. this.CommandText = String.Empty;
  71. this.CommandTimeout = 30; // default timeout
  72. this.CommandType = CommandType.Text;
  73. Connection = null;
  74. _parameters = new OdbcParameterCollection ();
  75. Transaction = null;
  76. designTimeVisible = false;
  77. dataReader = null;
  78. }
  79. public OdbcCommand (string cmdText) : this ()
  80. {
  81. CommandText = cmdText;
  82. }
  83. public OdbcCommand (string cmdText, OdbcConnection connection)
  84. : this (cmdText)
  85. {
  86. Connection = connection;
  87. }
  88. public OdbcCommand (string cmdText,
  89. OdbcConnection connection,
  90. OdbcTransaction transaction) : this (cmdText, connection)
  91. {
  92. this.Transaction = transaction;
  93. }
  94. #endregion // Constructors
  95. #region Properties
  96. internal IntPtr hStmt
  97. {
  98. get { return hstmt; }
  99. }
  100. #if ONLY_1_1
  101. [OdbcCategory ("Data")]
  102. [DefaultValue ("")]
  103. [OdbcDescriptionAttribute ("Command text to execute")]
  104. [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  105. [RefreshPropertiesAttribute (RefreshProperties.All)]
  106. public string CommandText
  107. {
  108. get {
  109. return commandText;
  110. }
  111. set {
  112. prepared=false;
  113. commandText = value;
  114. }
  115. }
  116. #else
  117. [OdbcCategory ("Data")]
  118. [DefaultValue ("")]
  119. [OdbcDescriptionAttribute ("Command text to execute")]
  120. [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  121. [RefreshPropertiesAttribute (RefreshProperties.All)]
  122. public override string CommandText
  123. {
  124. get {
  125. return base.CommandText;
  126. }
  127. set {
  128. prepared=false;
  129. base.CommandText = value;
  130. }
  131. }
  132. #endif // ONLY_1_1
  133. #if ONLY_1_1
  134. [OdbcDescriptionAttribute ("Time to wait for command to execute")]
  135. [DefaultValue (30)]
  136. public int CommandTimeout {
  137. get {
  138. return timeout;
  139. }
  140. set {
  141. timeout = value;
  142. }
  143. }
  144. [OdbcCategory ("Data")]
  145. [DefaultValue ("Text")]
  146. [OdbcDescriptionAttribute ("How to interpret the CommandText")]
  147. [RefreshPropertiesAttribute (RefreshProperties.All)]
  148. public CommandType CommandType {
  149. get {
  150. return commandType;
  151. }
  152. set {
  153. commandType = value;
  154. }
  155. }
  156. [OdbcCategory ("Behavior")]
  157. [OdbcDescriptionAttribute ("Connection used by the command")]
  158. [DefaultValue (null)]
  159. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  160. public OdbcConnection Connection {
  161. get {
  162. return connection;
  163. }
  164. set {
  165. connection = value;
  166. }
  167. }
  168. #endif // ONLY_1_1
  169. #if NET_2_0
  170. public new OdbcConnection Connection
  171. {
  172. get { return DbConnection as OdbcConnection; }
  173. set { DbConnection = value; }
  174. }
  175. #endif // NET_2_0
  176. #if ONLY_1_1
  177. [BrowsableAttribute (false)]
  178. [DesignOnlyAttribute (true)]
  179. [DefaultValue (true)]
  180. public bool DesignTimeVisible {
  181. get {
  182. return designTimeVisible;
  183. }
  184. set {
  185. designTimeVisible = value;
  186. }
  187. }
  188. #endif // ONLY_1_1
  189. [OdbcCategory ("Data")]
  190. [OdbcDescriptionAttribute ("The parameters collection")]
  191. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  192. public
  193. #if NET_2_0
  194. new
  195. #endif // NET_2_0
  196. OdbcParameterCollection Parameters {
  197. get {
  198. #if ONLY_1_1
  199. return _parameters;
  200. #else
  201. return base.Parameters as OdbcParameterCollection;
  202. #endif // ONLY_1_1
  203. }
  204. }
  205. [BrowsableAttribute (false)]
  206. [OdbcDescriptionAttribute ("The transaction used by the command")]
  207. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  208. public
  209. #if NET_2_0
  210. new
  211. #endif // NET_2_0
  212. OdbcTransaction Transaction {
  213. get {
  214. return transaction;
  215. }
  216. set {
  217. transaction = value;
  218. }
  219. }
  220. #if ONLY_1_1
  221. [OdbcCategory ("Behavior")]
  222. [DefaultValue (UpdateRowSource.Both)]
  223. [OdbcDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
  224. public UpdateRowSource UpdatedRowSource {
  225. [MonoTODO]
  226. get {
  227. throw new NotImplementedException ();
  228. }
  229. [MonoTODO]
  230. set {
  231. throw new NotImplementedException ();
  232. }
  233. }
  234. IDbConnection IDbCommand.Connection {
  235. get {
  236. return Connection;
  237. }
  238. set {
  239. Connection = (OdbcConnection) value;
  240. }
  241. }
  242. #endif // ONLY_1_1
  243. #if NET_2_0
  244. protected override DbConnection DbConnection
  245. {
  246. get { return connection; }
  247. set {
  248. connection = (OdbcConnection) value;
  249. }
  250. }
  251. #endif // NET_2_0
  252. #if ONLY_1_1
  253. IDataParameterCollection IDbCommand.Parameters {
  254. get {
  255. return Parameters;
  256. }
  257. }
  258. #else
  259. protected override DbParameterCollection DbParameterCollection
  260. {
  261. get { return _parameters as DbParameterCollection;}
  262. }
  263. #endif // NET_2_0
  264. #if ONLY_1_1
  265. IDbTransaction IDbCommand.Transaction {
  266. get {
  267. return (IDbTransaction) Transaction;
  268. }
  269. set {
  270. if (value is OdbcTransaction)
  271. {
  272. Transaction = (OdbcTransaction)value;
  273. }
  274. else
  275. {
  276. throw new ArgumentException ();
  277. }
  278. }
  279. }
  280. #else
  281. protected override DbTransaction DbTransaction
  282. {
  283. get { return transaction; }
  284. set {
  285. transaction = (OdbcTransaction)value;
  286. }
  287. }
  288. #endif // ONLY_1_1
  289. #endregion // Properties
  290. #region Methods
  291. public
  292. #if NET_2_0
  293. override
  294. #endif // NET_2_0
  295. void Cancel ()
  296. {
  297. if (hstmt!=IntPtr.Zero)
  298. {
  299. OdbcReturn Ret=libodbc.SQLCancel(hstmt);
  300. if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
  301. throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
  302. }
  303. else
  304. throw new InvalidOperationException();
  305. }
  306. #if ONLY_1_1
  307. IDbDataParameter IDbCommand.CreateParameter ()
  308. {
  309. return CreateParameter ();
  310. }
  311. public OdbcParameter CreateParameter ()
  312. {
  313. return new OdbcParameter ();
  314. }
  315. #else
  316. protected override DbParameter CreateDbParameter ()
  317. {
  318. return CreateParameter ();
  319. }
  320. #endif // ONLY_1_1
  321. [MonoTODO]
  322. protected override void Dispose (bool disposing)
  323. {
  324. }
  325. private void ExecSQL(string sql)
  326. {
  327. OdbcReturn ret;
  328. if ((Parameters.Count>0) && !prepared)
  329. Prepare();
  330. if (prepared)
  331. {
  332. ret=libodbc.SQLExecute(hstmt);
  333. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  334. throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
  335. }
  336. else
  337. {
  338. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  339. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  340. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  341. ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
  342. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  343. throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
  344. }
  345. }
  346. public
  347. #if NET_2_0
  348. override
  349. #endif // NET_2_0
  350. int ExecuteNonQuery ()
  351. {
  352. return ExecuteNonQuery (true);
  353. }
  354. private int ExecuteNonQuery (bool freeHandle)
  355. {
  356. int records = 0;
  357. if (Connection == null)
  358. throw new InvalidOperationException ();
  359. if (Connection.State == ConnectionState.Closed)
  360. throw new InvalidOperationException ();
  361. // FIXME: a third check is mentioned in .NET docs
  362. ExecSQL(CommandText);
  363. // .NET documentation says that except for INSERT, UPDATE and
  364. // DELETE where the return value is the number of rows affected
  365. // for the rest of the commands the return value is -1.
  366. if ((CommandText.ToUpper().IndexOf("UPDATE")!=-1) ||
  367. (CommandText.ToUpper().IndexOf("INSERT")!=-1) ||
  368. (CommandText.ToUpper().IndexOf("DELETE")!=-1)) {
  369. int numrows = 0;
  370. OdbcReturn ret = libodbc.SQLRowCount(hstmt,ref numrows);
  371. records = numrows;
  372. }
  373. else
  374. records = -1;
  375. if (freeHandle && !prepared) {
  376. OdbcReturn ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  377. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  378. throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,hstmt));
  379. }
  380. return records;
  381. }
  382. public
  383. #if NET_2_0
  384. override
  385. #endif // NET_2_0
  386. void Prepare()
  387. {
  388. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  389. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  390. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  391. ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
  392. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  393. throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
  394. int i=1;
  395. foreach (OdbcParameter p in Parameters)
  396. {
  397. p.Bind(hstmt, i);
  398. i++;
  399. }
  400. prepared=true;
  401. }
  402. public
  403. #if NET_2_0
  404. new
  405. #endif // NET_2_0
  406. OdbcDataReader ExecuteReader ()
  407. {
  408. return ExecuteReader (CommandBehavior.Default);
  409. }
  410. #if ONLY_1_1
  411. IDataReader IDbCommand.ExecuteReader ()
  412. {
  413. return ExecuteReader ();
  414. }
  415. #else
  416. protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
  417. {
  418. return ExecuteReader (behavior);
  419. }
  420. #endif // ONLY_1_1
  421. public
  422. #if NET_2_0
  423. new
  424. #endif // NET_2_0
  425. OdbcDataReader ExecuteReader (CommandBehavior behavior)
  426. {
  427. ExecuteNonQuery(false);
  428. dataReader=new OdbcDataReader(this,behavior);
  429. return dataReader;
  430. }
  431. #if ONLY_1_1
  432. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  433. {
  434. return ExecuteReader (behavior);
  435. }
  436. #endif // ONLY_1_1
  437. #if ONLY_1_1
  438. public object ExecuteScalar ()
  439. {
  440. object val = null;
  441. OdbcDataReader reader=ExecuteReader();
  442. try
  443. {
  444. if (reader.Read ())
  445. val=reader[0];
  446. }
  447. finally
  448. {
  449. reader.Close();
  450. }
  451. return val;
  452. }
  453. #endif // ONLY_1_1
  454. [MonoTODO]
  455. object ICloneable.Clone ()
  456. {
  457. throw new NotImplementedException ();
  458. }
  459. public
  460. #if NET_2_0
  461. override
  462. #endif // NET_2_0
  463. void ResetCommandTimeout ()
  464. {
  465. CommandTimeout = 30;
  466. }
  467. #endregion
  468. }
  469. }