OdbcCommand.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. public sealed class OdbcCommand : Component, ICloneable, IDbCommand
  20. {
  21. #region Fields
  22. string commandText;
  23. int timeout;
  24. CommandType commandType;
  25. OdbcConnection connection;
  26. OdbcParameterCollection parameters;
  27. OdbcTransaction transaction;
  28. bool designTimeVisible;
  29. bool prepared=false;
  30. OdbcDataReader dataReader;
  31. IntPtr hstmt;
  32. #endregion // Fields
  33. #region Constructors
  34. public OdbcCommand ()
  35. {
  36. commandText = String.Empty;
  37. timeout = 30; // default timeout
  38. commandType = CommandType.Text;
  39. connection = null;
  40. parameters = new OdbcParameterCollection ();
  41. transaction = null;
  42. designTimeVisible = false;
  43. dataReader = null;
  44. }
  45. public OdbcCommand (string cmdText) : this ()
  46. {
  47. CommandText = cmdText;
  48. }
  49. public OdbcCommand (string cmdText, OdbcConnection connection)
  50. : this (cmdText)
  51. {
  52. Connection = connection;
  53. }
  54. public OdbcCommand (string cmdText,
  55. OdbcConnection connection,
  56. OdbcTransaction transaction) : this (cmdText, connection)
  57. {
  58. this.transaction = transaction;
  59. }
  60. #endregion // Constructors
  61. #region Properties
  62. internal IntPtr hStmt
  63. {
  64. get { return hstmt; }
  65. }
  66. public string CommandText
  67. {
  68. get {
  69. return commandText;
  70. }
  71. set {
  72. prepared=false;
  73. commandText = value;
  74. }
  75. }
  76. public int CommandTimeout {
  77. get {
  78. return timeout;
  79. }
  80. set {
  81. timeout = value;
  82. }
  83. }
  84. public CommandType CommandType {
  85. get {
  86. return commandType;
  87. }
  88. set {
  89. commandType = value;
  90. }
  91. }
  92. public OdbcConnection Connection {
  93. get {
  94. return connection;
  95. }
  96. set {
  97. connection = value;
  98. }
  99. }
  100. public bool DesignTimeVisible {
  101. get {
  102. return designTimeVisible;
  103. }
  104. set {
  105. designTimeVisible = value;
  106. }
  107. }
  108. public OdbcParameterCollection Parameters {
  109. get {
  110. return parameters;
  111. }
  112. }
  113. public OdbcTransaction Transaction {
  114. get {
  115. return transaction;
  116. }
  117. set {
  118. transaction = value;
  119. }
  120. }
  121. public UpdateRowSource UpdatedRowSource {
  122. [MonoTODO]
  123. get {
  124. throw new NotImplementedException ();
  125. }
  126. [MonoTODO]
  127. set {
  128. throw new NotImplementedException ();
  129. }
  130. }
  131. IDbConnection IDbCommand.Connection {
  132. get {
  133. return Connection;
  134. }
  135. set {
  136. Connection = (OdbcConnection) value;
  137. }
  138. }
  139. IDataParameterCollection IDbCommand.Parameters {
  140. get {
  141. return Parameters;
  142. }
  143. }
  144. IDbTransaction IDbCommand.Transaction {
  145. get {
  146. return (IDbTransaction) Transaction;
  147. }
  148. set {
  149. throw new NotImplementedException ();
  150. }
  151. }
  152. #endregion // Properties
  153. #region Methods
  154. public void Cancel ()
  155. {
  156. if (hstmt!=IntPtr.Zero)
  157. {
  158. OdbcReturn Ret=libodbc.SQLCancel(hstmt);
  159. if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
  160. throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
  161. }
  162. else
  163. throw new InvalidOperationException();
  164. }
  165. public OdbcParameter CreateParameter ()
  166. {
  167. return new OdbcParameter ();
  168. }
  169. IDbDataParameter IDbCommand.CreateParameter ()
  170. {
  171. return CreateParameter ();
  172. }
  173. [MonoTODO]
  174. protected override void Dispose (bool disposing)
  175. {
  176. }
  177. private void ExecSQL(string sql)
  178. {
  179. OdbcReturn ret;
  180. if ((parameters.Count>0) && !prepared)
  181. Prepare();
  182. if (prepared)
  183. {
  184. ret=libodbc.SQLExecute(hstmt);
  185. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  186. throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
  187. }
  188. else
  189. {
  190. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  191. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  192. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  193. ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
  194. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  195. throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
  196. }
  197. }
  198. public int ExecuteNonQuery ()
  199. {
  200. if (connection == null)
  201. throw new InvalidOperationException ();
  202. if (connection.State == ConnectionState.Closed)
  203. throw new InvalidOperationException ();
  204. // FIXME: a third check is mentioned in .NET docs
  205. ExecSQL(CommandText);
  206. // if (!prepared)
  207. // libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  208. return 0;
  209. }
  210. public void Prepare()
  211. {
  212. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  213. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  214. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  215. ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
  216. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  217. throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
  218. int i=1;
  219. foreach (OdbcParameter p in parameters)
  220. {
  221. p.Bind(hstmt, i);
  222. i++;
  223. }
  224. prepared=true;
  225. }
  226. public OdbcDataReader ExecuteReader ()
  227. {
  228. return ExecuteReader (CommandBehavior.Default);
  229. }
  230. IDataReader IDbCommand.ExecuteReader ()
  231. {
  232. return ExecuteReader ();
  233. }
  234. public OdbcDataReader ExecuteReader (CommandBehavior behavior)
  235. {
  236. ExecuteNonQuery();
  237. dataReader=new OdbcDataReader(this,behavior);
  238. return dataReader;
  239. }
  240. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  241. {
  242. return ExecuteReader (behavior);
  243. }
  244. public object ExecuteScalar ()
  245. {
  246. object val;
  247. OdbcDataReader reader=ExecuteReader();
  248. try
  249. {
  250. val=reader[0];
  251. }
  252. finally
  253. {
  254. reader.Close();
  255. }
  256. return val;
  257. }
  258. [MonoTODO]
  259. object ICloneable.Clone ()
  260. {
  261. throw new NotImplementedException ();
  262. }
  263. public void ResetCommandTimeout ()
  264. {
  265. timeout = 30;
  266. }
  267. #endregion
  268. }
  269. }