OdbcCommand.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // System.Data.Odbc.OdbcCommand
  3. //
  4. // Authors:
  5. // Brian Ritchie ([email protected])
  6. //
  7. // Copyright (C) Brian Ritchie, 2002
  8. //
  9. using System.ComponentModel;
  10. using System.Data;
  11. using System.Data.Common;
  12. using System.Collections;
  13. using System.Runtime.InteropServices;
  14. namespace System.Data.Odbc
  15. {
  16. /// <summary>
  17. /// Represents an SQL statement or stored procedure to execute against a data source.
  18. /// </summary>
  19. [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OdbcCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
  20. [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
  21. public sealed class OdbcCommand : Component, ICloneable, IDbCommand
  22. {
  23. #region Fields
  24. string commandText;
  25. int timeout;
  26. CommandType commandType;
  27. OdbcConnection connection;
  28. OdbcParameterCollection parameters;
  29. OdbcTransaction transaction;
  30. bool designTimeVisible;
  31. bool prepared=false;
  32. OdbcDataReader dataReader;
  33. IntPtr hstmt;
  34. #endregion // Fields
  35. #region Constructors
  36. public OdbcCommand ()
  37. {
  38. commandText = String.Empty;
  39. timeout = 30; // default timeout
  40. commandType = CommandType.Text;
  41. connection = null;
  42. parameters = new OdbcParameterCollection ();
  43. transaction = null;
  44. designTimeVisible = false;
  45. dataReader = null;
  46. }
  47. public OdbcCommand (string cmdText) : this ()
  48. {
  49. CommandText = cmdText;
  50. }
  51. public OdbcCommand (string cmdText, OdbcConnection connection)
  52. : this (cmdText)
  53. {
  54. Connection = connection;
  55. }
  56. public OdbcCommand (string cmdText,
  57. OdbcConnection connection,
  58. OdbcTransaction transaction) : this (cmdText, connection)
  59. {
  60. this.transaction = transaction;
  61. }
  62. #endregion // Constructors
  63. #region Properties
  64. internal IntPtr hStmt
  65. {
  66. get { return hstmt; }
  67. }
  68. [OdbcCategory ("Data")]
  69. [DefaultValue ("")]
  70. [OdbcDescriptionAttribute ("Command text to execute")]
  71. [EditorAttribute ("Microsoft.VSDesigner.Data.Odbc.Design.OdbcCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  72. [RefreshPropertiesAttribute (RefreshProperties.All)]
  73. public string CommandText
  74. {
  75. get {
  76. return commandText;
  77. }
  78. set {
  79. prepared=false;
  80. commandText = value;
  81. }
  82. }
  83. [OdbcDescriptionAttribute ("Time to wait for command to execute")]
  84. [DefaultValue (30)]
  85. public int CommandTimeout {
  86. get {
  87. return timeout;
  88. }
  89. set {
  90. timeout = value;
  91. }
  92. }
  93. [OdbcCategory ("Data")]
  94. [DefaultValue ("Text")]
  95. [OdbcDescriptionAttribute ("How to interpret the CommandText")]
  96. [RefreshPropertiesAttribute (RefreshProperties.All)]
  97. public CommandType CommandType {
  98. get {
  99. return commandType;
  100. }
  101. set {
  102. commandType = value;
  103. }
  104. }
  105. [OdbcCategory ("Behavior")]
  106. [OdbcDescriptionAttribute ("Connection used by the command")]
  107. [DefaultValue (null)]
  108. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  109. public OdbcConnection Connection {
  110. get {
  111. return connection;
  112. }
  113. set {
  114. connection = value;
  115. }
  116. }
  117. [BrowsableAttribute (false)]
  118. [DesignOnlyAttribute (true)]
  119. [DefaultValue (true)]
  120. public bool DesignTimeVisible {
  121. get {
  122. return designTimeVisible;
  123. }
  124. set {
  125. designTimeVisible = value;
  126. }
  127. }
  128. [OdbcCategory ("Data")]
  129. [OdbcDescriptionAttribute ("The parameters collection")]
  130. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  131. public OdbcParameterCollection Parameters {
  132. get {
  133. return parameters;
  134. }
  135. }
  136. [BrowsableAttribute (false)]
  137. [OdbcDescriptionAttribute ("The transaction used by the command")]
  138. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  139. public OdbcTransaction Transaction {
  140. get {
  141. return transaction;
  142. }
  143. set {
  144. transaction = value;
  145. }
  146. }
  147. [OdbcCategory ("Behavior")]
  148. [DefaultValue (UpdateRowSource.Both)]
  149. [OdbcDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
  150. public UpdateRowSource UpdatedRowSource {
  151. [MonoTODO]
  152. get {
  153. throw new NotImplementedException ();
  154. }
  155. [MonoTODO]
  156. set {
  157. throw new NotImplementedException ();
  158. }
  159. }
  160. IDbConnection IDbCommand.Connection {
  161. get {
  162. return Connection;
  163. }
  164. set {
  165. Connection = (OdbcConnection) value;
  166. }
  167. }
  168. IDataParameterCollection IDbCommand.Parameters {
  169. get {
  170. return Parameters;
  171. }
  172. }
  173. IDbTransaction IDbCommand.Transaction {
  174. get {
  175. return (IDbTransaction) Transaction;
  176. }
  177. set {
  178. throw new NotImplementedException ();
  179. }
  180. }
  181. #endregion // Properties
  182. #region Methods
  183. public void Cancel ()
  184. {
  185. if (hstmt!=IntPtr.Zero)
  186. {
  187. OdbcReturn Ret=libodbc.SQLCancel(hstmt);
  188. if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
  189. throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
  190. }
  191. else
  192. throw new InvalidOperationException();
  193. }
  194. public OdbcParameter CreateParameter ()
  195. {
  196. return new OdbcParameter ();
  197. }
  198. IDbDataParameter IDbCommand.CreateParameter ()
  199. {
  200. return CreateParameter ();
  201. }
  202. [MonoTODO]
  203. protected override void Dispose (bool disposing)
  204. {
  205. }
  206. private void ExecSQL(string sql)
  207. {
  208. OdbcReturn ret;
  209. if ((parameters.Count>0) && !prepared)
  210. Prepare();
  211. if (prepared)
  212. {
  213. ret=libodbc.SQLExecute(hstmt);
  214. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  215. throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
  216. }
  217. else
  218. {
  219. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  220. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  221. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  222. ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
  223. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  224. throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
  225. }
  226. }
  227. public int ExecuteNonQuery ()
  228. {
  229. if (connection == null)
  230. throw new InvalidOperationException ();
  231. if (connection.State == ConnectionState.Closed)
  232. throw new InvalidOperationException ();
  233. // FIXME: a third check is mentioned in .NET docs
  234. ExecSQL(CommandText);
  235. // if (!prepared)
  236. // libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  237. return 0;
  238. }
  239. public void Prepare()
  240. {
  241. OdbcReturn 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.SQLPrepare(hstmt, CommandText, CommandText.Length);
  245. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  246. throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
  247. int i=1;
  248. foreach (OdbcParameter p in parameters)
  249. {
  250. p.Bind(hstmt, i);
  251. i++;
  252. }
  253. prepared=true;
  254. }
  255. public OdbcDataReader ExecuteReader ()
  256. {
  257. return ExecuteReader (CommandBehavior.Default);
  258. }
  259. IDataReader IDbCommand.ExecuteReader ()
  260. {
  261. return ExecuteReader ();
  262. }
  263. public OdbcDataReader ExecuteReader (CommandBehavior behavior)
  264. {
  265. ExecuteNonQuery();
  266. dataReader=new OdbcDataReader(this,behavior);
  267. return dataReader;
  268. }
  269. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  270. {
  271. return ExecuteReader (behavior);
  272. }
  273. public object ExecuteScalar ()
  274. {
  275. object val;
  276. OdbcDataReader reader=ExecuteReader();
  277. try
  278. {
  279. val=reader[0];
  280. }
  281. finally
  282. {
  283. reader.Close();
  284. }
  285. return val;
  286. }
  287. [MonoTODO]
  288. object ICloneable.Clone ()
  289. {
  290. throw new NotImplementedException ();
  291. }
  292. public void ResetCommandTimeout ()
  293. {
  294. timeout = 30;
  295. }
  296. #endregion
  297. }
  298. }