2002-11-09 Tim Coleman <tim@timcoleman.com>
[mono.git] / mcs / class / Mono.Data.TdsClient / Mono.Data.TdsClient / TdsCommand.cs
1 //
2 // Mono.Data.TdsClient.TdsCommand.cs
3 //
4 // Author:
5 //   Tim Coleman (tim@timcoleman.com)
6 //
7 // Copyright (C) 2002 Tim Coleman
8 //
9
10 using Mono.Data.TdsClient.Internal;
11 using System;
12 using System.ComponentModel;
13 using System.Data;
14
15 namespace Mono.Data.TdsClient {
16         public class TdsCommand : Component, ICloneable, IDbCommand
17         {
18                 #region Fields
19
20                 string commandText;
21                 int commandTimeout;
22                 CommandType commandType;
23                 TdsConnection connection;
24                 TdsParameterCollection parameters;
25                 TdsTransaction transaction;
26
27                 #endregion // Fields
28
29                 #region Constructors
30
31                 public TdsCommand ()
32                         : this (String.Empty, null, null)
33                 {
34                 }
35
36                 public TdsCommand (string commandText)
37                         : this (commandText, null, null)
38                 {
39                 }
40
41                 public TdsCommand (string commandText, TdsConnection connection)
42                         : this (commandText, connection, null)
43                 {
44                 }
45
46                 public TdsCommand (string commandText, TdsConnection connection, TdsTransaction transaction)
47                 {
48                         this.commandText = commandText;
49                         this.transaction = transaction;
50                         this.commandType = CommandType.Text;
51                         this.connection = connection;
52                 }
53
54                 #endregion // Constructors
55
56                 #region Properties
57
58                 public string CommandText {
59                         get { return commandText; }
60                         set { commandText = value; }
61                 }
62
63                 public int CommandTimeout {
64                         get { return commandTimeout; }
65                         set { commandTimeout = value; }
66                 }
67
68                 public CommandType CommandType {
69                         get { return commandType; }
70                         set { commandType = value; }
71                 }
72
73                 public TdsConnection Connection {       
74                         get { return connection; }
75                         set { 
76                                 if (transaction != null && connection.Transaction != null && connection.Transaction.IsOpen)
77                                         throw new InvalidOperationException ("The Connection property was changed while a transaction was in progress.");
78                                 transaction = null;
79                                 connection = value;
80                         }
81                 }
82
83                 IDbConnection IDbCommand.Connection {
84                         get { return Connection; }
85                         set { 
86                                 if (!(value is TdsConnection)) 
87                                         throw new ArgumentException ();
88                                 Connection = (TdsConnection) value;     
89                         }
90                 }
91
92                 IDataParameterCollection IDbCommand.Parameters {
93                         get { return Parameters; }
94                 }
95
96                 IDbTransaction IDbCommand.Transaction {
97                         get { return Transaction; }
98                         set { 
99                                 if (!(value is TdsTransaction)) 
100                                         throw new ArgumentException ();
101                                 Transaction = (TdsTransaction) value; 
102                         }
103                 }
104
105                 public TdsParameterCollection Parameters {
106                         get { return parameters; }
107                 }
108
109                 internal ITds Tds {
110                         get { return connection.Tds; }
111                 }
112
113                 public TdsTransaction Transaction {
114                         get { return transaction; }
115                         set { transaction = value; }
116                 }
117
118                 [MonoTODO]
119                 public UpdateRowSource UpdatedRowSource {
120                         get { throw new NotImplementedException (); }
121                         set { throw new NotImplementedException (); }
122                 }
123
124                 #endregion // Properties
125
126                 #region Methods
127
128                 [MonoTODO]
129                 public void Cancel ()
130                 {
131                         throw new NotImplementedException ();
132                 }
133
134                 [MonoTODO]
135                 TdsParameter CreateParameter ()
136                 {
137                         throw new NotImplementedException ();
138                 }
139
140                 public int ExecuteNonQuery ()
141                 {
142                         ValidateCommand ("ExecuteNonQuery");
143                         return connection.Tds.ExecuteNonQuery (FormatQuery (commandText, commandType));
144                 }
145
146                 public TdsDataReader ExecuteReader ()
147                 {
148                         return ExecuteReader (CommandBehavior.Default);
149                 }
150
151                 public TdsDataReader ExecuteReader (CommandBehavior behavior)
152                 {
153                         ValidateCommand ("ExecuteReader");
154                         connection.DataReader = new TdsDataReader (this);
155                         return connection.DataReader;
156                 }
157
158                 [MonoTODO]
159                 public object ExecuteScalar ()
160                 {
161                         throw new NotImplementedException ();
162                 }
163
164                 private static string FormatQuery (string commandText, CommandType commandType)
165                 {
166                         switch (commandType) {
167                         case CommandType.Text :
168                                 return commandText;
169                         case CommandType.TableDirect :
170                                 return String.Format ("select * from {0}", commandText);
171                         case CommandType.StoredProcedure :
172                                 return String.Format ("exec {0}", commandText);
173                         }
174                         throw new InvalidOperationException ("Invalid command type");
175                 }
176
177                 [MonoTODO]
178                 object ICloneable.Clone()
179                 {
180                         throw new NotImplementedException ();
181                 }
182
183                 IDbDataParameter IDbCommand.CreateParameter ()
184                 {
185                         return CreateParameter ();
186                 }
187
188                 IDataReader IDbCommand.ExecuteReader ()
189                 {
190                         return ExecuteReader ();
191                 }
192
193                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
194                 {
195                         return ExecuteReader (behavior);
196                 }
197
198                 [MonoTODO]
199                 public void Prepare ()
200                 {
201                         throw new NotImplementedException ();
202                 }
203
204                 private void ValidateCommand (string method)
205                 {
206                         if (connection == null)
207                                 throw new InvalidOperationException (String.Format ("{0} requires a Connection object to continue.", method));
208                         if (connection.Transaction != null && transaction != connection.Transaction)
209                                 throw new InvalidOperationException ("The Connection object does not have the same transaction as the command object.");
210                         if (connection.State != ConnectionState.Open)
211                                 throw new InvalidOperationException (String.Format ("ExecuteNonQuery requires an open Connection object to continue. This connection is closed.", method));
212                         if (commandText == String.Empty || commandText == null)
213                                 throw new InvalidOperationException ("The command text for this Command has not been set.");
214                         if (connection.DataReader != null)
215                                 throw new InvalidOperationException ("There is already an open DataReader associated with this Connection which must be closed first.");
216                 }
217
218                 #endregion // Methods
219         }
220 }