OleDbCommand.cs 10 KB

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