2003-12-13 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / System.Data.OracleClient / System.Data.OracleClient / OracleCommand.cs
1 // 
2 // OracleCommand.cs
3 //
4 // Part of the Mono class libraries at
5 // mcs/class/System.Data.OracleClient/System.Data.OracleClient
6 //
7 // Assembly: System.Data.OracleClient.dll
8 // Namespace: System.Data.OracleClient
9 //
10 // Authors: 
11 //    Daniel Morgan <danmorg@sc.rr.com>
12 //    Tim Coleman <tim@timcoleman.com>
13 //
14 // Copyright (C) Daniel Morgan, 2002
15 // Copyright (C) Tim Coleman , 2003
16 //
17 // Licensed under the MIT/X11 License.
18 //
19
20 using System;
21 using System.ComponentModel;
22 using System.Data;
23 using System.Data.OracleClient.Oci;
24
25 namespace System.Data.OracleClient {
26         public class OracleCommand : Component, ICloneable, IDbCommand
27         {
28                 #region Fields
29
30                 bool disposed = false;
31                 CommandBehavior behavior;
32                 string commandText;
33                 CommandType commandType;
34                 OracleConnection connection;
35                 bool designTimeVisible;
36                 OracleParameterCollection parameters;
37                 OracleTransaction transaction;
38                 UpdateRowSource updatedRowSource;
39                 
40                 OciStatementHandle statement;
41                 OciStatementType statementType;
42
43                 #endregion // Fields
44
45                 #region Constructors
46
47                 public OracleCommand ()
48                         : this (String.Empty, null, null)
49                 {
50                 }
51
52                 public OracleCommand (string commandText)
53                         : this (commandText, null, null)
54                 {
55                 }
56
57                 public OracleCommand (string commandText, OracleConnection connection)
58                         : this (commandText, connection, null)
59                 {
60                 }
61
62                 public OracleCommand (string commandText, OracleConnection connection, OracleTransaction tx)
63                 {
64                         this.commandText = commandText;
65                         this.connection = connection;
66                         this.transaction = tx;
67                         this.commandType = CommandType.Text;
68                         this.updatedRowSource = UpdateRowSource.Both;
69                         this.designTimeVisible = false;
70                         parameters = new OracleParameterCollection (this);
71                 }
72
73                 #endregion // Constructors
74
75                 #region Properties
76
77                 public string CommandText {
78                         get { return commandText; }
79                         set { commandText = value; }
80                 }
81
82                 public CommandType CommandType {
83                         get { return commandType; }
84                         set { commandType = value; }
85                 }
86
87                 public OracleConnection Connection {
88                         get { return connection; }
89                         set { connection = value; }
90                 }
91
92                 public bool DesignTimeVisible {
93                         get { return designTimeVisible; }
94                         set { designTimeVisible = value; }
95                 }
96
97                 int IDbCommand.CommandTimeout {
98                         get { throw new InvalidOperationException (); }
99                         set { throw new InvalidOperationException (); }
100                 }
101
102                 IDbConnection IDbCommand.Connection {
103                         get { return Connection; }
104                         set { 
105                                 if (!(value is OracleConnection))
106                                         throw new InvalidCastException ("The value was not a valid OracleConnection.");
107                                 Connection = (OracleConnection) value;
108                         }
109                 }
110
111                 IDataParameterCollection IDbCommand.Parameters {
112                         get { return Parameters; }
113                 }
114
115                 IDbTransaction IDbCommand.Transaction {
116                         get { return Transaction; }
117                         set { 
118                                 if (!(value is OracleTransaction))
119                                         throw new ArgumentException ();
120                                 Transaction = (OracleTransaction) value; 
121                         }
122                 }
123
124                 public OracleParameterCollection Parameters {
125                         get { return parameters; }
126                 }
127
128                 internal OciStatementHandle StatementHandle {
129                         get { return statement; }
130                 }
131
132                 public OracleTransaction Transaction {
133                         get { return transaction; }
134                         set { transaction = value; }
135                 }
136
137                 public UpdateRowSource UpdatedRowSource {
138                         get { return updatedRowSource; }
139                         set { updatedRowSource = value; }
140                 }
141
142                 #endregion
143
144                 #region Methods
145
146                 [MonoTODO]
147                 public void Cancel ()
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 [MonoTODO]
153                 public object Clone ()
154                 {
155                         throw new NotImplementedException ();
156                 }
157
158                 /*
159                 [MonoTODO("Still need to Dispose correctly")]
160                 public new void Dispose () 
161                 {       
162                         if (!disposed) {
163                                 if (statementHandle != IntPtr.Zero) {
164                                         OciGlue.OCIHandleFree (statementHandle, OciHandleType.Statement);
165                                         statementHandle = IntPtr.Zero;
166                                 }
167                                 //base.Dispose ();
168                         }
169                 }
170                 
171                 [MonoTODO("still need to Finalize correctly")]
172                 ~OracleCommand() {
173                         Dispose();
174                 }
175                 */
176
177                 internal void CloseDataReader ()
178                 {
179                         Connection.DataReader = null;
180                         if ((behavior & CommandBehavior.CloseConnection) != 0)
181                                 Connection.Close ();
182                 }
183
184                 public OracleParameter CreateParameter ()
185                 {
186                         return new OracleParameter ();
187                 }
188
189                 public int ExecuteNonQuery () 
190                 {
191                         int rowsAffected = -1;
192
193                         ValidateCommand ("ExecuteNonQuery");
194                         statement = Connection.Oci.CreateStatement ();
195                         statement.Prepare (CommandText);
196                         statement.ExecuteNonQuery ();
197
198                         return rowsAffected;
199                 }
200
201                 [MonoTODO]
202                 public int ExecuteOracleNonQuery (out OracleString rowid)
203                 {
204                         throw new NotImplementedException ();
205                 }
206
207                 [MonoTODO]
208                 public object ExecuteOracleScalar ()
209                 {
210                         throw new NotImplementedException ();
211                 }
212
213                 public OracleDataReader ExecuteReader ()
214                 {
215                         return ExecuteReader (CommandBehavior.Default);
216                 }
217
218                 public OracleDataReader ExecuteReader (CommandBehavior behavior)
219                 {
220                         statement = Connection.Oci.CreateStatement ();
221                         statement.Prepare (CommandText);
222                         statement.ExecuteQuery ();
223                         return new OracleDataReader (this);
224                 }
225
226                 [MonoTODO]
227                 public object ExecuteScalar ()
228                 {
229                         throw new NotImplementedException ();
230                 }
231
232                 IDbDataParameter IDbCommand.CreateParameter ()
233                 {
234                         return CreateParameter ();
235                 }
236
237                 IDataReader IDbCommand.ExecuteReader ()
238                 {
239                         return ExecuteReader ();
240                 }
241
242                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
243                 {
244                         return ExecuteReader (behavior);
245                 }
246
247                 [MonoTODO]
248                 public void Prepare ()
249                 {
250                         throw new NotImplementedException ();
251                 }
252
253                 private void ValidateCommand (string method)
254                 {
255                         if (Connection == null)
256                                 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
257                         if (Connection.Transaction != null && Transaction != Connection.Transaction)
258                                 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
259                         if (Connection.State != ConnectionState.Open)
260                                 throw new InvalidOperationException (String.Format ("{0} requires an open Connection object to continue. This connection is closed.", method));
261                         if (CommandText == String.Empty || CommandText == null)
262                                 throw new InvalidOperationException ("The command text for this Command has not been set.");
263                         if (Connection.DataReader != null)
264                                 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
265                 }
266
267                 #endregion // Methods
268         }
269 }