OdbcCommand.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. public int 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. public int 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. throw new NotImplementedException ();
  147. //return Parameters;
  148. }
  149. }
  150. IDbTransaction IDbCommand.Transaction {
  151. get {
  152. throw new NotImplementedException ();
  153. //return Transaction;
  154. }
  155. set {
  156. throw new NotImplementedException ();
  157. }
  158. }
  159. #endregion // Properties
  160. #region Methods
  161. [MonoTODO]
  162. public void Cancel ()
  163. {
  164. throw new NotImplementedException ();
  165. }
  166. public OdbcParameter CreateParameter ()
  167. {
  168. return new OdbcParameter ();
  169. }
  170. IDbDataParameter IDbCommand.CreateParameter ()
  171. {
  172. return CreateParameter ();
  173. }
  174. [MonoTODO]
  175. protected override void Dispose (bool disposing)
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. protected void ExecSQL(string sql)
  180. {
  181. OdbcReturn ret;
  182. if (!prepared)
  183. {
  184. Prepare();
  185. if (Parameters.Count>0)
  186. Parameters.Bind(hstmt);
  187. }
  188. if (prepared)
  189. {
  190. ret=libodbc.SQLExecute(hstmt);
  191. libodbc.DisplayError("SQLExecute",ret);
  192. }
  193. else
  194. {
  195. ret=libodbc.SQLAllocHandle((ushort) OdbcHandleType.Stmt,
  196. Connection.hDbc, ref hstmt);
  197. libodbc.DisplayError("SQLAllocHandle(hstmt)",ret);
  198. ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
  199. libodbc.DisplayError("SQLExecDirect",ret);
  200. }
  201. }
  202. public int ExecuteNonQuery ()
  203. {
  204. if (connection == null)
  205. throw new InvalidOperationException ();
  206. if (connection.State == ConnectionState.Closed)
  207. throw new InvalidOperationException ();
  208. // FIXME: a third check is mentioned in .NET docs
  209. if (connection.DataReader != null)
  210. throw new InvalidOperationException ();
  211. ExecSQL(CommandText);
  212. if (!prepared)
  213. libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  214. return 0;
  215. }
  216. public void Prepare()
  217. {
  218. OdbcReturn ret;
  219. ret=libodbc.SQLAllocHandle((ushort) OdbcHandleType.Stmt, Connection.hDbc,
  220. ref hstmt);
  221. libodbc.DisplayError("SQLAlloc(Prepare)",ret);
  222. ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
  223. libodbc.DisplayError("SQLPrepare",ret);
  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);
  238. return dataReader;
  239. }
  240. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  241. {
  242. return ExecuteReader (behavior);
  243. }
  244. public object ExecuteScalar ()
  245. {
  246. throw new NotImplementedException ();
  247. // if (connection.DataReader != null)
  248. // throw new InvalidOperationException ();
  249. //
  250. }
  251. [MonoTODO]
  252. object ICloneable.Clone ()
  253. {
  254. throw new NotImplementedException ();
  255. }
  256. public void ResetCommandTimeout ()
  257. {
  258. timeout = 30;
  259. }
  260. #endregion
  261. }
  262. }