2002-10-24 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                 bool readerIsOpen = false;
27
28                 #endregion // Fields
29
30                 #region Constructors
31
32                 public TdsCommand ()
33                         : this (String.Empty, null, null)
34                 {
35                 }
36
37                 public TdsCommand (string commandText)
38                         : this (commandText, null, null)
39                 {
40                 }
41
42                 public TdsCommand (string commandText, TdsConnection connection)
43                         : this (commandText, connection, null)
44                 {
45                 }
46
47                 public TdsCommand (string commandText, TdsConnection connection, TdsTransaction transaction)
48                 {
49                         this.commandText = commandText;
50                         this.transaction = transaction;
51                         this.commandType = CommandType.Text;
52                         this.connection = connection;
53                 }
54
55                 #endregion // Constructors
56
57                 #region Properties
58
59                 public string CommandText {
60                         get { return commandText; }
61                         set { commandText = value; }
62                 }
63
64                 public int CommandTimeout {
65                         get { return commandTimeout; }
66                         set { commandTimeout = value; }
67                 }
68
69                 public CommandType CommandType {
70                         get { return commandType; }
71                         set { commandType = value; }
72                 }
73
74                 public TdsConnection Connection {       
75                         get { return connection; }
76                         set { connection = value; }
77                 }
78
79                 IDbConnection IDbCommand.Connection {
80                         get { return Connection; }
81                         set { 
82                                 if (!(value is TdsConnection)) 
83                                         throw new ArgumentException ();
84                                 Connection = (TdsConnection) value;     
85                         }
86                 }
87
88                 IDataParameterCollection IDbCommand.Parameters {
89                         get { return Parameters; }
90                 }
91
92                 IDbTransaction IDbCommand.Transaction {
93                         get { return Transaction; }
94                         set { 
95                                 if (!(value is TdsTransaction)) 
96                                         throw new ArgumentException ();
97                                 Transaction = (TdsTransaction) value; 
98                         }
99                 }
100
101                 public TdsParameterCollection Parameters {
102                         get { return parameters; }
103                 }
104
105                 internal ITds Tds {
106                         get { return connection.Tds; }
107                 }
108
109                 public TdsTransaction Transaction {
110                         get { return transaction; }
111                         set { transaction = value; }
112                 }
113
114                 [MonoTODO]
115                 public UpdateRowSource UpdatedRowSource {
116                         get { throw new NotImplementedException (); }
117                         set { throw new NotImplementedException (); }
118                 }
119
120                 #endregion // Properties
121
122                 #region Methods
123
124                 [MonoTODO]
125                 public void Cancel ()
126                 {
127                         throw new NotImplementedException ();
128                 }
129
130                 [MonoTODO]
131                 TdsParameter CreateParameter ()
132                 {
133                         throw new NotImplementedException ();
134                 }
135
136                 public int ExecuteNonQuery ()
137                 {
138                         if (connection == null)
139                                 throw new TdsException ("The connection is not set.");
140                         if (connection.State != ConnectionState.Open)
141                                 throw new TdsException ("The connection is closed.");
142                         if (commandText == String.Empty || commandText == null)
143                                 throw new TdsException ("The command text is not set.");
144
145                         return connection.Tds.ExecuteNonQuery (FormatQuery (commandText, commandType));
146                 }
147
148                 public TdsDataReader ExecuteReader ()
149                 {
150                         return ExecuteReader (CommandBehavior.Default);
151                 }
152
153                 [MonoTODO]
154                 public TdsDataReader ExecuteReader (CommandBehavior behavior)
155                 {
156                         if (connection == null)
157                                 throw new TdsException ("The connection is not set.");
158                         if (connection.State != ConnectionState.Open)
159                                 throw new TdsException ("The connection is closed.");
160                         if (commandText == String.Empty || commandText == null)
161                                 throw new TdsException ("The command text is not set.");
162                         connection.Tds.ExecuteQuery (FormatQuery (commandText, commandType));
163
164                         return new TdsDataReader (this);
165                 }
166
167                 [MonoTODO]
168                 public object ExecuteScalar ()
169                 {
170                         throw new NotImplementedException ();
171                 }
172
173                 private static string FormatQuery (string commandText, CommandType commandType)
174                 {
175                         switch (commandType) {
176                         case CommandType.Text :
177                                 return commandText;
178                         case CommandType.TableDirect :
179                                 return String.Format ("select * from {0}", commandText);
180                         case CommandType.StoredProcedure :
181                                 return String.Format ("exec {0}", commandText);
182                         }
183                         throw new TdsException ("Invalid command type");
184                 }
185
186                 [MonoTODO]
187                 object ICloneable.Clone()
188                 {
189                         throw new NotImplementedException ();
190                 }
191
192                 IDbDataParameter IDbCommand.CreateParameter ()
193                 {
194                         return CreateParameter ();
195                 }
196
197                 IDataReader IDbCommand.ExecuteReader ()
198                 {
199                         return ExecuteReader ();
200                 }
201
202                 IDataReader IDbCommand.ExecuteReader (CommandBehavior behavior)
203                 {
204                         return ExecuteReader (behavior);
205                 }
206
207                 [MonoTODO]
208                 public void Prepare ()
209                 {
210                         throw new NotImplementedException ();
211                 }
212
213                 /*
214                 internal void SkipToEnd ()
215                 {
216                         if (tds != null)
217                                 while (GetMoreResults (tds, false) || updateCount != 1);
218                 }
219                 */
220
221                 #endregion // Methods
222         }
223 }