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