OdbcCommand.cs 5.6 KB

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