2002-07-09 Tim Coleman <tim@timcoleman.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                 ArrayList gdaCommands;
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                         gdaCommands = new ArrayList ();
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                                 string[] queries = value.Split (new Char[] {';'});
84                                 gdaCommands.Clear ();
85
86                                 foreach (string query in queries) 
87                                         gdaCommands.Add (libgda.gda_command_new (query, 0, 0));
88
89                                 commandText = value; 
90                         }
91                 }
92
93                 public int CommandTimeout {
94                         get { return timeout; }
95                         set { timeout = value; }
96                 }
97
98                 public CommandType CommandType { 
99                         get { return commandType; }
100                         set { commandType = value; }
101                 }
102
103                 public OleDbConnection Connection { 
104                         get { return connection; }
105                         set { connection = value; }
106                 }
107
108                 public bool DesignTimeVisible { 
109                         get { return designTimeVisible; }
110                         set { designTimeVisible = value; }
111                 }
112
113                 public OleDbParameterCollection Parameters {
114                         get { return parameters; }
115                         set { parameters = value; }
116                 }
117
118                 public OleDbTransaction Transaction {
119                         get { return transaction; }
120                         set { transaction = value; }
121                 }
122
123                 public UpdateRowSource UpdatedRowSource { 
124                         [MonoTODO]
125                         get { throw new NotImplementedException (); }
126                         [MonoTODO]
127                         set { throw new NotImplementedException (); }
128                 }
129
130                 IDbConnection IDbCommand.Connection {
131                         get { return Connection; }
132                         set { Connection = (OleDbConnection) value; }
133                 }
134
135                 IDataParameterCollection IDbCommand.Parameters  {
136                         get { return Parameters; }
137                 }
138
139                 IDbTransaction IDbCommand.Transaction  {
140                         get { return Transaction; }
141                         set { Transaction = (OleDbTransaction) value; }
142                 }
143
144                 internal ArrayList GdaResults {
145                         get { return gdaResults; }
146                 }
147
148                 #endregion // Properties
149
150                 #region Methods
151
152                 [MonoTODO]
153                 public void Cancel () 
154                 {
155                         throw new NotImplementedException ();
156                 }
157
158                 public OleDbParameter CreateParameter ()
159                 {
160                         return new OleDbParameter ();
161                 }
162
163                 [MonoTODO]
164                 protected override void Dispose (bool disposing)
165                 {
166                         throw new NotImplementedException ();
167                 }
168
169                 public int ExecuteNonQuery ()
170                 {
171                         if (connection == null)
172                                 throw new InvalidOperationException ();
173                         if (connection.State == ConnectionState.Closed)
174                                 throw new InvalidOperationException ();
175                         // FIXME: a third check is mentioned in .NET docs
176
177                         IntPtr gdaConnection = connection.GdaConnection;
178                         IntPtr gdaParameterList = parameters.GdaParameterList;
179
180                         return libgda.gda_connection_execute_non_query (gdaConnection, (IntPtr) gdaCommands[0], gdaParameterList);
181                 }
182
183                 public OleDbDataReader ExecuteReader ()
184                 {
185                         return ExecuteReader (CommandBehavior.Default);
186                 }
187
188                 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
189                 {
190                         if (connection.State != ConnectionState.Open)
191                                 throw new InvalidOperationException ();
192
193                         this.behavior = behavior;
194
195                         IntPtr gdaConnection = connection.GdaConnection;
196                         IntPtr gdaParameterList = parameters.GdaParameterList;
197
198                         foreach (IntPtr gdaCommand in gdaCommands) 
199                                 GdaResults.Add (libgda.gda_connection_execute_single_command (gdaConnection, gdaCommand, gdaParameterList));
200
201                         dataReader = new OleDbDataReader (this);
202
203                         dataReader.NextResult ();
204
205                         return dataReader;
206                 }
207
208                 [MonoTODO]
209                 public object ExecuteScalar ()
210                 {
211                         throw new NotImplementedException ();   
212                 }
213
214                 [MonoTODO]
215                 object ICloneable.Clone ()
216                 {
217                         throw new NotImplementedException ();   
218                 }
219
220                 [MonoTODO]
221                 IDbDataParameter IDbCommand.CreateParameter ()
222                 {
223                         throw new NotImplementedException ();   
224                 }
225
226                 [MonoTODO]
227                 IDataReader IDbCommand.ExecuteReader ()
228                 {
229                         throw new NotImplementedException ();   
230                 }
231
232                 [MonoTODO]
233                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
234                 {
235                         throw new NotImplementedException ();   
236                 }
237
238                 [MonoTODO]
239                 public void Prepare ()
240                 {
241                         throw new NotImplementedException ();   
242                 }
243
244                 public void ResetCommandTimeout ()
245                 {
246                         timeout = 30;
247                 }
248
249                 #endregion
250
251                 #region Internal Methods
252
253                 // only meant to be used between OleDbConnectioin,
254                 // OleDbCommand, and OleDbDataReader
255                 internal void OpenReader (OleDbDataReader reader) 
256                 {
257                         connection.OpenReader (reader);
258                 }
259
260                 #endregion
261
262         }
263 }