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;
46 namespace System.Data.OleDb
49 /// Represents an SQL statement or stored procedure to execute against a data source.
51 [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OleDbCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
52 [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
53 public sealed class OleDbCommand :
59 , ICloneable, IDbCommand
65 CommandType commandType;
66 OleDbConnection connection;
67 OleDbParameterCollection parameters;
68 OleDbTransaction transaction;
69 bool designTimeVisible;
70 OleDbDataReader dataReader;
71 CommandBehavior behavior;
78 public OleDbCommand ()
80 commandText = String.Empty;
81 timeout = 30; // default timeout per .NET
82 commandType = CommandType.Text;
84 parameters = new OleDbParameterCollection ();
86 designTimeVisible = false;
88 behavior = CommandBehavior.Default;
89 gdaCommand = IntPtr.Zero;
92 public OleDbCommand (string cmdText) : this ()
94 CommandText = cmdText;
97 public OleDbCommand (string cmdText, OleDbConnection connection)
100 Connection = connection;
103 public OleDbCommand (string cmdText,
104 OleDbConnection connection,
105 OleDbTransaction transaction) : this (cmdText, connection)
107 this.transaction = transaction;
110 #endregion // Constructors
114 [DataCategory ("Data")]
116 [DataSysDescriptionAttribute ("Command text to execute.")]
117 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
118 [RefreshPropertiesAttribute (RefreshProperties.All)]
119 public string CommandText
129 [DataSysDescriptionAttribute ("Time to wait for command to execute.")]
131 public int CommandTimeout {
140 [DataCategory ("Data")]
141 [DefaultValue ("Text")]
142 [DataSysDescriptionAttribute ("How to interpret the CommandText.")]
143 [RefreshPropertiesAttribute (RefreshProperties.All)]
144 public CommandType CommandType {
153 [DataCategory ("Behavior")]
154 [DataSysDescriptionAttribute ("Connection used by the command.")]
155 [DefaultValue (null)]
156 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
157 public OleDbConnection Connection {
166 [BrowsableAttribute (false)]
167 [DesignOnlyAttribute (true)]
168 [DefaultValue (true)]
169 public bool DesignTimeVisible {
171 return designTimeVisible;
174 designTimeVisible = value;
178 [DataCategory ("Data")]
179 [DataSysDescriptionAttribute ("The parameters collection.")]
180 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
181 public OleDbParameterCollection Parameters {
187 [BrowsableAttribute (false)]
188 [DataSysDescriptionAttribute ("The transaction used by the command.")]
189 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
190 public OleDbTransaction Transaction {
199 [DataCategory ("Behavior")]
200 [DefaultValue (UpdateRowSource.Both)]
201 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
202 public UpdateRowSource UpdatedRowSource {
205 throw new NotImplementedException ();
209 throw new NotImplementedException ();
213 IDbConnection IDbCommand.Connection {
218 Connection = (OleDbConnection) value;
222 IDataParameterCollection IDbCommand.Parameters {
228 IDbTransaction IDbCommand.Transaction {
233 Transaction = (OleDbTransaction) value;
237 #endregion // Properties
242 public void Cancel ()
244 throw new NotImplementedException ();
247 public OleDbParameter CreateParameter ()
249 return new OleDbParameter ();
252 IDbDataParameter IDbCommand.CreateParameter ()
254 return CreateParameter ();
258 protected override void Dispose (bool disposing)
260 throw new NotImplementedException ();
263 private void SetupGdaCommand ()
267 switch (commandType) {
268 case CommandType.TableDirect :
269 type = GdaCommandType.Table;
271 case CommandType.StoredProcedure :
272 type = GdaCommandType.Procedure;
274 case CommandType.Text :
276 type = GdaCommandType.Sql;
280 if (gdaCommand != IntPtr.Zero) {
281 libgda.gda_command_set_text (gdaCommand, commandText);
282 libgda.gda_command_set_command_type (gdaCommand, type);
284 gdaCommand = libgda.gda_command_new (commandText, type, 0);
287 //libgda.gda_command_set_transaction
290 public int ExecuteNonQuery ()
292 if (connection == null)
293 throw new InvalidOperationException ("connection == null");
294 if (connection.State == ConnectionState.Closed)
295 throw new InvalidOperationException ("State == Closed");
296 // FIXME: a third check is mentioned in .NET docs
298 IntPtr gdaConnection = connection.GdaConnection;
299 IntPtr gdaParameterList = parameters.GdaParameterList;
302 return libgda.gda_connection_execute_non_query (gdaConnection,
307 public OleDbDataReader ExecuteReader ()
309 return ExecuteReader (CommandBehavior.Default);
312 IDataReader IDbCommand.ExecuteReader ()
314 return ExecuteReader ();
317 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
319 ArrayList results = new ArrayList ();
323 if (connection.State != ConnectionState.Open)
324 throw new InvalidOperationException ("State != Open");
326 this.behavior = behavior;
328 IntPtr gdaConnection = connection.GdaConnection;
329 IntPtr gdaParameterList = parameters.GdaParameterList;
331 /* execute the command */
333 rs_list = libgda.gda_connection_execute_command (
337 if (rs_list != IntPtr.Zero) {
338 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
340 while (glist_node != null) {
341 results.Add (glist_node.data);
342 if (glist_node.next == IntPtr.Zero)
345 glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
348 dataReader = new OleDbDataReader (this, results);
349 dataReader.NextResult ();
355 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
357 return ExecuteReader (behavior);
360 public object ExecuteScalar ()
363 OleDbDataReader reader = ExecuteReader ();
364 if (reader == null) {
367 if (!reader.Read ()) {
371 object o = reader.GetValue (0);
377 object ICloneable.Clone ()
379 throw new NotImplementedException ();
383 public void Prepare ()
385 throw new NotImplementedException ();
388 public void ResetCommandTimeout ()
395 protected override DbParameter CreateDbParameter ()
397 throw new NotImplementedException ();
401 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
403 throw new NotImplementedException ();
407 protected override DbConnection DbConnection {
408 get { throw new NotImplementedException (); }
409 set { throw new NotImplementedException (); }
413 protected override DbParameterCollection DbParameterCollection {
414 get { throw new NotImplementedException (); }
418 protected override DbTransaction DbTransaction {
419 get { throw new NotImplementedException (); }
420 set { throw new NotImplementedException (); }