2003-02-12 Daniel Morgan <danmorg@sc.rr.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                 IntPtr statementHandle;
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
74                 #endregion // Constructors
75
76                 #region Properties
77
78                 public string CommandText {
79                         get { return commandText; }
80                         set { commandText = value; }
81                 }
82
83                 public CommandType CommandType {
84                         get { return commandType; }
85                         set { commandType = value; }
86                 }
87
88                 public OracleConnection Connection {
89                         get { return connection; }
90                         set { connection = value; }
91                 }
92
93                 public bool DesignTimeVisible {
94                         get { return designTimeVisible; }
95                         set { designTimeVisible = value; }
96                 }
97
98                 int IDbCommand.CommandTimeout {
99                         get { throw new InvalidOperationException (); }
100                         set { throw new InvalidOperationException (); }
101                 }
102
103                 IDbConnection IDbCommand.Connection {
104                         get { return Connection; }
105                         set { 
106                                 if (!(value is OracleConnection))
107                                         throw new InvalidCastException ("The value was not a valid OracleConnection.");
108                                 Connection = (OracleConnection) value;
109                         }
110                 }
111
112                 IDataParameterCollection IDbCommand.Parameters {
113                         get { return Parameters; }
114                 }
115
116                 IDbTransaction IDbCommand.Transaction {
117                         get { return Transaction; }
118                         set { 
119                                 if (!(value is OracleTransaction))
120                                         throw new ArgumentException ();
121                                 Transaction = (OracleTransaction) value; 
122                         }
123                 }
124
125                 public OracleParameterCollection Parameters {
126                         get { return parameters; }
127                 }
128
129                 public OracleTransaction Transaction {
130                         get { return transaction; }
131                         set { transaction = value; }
132                 }
133
134                 public UpdateRowSource UpdatedRowSource {
135                         get { return updatedRowSource; }
136                         set { updatedRowSource = value; }
137                 }
138
139                 #endregion
140
141                 #region Methods
142
143                 [MonoTODO]
144                 public void Cancel ()
145                 {
146                         throw new NotImplementedException ();
147                 }
148
149                 [MonoTODO]
150                 public object Clone ()
151                 {
152                         throw new NotImplementedException ();
153                 }
154
155                 /*
156                 [MonoTODO("Still need to Dispose correctly")]
157                 public new void Dispose () 
158                 {       
159                         if (!disposed) {
160                                 if (statementHandle != IntPtr.Zero) {
161                                         OciGlue.OCIHandleFree (statementHandle, OciHandleType.Statement);
162                                         statementHandle = IntPtr.Zero;
163                                 }
164                                 //base.Dispose ();
165                         }
166                 }
167                 
168                 [MonoTODO("still need to Finalize correctly")]
169                 ~OracleCommand() {
170                         Dispose();
171                 }
172                 */
173
174                 internal void CloseDataReader ()
175                 {
176                         Connection.DataReader = null;
177                         if ((behavior & CommandBehavior.CloseConnection) != 0)
178                                 Connection.Close ();
179                 }
180
181                 public OracleParameter CreateParameter ()
182                 {
183                         return new OracleParameter ();
184                 }
185
186                 public int ExecuteNonQuery () 
187                 {
188                         int rowsAffected = -1;
189
190                         ValidateCommand ("ExecuteNonQuery");
191                         statementHandle = Connection.Oci.PrepareStatement (CommandText);
192                         statementType = Connection.Oci.GetStatementType (statementHandle);
193                         Connection.Oci.ExecuteStatement (statementHandle, statementType);
194
195                         return rowsAffected;
196                 }
197
198                 [MonoTODO]
199                 public int ExecuteOracleNonQuery (out OracleString rowid)
200                 {
201                         throw new NotImplementedException ();
202                 }
203
204                 [MonoTODO]
205                 public object ExecuteOracleScalar ()
206                 {
207                         throw new NotImplementedException ();
208                 }
209
210                 public OracleDataReader ExecuteReader ()
211                 {
212                         return ExecuteReader (CommandBehavior.Default);
213                 }
214
215                 [MonoTODO]
216                 public OracleDataReader ExecuteReader (CommandBehavior behavior)
217                 {
218                         throw new NotImplementedException ();
219                 }
220
221                 [MonoTODO]
222                 public object ExecuteScalar ()
223                 {
224                         throw new NotImplementedException ();
225                 }
226
227                 IDbDataParameter IDbCommand.CreateParameter ()
228                 {
229                         return CreateParameter ();
230                 }
231
232                 IDataReader IDbCommand.ExecuteReader ()
233                 {
234                         return ExecuteReader ();
235                 }
236
237                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
238                 {
239                         return ExecuteReader (behavior);
240                 }
241
242                 [MonoTODO]
243                 public void Prepare ()
244                 {
245                         throw new NotImplementedException ();
246                 }
247
248                 private void ValidateCommand (string method)
249                 {
250                         if (Connection == null)
251                                 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
252                         if (Connection.Transaction != null && Transaction != Connection.Transaction)
253                                 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
254                         if (Connection.State != ConnectionState.Open)
255                                 throw new InvalidOperationException (String.Format ("{0} requires an open Connection object to continue. This connection is closed.", method));
256                         if (CommandText == String.Empty || CommandText == null)
257                                 throw new InvalidOperationException ("The command text for this Command has not been set.");
258                         if (Connection.DataReader != null)
259                                 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
260                 }
261
262                 #endregion // Methods
263         }
264 }