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