OleDbCommand.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System.ComponentModel;
  34. using System.Data;
  35. using System.Data.Common;
  36. using System.Collections;
  37. using System.Runtime.InteropServices;
  38. namespace System.Data.OleDb
  39. {
  40. /// <summary>
  41. /// Represents an SQL statement or stored procedure to execute against a data source.
  42. /// </summary>
  43. [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OleDbCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
  44. [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
  45. public sealed class OleDbCommand : Component, ICloneable, IDbCommand
  46. {
  47. #region Fields
  48. string commandText;
  49. int timeout;
  50. CommandType commandType;
  51. OleDbConnection connection;
  52. OleDbParameterCollection parameters;
  53. OleDbTransaction transaction;
  54. bool designTimeVisible;
  55. OleDbDataReader dataReader;
  56. CommandBehavior behavior;
  57. IntPtr gdaCommand;
  58. #endregion // Fields
  59. #region Constructors
  60. public OleDbCommand ()
  61. {
  62. commandText = String.Empty;
  63. timeout = 30; // default timeout per .NET
  64. commandType = CommandType.Text;
  65. connection = null;
  66. parameters = new OleDbParameterCollection ();
  67. transaction = null;
  68. designTimeVisible = false;
  69. dataReader = null;
  70. behavior = CommandBehavior.Default;
  71. gdaCommand = IntPtr.Zero;
  72. }
  73. public OleDbCommand (string cmdText) : this ()
  74. {
  75. CommandText = cmdText;
  76. }
  77. public OleDbCommand (string cmdText, OleDbConnection connection)
  78. : this (cmdText)
  79. {
  80. Connection = connection;
  81. }
  82. public OleDbCommand (string cmdText,
  83. OleDbConnection connection,
  84. OleDbTransaction transaction) : this (cmdText, connection)
  85. {
  86. this.transaction = transaction;
  87. }
  88. #endregion // Constructors
  89. #region Properties
  90. [DataCategory ("Data")]
  91. [DefaultValue ("")]
  92. [DataSysDescriptionAttribute ("Command text to execute")]
  93. [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  94. [RefreshPropertiesAttribute (RefreshProperties.All)]
  95. public string CommandText
  96. {
  97. get {
  98. return commandText;
  99. }
  100. set {
  101. commandText = value;
  102. }
  103. }
  104. [DataSysDescriptionAttribute ("Time to wait for command to execute")]
  105. [DefaultValue (30)]
  106. public int CommandTimeout {
  107. get {
  108. return timeout;
  109. }
  110. set {
  111. timeout = value;
  112. }
  113. }
  114. [DataCategory ("Data")]
  115. [DefaultValue ("Text")]
  116. [DataSysDescriptionAttribute ("How to interpret the CommandText")]
  117. [RefreshPropertiesAttribute (RefreshProperties.All)]
  118. public CommandType CommandType {
  119. get {
  120. return commandType;
  121. }
  122. set {
  123. commandType = value;
  124. }
  125. }
  126. [DataCategory ("Behavior")]
  127. [DataSysDescriptionAttribute ("Connection used by the command")]
  128. [DefaultValue (null)]
  129. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  130. public OleDbConnection Connection {
  131. get {
  132. return connection;
  133. }
  134. set {
  135. connection = value;
  136. }
  137. }
  138. [BrowsableAttribute (false)]
  139. [DesignOnlyAttribute (true)]
  140. [DefaultValue (true)]
  141. public bool DesignTimeVisible {
  142. get {
  143. return designTimeVisible;
  144. }
  145. set {
  146. designTimeVisible = value;
  147. }
  148. }
  149. [DataCategory ("Data")]
  150. [DataSysDescriptionAttribute ("The parameters collection")]
  151. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  152. public OleDbParameterCollection Parameters {
  153. get {
  154. return parameters;
  155. }
  156. }
  157. [BrowsableAttribute (false)]
  158. [DataSysDescriptionAttribute ("The transaction used by the command")]
  159. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  160. public OleDbTransaction Transaction {
  161. get {
  162. return transaction;
  163. }
  164. set {
  165. transaction = value;
  166. }
  167. }
  168. [DataCategory ("Behavior")]
  169. [DefaultValue (UpdateRowSource.Both)]
  170. [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
  171. public UpdateRowSource UpdatedRowSource {
  172. [MonoTODO]
  173. get {
  174. throw new NotImplementedException ();
  175. }
  176. [MonoTODO]
  177. set {
  178. throw new NotImplementedException ();
  179. }
  180. }
  181. IDbConnection IDbCommand.Connection {
  182. get {
  183. return Connection;
  184. }
  185. set {
  186. Connection = (OleDbConnection) value;
  187. }
  188. }
  189. IDataParameterCollection IDbCommand.Parameters {
  190. get {
  191. return Parameters;
  192. }
  193. }
  194. IDbTransaction IDbCommand.Transaction {
  195. get {
  196. return Transaction;
  197. }
  198. set {
  199. Transaction = (OleDbTransaction) value;
  200. }
  201. }
  202. #endregion // Properties
  203. #region Methods
  204. [MonoTODO]
  205. public void Cancel ()
  206. {
  207. throw new NotImplementedException ();
  208. }
  209. public OleDbParameter CreateParameter ()
  210. {
  211. return new OleDbParameter ();
  212. }
  213. IDbDataParameter IDbCommand.CreateParameter ()
  214. {
  215. return CreateParameter ();
  216. }
  217. [MonoTODO]
  218. protected override void Dispose (bool disposing)
  219. {
  220. throw new NotImplementedException ();
  221. }
  222. private void SetupGdaCommand ()
  223. {
  224. GdaCommandType type;
  225. switch (commandType) {
  226. case CommandType.TableDirect :
  227. type = GdaCommandType.Table;
  228. break;
  229. case CommandType.StoredProcedure :
  230. type = GdaCommandType.Procedure;
  231. break;
  232. case CommandType.Text :
  233. default :
  234. type = GdaCommandType.Sql;
  235. break;
  236. }
  237. if (gdaCommand != IntPtr.Zero) {
  238. libgda.gda_command_set_text (gdaCommand, commandText);
  239. libgda.gda_command_set_command_type (gdaCommand, type);
  240. } else {
  241. gdaCommand = libgda.gda_command_new (commandText, type, 0);
  242. }
  243. //libgda.gda_command_set_transaction
  244. }
  245. public int ExecuteNonQuery ()
  246. {
  247. if (connection == null)
  248. throw new InvalidOperationException ("connection == null");
  249. if (connection.State == ConnectionState.Closed)
  250. throw new InvalidOperationException ("State == Closed");
  251. // FIXME: a third check is mentioned in .NET docs
  252. IntPtr gdaConnection = connection.GdaConnection;
  253. IntPtr gdaParameterList = parameters.GdaParameterList;
  254. SetupGdaCommand ();
  255. return libgda.gda_connection_execute_non_query (gdaConnection,
  256. (IntPtr) gdaCommand,
  257. gdaParameterList);
  258. }
  259. public OleDbDataReader ExecuteReader ()
  260. {
  261. return ExecuteReader (CommandBehavior.Default);
  262. }
  263. IDataReader IDbCommand.ExecuteReader ()
  264. {
  265. return ExecuteReader ();
  266. }
  267. public OleDbDataReader ExecuteReader (CommandBehavior behavior)
  268. {
  269. ArrayList results = new ArrayList ();
  270. IntPtr rs_list;
  271. GdaList glist_node;
  272. if (connection.State != ConnectionState.Open)
  273. throw new InvalidOperationException ("State != Open");
  274. this.behavior = behavior;
  275. IntPtr gdaConnection = connection.GdaConnection;
  276. IntPtr gdaParameterList = parameters.GdaParameterList;
  277. /* execute the command */
  278. SetupGdaCommand ();
  279. rs_list = libgda.gda_connection_execute_command (
  280. gdaConnection,
  281. gdaCommand,
  282. gdaParameterList);
  283. if (rs_list != IntPtr.Zero) {
  284. glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
  285. while (glist_node != null) {
  286. results.Add (glist_node.data);
  287. if (glist_node.next == IntPtr.Zero)
  288. break;
  289. glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
  290. typeof (GdaList));
  291. }
  292. dataReader = new OleDbDataReader (this, results);
  293. dataReader.NextResult ();
  294. }
  295. return dataReader;
  296. }
  297. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  298. {
  299. return ExecuteReader (behavior);
  300. }
  301. public object ExecuteScalar ()
  302. {
  303. SetupGdaCommand ();
  304. OleDbDataReader reader = ExecuteReader ();
  305. if (reader == null) {
  306. return null;
  307. }
  308. if (!reader.Read ()) {
  309. reader.Close ();
  310. return null;
  311. }
  312. object o = reader.GetValue (0);
  313. reader.Close ();
  314. return o;
  315. }
  316. [MonoTODO]
  317. object ICloneable.Clone ()
  318. {
  319. throw new NotImplementedException ();
  320. }
  321. [MonoTODO]
  322. public void Prepare ()
  323. {
  324. throw new NotImplementedException ();
  325. }
  326. public void ResetCommandTimeout ()
  327. {
  328. timeout = 30;
  329. }
  330. #endregion
  331. }
  332. }