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