(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Data / System.Data.OleDb / OleDbCommand.cs
blob72a7477e63c58991f0d15a4dcbe4ede7070116cf
1 //
2 // System.Data.OleDb.OleDbCommand
3 //
4 // Authors:
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
7 //
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:
22 //
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 //
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;
36 using System.Data;
37 using System.Data.Common;
38 using System.Collections;
39 using System.Runtime.InteropServices;
41 namespace System.Data.OleDb
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 : Component, ICloneable, IDbCommand
50 #region Fields
52 string commandText;
53 int timeout;
54 CommandType commandType;
55 OleDbConnection connection;
56 OleDbParameterCollection parameters;
57 OleDbTransaction transaction;
58 bool designTimeVisible;
59 OleDbDataReader dataReader;
60 CommandBehavior behavior;
61 IntPtr gdaCommand;
63 #endregion // Fields
65 #region Constructors
67 public OleDbCommand ()
69 commandText = String.Empty;
70 timeout = 30; // default timeout per .NET
71 commandType = CommandType.Text;
72 connection = null;
73 parameters = new OleDbParameterCollection ();
74 transaction = null;
75 designTimeVisible = false;
76 dataReader = null;
77 behavior = CommandBehavior.Default;
78 gdaCommand = IntPtr.Zero;
81 public OleDbCommand (string cmdText) : this ()
83 CommandText = cmdText;
86 public OleDbCommand (string cmdText, OleDbConnection connection)
87 : this (cmdText)
89 Connection = connection;
92 public OleDbCommand (string cmdText,
93 OleDbConnection connection,
94 OleDbTransaction transaction) : this (cmdText, connection)
96 this.transaction = transaction;
99 #endregion // Constructors
101 #region Properties
103 [DataCategory ("Data")]
104 [DefaultValue ("")]
105 [DataSysDescriptionAttribute ("Command text to execute")]
106 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
107 [RefreshPropertiesAttribute (RefreshProperties.All)]
108 public string CommandText
110 get {
111 return commandText;
113 set {
114 commandText = value;
118 [DataSysDescriptionAttribute ("Time to wait for command to execute")]
119 [DefaultValue (30)]
120 public int CommandTimeout {
121 get {
122 return timeout;
124 set {
125 timeout = value;
129 [DataCategory ("Data")]
130 [DefaultValue ("Text")]
131 [DataSysDescriptionAttribute ("How to interpret the CommandText")]
132 [RefreshPropertiesAttribute (RefreshProperties.All)]
133 public CommandType CommandType {
134 get {
135 return commandType;
137 set {
138 commandType = value;
142 [DataCategory ("Behavior")]
143 [DataSysDescriptionAttribute ("Connection used by the command")]
144 [DefaultValue (null)]
145 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
146 public OleDbConnection Connection {
147 get {
148 return connection;
150 set {
151 connection = value;
155 [BrowsableAttribute (false)]
156 [DesignOnlyAttribute (true)]
157 [DefaultValue (true)]
158 public bool DesignTimeVisible {
159 get {
160 return designTimeVisible;
162 set {
163 designTimeVisible = value;
167 [DataCategory ("Data")]
168 [DataSysDescriptionAttribute ("The parameters collection")]
169 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
170 public OleDbParameterCollection Parameters {
171 get {
172 return parameters;
176 [BrowsableAttribute (false)]
177 [DataSysDescriptionAttribute ("The transaction used by the command")]
178 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
179 public OleDbTransaction Transaction {
180 get {
181 return transaction;
183 set {
184 transaction = value;
188 [DataCategory ("Behavior")]
189 [DefaultValue (UpdateRowSource.Both)]
190 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
191 public UpdateRowSource UpdatedRowSource {
192 [MonoTODO]
193 get {
194 throw new NotImplementedException ();
196 [MonoTODO]
197 set {
198 throw new NotImplementedException ();
202 IDbConnection IDbCommand.Connection {
203 get {
204 return Connection;
206 set {
207 Connection = (OleDbConnection) value;
211 IDataParameterCollection IDbCommand.Parameters {
212 get {
213 return Parameters;
217 IDbTransaction IDbCommand.Transaction {
218 get {
219 return Transaction;
221 set {
222 Transaction = (OleDbTransaction) value;
226 #endregion // Properties
228 #region Methods
230 [MonoTODO]
231 public void Cancel ()
233 throw new NotImplementedException ();
236 public OleDbParameter CreateParameter ()
238 return new OleDbParameter ();
241 IDbDataParameter IDbCommand.CreateParameter ()
243 return CreateParameter ();
246 [MonoTODO]
247 protected override void Dispose (bool disposing)
249 throw new NotImplementedException ();
252 private void SetupGdaCommand ()
254 GdaCommandType type;
256 switch (commandType) {
257 case CommandType.TableDirect :
258 type = GdaCommandType.Table;
259 break;
260 case CommandType.StoredProcedure :
261 type = GdaCommandType.Procedure;
262 break;
263 case CommandType.Text :
264 default :
265 type = GdaCommandType.Sql;
266 break;
269 if (gdaCommand != IntPtr.Zero) {
270 libgda.gda_command_set_text (gdaCommand, commandText);
271 libgda.gda_command_set_command_type (gdaCommand, type);
272 } else {
273 gdaCommand = libgda.gda_command_new (commandText, type, 0);
276 //libgda.gda_command_set_transaction
279 public int ExecuteNonQuery ()
281 if (connection == null)
282 throw new InvalidOperationException ("connection == null");
283 if (connection.State == ConnectionState.Closed)
284 throw new InvalidOperationException ("State == Closed");
285 // FIXME: a third check is mentioned in .NET docs
287 IntPtr gdaConnection = connection.GdaConnection;
288 IntPtr gdaParameterList = parameters.GdaParameterList;
290 SetupGdaCommand ();
291 return libgda.gda_connection_execute_non_query (gdaConnection,
292 (IntPtr) gdaCommand,
293 gdaParameterList);
296 public OleDbDataReader ExecuteReader ()
298 return ExecuteReader (CommandBehavior.Default);
301 IDataReader IDbCommand.ExecuteReader ()
303 return ExecuteReader ();
306 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
308 ArrayList results = new ArrayList ();
309 IntPtr rs_list;
310 GdaList glist_node;
312 if (connection.State != ConnectionState.Open)
313 throw new InvalidOperationException ("State != Open");
315 this.behavior = behavior;
317 IntPtr gdaConnection = connection.GdaConnection;
318 IntPtr gdaParameterList = parameters.GdaParameterList;
320 /* execute the command */
321 SetupGdaCommand ();
322 rs_list = libgda.gda_connection_execute_command (
323 gdaConnection,
324 gdaCommand,
325 gdaParameterList);
326 if (rs_list != IntPtr.Zero) {
327 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
329 while (glist_node != null) {
330 results.Add (glist_node.data);
331 if (glist_node.next == IntPtr.Zero)
332 break;
334 glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
335 typeof (GdaList));
337 dataReader = new OleDbDataReader (this, results);
338 dataReader.NextResult ();
341 return dataReader;
344 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
346 return ExecuteReader (behavior);
349 public object ExecuteScalar ()
351 SetupGdaCommand ();
352 OleDbDataReader reader = ExecuteReader ();
353 if (reader == null) {
354 return null;
356 if (!reader.Read ()) {
357 reader.Close ();
358 return null;
360 object o = reader.GetValue (0);
361 reader.Close ();
362 return o;
365 [MonoTODO]
366 object ICloneable.Clone ()
368 throw new NotImplementedException ();
371 [MonoTODO]
372 public void Prepare ()
374 throw new NotImplementedException ();
377 public void ResetCommandTimeout ()
379 timeout = 30;
382 #endregion