OleDbCommand.cs 11 KB

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