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