2002-10-09 Rodrigo Moya <rodrigo@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data.Odbc / OdbcCommand.cs
1 //
2 // System.Data.Odbc.OdbcCommand
3 //
4 // Authors:
5 //   Brian Ritchie (brianlritchie@hotmail.com)
6 //
7 // Copyright (C) Brian Ritchie, 2002
8 //
9
10 using System.ComponentModel;
11 using System.Data;
12 using System.Data.Common;
13 using System.Collections;
14 using System.Runtime.InteropServices;
15
16 namespace System.Data.Odbc
17 {
18         /// <summary>
19         /// Represents an SQL statement or stored procedure to execute against a data source.
20         /// </summary>
21         public sealed class OdbcCommand : Component, ICloneable, IDbCommand
22         {
23                 #region Fields
24
25                 string commandText;
26                 int timeout;
27                 CommandType commandType;
28                 OdbcConnection connection;
29                 OdbcParameterCollection parameters;
30                 //OdbcTransaction transaction;
31                 bool designTimeVisible;
32                 bool prepared=false;
33                 OdbcDataReader dataReader;
34                 CommandBehavior behavior;
35                 public int hstmt;
36
37                 #endregion // Fields
38
39                 #region Constructors
40
41                 public OdbcCommand ()
42                 {
43                         commandText = String.Empty;
44                         timeout = 30; // default timeout
45                         commandType = CommandType.Text;
46                         connection = null;
47                         parameters = new OdbcParameterCollection ();
48                         //transaction = null;
49                         designTimeVisible = false;
50                         dataReader = null;
51                         behavior = CommandBehavior.Default;
52                 }
53
54                 public OdbcCommand (string cmdText) : this ()
55                 {
56                         CommandText = cmdText;
57                 }
58
59                 public OdbcCommand (string cmdText, OdbcConnection connection)
60                         : this (cmdText)
61                 {
62                         Connection = connection;
63                 }
64
65 //              public OdbcCommand (string cmdText,
66 //                                   OdbcConnection connection,
67 //                                   OdbcTransaction transaction) : this (cmdText, connection)
68 //              {
69 //                      this.transaction = transaction;
70 //              }
71
72                 #endregion // Constructors
73
74                 #region Properties
75
76                 public int hStmt
77                 {
78                         get { return hstmt; }
79                 }
80
81                 public string CommandText
82                 {
83                         get {
84                                 return commandText;
85                         }
86                         set {
87                                 prepared=false;
88                                 commandText = value;
89                         }
90                 }
91
92                 public int CommandTimeout {
93                         get {
94                                 return timeout;
95                         }
96                         set {
97                                 timeout = value;
98                         }
99                 }
100
101                 public CommandType CommandType {
102                         get {
103                                 return commandType;
104                         }
105                         set {
106                                 commandType = value;
107                         }
108                 }
109
110                 public OdbcConnection Connection {
111                         get {
112                                 return connection;
113                         }
114                         set {
115                                 connection = value;
116                         }
117                 }
118
119                 public bool DesignTimeVisible {
120                         get {
121                                 return designTimeVisible;
122                         }
123                         set {
124                                 designTimeVisible = value;
125                         }
126                 }
127
128                 public OdbcParameterCollection Parameters {
129                         get {
130                                 return parameters;
131                         }
132                         set {
133                                 parameters = value;
134                         }
135                 }
136
137 //              public OdbcTransaction Transaction {
138 //                      get {
139 //                              return transaction;
140 //                      }
141 //                      set {
142 //                              transaction = value;
143 //                      }
144 //              }
145
146                 public UpdateRowSource UpdatedRowSource {
147                         [MonoTODO]
148                         get {
149                                 throw new NotImplementedException ();
150                         }
151                         [MonoTODO]
152                         set {
153                                 throw new NotImplementedException ();
154                         }
155                 }
156
157                 IDbConnection IDbCommand.Connection {
158                         get {
159                                 return Connection;
160                         }
161                         set {
162                                 Connection = (OdbcConnection) value;
163                         }
164                 }
165
166                 IDataParameterCollection IDbCommand.Parameters  {
167                         get {
168                                 throw new NotImplementedException ();
169                                 //return Parameters;
170                         }
171                 }
172
173                 IDbTransaction IDbCommand.Transaction  {
174                         get {
175                                 throw new NotImplementedException ();
176                                 //return Transaction;
177                         }
178                         set {
179                                 throw new NotImplementedException ();
180                         }
181                 }
182
183                 #endregion // Properties
184
185                 #region Methods
186
187                 [MonoTODO]
188                 public void Cancel ()
189                 {
190                         throw new NotImplementedException ();
191                 }
192
193                 public OdbcParameter CreateParameter ()
194                 {
195                         return new OdbcParameter ();
196                 }
197
198                 IDbDataParameter IDbCommand.CreateParameter ()
199                 {
200                         return CreateParameter ();
201                 }
202
203                 [MonoTODO]
204                 protected override void Dispose (bool disposing)
205                 {
206                         throw new NotImplementedException ();
207                 }
208
209                 protected void ExecSQL(string sql)
210                 {
211                         OdbcReturn ret;
212
213                         if (!prepared)
214                         {
215                                 Prepare();
216                                 if (Parameters.Count>0)
217                                         Parameters.Bind(hstmt);
218                         }
219
220                         if (prepared)
221                         {
222                                 ret=libodbc.SQLExecute(hstmt);
223                                 libodbc.DisplayError("SQLExecute",ret);
224                         }
225                         else
226                         {
227                                 ret=libodbc.SQLAllocHandle((ushort) OdbcHandleType.Stmt, 
228 Connection.hDbc, ref hstmt);
229                                 libodbc.DisplayError("SQLAllocHandle(hstmt)",ret);
230                                 ret=libodbc.SQLExecDirect(hstmt, sql, sql.Length);
231                                 libodbc.DisplayError("SQLExecDirect",ret);
232                         }
233                 }
234
235                 public int ExecuteNonQuery ()
236                 {
237                         if (connection == null)
238                                 throw new InvalidOperationException ();
239                         if (connection.State == ConnectionState.Closed)
240                                 throw new InvalidOperationException ();
241                         // FIXME: a third check is mentioned in .NET docs
242                         if (connection.DataReader != null)
243                                 throw new InvalidOperationException ();
244
245                         ExecSQL(CommandText);
246
247                         if (!prepared)
248                                 libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
249                         return 0;
250                 }
251
252                 public void Prepare()
253                 {
254                         OdbcReturn ret;
255                         ret=libodbc.SQLAllocHandle((ushort) OdbcHandleType.Stmt, Connection.hDbc, 
256 ref hstmt);
257                         libodbc.DisplayError("SQLAlloc(Prepare)",ret);
258                         ret=libodbc.SQLPrepare(hstmt, CommandText, CommandText.Length);
259                         libodbc.DisplayError("SQLPrepare",ret);
260                         prepared=true;
261                 }
262
263                 public OdbcDataReader ExecuteReader ()
264                 {
265                         return ExecuteReader (CommandBehavior.Default);
266                 }
267
268                 IDataReader IDbCommand.ExecuteReader ()
269                 {
270                         return ExecuteReader ();
271                 }
272
273                 public OdbcDataReader ExecuteReader (CommandBehavior behavior)
274                 {
275                         ExecuteNonQuery();
276                         dataReader=new OdbcDataReader(this);
277                         return dataReader;
278                 }
279
280                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
281                 {
282                         return ExecuteReader (behavior);
283                 }
284
285                 public object ExecuteScalar ()
286                 {
287                                         throw new NotImplementedException ();
288 //                      if (connection.DataReader != null)
289 //                              throw new InvalidOperationException ();
290 //
291                 }
292
293                 [MonoTODO]
294                 object ICloneable.Clone ()
295                 {
296                         throw new NotImplementedException ();
297                 }
298
299                 public void ResetCommandTimeout ()
300                 {
301                         timeout = 30;
302                 }
303
304                 #endregion
305         }
306 }
307