OleDbCommand.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OleDbCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
  22. [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
  23. public sealed class OleDbCommand : Component, ICloneable, IDbCommand
  24. {
  25. #region Fields
  26. string commandText;
  27. int timeout;
  28. CommandType commandType;
  29. OleDbConnection connection;
  30. OleDbParameterCollection parameters;
  31. OleDbTransaction transaction;
  32. bool designTimeVisible;
  33. OleDbDataReader dataReader;
  34. CommandBehavior behavior;
  35. IntPtr gdaCommand;
  36. #endregion // Fields
  37. #region Constructors
  38. public OleDbCommand ()
  39. {
  40. commandText = String.Empty;
  41. timeout = 30; // default timeout per .NET
  42. commandType = CommandType.Text;
  43. connection = null;
  44. parameters = new OleDbParameterCollection ();
  45. transaction = null;
  46. designTimeVisible = false;
  47. dataReader = null;
  48. behavior = CommandBehavior.Default;
  49. gdaCommand = IntPtr.Zero;
  50. }
  51. public OleDbCommand (string cmdText) : this ()
  52. {
  53. CommandText = cmdText;
  54. }
  55. public OleDbCommand (string cmdText, OleDbConnection connection)
  56. : this (cmdText)
  57. {
  58. Connection = connection;
  59. }
  60. public OleDbCommand (string cmdText,
  61. OleDbConnection connection,
  62. OleDbTransaction transaction) : this (cmdText, connection)
  63. {
  64. this.transaction = transaction;
  65. }
  66. #endregion // Constructors
  67. #region Properties
  68. [DataCategory ("Data")]
  69. [DefaultValue ("")]
  70. [DataSysDescriptionAttribute ("Command text to execute")]
  71. [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  72. [RefreshPropertiesAttribute (RefreshProperties.All)]
  73. public string CommandText
  74. {
  75. get {
  76. return commandText;
  77. }
  78. set {
  79. commandText = value;
  80. }
  81. }
  82. [DataSysDescriptionAttribute ("Time to wait for command to execute")]
  83. [DefaultValue (30)]
  84. public int CommandTimeout {
  85. get {
  86. return timeout;
  87. }
  88. set {
  89. timeout = value;
  90. }
  91. }
  92. [DataCategory ("Data")]
  93. [DefaultValue ("Text")]
  94. [DataSysDescriptionAttribute ("How to interpret the CommandText")]
  95. [RefreshPropertiesAttribute (RefreshProperties.All)]
  96. public CommandType CommandType {
  97. get {
  98. return commandType;
  99. }
  100. set {
  101. commandType = value;
  102. }
  103. }
  104. [DataCategory ("Behavior")]
  105. [DataSysDescriptionAttribute ("Connection used by the command")]
  106. [DefaultValue (null)]
  107. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  108. public OleDbConnection Connection {
  109. get {
  110. return connection;
  111. }
  112. set {
  113. connection = value;
  114. }
  115. }
  116. [BrowsableAttribute (false)]
  117. [DesignOnlyAttribute (true)]
  118. [DefaultValue (true)]
  119. public bool DesignTimeVisible {
  120. get {
  121. return designTimeVisible;
  122. }
  123. set {
  124. designTimeVisible = value;
  125. }
  126. }
  127. [DataCategory ("Data")]
  128. [DataSysDescriptionAttribute ("The parameters collection")]
  129. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  130. public OleDbParameterCollection Parameters {
  131. get {
  132. return parameters;
  133. }
  134. }
  135. [BrowsableAttribute (false)]
  136. [DataSysDescriptionAttribute ("The transaction used by the command")]
  137. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  138. public OleDbTransaction Transaction {
  139. get {
  140. return transaction;
  141. }
  142. set {
  143. transaction = value;
  144. }
  145. }
  146. [DataCategory ("Behavior")]
  147. [DefaultValue (UpdateRowSource.Both)]
  148. [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
  149. public UpdateRowSource UpdatedRowSource {
  150. [MonoTODO]
  151. get {
  152. throw new NotImplementedException ();
  153. }
  154. [MonoTODO]
  155. set {
  156. throw new NotImplementedException ();
  157. }
  158. }
  159. IDbConnection IDbCommand.Connection {
  160. get {
  161. return Connection;
  162. }
  163. set {
  164. Connection = (OleDbConnection) value;
  165. }
  166. }
  167. IDataParameterCollection IDbCommand.Parameters {
  168. get {
  169. return Parameters;
  170. }
  171. }
  172. IDbTransaction IDbCommand.Transaction {
  173. get {
  174. return Transaction;
  175. }
  176. set {
  177. Transaction = (OleDbTransaction) value;
  178. }
  179. }
  180. #endregion // Properties
  181. #region Methods
  182. [MonoTODO]
  183. public void Cancel ()
  184. {
  185. throw new NotImplementedException ();
  186. }
  187. public OleDbParameter CreateParameter ()
  188. {
  189. return new OleDbParameter ();
  190. }
  191. IDbDataParameter IDbCommand.CreateParameter ()
  192. {
  193. return CreateParameter ();
  194. }
  195. [MonoTODO]
  196. protected override void Dispose (bool disposing)
  197. {
  198. throw new NotImplementedException ();
  199. }
  200. private void SetupGdaCommand ()
  201. {
  202. GdaCommandType type;
  203. switch (commandType) {
  204. case CommandType.TableDirect :
  205. type = GdaCommandType.Table;
  206. break;
  207. case CommandType.StoredProcedure :
  208. type = GdaCommandType.Procedure;
  209. break;
  210. case CommandType.Text :
  211. default :
  212. type = GdaCommandType.Sql;
  213. break;
  214. }
  215. if (gdaCommand != IntPtr.Zero) {
  216. libgda.gda_command_set_text (gdaCommand, commandText);
  217. libgda.gda_command_set_command_type (gdaCommand, type);
  218. } else {
  219. gdaCommand = libgda.gda_command_new (commandText, type, 0);
  220. }
  221. //libgda.gda_command_set_transaction
  222. }
  223. public int ExecuteNonQuery ()
  224. {
  225. if (connection == null)
  226. throw new InvalidOperationException ("connection == null");
  227. if (connection.State == ConnectionState.Closed)
  228. throw new InvalidOperationException ("State == Closed");
  229. // FIXME: a third check is mentioned in .NET docs
  230. IntPtr gdaConnection = connection.GdaConnection;
  231. IntPtr gdaParameterList = parameters.GdaParameterList;
  232. SetupGdaCommand ();
  233. return libgda.gda_connection_execute_non_query (gdaConnection,
  234. (IntPtr) gdaCommand,
  235. gdaParameterList);
  236. }
  237. public OleDbDataReader ExecuteReader ()
  238. {
  239. return ExecuteReader (CommandBehavior.Default);
  240. }
  241. IDataReader IDbCommand.ExecuteReader ()
  242. {
  243. return ExecuteReader ();
  244. }
  245. public OleDbDataReader ExecuteReader (CommandBehavior behavior)
  246. {
  247. ArrayList results = new ArrayList ();
  248. IntPtr rs_list;
  249. GdaList glist_node;
  250. if (connection.State != ConnectionState.Open)
  251. throw new InvalidOperationException ("State != Open");
  252. this.behavior = behavior;
  253. IntPtr gdaConnection = connection.GdaConnection;
  254. IntPtr gdaParameterList = parameters.GdaParameterList;
  255. /* execute the command */
  256. SetupGdaCommand ();
  257. rs_list = libgda.gda_connection_execute_command (
  258. gdaConnection,
  259. gdaCommand,
  260. gdaParameterList);
  261. if (rs_list != IntPtr.Zero) {
  262. glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
  263. while (glist_node != null) {
  264. results.Add (glist_node.data);
  265. if (glist_node.next == IntPtr.Zero)
  266. break;
  267. glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
  268. typeof (GdaList));
  269. }
  270. dataReader = new OleDbDataReader (this, results);
  271. dataReader.NextResult ();
  272. }
  273. return dataReader;
  274. }
  275. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  276. {
  277. return ExecuteReader (behavior);
  278. }
  279. public object ExecuteScalar ()
  280. {
  281. SetupGdaCommand ();
  282. OleDbDataReader reader = ExecuteReader ();
  283. if (reader == null) {
  284. return null;
  285. }
  286. if (!reader.Read ()) {
  287. reader.Close ();
  288. return null;
  289. }
  290. object o = reader.GetValue (0);
  291. reader.Close ();
  292. return o;
  293. }
  294. [MonoTODO]
  295. object ICloneable.Clone ()
  296. {
  297. throw new NotImplementedException ();
  298. }
  299. [MonoTODO]
  300. public void Prepare ()
  301. {
  302. throw new NotImplementedException ();
  303. }
  304. public void ResetCommandTimeout ()
  305. {
  306. timeout = 30;
  307. }
  308. #endregion
  309. }
  310. }