2003-02-11 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                 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                 internal void CloseDataReader ()
156                 {
157                         Connection.DataReader = null;
158                         if ((behavior & CommandBehavior.CloseConnection) != 0)
159                                 Connection.Close ();
160                 }
161
162                 public OracleParameter CreateParameter ()
163                 {
164                         return new OracleParameter ();
165                 }
166
167                 public int ExecuteNonQuery () 
168                 {
169                         int rowsAffected = -1;
170
171                         ValidateCommand ("ExecuteNonQuery");
172                         statementHandle = Connection.Oci.PrepareStatement (CommandText);
173                         statementType = Connection.Oci.GetStatementType (statementHandle);
174                         Connection.Oci.ExecuteStatement (statementHandle, statementType);
175
176                         /*
177                         Int32 status;
178                         status = connection.Oci.PrepareAndExecuteNonQuerySimple (commandText);
179                         if(status != 0) {
180                                 string statusText;
181                                 statusText = connection.Oci.CheckStatus(status);
182                                 string msg = 
183                                         "SQL Error: [" + 
184                                         commandText + "] " +
185                                         statusText;
186                                 throw new Exception(msg);
187                         }
188                         */
189
190                         return rowsAffected;
191                 }
192
193                 [MonoTODO]
194                 public int ExecuteOracleNonQuery (out OracleString rowid)
195                 {
196                         throw new NotImplementedException ();
197                 }
198
199                 [MonoTODO]
200                 public object ExecuteOracleScalar ()
201                 {
202                         throw new NotImplementedException ();
203                 }
204
205                 public OracleDataReader ExecuteReader ()
206                 {
207                         return ExecuteReader (CommandBehavior.Default);
208                 }
209
210                 [MonoTODO]
211                 public OracleDataReader ExecuteReader (CommandBehavior behavior)
212                 {
213                         throw new NotImplementedException ();
214                 }
215
216                 [MonoTODO]
217                 public object ExecuteScalar ()
218                 {
219                         throw new NotImplementedException ();
220                 }
221
222                 IDbDataParameter IDbCommand.CreateParameter ()
223                 {
224                         return CreateParameter ();
225                 }
226
227                 IDataReader IDbCommand.ExecuteReader ()
228                 {
229                         return ExecuteReader ();
230                 }
231
232                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
233                 {
234                         return ExecuteReader (behavior);
235                 }
236
237                 [MonoTODO]
238                 public void Prepare ()
239                 {
240                         throw new NotImplementedException ();
241                 }
242
243                 private void ValidateCommand (string method)
244                 {
245                         if (Connection == null)
246                                 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
247                         if (Connection.Transaction != null && Transaction != Connection.Transaction)
248                                 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
249                         if (Connection.State != ConnectionState.Open)
250                                 throw new InvalidOperationException (String.Format ("{0} requires an open Connection object to continue. This connection is closed.", method));
251                         if (CommandText == String.Empty || CommandText == null)
252                                 throw new InvalidOperationException ("The command text for this Command has not been set.");
253                         if (Connection.DataReader != null)
254                                 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
255                 }
256
257                 #endregion // Methods
258         }
259 }