OleDbCommand.cs 10 KB

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