OleDbCommand.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. {
  221. if (!Enum.IsDefined (typeof (UpdateRowSource), value))
  222. throw ExceptionHelper.InvalidEnumValueException ("UpdateRowSource", value);
  223. updatedRowSource = value;
  224. }
  225. }
  226. IDbConnection IDbCommand.Connection {
  227. get {
  228. return Connection;
  229. }
  230. set {
  231. Connection = (OleDbConnection) value;
  232. }
  233. }
  234. IDataParameterCollection IDbCommand.Parameters {
  235. get {
  236. return Parameters;
  237. }
  238. }
  239. IDbTransaction IDbCommand.Transaction {
  240. get {
  241. return Transaction;
  242. }
  243. set {
  244. Transaction = (OleDbTransaction) value;
  245. }
  246. }
  247. #endregion // Properties
  248. #region Methods
  249. [MonoTODO]
  250. public
  251. #if NET_2_0
  252. override
  253. #endif
  254. void Cancel ()
  255. {
  256. throw new NotImplementedException ();
  257. }
  258. public new OleDbParameter CreateParameter ()
  259. {
  260. return new OleDbParameter ();
  261. }
  262. #if !NET_2_0
  263. IDbDataParameter IDbCommand.CreateParameter ()
  264. {
  265. return CreateParameter ();
  266. }
  267. #endif
  268. protected override void Dispose (bool disposing)
  269. {
  270. if (disposed)
  271. return;
  272. Connection = null;
  273. Transaction = null;
  274. disposed = true;
  275. }
  276. private void SetupGdaCommand ()
  277. {
  278. GdaCommandType type;
  279. switch (commandType) {
  280. case CommandType.TableDirect :
  281. type = GdaCommandType.Table;
  282. break;
  283. case CommandType.StoredProcedure :
  284. type = GdaCommandType.Procedure;
  285. break;
  286. case CommandType.Text :
  287. default :
  288. type = GdaCommandType.Sql;
  289. break;
  290. }
  291. if (gdaCommand != IntPtr.Zero) {
  292. libgda.gda_command_set_text (gdaCommand, CommandText);
  293. libgda.gda_command_set_command_type (gdaCommand, type);
  294. } else {
  295. gdaCommand = libgda.gda_command_new (CommandText, type, 0);
  296. }
  297. //libgda.gda_command_set_transaction
  298. }
  299. public
  300. #if NET_2_0
  301. override
  302. #endif
  303. int ExecuteNonQuery ()
  304. {
  305. if (connection == null)
  306. throw new InvalidOperationException ("connection == null");
  307. if (connection.State == ConnectionState.Closed)
  308. throw new InvalidOperationException ("State == Closed");
  309. // FIXME: a third check is mentioned in .NET docs
  310. IntPtr gdaConnection = connection.GdaConnection;
  311. IntPtr gdaParameterList = parameters.GdaParameterList;
  312. SetupGdaCommand ();
  313. return libgda.gda_connection_execute_non_query (gdaConnection,
  314. (IntPtr) gdaCommand,
  315. gdaParameterList);
  316. }
  317. public new OleDbDataReader ExecuteReader ()
  318. {
  319. return ExecuteReader (behavior);
  320. }
  321. IDataReader IDbCommand.ExecuteReader ()
  322. {
  323. return ExecuteReader ();
  324. }
  325. public new OleDbDataReader ExecuteReader (CommandBehavior behavior)
  326. {
  327. ArrayList results = new ArrayList ();
  328. IntPtr rs_list;
  329. GdaList glist_node;
  330. if (connection.State != ConnectionState.Open)
  331. throw new InvalidOperationException ("State != Open");
  332. this.behavior = behavior;
  333. IntPtr gdaConnection = connection.GdaConnection;
  334. IntPtr gdaParameterList = parameters.GdaParameterList;
  335. /* execute the command */
  336. SetupGdaCommand ();
  337. rs_list = libgda.gda_connection_execute_command (
  338. gdaConnection,
  339. gdaCommand,
  340. gdaParameterList);
  341. if (rs_list != IntPtr.Zero) {
  342. glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
  343. while (glist_node != null) {
  344. results.Add (glist_node.data);
  345. if (glist_node.next == IntPtr.Zero)
  346. break;
  347. glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
  348. typeof (GdaList));
  349. }
  350. dataReader = new OleDbDataReader (this, results);
  351. dataReader.NextResult ();
  352. }
  353. return dataReader;
  354. }
  355. IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
  356. {
  357. return ExecuteReader (behavior);
  358. }
  359. public
  360. #if NET_2_0
  361. override
  362. #endif
  363. object ExecuteScalar ()
  364. {
  365. SetupGdaCommand ();
  366. OleDbDataReader reader = ExecuteReader ();
  367. if (reader == null) {
  368. return null;
  369. }
  370. if (!reader.Read ()) {
  371. reader.Close ();
  372. return null;
  373. }
  374. object o = reader.GetValue (0);
  375. reader.Close ();
  376. return o;
  377. }
  378. #if NET_2_0
  379. public
  380. #else
  381. internal
  382. #endif
  383. OleDbCommand Clone ()
  384. {
  385. OleDbCommand command = new OleDbCommand ();
  386. command.CommandText = this.CommandText;
  387. command.CommandTimeout = this.CommandTimeout;
  388. command.CommandType = this.CommandType;
  389. command.Connection = this.Connection;
  390. command.DesignTimeVisible = this.DesignTimeVisible;
  391. command.Parameters = this.Parameters;
  392. command.Transaction = this.Transaction;
  393. return command;
  394. }
  395. object ICloneable.Clone ()
  396. {
  397. return Clone ();
  398. }
  399. [MonoTODO]
  400. public
  401. #if NET_2_0
  402. override
  403. #endif
  404. void Prepare ()
  405. {
  406. throw new NotImplementedException ();
  407. }
  408. public void ResetCommandTimeout ()
  409. {
  410. timeout = DEFAULT_COMMAND_TIMEOUT;
  411. }
  412. #if NET_2_0
  413. protected override DbParameter CreateDbParameter ()
  414. {
  415. return (DbParameter) CreateParameter ();
  416. }
  417. protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
  418. {
  419. return (DbDataReader) ExecuteReader (behavior);
  420. }
  421. protected override DbConnection DbConnection {
  422. get { return Connection; }
  423. set { Connection = (OleDbConnection) value; }
  424. }
  425. protected override DbParameterCollection DbParameterCollection {
  426. get { return Parameters; }
  427. }
  428. protected override DbTransaction DbTransaction {
  429. get { return Transaction; }
  430. set { Transaction = (OleDbTransaction) value; }
  431. }
  432. #endif
  433. #endregion
  434. }
  435. }