OleDbCommand.cs 6.5 KB

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