OdbcCommand.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. public 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. set {
  113. parameters = value;
  114. }
  115. }
  116. public OdbcTransaction Transaction {
  117. get {
  118. return transaction;
  119. }
  120. set {
  121. transaction = value;
  122. }
  123. }
  124. public UpdateRowSource UpdatedRowSource {
  125. [MonoTODO]
  126. get {
  127. throw new NotImplementedException ();
  128. }
  129. [MonoTODO]
  130. set {
  131. throw new NotImplementedException ();
  132. }
  133. }
  134. IDbConnection IDbCommand.Connection {
  135. get {
  136. return Connection;
  137. }
  138. set {
  139. Connection = (OdbcConnection) value;
  140. }
  141. }
  142. IDataParameterCollection IDbCommand.Parameters {
  143. get {
  144. return Parameters;
  145. }
  146. }
  147. IDbTransaction IDbCommand.Transaction {
  148. get {
  149. return (IDbTransaction) Transaction;
  150. }
  151. set {
  152. throw new NotImplementedException ();
  153. }
  154. }
  155. #endregion // Properties
  156. #region Methods
  157. public void Cancel ()
  158. {
  159. if (hstmt!=IntPtr.Zero)
  160. {
  161. OdbcReturn Ret=libodbc.SQLCancel(hstmt);
  162. if ((Ret!=OdbcReturn.Success) && (Ret!=OdbcReturn.SuccessWithInfo))
  163. throw new OdbcException(new OdbcError("SQLCancel",OdbcHandleType.Stmt,hstmt));
  164. }
  165. else
  166. throw new InvalidOperationException();
  167. }
  168. public OdbcParameter CreateParameter ()
  169. {
  170. return new OdbcParameter ();
  171. }
  172. IDbDataParameter IDbCommand.CreateParameter ()
  173. {
  174. return CreateParameter ();
  175. }
  176. [MonoTODO]
  177. protected override void Dispose (bool disposing)
  178. {
  179. }
  180. private void ExecSQL(string sql)
  181. {
  182. OdbcReturn ret;
  183. if ((parameters.Count>0) && !prepared)
  184. Prepare();
  185. if (prepared)
  186. {
  187. ret=libodbc.SQLExecute(hstmt);
  188. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  189. throw new OdbcException(new OdbcError("SQLExecute",OdbcHandleType.Stmt,hstmt));
  190. }
  191. else
  192. {
  193. ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  194. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  195. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  196. ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
  197. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  198. throw new OdbcException(new OdbcError("SQLExecDirect",OdbcHandleType.Stmt,hstmt));
  199. }
  200. }
  201. public int ExecuteNonQuery ()
  202. {
  203. if (connection == null)
  204. throw new InvalidOperationException ();
  205. if (connection.State == ConnectionState.Closed)
  206. throw new InvalidOperationException ();
  207. // FIXME: a third check is mentioned in .NET docs
  208. if (connection.DataReader != null)
  209. throw new InvalidOperationException ();
  210. ExecSQL(CommandText);
  211. // if (!prepared)
  212. // libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  213. return 0;
  214. }
  215. public void Prepare()
  216. {
  217. OdbcReturn ret=libodbc.SQLAllocHandle(OdbcHandleType.Stmt, Connection.hDbc, ref hstmt);
  218. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  219. throw new OdbcException(new OdbcError("SQLAllocHandle",OdbcHandleType.Dbc,Connection.hDbc));
  220. ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
  221. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  222. throw new OdbcException(new OdbcError("SQLPrepare",OdbcHandleType.Stmt,hstmt));
  223. int i=1;
  224. foreach (OdbcParameter p in parameters)
  225. {
  226. p.Bind(hstmt, i);
  227. i++;
  228. }
  229. prepared=true;
  230. }
  231. public OdbcDataReader ExecuteReader ()
  232. {
  233. return ExecuteReader (CommandBehavior.Default);
  234. }
  235. IDataReader IDbCommand.ExecuteReader ()
  236. {
  237. return ExecuteReader ();
  238. }
  239. public OdbcDataReader ExecuteReader (CommandBehavior behavior)
  240. {
  241. ExecuteNonQuery();
  242. dataReader=new OdbcDataReader(this,behavior);
  243. return dataReader;
  244. }
  245. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  246. {
  247. return ExecuteReader (behavior);
  248. }
  249. public object ExecuteScalar ()
  250. {
  251. if (connection.DataReader != null)
  252. throw new InvalidOperationException ();
  253. object val;
  254. OdbcDataReader reader=ExecuteReader();
  255. try
  256. {
  257. val=reader[0];
  258. }
  259. finally
  260. {
  261. reader.Close();
  262. }
  263. return val;
  264. }
  265. [MonoTODO]
  266. object ICloneable.Clone ()
  267. {
  268. throw new NotImplementedException ();
  269. }
  270. public void ResetCommandTimeout ()
  271. {
  272. timeout = 30;
  273. }
  274. #endregion
  275. }
  276. }