* OleDbDataReader.cs: Removed bogus MonoTODO.
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbCommand.cs
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
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
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.
33 //
34
35 using System.ComponentModel;
36 using System.Data;
37 using System.Data.Common;
38 using System.Collections;
39 using System.Runtime.InteropServices;
40
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 #if NET_2_0
49         [DefaultEvent( "RecordsAffected")]
50 #endif
51         public sealed class OleDbCommand : 
52 #if NET_2_0
53         DbCommand
54 #else
55         Component
56 #endif
57         , ICloneable, IDbCommand
58         {
59                 #region Fields
60
61                 string commandText;
62                 int timeout;
63                 CommandType commandType;
64                 OleDbConnection connection;
65                 OleDbParameterCollection parameters;
66                 OleDbTransaction transaction;
67                 bool designTimeVisible;
68                 OleDbDataReader dataReader;
69                 CommandBehavior behavior;
70                 IntPtr gdaCommand;
71
72                 #endregion // Fields
73
74                 #region Constructors
75
76                 public OleDbCommand ()
77                 {
78                         commandText = String.Empty;
79                         timeout = 30; // default timeout per .NET
80                         commandType = CommandType.Text;
81                         parameters = new OleDbParameterCollection ();
82                         behavior = CommandBehavior.Default;
83                         gdaCommand = IntPtr.Zero;
84                 }
85
86                 public OleDbCommand (string cmdText) : this ()
87                 {
88                         CommandText = cmdText;
89                 }
90
91                 public OleDbCommand (string cmdText, OleDbConnection connection)
92                         : this (cmdText)
93                 {
94                         Connection = connection;
95                 }
96
97                 public OleDbCommand (string cmdText, OleDbConnection connection,
98                         OleDbTransaction transaction) : this (cmdText, connection)
99                 {
100                         this.transaction = transaction;
101                 }
102
103                 #endregion // Constructors
104
105                 #region Properties
106         
107                 [DataCategory ("Data")]
108                 [DefaultValue ("")]
109 #if !NET_2_0
110                 [DataSysDescriptionAttribute ("Command text to execute.")]
111 #endif
112                 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing)]
113                 [RefreshPropertiesAttribute (RefreshProperties.All)]
114                 public 
115 #if NET_2_0
116                 override
117 #endif
118                 string CommandText
119                 {
120                         get {
121                                 return commandText;
122                         }
123                         set {
124                                 commandText = value;
125                         }
126                 }
127
128 #if !NET_2_0
129                 [DataSysDescriptionAttribute ("Time to wait for command to execute.")]
130                 [DefaultValue (30)]
131 #endif
132                 public
133 #if NET_2_0
134                 override
135 #endif
136                 int CommandTimeout {
137                         get {
138                                 return timeout;
139                         }
140                         set {
141                                 timeout = value;
142                         }
143                 }
144
145                 [DataCategory ("Data")]
146                 [DefaultValue ("Text")]
147 #if !NET_2_0
148                 [DataSysDescriptionAttribute ("How to interpret the CommandText.")]
149 #endif
150                 [RefreshPropertiesAttribute (RefreshProperties.All)]
151                 public
152 #if NET_2_0
153                 override
154 #endif
155                 CommandType CommandType {
156                         get {
157                                 return commandType;
158                         }
159                         set {
160                                 commandType = value;
161                         }
162                 }
163
164                 [DataCategory ("Behavior")]
165 #if !NET_2_0
166                 [DataSysDescriptionAttribute ("Connection used by the command.")]
167 #endif
168                 [DefaultValue (null)]
169                 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
170                 public new OleDbConnection Connection {
171                         get {
172                                 return connection;
173                         }
174                         set {
175                                 connection = value;
176                         }
177                 }
178                 
179                 [BrowsableAttribute (false)]
180                 [DesignOnlyAttribute (true)]
181                 [DefaultValue (true)]
182 #if NET_2_0
183                 [EditorBrowsable(EditorBrowsableState.Never)]
184 #endif
185                 public
186 #if NET_2_0
187                 override
188 #endif
189                 bool DesignTimeVisible {
190                         get {
191                                 return designTimeVisible;
192                         }
193                         set {
194                                 designTimeVisible = value;
195                         }
196                 }
197
198                 [DataCategory ("Data")]
199 #if !NET_2_0
200                 [DataSysDescriptionAttribute ("The parameters collection.")]
201 #endif
202                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
203                 public new OleDbParameterCollection Parameters {
204                         get {
205                                 return parameters;
206                         }
207                 }
208
209                 [BrowsableAttribute (false)]
210 #if !NET_2_0
211                 [DataSysDescriptionAttribute ("The transaction used by the command.")]
212 #endif
213                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
214                 public new OleDbTransaction Transaction {
215                         get {
216                                 return transaction;
217                         }
218                         set {
219                                 transaction = value;
220                         }
221                 }
222
223                 [DataCategory ("Behavior")]
224                 [DefaultValue (UpdateRowSource.Both)]
225 #if !NET_2_0
226                 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow.")]
227 #endif
228                 public
229 #if NET_2_0
230                 override
231 #endif
232                 UpdateRowSource UpdatedRowSource {
233                         [MonoTODO]
234                         get {
235                                 throw new NotImplementedException ();
236                         }
237                         [MonoTODO]
238                         set {
239                                 throw new NotImplementedException ();
240                         }
241                 }
242
243                 IDbConnection IDbCommand.Connection {
244                         get {
245                                 return Connection;
246                         }
247                         set {
248                                 Connection = (OleDbConnection) value;
249                         }
250                 }
251
252                 IDataParameterCollection IDbCommand.Parameters {
253                         get {
254                                 return Parameters;
255                         }
256                 }
257
258                 IDbTransaction IDbCommand.Transaction {
259                         get {
260                                 return Transaction;
261                         }
262                         set {
263                                 Transaction = (OleDbTransaction) value;
264                         }
265                 }
266
267                 #endregion // Properties
268
269                 #region Methods
270
271                 [MonoTODO]
272                 public 
273 #if NET_2_0
274                 override 
275 #endif
276                 void Cancel () 
277                 {
278                         throw new NotImplementedException ();
279                 }
280
281                 public new OleDbParameter CreateParameter ()
282                 {
283                         return new OleDbParameter ();
284                 }
285
286 #if !NET_2_0
287                 IDbDataParameter IDbCommand.CreateParameter ()
288                 {
289                         return CreateParameter ();
290                 }
291 #endif
292                 
293                 [MonoTODO]
294                 protected override void Dispose (bool disposing)
295                 {
296                         throw new NotImplementedException ();
297                 }
298
299                 private void SetupGdaCommand ()
300                 {
301                         GdaCommandType type;
302                         
303                         switch (commandType) {
304                         case CommandType.TableDirect :
305                                 type = GdaCommandType.Table;
306                                 break;
307                         case CommandType.StoredProcedure :
308                                 type = GdaCommandType.Procedure;
309                                 break;
310                         case CommandType.Text :
311                         default :
312                                 type = GdaCommandType.Sql;
313                                 break;
314                         }
315                         
316                         if (gdaCommand != IntPtr.Zero) {
317                                 libgda.gda_command_set_text (gdaCommand, commandText);
318                                 libgda.gda_command_set_command_type (gdaCommand, type);
319                         } else {
320                                 gdaCommand = libgda.gda_command_new (commandText, type, 0);
321                         }
322
323                         //libgda.gda_command_set_transaction 
324                 }
325
326                 public 
327 #if NET_2_0
328                 override
329 #endif
330                 int ExecuteNonQuery ()
331                 {
332                         if (connection == null)
333                                 throw new InvalidOperationException ("connection == null");
334                         if (connection.State == ConnectionState.Closed)
335                                 throw new InvalidOperationException ("State == Closed");
336                         // FIXME: a third check is mentioned in .NET docs
337
338                         IntPtr gdaConnection = connection.GdaConnection;
339                         IntPtr gdaParameterList = parameters.GdaParameterList;
340
341                         SetupGdaCommand ();
342                         return libgda.gda_connection_execute_non_query (gdaConnection,
343                                                                         (IntPtr) gdaCommand,
344                                                                         gdaParameterList);
345                 }
346
347                 public new OleDbDataReader ExecuteReader ()
348                 {
349                         return ExecuteReader (behavior);
350                 }
351
352                 IDataReader IDbCommand.ExecuteReader ()
353                 {
354                         return ExecuteReader ();
355                 }
356
357                 public new OleDbDataReader ExecuteReader (CommandBehavior behavior)
358                 {
359                         ArrayList results = new ArrayList ();
360                         IntPtr rs_list;
361                         GdaList glist_node;
362
363                         if (connection.State != ConnectionState.Open)
364                                 throw new InvalidOperationException ("State != Open");
365
366                         this.behavior = behavior;
367
368                         IntPtr gdaConnection = connection.GdaConnection;
369                         IntPtr gdaParameterList = parameters.GdaParameterList;
370
371                         /* execute the command */
372                         SetupGdaCommand ();
373                         rs_list = libgda.gda_connection_execute_command (
374                                 gdaConnection,
375                                 gdaCommand,
376                                 gdaParameterList);
377                         if (rs_list != IntPtr.Zero) {
378                                 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
379
380                                 while (glist_node != null) {
381                                         results.Add (glist_node.data);
382                                         if (glist_node.next == IntPtr.Zero)
383                                                 break;
384
385                                         glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
386                                                 typeof (GdaList));
387                                 }
388                                 dataReader = new OleDbDataReader (this, results);
389                                 dataReader.NextResult ();
390                         }
391
392                         return dataReader;
393                 }
394
395                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
396                 {
397                         return ExecuteReader (behavior);
398                 }
399                 
400                 public
401 #if NET_2_0
402                 override
403 #endif
404                 object ExecuteScalar ()
405                 {
406                         SetupGdaCommand ();
407                         OleDbDataReader reader = ExecuteReader ();
408                         if (reader == null) {
409                                 return null;
410                         }
411                         if (!reader.Read ()) {
412                                 reader.Close ();
413                                 return null;
414                         }
415                         object o = reader.GetValue (0);
416                         reader.Close ();
417                         return o;
418                 }
419
420 #if NET_2_0
421                 [MonoTODO]
422                 public OleDbCommand Clone ()
423                 {
424                         throw new NotImplementedException ();
425                 }
426 #endif
427
428                 [MonoTODO]
429                 object ICloneable.Clone ()
430                 {
431                         throw new NotImplementedException ();
432                 }
433
434                 [MonoTODO]
435                 public 
436 #if NET_2_0
437                 override
438 #endif
439                 void Prepare ()
440                 {
441                         throw new NotImplementedException ();
442                 }
443
444                 public void ResetCommandTimeout ()
445                 {
446                         timeout = 30;
447                 }
448                 
449 #if NET_2_0
450                 [MonoTODO]
451                 protected override DbParameter CreateDbParameter ()
452                 {
453                         throw new NotImplementedException ();
454                 }
455                 
456                 [MonoTODO]
457                 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
458                 {
459                         throw new NotImplementedException ();
460                 }
461                 
462                 [MonoTODO]
463                 protected override DbConnection DbConnection {
464                         get { throw new NotImplementedException (); }
465                         set { throw new NotImplementedException (); }
466                 }
467                 
468                 [MonoTODO]
469                 protected override DbParameterCollection DbParameterCollection {
470                         get { throw new NotImplementedException (); }
471                 }
472                 
473                 [MonoTODO]
474                 protected override DbTransaction DbTransaction {
475                         get { throw new NotImplementedException (); }
476                         set { throw new NotImplementedException (); }
477                 }
478 #endif
479
480                 #endregion
481         }
482 }