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