OleDbCommand.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // System.Data.OleDb.OleDbCommand
  3. //
  4. // Authors:
  5. // Rodrigo Moya ([email protected])
  6. // Tim Coleman ([email protected])
  7. //
  8. // Copyright (C) Rodrigo Moya, 2002
  9. // Copyright (C) Tim Coleman, 2002
  10. //
  11. using System.ComponentModel;
  12. using System.Data;
  13. using System.Data.Common;
  14. using System.Collections;
  15. namespace System.Data.OleDb
  16. {
  17. /// <summary>
  18. /// Represents an SQL statement or stored procedure to execute against a data source.
  19. /// </summary>
  20. public sealed class OleDbCommand : Component, ICloneable, IDbCommand
  21. {
  22. #region Fields
  23. string commandText;
  24. int timeout;
  25. CommandType commandType;
  26. OleDbConnection connection;
  27. OleDbParameterCollection parameters;
  28. OleDbTransaction transaction;
  29. bool designTimeVisible;
  30. OleDbDataReader dataReader;
  31. CommandBehavior behavior;
  32. IntPtr gdaCommand;
  33. #endregion // Fields
  34. #region Constructors
  35. public OleDbCommand ()
  36. {
  37. commandText = String.Empty;
  38. timeout = 30; // default timeout per .NET
  39. commandType = CommandType.Text;
  40. connection = null;
  41. parameters = new OleDbParameterCollection ();
  42. transaction = null;
  43. designTimeVisible = false;
  44. dataReader = null;
  45. behavior = CommandBehavior.Default;
  46. gdaCommand = IntPtr.Zero;
  47. }
  48. public OleDbCommand (string cmdText)
  49. : this ()
  50. {
  51. CommandText = cmdText;
  52. }
  53. public OleDbCommand (string cmdText, OleDbConnection connection)
  54. : this (cmdText)
  55. {
  56. Connection = connection;
  57. }
  58. public OleDbCommand (string cmdText, OleDbConnection connection, OleDbTransaction transaction)
  59. : this (cmdText, connection)
  60. {
  61. this.transaction = transaction;
  62. }
  63. #endregion // Constructors
  64. #region Properties
  65. public string CommandText
  66. {
  67. get {
  68. return commandText;
  69. }
  70. set {
  71. commandText = value;
  72. }
  73. }
  74. public int CommandTimeout {
  75. get {
  76. return timeout;
  77. }
  78. set {
  79. timeout = value;
  80. }
  81. }
  82. public CommandType CommandType {
  83. get {
  84. return commandType;
  85. }
  86. set {
  87. commandType = value;
  88. }
  89. }
  90. public OleDbConnection Connection {
  91. get {
  92. return connection;
  93. }
  94. set {
  95. connection = value;
  96. }
  97. }
  98. public bool DesignTimeVisible {
  99. get {
  100. return designTimeVisible;
  101. }
  102. set {
  103. designTimeVisible = value;
  104. }
  105. }
  106. public OleDbParameterCollection Parameters {
  107. get {
  108. return parameters;
  109. }
  110. set {
  111. parameters = value;
  112. }
  113. }
  114. public OleDbTransaction Transaction {
  115. get {
  116. return transaction;
  117. }
  118. set {
  119. transaction = value;
  120. }
  121. }
  122. public UpdateRowSource UpdatedRowSource {
  123. [MonoTODO]
  124. get {
  125. throw new NotImplementedException ();
  126. }
  127. [MonoTODO]
  128. set {
  129. throw new NotImplementedException ();
  130. }
  131. }
  132. IDbConnection IDbCommand.Connection {
  133. get {
  134. return Connection;
  135. }
  136. set {
  137. Connection = (OleDbConnection) value;
  138. }
  139. }
  140. IDataParameterCollection IDbCommand.Parameters {
  141. get {
  142. return Parameters;
  143. }
  144. }
  145. IDbTransaction IDbCommand.Transaction {
  146. get {
  147. return Transaction;
  148. }
  149. set {
  150. Transaction = (OleDbTransaction) value;
  151. }
  152. }
  153. #endregion // Properties
  154. #region Methods
  155. [MonoTODO]
  156. public void Cancel ()
  157. {
  158. throw new NotImplementedException ();
  159. }
  160. public OleDbParameter CreateParameter ()
  161. {
  162. return new OleDbParameter ();
  163. }
  164. IDbDataParameter IDbCommand.CreateParameter ()
  165. {
  166. return CreateParameter ();
  167. }
  168. [MonoTODO]
  169. protected override void Dispose (bool disposing)
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. private void SetupGdaCommand ()
  174. {
  175. GdaCommandType type;
  176. switch (commandType) {
  177. case CommandType.TableDirect :
  178. type = GdaCommandType.Table;
  179. break;
  180. case CommandType.StoredProcedure :
  181. type = GdaCommandType.Procedure;
  182. break;
  183. case CommandType.Text :
  184. default :
  185. type = GdaCommandType.Sql;
  186. break;
  187. }
  188. if (gdaCommand != IntPtr.Zero) {
  189. libgda.gda_command_set_text (gdaCommand, commandText);
  190. libgda.gda_command_set_command_type (gdaCommand, type);
  191. } else {
  192. gdaCommand = libgda.gda_command_new (commandText, type, 0);
  193. }
  194. //libgda.gda_command_set_transaction
  195. }
  196. public int ExecuteNonQuery ()
  197. {
  198. if (connection == null)
  199. throw new InvalidOperationException ();
  200. if (connection.State == ConnectionState.Closed)
  201. throw new InvalidOperationException ();
  202. // FIXME: a third check is mentioned in .NET docs
  203. IntPtr gdaConnection = connection.GdaConnection;
  204. IntPtr gdaParameterList = parameters.GdaParameterList;
  205. SetupGdaCommand ();
  206. return libgda.gda_connection_execute_non_query (gdaConnection,
  207. (IntPtr) gdaCommand,
  208. gdaParameterList);
  209. }
  210. public OleDbDataReader ExecuteReader ()
  211. {
  212. return ExecuteReader (CommandBehavior.Default);
  213. }
  214. IDataReader IDbCommand.ExecuteReader ()
  215. {
  216. return ExecuteReader ();
  217. }
  218. public OleDbDataReader ExecuteReader (CommandBehavior behavior)
  219. {
  220. ArrayList results = new ArrayList ();
  221. if (connection.State != ConnectionState.Open)
  222. throw new InvalidOperationException ();
  223. this.behavior = behavior;
  224. IntPtr gdaConnection = connection.GdaConnection;
  225. IntPtr gdaParameterList = parameters.GdaParameterList;
  226. SetupGdaCommand ();
  227. /* FIXME: split all returned resultsets into the array
  228. list of results */
  229. results.Add (libgda.gda_connection_execute_command (
  230. gdaConnection,
  231. gdaCommand,
  232. gdaParameterList));
  233. dataReader = new OleDbDataReader (this, results);
  234. dataReader.NextResult ();
  235. return dataReader;
  236. }
  237. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  238. {
  239. return ExecuteReader (behavior);
  240. }
  241. public object ExecuteScalar ()
  242. {
  243. SetupGdaCommand ();
  244. OleDbDataReader reader = ExecuteReader ();
  245. return reader.GetValue (0);
  246. }
  247. [MonoTODO]
  248. object ICloneable.Clone ()
  249. {
  250. throw new NotImplementedException ();
  251. }
  252. [MonoTODO]
  253. public void Prepare ()
  254. {
  255. throw new NotImplementedException ();
  256. }
  257. public void ResetCommandTimeout ()
  258. {
  259. timeout = 30;
  260. }
  261. #endregion
  262. }
  263. }