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