2002-07-28 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
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                 ArrayList gdaResults;
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                         gdaResults = new ArrayList ();
55                 }
56
57                 public OleDbCommand (string cmdText)
58                         : 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, OleDbConnection connection, OleDbTransaction transaction)
70                         : this (cmdText, connection)
71                 {
72                         this.transaction = transaction;
73                 }
74
75                 #endregion // Constructors
76
77                 #region Properties
78         
79                 public string CommandText 
80                 {
81                         get { return commandText; }
82                         set { 
83                                 commandText = value;
84                         }
85                 }
86
87                 public int CommandTimeout {
88                         get { return timeout; }
89                         set { timeout = value; }
90                 }
91
92                 public CommandType CommandType { 
93                         get { return commandType; }
94                         set { commandType = value; }
95                 }
96
97                 public OleDbConnection Connection { 
98                         get { return connection; }
99                         set { connection = value; }
100                 }
101
102                 public bool DesignTimeVisible { 
103                         get { return designTimeVisible; }
104                         set { designTimeVisible = value; }
105                 }
106
107                 public OleDbParameterCollection Parameters {
108                         get { return parameters; }
109                         set { parameters = value; }
110                 }
111
112                 public OleDbTransaction Transaction {
113                         get { return transaction; }
114                         set { transaction = value; }
115                 }
116
117                 public UpdateRowSource UpdatedRowSource { 
118                         [MonoTODO]
119                         get { throw new NotImplementedException (); }
120                         [MonoTODO]
121                         set { throw new NotImplementedException (); }
122                 }
123
124                 IDbConnection IDbCommand.Connection {
125                         get { return Connection; }
126                         set { Connection = (OleDbConnection) value; }
127                 }
128
129                 IDataParameterCollection IDbCommand.Parameters  {
130                         get { return Parameters; }
131                 }
132
133                 IDbTransaction IDbCommand.Transaction  {
134                         get { return Transaction; }
135                         set { Transaction = (OleDbTransaction) value; }
136                 }
137
138                 internal ArrayList GdaResults {
139                         get { return gdaResults; }
140                 }
141
142                 #endregion // Properties
143
144                 #region Methods
145
146                 [MonoTODO]
147                 public void Cancel () 
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 public OleDbParameter CreateParameter ()
153                 {
154                         return new OleDbParameter ();
155                 }
156
157                 IDbDataParameter IDbCommand.CreateParameter ()
158                 {
159                         return CreateParameter ();
160                 }
161                 
162                 [MonoTODO]
163                 protected override void Dispose (bool disposing)
164                 {
165                         throw new NotImplementedException ();
166                 }
167
168                 private void SetupGdaCommand ()
169                 {
170                         GdaCommandType type;
171                         
172                         switch (commandType) {
173                         case CommandType.TableDirect :
174                                 type = GdaCommandType.Table;
175                                 break;
176                         case CommandType.StoredProcedure :
177                                 type = GdaCommandType.Procedure;
178                                 break;
179                         case CommandType.Text :
180                         default :
181                                 type = GdaCommandType.Sql;
182                                 break;
183                         }
184                         
185                         if (gdaCommand != IntPtr.Zero) {
186                                 libgda.gda_command_set_text (gdaCommand, commandText);
187                                 libgda.gda_command_set_command_type (gdaCommand, type);
188                         } else {
189                                 gdaCommand = libgda.gda_command_new (commandText, type, 0);
190                         }
191
192                         //libgda.gda_command_set_transaction 
193                 }
194                 
195                 public int ExecuteNonQuery ()
196                 {
197                         if (connection == null)
198                                 throw new InvalidOperationException ();
199                         if (connection.State == ConnectionState.Closed)
200                                 throw new InvalidOperationException ();
201                         // FIXME: a third check is mentioned in .NET docs
202
203                         IntPtr gdaConnection = connection.GdaConnection;
204                         IntPtr gdaParameterList = parameters.GdaParameterList;
205
206                         SetupGdaCommand ();
207                         return libgda.gda_connection_execute_non_query (gdaConnection, (IntPtr) gdaCommand, gdaParameterList);
208                 }
209
210                 public OleDbDataReader ExecuteReader ()
211                 {
212                         return ExecuteReader (CommandBehavior.Default);
213                 }
214
215                 IDataReader IDbCommand.ExecuteReader ()
216                 {
217                         return ExecuteReader ();
218                 }
219
220                 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
221                 {
222                         if (connection.State != ConnectionState.Open)
223                                 throw new InvalidOperationException ();
224
225                         this.behavior = behavior;
226
227                         IntPtr gdaConnection = connection.GdaConnection;
228                         IntPtr gdaParameterList = parameters.GdaParameterList;
229
230                         /* FIXME: split all returned resultsets into
231                            our internal GdaResults */
232                         GdaResults.Add (libgda.gda_connection_execute_command (
233                                                 gdaConnection,
234                                                 gdaCommand,
235                                                 gdaParameterList));
236
237                         dataReader = new OleDbDataReader (this);
238                         dataReader.NextResult ();
239
240                         return dataReader;
241                 }
242
243                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
244                 {
245                         return ExecuteReader (behavior);
246                 }
247                 
248                 [MonoTODO]
249                 public object ExecuteScalar ()
250                 {
251                         throw new NotImplementedException ();   
252                 }
253
254                 [MonoTODO]
255                 object ICloneable.Clone ()
256                 {
257                         throw new NotImplementedException ();   
258                 }
259
260                 [MonoTODO]
261                 public void Prepare ()
262                 {
263                         throw new NotImplementedException ();   
264                 }
265
266                 public void ResetCommandTimeout ()
267                 {
268                         timeout = 30;
269                 }
270
271                 #endregion
272
273                 #region Internal Methods
274
275                 // only meant to be used between OleDbConnection,
276                 // OleDbCommand, and OleDbDataReader
277                 internal void OpenReader (OleDbDataReader reader) 
278                 {
279                         connection.OpenReader (reader);
280                 }
281
282                 #endregion
283
284         }
285 }