2002-08-15 Rodrigo Moya <rodrigo@ximian.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 ();
234                         if (connection.State == ConnectionState.Closed)
235                                 throw new InvalidOperationException ();
236                         // FIXME: a third check is mentioned in .NET docs
237
238                         IntPtr gdaConnection = connection.GdaConnection;
239                         IntPtr gdaParameterList = parameters.GdaParameterList;
240
241                         SetupGdaCommand ();
242                         return libgda.gda_connection_execute_non_query (gdaConnection,
243                                                                         (IntPtr) gdaCommand,
244                                                                         gdaParameterList);
245                 }
246
247                 public OleDbDataReader ExecuteReader ()
248                 {
249                         return ExecuteReader (CommandBehavior.Default);
250                 }
251
252                 IDataReader IDbCommand.ExecuteReader ()
253                 {
254                         return ExecuteReader ();
255                 }
256
257                 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
258                 {
259                         ArrayList results = new ArrayList ();
260                         IntPtr rs_list;
261                         GdaList glist_node;
262
263                         if (connection.State != ConnectionState.Open)
264                                 throw new InvalidOperationException ();
265
266                         this.behavior = behavior;
267
268                         IntPtr gdaConnection = connection.GdaConnection;
269                         IntPtr gdaParameterList = parameters.GdaParameterList;
270
271                         /* execute the command */
272                         SetupGdaCommand ();
273                         rs_list = libgda.gda_connection_execute_command (
274                                 gdaConnection,
275                                 gdaCommand,
276                                 gdaParameterList);
277                         if (rs_list != IntPtr.Zero) {
278                                 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
279
280                                 while (glist_node != null) {
281                                         results.Add (glist_node.data);
282                                         if (glist_node.next == IntPtr.Zero)
283                                                 break;
284
285                                         glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
286                                                                                        typeof (GdaList));
287                                 }
288                                 dataReader = new OleDbDataReader (this, results);
289                                 dataReader.NextResult ();
290                         }
291
292                         return dataReader;
293                 }
294
295                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
296                 {
297                         return ExecuteReader (behavior);
298                 }
299                 
300                 public object ExecuteScalar ()
301                 {
302                         SetupGdaCommand ();
303                         OleDbDataReader reader = ExecuteReader ();
304                         return reader.GetValue (0);
305                 }
306
307                 [MonoTODO]
308                 object ICloneable.Clone ()
309                 {
310                         throw new NotImplementedException ();   
311                 }
312
313                 [MonoTODO]
314                 public void Prepare ()
315                 {
316                         throw new NotImplementedException ();   
317                 }
318
319                 public void ResetCommandTimeout ()
320                 {
321                         timeout = 30;
322                 }
323
324                 #endregion
325         }
326 }