OleDbCommand.cs 11 KB

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