2 // System.Data.OleDb.OleDbCommand
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.ComponentModel;
37 using System.Data.Common;
38 using System.Collections;
39 using System.Runtime.InteropServices;
42 using System.Data.ProviderBase;
45 namespace System.Data.OleDb
48 /// Represents an SQL statement or stored procedure to execute against a data source.
50 [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OleDbCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
51 [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
52 public sealed class OleDbCommand :
58 , ICloneable, IDbCommand
64 CommandType commandType;
65 OleDbConnection connection;
66 OleDbParameterCollection parameters;
67 OleDbTransaction transaction;
68 bool designTimeVisible;
69 OleDbDataReader dataReader;
70 CommandBehavior behavior;
77 public OleDbCommand ()
79 commandText = String.Empty;
80 timeout = 30; // default timeout per .NET
81 commandType = CommandType.Text;
83 parameters = new OleDbParameterCollection ();
85 designTimeVisible = false;
87 behavior = CommandBehavior.Default;
88 gdaCommand = IntPtr.Zero;
91 public OleDbCommand (string cmdText) : this ()
93 CommandText = cmdText;
96 public OleDbCommand (string cmdText, OleDbConnection connection)
99 Connection = connection;
102 public OleDbCommand (string cmdText,
103 OleDbConnection connection,
104 OleDbTransaction transaction) : this (cmdText, connection)
106 this.transaction = transaction;
109 #endregion // Constructors
113 [DataCategory ("Data")]
116 [DataSysDescriptionAttribute ("Command text to execute.")]
118 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
119 [RefreshPropertiesAttribute (RefreshProperties.All)]
120 public string CommandText
131 [DataSysDescriptionAttribute ("Time to wait for command to execute.")]
134 public int CommandTimeout {
143 [DataCategory ("Data")]
144 [DefaultValue ("Text")]
146 [DataSysDescriptionAttribute ("How to interpret the CommandText.")]
148 [RefreshPropertiesAttribute (RefreshProperties.All)]
149 public CommandType CommandType {
158 [DataCategory ("Behavior")]
160 [DataSysDescriptionAttribute ("Connection used by the command.")]
162 [DefaultValue (null)]
163 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
164 public OleDbConnection Connection {
173 [BrowsableAttribute (false)]
174 [DesignOnlyAttribute (true)]
175 [DefaultValue (true)]
176 public bool DesignTimeVisible {
178 return designTimeVisible;
181 designTimeVisible = value;
185 [DataCategory ("Data")]
187 [DataSysDescriptionAttribute ("The parameters collection.")]
189 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
190 public OleDbParameterCollection Parameters {
196 [BrowsableAttribute (false)]
198 [DataSysDescriptionAttribute ("The transaction used by the command.")]
200 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
201 public OleDbTransaction Transaction {
210 [DataCategory ("Behavior")]
211 [DefaultValue (UpdateRowSource.Both)]
213 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
215 public UpdateRowSource UpdatedRowSource {
218 throw new NotImplementedException ();
222 throw new NotImplementedException ();
226 IDbConnection IDbCommand.Connection {
231 Connection = (OleDbConnection) value;
235 IDataParameterCollection IDbCommand.Parameters {
241 IDbTransaction IDbCommand.Transaction {
246 Transaction = (OleDbTransaction) value;
250 #endregion // Properties
255 public void Cancel ()
257 throw new NotImplementedException ();
260 public OleDbParameter CreateParameter ()
262 return new OleDbParameter ();
265 IDbDataParameter IDbCommand.CreateParameter ()
267 return CreateParameter ();
271 protected override void Dispose (bool disposing)
273 throw new NotImplementedException ();
276 private void SetupGdaCommand ()
280 switch (commandType) {
281 case CommandType.TableDirect :
282 type = GdaCommandType.Table;
284 case CommandType.StoredProcedure :
285 type = GdaCommandType.Procedure;
287 case CommandType.Text :
289 type = GdaCommandType.Sql;
293 if (gdaCommand != IntPtr.Zero) {
294 libgda.gda_command_set_text (gdaCommand, commandText);
295 libgda.gda_command_set_command_type (gdaCommand, type);
297 gdaCommand = libgda.gda_command_new (commandText, type, 0);
300 //libgda.gda_command_set_transaction
303 public int ExecuteNonQuery ()
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
311 IntPtr gdaConnection = connection.GdaConnection;
312 IntPtr gdaParameterList = parameters.GdaParameterList;
315 return libgda.gda_connection_execute_non_query (gdaConnection,
320 public OleDbDataReader ExecuteReader ()
322 return ExecuteReader (CommandBehavior.Default);
325 IDataReader IDbCommand.ExecuteReader ()
327 return ExecuteReader ();
330 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
332 ArrayList results = new ArrayList ();
336 if (connection.State != ConnectionState.Open)
337 throw new InvalidOperationException ("State != Open");
339 this.behavior = behavior;
341 IntPtr gdaConnection = connection.GdaConnection;
342 IntPtr gdaParameterList = parameters.GdaParameterList;
344 /* execute the command */
346 rs_list = libgda.gda_connection_execute_command (
350 if (rs_list != IntPtr.Zero) {
351 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
353 while (glist_node != null) {
354 results.Add (glist_node.data);
355 if (glist_node.next == IntPtr.Zero)
358 glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
361 dataReader = new OleDbDataReader (this, results);
362 dataReader.NextResult ();
368 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
370 return ExecuteReader (behavior);
373 public object ExecuteScalar ()
376 OleDbDataReader reader = ExecuteReader ();
377 if (reader == null) {
380 if (!reader.Read ()) {
384 object o = reader.GetValue (0);
390 object ICloneable.Clone ()
392 throw new NotImplementedException ();
396 public void Prepare ()
398 throw new NotImplementedException ();
401 public void ResetCommandTimeout ()
408 protected override DbParameter CreateDbParameter ()
410 throw new NotImplementedException ();
414 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
416 throw new NotImplementedException ();
420 protected override DbConnection DbConnection {
421 get { throw new NotImplementedException (); }
422 set { throw new NotImplementedException (); }
426 protected override DbParameterCollection DbParameterCollection {
427 get { throw new NotImplementedException (); }
431 protected override DbTransaction DbTransaction {
432 get { throw new NotImplementedException (); }
433 set { throw new NotImplementedException (); }