OleDbCommand.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 :
  46. #if NET_2_0
  47. DbCommand
  48. #else
  49. Component
  50. #endif
  51. , ICloneable, IDbCommand
  52. {
  53. #region Fields
  54. string commandText;
  55. int timeout;
  56. CommandType commandType;
  57. OleDbConnection connection;
  58. OleDbParameterCollection parameters;
  59. OleDbTransaction transaction;
  60. bool designTimeVisible;
  61. OleDbDataReader dataReader;
  62. CommandBehavior behavior;
  63. IntPtr gdaCommand;
  64. #endregion // Fields
  65. #region Constructors
  66. public OleDbCommand ()
  67. {
  68. commandText = String.Empty;
  69. timeout = 30; // default timeout per .NET
  70. commandType = CommandType.Text;
  71. connection = null;
  72. parameters = new OleDbParameterCollection ();
  73. transaction = null;
  74. designTimeVisible = false;
  75. dataReader = null;
  76. behavior = CommandBehavior.Default;
  77. gdaCommand = IntPtr.Zero;
  78. }
  79. public OleDbCommand (string cmdText) : this ()
  80. {
  81. CommandText = cmdText;
  82. }
  83. public OleDbCommand (string cmdText, OleDbConnection connection)
  84. : this (cmdText)
  85. {
  86. Connection = connection;
  87. }
  88. public OleDbCommand (string cmdText,
  89. OleDbConnection connection,
  90. OleDbTransaction transaction) : this (cmdText, connection)
  91. {
  92. this.transaction = transaction;
  93. }
  94. #endregion // Constructors
  95. #region Properties
  96. [DataCategory ("Data")]
  97. [DefaultValue ("")]
  98. #if !NET_2_0
  99. [DataSysDescriptionAttribute ("Command text to execute.")]
  100. #endif
  101. [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  102. [RefreshPropertiesAttribute (RefreshProperties.All)]
  103. public
  104. #if NET_2_0
  105. override
  106. #endif
  107. string CommandText
  108. {
  109. get {
  110. return commandText;
  111. }
  112. set {
  113. commandText = value;
  114. }
  115. }
  116. #if !NET_2_0
  117. [DataSysDescriptionAttribute ("Time to wait for command to execute.")]
  118. #endif
  119. [DefaultValue (30)]
  120. public
  121. #if NET_2_0
  122. override
  123. #endif
  124. int CommandTimeout {
  125. get {
  126. return timeout;
  127. }
  128. set {
  129. timeout = value;
  130. }
  131. }
  132. [DataCategory ("Data")]
  133. [DefaultValue ("Text")]
  134. #if !NET_2_0
  135. [DataSysDescriptionAttribute ("How to interpret the CommandText.")]
  136. #endif
  137. [RefreshPropertiesAttribute (RefreshProperties.All)]
  138. public
  139. #if NET_2_0
  140. override
  141. #endif
  142. CommandType CommandType {
  143. get {
  144. return commandType;
  145. }
  146. set {
  147. commandType = value;
  148. }
  149. }
  150. [DataCategory ("Behavior")]
  151. #if !NET_2_0
  152. [DataSysDescriptionAttribute ("Connection used by the command.")]
  153. #endif
  154. [DefaultValue (null)]
  155. [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
  156. public OleDbConnection Connection {
  157. get {
  158. return connection;
  159. }
  160. set {
  161. connection = value;
  162. }
  163. }
  164. [BrowsableAttribute (false)]
  165. [DesignOnlyAttribute (true)]
  166. [DefaultValue (true)]
  167. public
  168. #if NET_2_0
  169. override
  170. #endif
  171. bool DesignTimeVisible {
  172. get {
  173. return designTimeVisible;
  174. }
  175. set {
  176. designTimeVisible = value;
  177. }
  178. }
  179. [DataCategory ("Data")]
  180. #if !NET_2_0
  181. [DataSysDescriptionAttribute ("The parameters collection.")]
  182. #endif
  183. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
  184. public OleDbParameterCollection Parameters {
  185. get {
  186. return parameters;
  187. }
  188. }
  189. [BrowsableAttribute (false)]
  190. #if !NET_2_0
  191. [DataSysDescriptionAttribute ("The transaction used by the command.")]
  192. #endif
  193. [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
  194. public OleDbTransaction Transaction {
  195. get {
  196. return transaction;
  197. }
  198. set {
  199. transaction = value;
  200. }
  201. }
  202. [DataCategory ("Behavior")]
  203. [DefaultValue (UpdateRowSource.Both)]
  204. #if !NET_2_0
  205. [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
  206. #endif
  207. public
  208. #if NET_2_0
  209. override
  210. #endif
  211. UpdateRowSource UpdatedRowSource {
  212. [MonoTODO]
  213. get {
  214. throw new NotImplementedException ();
  215. }
  216. [MonoTODO]
  217. set {
  218. throw new NotImplementedException ();
  219. }
  220. }
  221. IDbConnection IDbCommand.Connection {
  222. get {
  223. return Connection;
  224. }
  225. set {
  226. Connection = (OleDbConnection) value;
  227. }
  228. }
  229. IDataParameterCollection IDbCommand.Parameters {
  230. get {
  231. return Parameters;
  232. }
  233. }
  234. IDbTransaction IDbCommand.Transaction {
  235. get {
  236. return Transaction;
  237. }
  238. set {
  239. Transaction = (OleDbTransaction) value;
  240. }
  241. }
  242. #endregion // Properties
  243. #region Methods
  244. [MonoTODO]
  245. public
  246. #if NET_2_0
  247. override
  248. #endif
  249. void Cancel ()
  250. {
  251. throw new NotImplementedException ();
  252. }
  253. public OleDbParameter CreateParameter ()
  254. {
  255. return new OleDbParameter ();
  256. }
  257. IDbDataParameter IDbCommand.CreateParameter ()
  258. {
  259. return CreateParameter ();
  260. }
  261. [MonoTODO]
  262. protected override void Dispose (bool disposing)
  263. {
  264. throw new NotImplementedException ();
  265. }
  266. private void SetupGdaCommand ()
  267. {
  268. GdaCommandType type;
  269. switch (commandType) {
  270. case CommandType.TableDirect :
  271. type = GdaCommandType.Table;
  272. break;
  273. case CommandType.StoredProcedure :
  274. type = GdaCommandType.Procedure;
  275. break;
  276. case CommandType.Text :
  277. default :
  278. type = GdaCommandType.Sql;
  279. break;
  280. }
  281. if (gdaCommand != IntPtr.Zero) {
  282. libgda.gda_command_set_text (gdaCommand, commandText);
  283. libgda.gda_command_set_command_type (gdaCommand, type);
  284. } else {
  285. gdaCommand = libgda.gda_command_new (commandText, type, 0);
  286. }
  287. //libgda.gda_command_set_transaction
  288. }
  289. public
  290. #if NET_2_0
  291. override
  292. #endif
  293. int ExecuteNonQuery ()
  294. {
  295. if (connection == null)
  296. throw new InvalidOperationException ("connection == null");
  297. if (connection.State == ConnectionState.Closed)
  298. throw new InvalidOperationException ("State == Closed");
  299. // FIXME: a third check is mentioned in .NET docs
  300. IntPtr gdaConnection = connection.GdaConnection;
  301. IntPtr gdaParameterList = parameters.GdaParameterList;
  302. SetupGdaCommand ();
  303. return libgda.gda_connection_execute_non_query (gdaConnection,
  304. (IntPtr) gdaCommand,
  305. gdaParameterList);
  306. }
  307. public OleDbDataReader ExecuteReader ()
  308. {
  309. return ExecuteReader (CommandBehavior.Default);
  310. }
  311. IDataReader IDbCommand.ExecuteReader ()
  312. {
  313. return ExecuteReader ();
  314. }
  315. public OleDbDataReader ExecuteReader (CommandBehavior behavior)
  316. {
  317. ArrayList results = new ArrayList ();
  318. IntPtr rs_list;
  319. GdaList glist_node;
  320. if (connection.State != ConnectionState.Open)
  321. throw new InvalidOperationException ("State != Open");
  322. this.behavior = behavior;
  323. IntPtr gdaConnection = connection.GdaConnection;
  324. IntPtr gdaParameterList = parameters.GdaParameterList;
  325. /* execute the command */
  326. SetupGdaCommand ();
  327. rs_list = libgda.gda_connection_execute_command (
  328. gdaConnection,
  329. gdaCommand,
  330. gdaParameterList);
  331. if (rs_list != IntPtr.Zero) {
  332. glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
  333. while (glist_node != null) {
  334. results.Add (glist_node.data);
  335. if (glist_node.next == IntPtr.Zero)
  336. break;
  337. glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
  338. typeof (GdaList));
  339. }
  340. dataReader = new OleDbDataReader (this, results);
  341. dataReader.NextResult ();
  342. }
  343. return dataReader;
  344. }
  345. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  346. {
  347. return ExecuteReader (behavior);
  348. }
  349. public
  350. #if NET_2_0
  351. override
  352. #endif
  353. object ExecuteScalar ()
  354. {
  355. SetupGdaCommand ();
  356. OleDbDataReader reader = ExecuteReader ();
  357. if (reader == null) {
  358. return null;
  359. }
  360. if (!reader.Read ()) {
  361. reader.Close ();
  362. return null;
  363. }
  364. object o = reader.GetValue (0);
  365. reader.Close ();
  366. return o;
  367. }
  368. [MonoTODO]
  369. object ICloneable.Clone ()
  370. {
  371. throw new NotImplementedException ();
  372. }
  373. [MonoTODO]
  374. public
  375. #if NET_2_0
  376. override
  377. #endif
  378. void Prepare ()
  379. {
  380. throw new NotImplementedException ();
  381. }
  382. public void ResetCommandTimeout ()
  383. {
  384. timeout = 30;
  385. }
  386. #if NET_2_0
  387. [MonoTODO]
  388. protected override DbParameter CreateDbParameter ()
  389. {
  390. throw new NotImplementedException ();
  391. }
  392. [MonoTODO]
  393. protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
  394. {
  395. throw new NotImplementedException ();
  396. }
  397. [MonoTODO]
  398. protected override DbConnection DbConnection {
  399. get { throw new NotImplementedException (); }
  400. set { throw new NotImplementedException (); }
  401. }
  402. [MonoTODO]
  403. protected override DbParameterCollection DbParameterCollection {
  404. get { throw new NotImplementedException (); }
  405. }
  406. [MonoTODO]
  407. protected override DbTransaction DbTransaction {
  408. get { throw new NotImplementedException (); }
  409. set { throw new NotImplementedException (); }
  410. }
  411. #endif
  412. #endregion
  413. }
  414. }