New test.
[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 = null;
79                         parameters = new OleDbParameterCollection ();
80                         transaction = null;
81                         designTimeVisible = false;
82                         dataReader = null;
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 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 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 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 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                 public 
323 #if NET_2_0
324                 override
325 #endif
326                 int ExecuteNonQuery ()
327                 {
328                         if (connection == null)
329                                 throw new InvalidOperationException ("connection == null");
330                         if (connection.State == ConnectionState.Closed)
331                                 throw new InvalidOperationException ("State == Closed");
332                         // FIXME: a third check is mentioned in .NET docs
333
334                         IntPtr gdaConnection = connection.GdaConnection;
335                         IntPtr gdaParameterList = parameters.GdaParameterList;
336
337                         SetupGdaCommand ();
338                         return libgda.gda_connection_execute_non_query (gdaConnection,
339                                                                         (IntPtr) gdaCommand,
340                                                                         gdaParameterList);
341                 }
342
343                 public OleDbDataReader ExecuteReader ()
344                 {
345                         return ExecuteReader (CommandBehavior.Default);
346                 }
347
348                 IDataReader IDbCommand.ExecuteReader ()
349                 {
350                         return ExecuteReader ();
351                 }
352
353                 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
354                 {
355                         ArrayList results = new ArrayList ();
356                         IntPtr rs_list;
357                         GdaList glist_node;
358
359                         if (connection.State != ConnectionState.Open)
360                                 throw new InvalidOperationException ("State != Open");
361
362                         this.behavior = behavior;
363
364                         IntPtr gdaConnection = connection.GdaConnection;
365                         IntPtr gdaParameterList = parameters.GdaParameterList;
366
367                         /* execute the command */
368                         SetupGdaCommand ();
369                         rs_list = libgda.gda_connection_execute_command (
370                                 gdaConnection,
371                                 gdaCommand,
372                                 gdaParameterList);
373                         if (rs_list != IntPtr.Zero) {
374                                 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
375
376                                 while (glist_node != null) {
377                                         results.Add (glist_node.data);
378                                         if (glist_node.next == IntPtr.Zero)
379                                                 break;
380
381                                         glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
382                                                                                        typeof (GdaList));
383                                 }
384                                 dataReader = new OleDbDataReader (this, results);
385                                 dataReader.NextResult ();
386                         }
387
388                         return dataReader;
389                 }
390
391                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
392                 {
393                         return ExecuteReader (behavior);
394                 }
395                 
396                 public
397 #if NET_2_0
398                 override
399 #endif
400                 object ExecuteScalar ()
401                 {
402                         SetupGdaCommand ();
403                         OleDbDataReader reader = ExecuteReader ();
404                         if (reader == null) {
405                                 return null;
406                         }
407                         if (!reader.Read ()) {
408                                 reader.Close ();
409                                 return null;
410                         }
411                         object o = reader.GetValue (0);
412                         reader.Close ();
413                         return o;
414                 }
415
416                 [MonoTODO]
417                 object ICloneable.Clone ()
418                 {
419                         throw new NotImplementedException ();   
420                 }
421
422                 [MonoTODO]
423                 public 
424 #if NET_2_0
425                 override
426 #endif
427                 void Prepare ()
428                 {
429                         throw new NotImplementedException ();   
430                 }
431
432                 public void ResetCommandTimeout ()
433                 {
434                         timeout = 30;
435                 }
436                 
437 #if NET_2_0
438                 [MonoTODO]
439                 protected override DbParameter CreateDbParameter ()
440                 {
441                         throw new NotImplementedException ();   
442                 }
443                 
444                 [MonoTODO]
445                 protected override DbDataReader ExecuteDbDataReader (CommandBehavior behavior)
446                 {
447                         throw new NotImplementedException ();   
448                 }
449                 
450                 [MonoTODO]
451                 protected override DbConnection DbConnection {
452                         get { throw new NotImplementedException (); }
453                         set { throw new NotImplementedException (); }
454                 }
455                 
456                 [MonoTODO]
457                 protected override DbParameterCollection DbParameterCollection {
458                         get { throw new NotImplementedException (); }
459                 }
460                 
461                 [MonoTODO]
462                 protected override DbTransaction DbTransaction {
463                         get { throw new NotImplementedException (); }
464                         set { throw new NotImplementedException (); }
465                 }
466 #endif
467
468                 #endregion
469         }
470 }