OleDbCommand.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. }
  112. public OleDbTransaction Transaction {
  113. get {
  114. return transaction;
  115. }
  116. set {
  117. transaction = value;
  118. }
  119. }
  120. public UpdateRowSource UpdatedRowSource {
  121. [MonoTODO]
  122. get {
  123. throw new NotImplementedException ();
  124. }
  125. [MonoTODO]
  126. set {
  127. throw new NotImplementedException ();
  128. }
  129. }
  130. IDbConnection IDbCommand.Connection {
  131. get {
  132. return Connection;
  133. }
  134. set {
  135. Connection = (OleDbConnection) value;
  136. }
  137. }
  138. IDataParameterCollection IDbCommand.Parameters {
  139. get {
  140. return Parameters;
  141. }
  142. }
  143. IDbTransaction IDbCommand.Transaction {
  144. get {
  145. return Transaction;
  146. }
  147. set {
  148. Transaction = (OleDbTransaction) value;
  149. }
  150. }
  151. #endregion // Properties
  152. #region Methods
  153. [MonoTODO]
  154. public void Cancel ()
  155. {
  156. throw new NotImplementedException ();
  157. }
  158. public OleDbParameter CreateParameter ()
  159. {
  160. return new OleDbParameter ();
  161. }
  162. IDbDataParameter IDbCommand.CreateParameter ()
  163. {
  164. return CreateParameter ();
  165. }
  166. [MonoTODO]
  167. protected override void Dispose (bool disposing)
  168. {
  169. throw new NotImplementedException ();
  170. }
  171. private void SetupGdaCommand ()
  172. {
  173. GdaCommandType type;
  174. switch (commandType) {
  175. case CommandType.TableDirect :
  176. type = GdaCommandType.Table;
  177. break;
  178. case CommandType.StoredProcedure :
  179. type = GdaCommandType.Procedure;
  180. break;
  181. case CommandType.Text :
  182. default :
  183. type = GdaCommandType.Sql;
  184. break;
  185. }
  186. if (gdaCommand != IntPtr.Zero) {
  187. libgda.gda_command_set_text (gdaCommand, commandText);
  188. libgda.gda_command_set_command_type (gdaCommand, type);
  189. } else {
  190. gdaCommand = libgda.gda_command_new (commandText, type, 0);
  191. }
  192. //libgda.gda_command_set_transaction
  193. }
  194. public int ExecuteNonQuery ()
  195. {
  196. if (connection == null)
  197. throw new InvalidOperationException ("connection == null");
  198. if (connection.State == ConnectionState.Closed)
  199. throw new InvalidOperationException ("State == Closed");
  200. // FIXME: a third check is mentioned in .NET docs
  201. IntPtr gdaConnection = connection.GdaConnection;
  202. IntPtr gdaParameterList = parameters.GdaParameterList;
  203. SetupGdaCommand ();
  204. return libgda.gda_connection_execute_non_query (gdaConnection,
  205. (IntPtr) gdaCommand,
  206. gdaParameterList);
  207. }
  208. public OleDbDataReader ExecuteReader ()
  209. {
  210. return ExecuteReader (CommandBehavior.Default);
  211. }
  212. IDataReader IDbCommand.ExecuteReader ()
  213. {
  214. return ExecuteReader ();
  215. }
  216. public OleDbDataReader ExecuteReader (CommandBehavior behavior)
  217. {
  218. ArrayList results = new ArrayList ();
  219. IntPtr rs_list;
  220. GdaList glist_node;
  221. if (connection.State != ConnectionState.Open)
  222. throw new InvalidOperationException ("State != Open");
  223. this.behavior = behavior;
  224. IntPtr gdaConnection = connection.GdaConnection;
  225. IntPtr gdaParameterList = parameters.GdaParameterList;
  226. /* execute the command */
  227. SetupGdaCommand ();
  228. rs_list = libgda.gda_connection_execute_command (
  229. gdaConnection,
  230. gdaCommand,
  231. gdaParameterList);
  232. if (rs_list != IntPtr.Zero) {
  233. glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
  234. while (glist_node != null) {
  235. results.Add (glist_node.data);
  236. if (glist_node.next == IntPtr.Zero)
  237. break;
  238. glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
  239. typeof (GdaList));
  240. }
  241. dataReader = new OleDbDataReader (this, results);
  242. dataReader.NextResult ();
  243. }
  244. return dataReader;
  245. }
  246. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  247. {
  248. return ExecuteReader (behavior);
  249. }
  250. public object ExecuteScalar ()
  251. {
  252. SetupGdaCommand ();
  253. OleDbDataReader reader = ExecuteReader ();
  254. if (reader == null) {
  255. return null;
  256. }
  257. if (!reader.Read ()) {
  258. reader.Close ();
  259. return null;
  260. }
  261. object o = reader.GetValue (0);
  262. reader.Close ();
  263. return o;
  264. }
  265. [MonoTODO]
  266. object ICloneable.Clone ()
  267. {
  268. throw new NotImplementedException ();
  269. }
  270. [MonoTODO]
  271. public void Prepare ()
  272. {
  273. throw new NotImplementedException ();
  274. }
  275. public void ResetCommandTimeout ()
  276. {
  277. timeout = 30;
  278. }
  279. #endregion
  280. }
  281. }