Add license and copyright to all source files in System.Data
[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 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System.ComponentModel;
36 using System.Data;
37 using System.Data.Common;
38 using System.Collections;
39 using System.Runtime.InteropServices;
40
41 namespace System.Data.OleDb
42 {
43         /// <summary>
44         /// Represents an SQL statement or stored procedure to execute against a data source.
45         /// </summary>
46         [DesignerAttribute ("Microsoft.VSDesigner.Data.VS.OleDbCommandDesigner, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.IDesigner")]
47         [ToolboxItemAttribute ("System.Drawing.Design.ToolboxItem, "+ Consts.AssemblySystem_Drawing)]
48         public sealed class OleDbCommand : Component, ICloneable, IDbCommand
49         {
50                 #region Fields
51
52                 string commandText;
53                 int timeout;
54                 CommandType commandType;
55                 OleDbConnection connection;
56                 OleDbParameterCollection parameters;
57                 OleDbTransaction transaction;
58                 bool designTimeVisible;
59                 OleDbDataReader dataReader;
60                 CommandBehavior behavior;
61                 IntPtr gdaCommand;
62
63                 #endregion // Fields
64
65                 #region Constructors
66
67                 public OleDbCommand ()
68                 {
69                         commandText = String.Empty;
70                         timeout = 30; // default timeout per .NET
71                         commandType = CommandType.Text;
72                         connection = null;
73                         parameters = new OleDbParameterCollection ();
74                         transaction = null;
75                         designTimeVisible = false;
76                         dataReader = null;
77                         behavior = CommandBehavior.Default;
78                         gdaCommand = IntPtr.Zero;
79                 }
80
81                 public OleDbCommand (string cmdText) : this ()
82                 {
83                         CommandText = cmdText;
84                 }
85
86                 public OleDbCommand (string cmdText, OleDbConnection connection)
87                         : this (cmdText)
88                 {
89                         Connection = connection;
90                 }
91
92                 public OleDbCommand (string cmdText,
93                                      OleDbConnection connection,
94                                      OleDbTransaction transaction) : this (cmdText, connection)
95                 {
96                         this.transaction = transaction;
97                 }
98
99                 #endregion // Constructors
100
101                 #region Properties
102         
103                 [DataCategory ("Data")]
104                 [DefaultValue ("")]
105                 [DataSysDescriptionAttribute ("Command text to execute")]
106                 [EditorAttribute ("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
107                 [RefreshPropertiesAttribute (RefreshProperties.All)]
108                 public string CommandText 
109                 {
110                         get {
111                                 return commandText;
112                         }
113                         set { 
114                                 commandText = value;
115                         }
116                 }
117
118                 [DataSysDescriptionAttribute ("Time to wait for command to execute")]
119                 [DefaultValue (30)]
120                 public int CommandTimeout {
121                         get {
122                                 return timeout;
123                         }
124                         set {
125                                 timeout = value;
126                         }
127                 }
128
129                 [DataCategory ("Data")]
130                 [DefaultValue ("Text")]
131                 [DataSysDescriptionAttribute ("How to interpret the CommandText")]
132                 [RefreshPropertiesAttribute (RefreshProperties.All)]
133                 public CommandType CommandType { 
134                         get {
135                                 return commandType;
136                         }
137                         set {
138                                 commandType = value;
139                         }
140                 }
141
142                 [DataCategory ("Behavior")]
143                 [DataSysDescriptionAttribute ("Connection used by the command")]
144                 [DefaultValue (null)]
145                 [EditorAttribute ("Microsoft.VSDesigner.Data.Design.DbConnectionEditor, "+ Consts.AssemblyMicrosoft_VSDesigner, "System.Drawing.Design.UITypeEditor, "+ Consts.AssemblySystem_Drawing )]
146                 public OleDbConnection Connection { 
147                         get {
148                                 return connection;
149                         }
150                         set {
151                                 connection = value;
152                         }
153                 }
154                 
155                 [BrowsableAttribute (false)]
156                 [DesignOnlyAttribute (true)]
157                 [DefaultValue (true)]
158                 public bool DesignTimeVisible { 
159                         get {
160                                 return designTimeVisible;
161                         }
162                         set {
163                                 designTimeVisible = value;
164                         }
165                 }
166
167                 [DataCategory ("Data")]
168                 [DataSysDescriptionAttribute ("The parameters collection")]
169                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Content)]
170                 public OleDbParameterCollection Parameters {
171                         get {
172                                 return parameters;
173                         }
174                 }
175                 
176                 [BrowsableAttribute (false)]
177                 [DataSysDescriptionAttribute ("The transaction used by the command")]
178                 [DesignerSerializationVisibilityAttribute (DesignerSerializationVisibility.Hidden)]
179                 public OleDbTransaction Transaction {
180                         get {
181                                 return transaction;
182                         }
183                         set {
184                                 transaction = value;
185                         }
186                 }
187
188                 [DataCategory ("Behavior")]
189                 [DefaultValue (UpdateRowSource.Both)]
190                 [DataSysDescriptionAttribute ("When used by a DataAdapter.Update, how command results are applied to the current DataRow")]
191                 public UpdateRowSource UpdatedRowSource { 
192                         [MonoTODO]
193                         get {
194                                 throw new NotImplementedException ();
195                         }
196                         [MonoTODO]
197                         set {
198                                 throw new NotImplementedException ();
199                         }
200                 }
201
202                 IDbConnection IDbCommand.Connection {
203                         get {
204                                 return Connection;
205                         }
206                         set {
207                                 Connection = (OleDbConnection) value;
208                         }
209                 }
210
211                 IDataParameterCollection IDbCommand.Parameters  {
212                         get {
213                                 return Parameters;
214                         }
215                 }
216
217                 IDbTransaction IDbCommand.Transaction  {
218                         get {
219                                 return Transaction;
220                         }
221                         set {
222                                 Transaction = (OleDbTransaction) value;
223                         }
224                 }
225
226                 #endregion // Properties
227
228                 #region Methods
229
230                 [MonoTODO]
231                 public void Cancel () 
232                 {
233                         throw new NotImplementedException ();
234                 }
235
236                 public OleDbParameter CreateParameter ()
237                 {
238                         return new OleDbParameter ();
239                 }
240
241                 IDbDataParameter IDbCommand.CreateParameter ()
242                 {
243                         return CreateParameter ();
244                 }
245                 
246                 [MonoTODO]
247                 protected override void Dispose (bool disposing)
248                 {
249                         throw new NotImplementedException ();
250                 }
251
252                 private void SetupGdaCommand ()
253                 {
254                         GdaCommandType type;
255                         
256                         switch (commandType) {
257                         case CommandType.TableDirect :
258                                 type = GdaCommandType.Table;
259                                 break;
260                         case CommandType.StoredProcedure :
261                                 type = GdaCommandType.Procedure;
262                                 break;
263                         case CommandType.Text :
264                         default :
265                                 type = GdaCommandType.Sql;
266                                 break;
267                         }
268                         
269                         if (gdaCommand != IntPtr.Zero) {
270                                 libgda.gda_command_set_text (gdaCommand, commandText);
271                                 libgda.gda_command_set_command_type (gdaCommand, type);
272                         } else {
273                                 gdaCommand = libgda.gda_command_new (commandText, type, 0);
274                         }
275
276                         //libgda.gda_command_set_transaction 
277                 }
278                 
279                 public int ExecuteNonQuery ()
280                 {
281                         if (connection == null)
282                                 throw new InvalidOperationException ("connection == null");
283                         if (connection.State == ConnectionState.Closed)
284                                 throw new InvalidOperationException ("State == Closed");
285                         // FIXME: a third check is mentioned in .NET docs
286
287                         IntPtr gdaConnection = connection.GdaConnection;
288                         IntPtr gdaParameterList = parameters.GdaParameterList;
289
290                         SetupGdaCommand ();
291                         return libgda.gda_connection_execute_non_query (gdaConnection,
292                                                                         (IntPtr) gdaCommand,
293                                                                         gdaParameterList);
294                 }
295
296                 public OleDbDataReader ExecuteReader ()
297                 {
298                         return ExecuteReader (CommandBehavior.Default);
299                 }
300
301                 IDataReader IDbCommand.ExecuteReader ()
302                 {
303                         return ExecuteReader ();
304                 }
305
306                 public OleDbDataReader ExecuteReader (CommandBehavior behavior)
307                 {
308                         ArrayList results = new ArrayList ();
309                         IntPtr rs_list;
310                         GdaList glist_node;
311
312                         if (connection.State != ConnectionState.Open)
313                                 throw new InvalidOperationException ("State != Open");
314
315                         this.behavior = behavior;
316
317                         IntPtr gdaConnection = connection.GdaConnection;
318                         IntPtr gdaParameterList = parameters.GdaParameterList;
319
320                         /* execute the command */
321                         SetupGdaCommand ();
322                         rs_list = libgda.gda_connection_execute_command (
323                                 gdaConnection,
324                                 gdaCommand,
325                                 gdaParameterList);
326                         if (rs_list != IntPtr.Zero) {
327                                 glist_node = (GdaList) Marshal.PtrToStructure (rs_list, typeof (GdaList));
328
329                                 while (glist_node != null) {
330                                         results.Add (glist_node.data);
331                                         if (glist_node.next == IntPtr.Zero)
332                                                 break;
333
334                                         glist_node = (GdaList) Marshal.PtrToStructure (glist_node.next,
335                                                                                        typeof (GdaList));
336                                 }
337                                 dataReader = new OleDbDataReader (this, results);
338                                 dataReader.NextResult ();
339                         }
340
341                         return dataReader;
342                 }
343
344                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
345                 {
346                         return ExecuteReader (behavior);
347                 }
348                 
349                 public object ExecuteScalar ()
350                 {
351                         SetupGdaCommand ();
352                         OleDbDataReader reader = ExecuteReader ();
353                         if (reader == null) {
354                                 return null;
355                         }
356                         if (!reader.Read ()) {
357                                 reader.Close ();
358                                 return null;
359                         }
360                         object o = reader.GetValue (0);
361                         reader.Close ();
362                         return o;
363                 }
364
365                 [MonoTODO]
366                 object ICloneable.Clone ()
367                 {
368                         throw new NotImplementedException ();   
369                 }
370
371                 [MonoTODO]
372                 public void Prepare ()
373                 {
374                         throw new NotImplementedException ();   
375                 }
376
377                 public void ResetCommandTimeout ()
378                 {
379                         timeout = 30;
380                 }
381
382                 #endregion
383         }
384 }