OdbcCommand.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. throw new NotImplementedException ();
  201. }
  202. }
  203. #endregion // Properties
  204. #region Methods
  205. public void Cancel ()
  206. {
  207. if (hstmt!=IntPtr.Zero)
  208. {
  209. OdbcReturn Ret=libodbc.SQLCancel(hstmt);
  210. if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
  211. throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
  212. }
  213. else
  214. throw new InvalidOperationException();
  215. }
  216. public OdbcParameter CreateParameter ()
  217. {
  218. return new OdbcParameter ();
  219. }
  220. IDbDataParameter IDbCommand.CreateParameter ()
  221. {
  222. return CreateParameter ();
  223. }
  224. [MonoTODO]
  225. protected override void Dispose (bool disposing)
  226. {
  227. }
  228. private void ExecSQL(string sql)
  229. {
  230. OdbcReturn ret;
  231. if ((parameters.Count>0) && !prepared)
  232. Prepare();
  233. if (prepared)
  234. {
  235. ret=libodbc.SQLExecute(hstmt);
  236. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  237. throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
  238. }
  239. else
  240. {
  241. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  242. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  243. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  244. ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
  245. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  246. throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
  247. }
  248. }
  249. public int ExecuteNonQuery ()
  250. {
  251. if (connection == null)
  252. throw new InvalidOperationException ();
  253. if (connection.State == ConnectionState.Closed)
  254. throw new InvalidOperationException ();
  255. // FIXME: a third check is mentioned in .NET docs
  256. ExecSQL(CommandText);
  257. // if (!prepared)
  258. // libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  259. return 0;
  260. }
  261. public void Prepare()
  262. {
  263. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  264. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  265. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  266. ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
  267. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  268. throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
  269. int i=1;
  270. foreach (OdbcParameter p in parameters)
  271. {
  272. p.Bind(hstmt, i);
  273. i++;
  274. }
  275. prepared=true;
  276. }
  277. public OdbcDataReader ExecuteReader ()
  278. {
  279. return ExecuteReader (CommandBehavior.Default);
  280. }
  281. IDataReader IDbCommand.ExecuteReader ()
  282. {
  283. return ExecuteReader ();
  284. }
  285. public OdbcDataReader ExecuteReader (CommandBehavior behavior)
  286. {
  287. ExecuteNonQuery();
  288. dataReader=new OdbcDataReader(this,behavior);
  289. return dataReader;
  290. }
  291. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  292. {
  293. return ExecuteReader (behavior);
  294. }
  295. public object ExecuteScalar ()
  296. {
  297. object val;
  298. OdbcDataReader reader=ExecuteReader();
  299. try
  300. {
  301. val=reader[0];
  302. }
  303. finally
  304. {
  305. reader.Close();
  306. }
  307. return val;
  308. }
  309. [MonoTODO]
  310. object ICloneable.Clone ()
  311. {
  312. throw new NotImplementedException ();
  313. }
  314. public void ResetCommandTimeout ()
  315. {
  316. timeout = 30;
  317. }
  318. #endregion
  319. }
  320. }