2003-02-10 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                 #endregion // Fields
41
42                 #region Constructors
43
44                 public OracleCommand ()
45                         : this (String.Empty, null, null)
46                 {
47                 }
48
49                 public OracleCommand (string commandText)
50                         : this (commandText, null, null)
51                 {
52                 }
53
54                 public OracleCommand (string commandText, OracleConnection connection)
55                         : this (commandText, connection, null)
56                 {
57                 }
58
59                 public OracleCommand (string commandText, OracleConnection connection, OracleTransaction tx)
60                 {
61                         this.commandText = commandText;
62                         this.connection = connection;
63                         this.transaction = tx;
64                         this.commandType = CommandType.Text;
65                         this.updatedRowSource = UpdateRowSource.Both;
66                         this.designTimeVisible = false;
67                         parameters = new OracleParameterCollection (this);
68
69                 }
70
71                 #endregion // Constructors
72
73                 #region Properties
74
75                 public string CommandText {
76                         get { return commandText; }
77                         set { commandText = value; }
78                 }
79
80                 public CommandType CommandType {
81                         get { return commandType; }
82                         set { commandType = value; }
83                 }
84
85                 public OracleConnection Connection {
86                         get { return connection; }
87                         set { connection = value; }
88                 }
89
90                 public bool DesignTimeVisible {
91                         get { return designTimeVisible; }
92                         set { designTimeVisible = value; }
93                 }
94
95                 int IDbCommand.CommandTimeout {
96                         get { throw new InvalidOperationException (); }
97                         set { throw new InvalidOperationException (); }
98                 }
99
100                 IDbConnection IDbCommand.Connection {
101                         get { return Connection; }
102                         set { 
103                                 if (!(value is OracleConnection))
104                                         throw new InvalidCastException ("The value was not a valid OracleConnection.");
105                                 Connection = (OracleConnection) value;
106                         }
107                 }
108
109                 IDataParameterCollection IDbCommand.Parameters {
110                         get { return Parameters; }
111                 }
112
113                 IDbTransaction IDbCommand.Transaction {
114                         get { return Transaction; }
115                         set { 
116                                 if (!(value is OracleTransaction))
117                                         throw new ArgumentException ();
118                                 Transaction = (OracleTransaction) value; 
119                         }
120                 }
121
122                 public OracleParameterCollection Parameters {
123                         get { return parameters; }
124                 }
125
126                 public OracleTransaction Transaction {
127                         get { return transaction; }
128                         set { transaction = value; }
129                 }
130
131                 public UpdateRowSource UpdatedRowSource {
132                         get { return updatedRowSource; }
133                         set { updatedRowSource = value; }
134                 }
135
136                 #endregion
137
138                 #region Methods
139
140                 [MonoTODO]
141                 public void Cancel ()
142                 {
143                         throw new NotImplementedException ();
144                 }
145
146                 [MonoTODO]
147                 public object Clone ()
148                 {
149                         throw new NotImplementedException ();
150                 }
151
152                 internal void CloseDataReader ()
153                 {
154                         Connection.DataReader = null;
155                         if ((behavior & CommandBehavior.CloseConnection) != 0)
156                                 Connection.Close ();
157                 }
158
159                 public OracleParameter CreateParameter ()
160                 {
161                         return new OracleParameter ();
162                 }
163
164                 public int ExecuteNonQuery () 
165                 {
166                         int rowsAffected = -1;
167
168                         ValidateCommand ("ExecuteNonQuery");
169
170                         Int32 status;
171                         status = connection.Oci.PrepareAndExecuteNonQuerySimple (commandText);
172                         if(status != 0) {
173                                 string statusText;
174                                 statusText = connection.Oci.CheckStatus(status);
175                                 string msg = 
176                                         "SQL Error: [" + 
177                                         commandText + "] " +
178                                         statusText;
179                                 throw new Exception(msg);
180                         }
181
182                         return rowsAffected;
183                 }
184
185                 [MonoTODO]
186                 public int ExecuteOracleNonQuery (out OracleString rowid)
187                 {
188                         throw new NotImplementedException ();
189                 }
190
191                 [MonoTODO]
192                 public object ExecuteOracleScalar ()
193                 {
194                         throw new NotImplementedException ();
195                 }
196
197                 public OracleDataReader ExecuteReader ()
198                 {
199                         return ExecuteReader (CommandBehavior.Default);
200                 }
201
202                 [MonoTODO]
203                 public OracleDataReader ExecuteReader (CommandBehavior behavior)
204                 {
205                         throw new NotImplementedException ();
206                 }
207
208                 [MonoTODO]
209                 public object ExecuteScalar ()
210                 {
211                         throw new NotImplementedException ();
212                 }
213
214                 IDbDataParameter IDbCommand.CreateParameter ()
215                 {
216                         return CreateParameter ();
217                 }
218
219                 IDataReader IDbCommand.ExecuteReader ()
220                 {
221                         return ExecuteReader ();
222                 }
223
224                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
225                 {
226                         return ExecuteReader (behavior);
227                 }
228
229                 [MonoTODO]
230                 public void Prepare ()
231                 {
232                         throw new NotImplementedException ();
233                 }
234
235                 private void ValidateCommand (string method)
236                 {
237                         if (Connection == null)
238                                 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
239                         if (Connection.Transaction != null && Transaction != Connection.Transaction)
240                                 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
241                         if (Connection.State != ConnectionState.Open)
242                                 throw new InvalidOperationException (String.Format ("{0} requires an open Connection object to continue. This connection is closed.", method));
243                         if (CommandText == String.Empty || CommandText == null)
244                                 throw new InvalidOperationException ("The command text for this Command has not been set.");
245                         if (Connection.DataReader != null)
246                                 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
247                 }
248
249                 #endregion // Methods
250         }
251 }