2002-04-17 Daniel Morgan <danmorg@sc.rr.com>
[mono.git] / mcs / class / Mono.Data.PostgreSqlClient / Mono.Data.PostgreSqlClient / PgSqlCommand.cs
1 //
2 // System.Data.SqlClient.SqlCommand.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rdorigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //
8 // (C) Ximian, Inc 2002
9 //
10
11 using System;
12 using System.ComponentModel;
13 using System.Data;
14 using System.Data.Common;
15 using System.Runtime.InteropServices;
16 using System.Xml;
17
18 namespace System.Data.SqlClient
19 {
20         /// <summary>
21         /// Represents a SQL statement that is executed 
22         /// while connected to a SQL database.
23         /// </summary>
24         // public sealed class SqlCommand : Component, IDbCommand, ICloneable
25         public sealed class SqlCommand : IDbCommand
26         {
27                 // FIXME: Console.WriteLine() is used for debugging throughout
28
29                 #region Fields
30
31                 string sql = "";
32                 int timeout = 30; 
33                 // default is 30 seconds 
34                 // for command execution
35
36                 SqlConnection conn = null;
37                 SqlTransaction trans = null;
38                 CommandType cmdType = CommandType.Text;
39                 bool designTime = false;
40                 SqlParameterCollection parmCollection = new 
41                         SqlParameterCollection();
42
43                 #endregion // Fields
44
45                 #region Constructors
46
47                 public SqlCommand()
48                 {
49                         sql = "";
50                 }
51
52                 public SqlCommand (string cmdText)
53                 {
54                         sql = cmdText;
55                 }
56
57                 public SqlCommand (string cmdText, SqlConnection connection)
58                 {
59                         sql = cmdText;
60                         conn = connection;
61                 }
62
63                 public SqlCommand (string cmdText, SqlConnection connection, 
64                                                 SqlTransaction transaction)
65                 {
66                         sql = cmdText;
67                         conn = connection;
68                         trans = transaction;
69                 }
70
71                 #endregion // Constructors
72
73                 #region Methods
74
75                 [MonoTODO]
76                 public void Cancel ()
77                 {
78                         throw new NotImplementedException ();
79                 }
80
81                 // FIXME: is this the correct way to return a stronger type?
82                 [MonoTODO]
83                 IDbDataParameter IDbCommand.CreateParameter ()
84                 {
85                         return CreateParameter ();
86                 }
87
88                 [MonoTODO]
89                 public SqlParameter CreateParameter ()
90                 {
91                         return new SqlParameter ();
92                 }
93
94                 [MonoTODO]
95                 public int ExecuteNonQuery ()
96                 {       
97                         IntPtr pgResult; // PGresult
98                         int rowsAffected = -1;
99                         ExecStatusType execStatus;
100                         String rowsAffectedString;
101
102                         // FIXME: throw an 
103                         // InvalidOperationException
104                         // exception if the the connection
105                         // does not exist or is not open
106
107                         // FIXME: PQexec blocks 
108                         // while PQsendQuery is non-blocking
109                         // which is better to use?
110                         // int PQsendQuery(PGconn *conn,
111                         //        const char *query);
112
113                         // execute SQL command
114                         // uses internal property to get the PGConn IntPtr
115                         pgResult = PostgresLibrary.
116                                 PQexec (conn.PostgresConnection, sql);
117
118                         /* FIXME: throw an SqlException exception
119                          * if there is a SQL Error
120                          */
121
122                         /*
123                          * FIXME: get status
124                          */
125                         execStatus = PostgresLibrary.
126                                         PQresultStatus (pgResult);
127                         
128                         if(execStatus == ExecStatusType.PGRES_COMMAND_OK)
129                         {
130                                 Console.WriteLine("*** SqlCommand Execute " +
131                                         "got PGRES_COMMAND_OK");
132                                 rowsAffectedString = PostgresLibrary.
133                                         PQcmdTuples (pgResult);
134                                 Console.WriteLine("*** Rows Affected: " + 
135                                         rowsAffectedString);
136                                 // FIXME: convert string to number
137                         }
138                         else
139                         {
140                                 Console.WriteLine("*** Error: SqlCommand " +
141                                         "did not get PGRES_COMMAND_OK");
142                                 String statusString;
143                                 
144                                 statusString = PostgresLibrary.
145                                         PQresStatus(execStatus);
146                                 Console.WriteLine("*** Command Status: " +
147                                         statusString);
148
149                                 String errorMessage;
150                                 errorMessage = PostgresLibrary.\r
151                                         PQresultErrorMessage(pgResult);\r
152 \r
153                                 Console.WriteLine("*** Error message: " +\r
154                                         statusString);                          
155                         }
156                         
157                         String cmdStatus;
158                         cmdStatus = PostgresLibrary.
159                                 PQcmdStatus(pgResult);
160
161                         Console.WriteLine("*** Command Status: " +
162                                 cmdStatus);
163
164                         PostgresLibrary.PQclear (pgResult);
165                         
166                         // FIXME: get number of rows
167                         // affected for INSERT, UPDATE, or DELETE
168                         // any other, return -1 (such as, CREATE TABLE)
169                         return rowsAffected;
170                 }
171                 
172                 // FIXME: temporarily commmented out, so I could get a simple working
173                 //        SqlConnection and SqlCommand.  I had to temporarily
174                 //        comment it out the ExecuteReader in IDbCommand as well.
175                 /*
176                 [MonoTODO]
177                 IDataReader IDbCommand.ExecuteReader ()
178                 {
179                         throw new NotImplementedException ();   
180                 }
181
182                 [MonoTODO]
183                 SqlDataReader ExecuteReader ()
184                 {
185                         throw new NotImplementedException ();   
186                 }
187
188                 [MonoTODO]
189                 IDataReader IDbCommand.ExecuteReader (
190                                         CommandBehavior behavior)
191                 {
192                         throw new NotImplementedException ();
193                 }
194
195                 [MonoTODO]
196                 public SqlDataReader ExecuteReader (CommandBehavior behavior)
197                 {
198                         throw new NotImplementedException ();
199                 }
200                 */
201
202                 [MonoTODO]
203                 public object ExecuteScalar ()
204                 {
205                         throw new NotImplementedException ();
206                 }
207
208                 [MonoTODO]
209                 public XmlReader ExecuteXmlReader ()
210                 {
211                         throw new NotImplementedException ();
212                 }
213
214                 [MonoTODO]
215                 public void Prepare ()
216                 {
217                         throw new NotImplementedException ();
218                 }
219
220                 [MonoTODO]
221                 public SqlCommand Clone ()
222                 {
223                         throw new NotImplementedException ();
224                 }
225
226                 #endregion // Methods
227
228                 #region Properties
229
230                 public string CommandText {
231                         get { 
232                                 return sql; 
233                         }
234
235                         set { 
236                                 sql = value; 
237                         }
238                 }
239
240                 public int CommandTimeout {
241                         get { 
242                                 return timeout;  
243                         }
244                         
245                         set {
246                                 // FIXME: if value < 0, throw
247                                 // ArgumentException
248                                 // if (value < 0)
249                                 //      throw ArgumentException;
250                                 timeout = value;
251                         }
252                 }
253
254                 public CommandType CommandType  {
255                         get {
256                                 return cmdType;
257                         }
258
259                         set { 
260                                 cmdType = value;
261                         }
262                 }
263
264                 // FIXME: for property Connection, is this the correct
265                 //        way to handle a return of a stronger type?
266                 IDbConnection IDbCommand.Connection {
267                         get { 
268                                 return Connection;
269                         }
270
271                         set { 
272                                 // FIXME: throw an InvalidOperationException
273                                 // if the change was during a 
274                                 // transaction in progress
275                                 Connection = (SqlConnection) value; 
276                                 // FIXME: set Transaction property to null
277                         }
278                 }
279                 
280                 public SqlConnection Connection {
281                         get { 
282                                 // conn defaults to null
283                                 return conn;
284                         }
285
286                         set { 
287                                 // FIXME: throw an InvalidOperationException
288                                 // if the change was during 
289                                 // a transaction in progress
290                                 conn = value; 
291                                 // FIXME: set Transaction property to null
292                         }
293                 }
294
295                 public bool DesignTimeVisible {
296                         get {
297                                 return designTime;
298                         } 
299                         
300                         set{
301                                 designTime = value;
302                         }
303                 }
304
305                 // FIXME; for property Parameters, is this the correct
306                 //        way to handle a stronger return type?
307                 IDataParameterCollection IDbCommand.Parameters  {
308                         get { 
309                                 return Parameters;
310                         }
311                 }
312
313                 SqlParameterCollection Parameters {
314                         get { 
315                                 return parmCollection;
316                         }
317                 }
318
319                 // FIXME: for property Transaction, is this the correct
320                 //        way to handle a return of a stronger type?
321                 IDbTransaction IDbCommand.Transaction   {
322                         get { 
323                                 return Transaction;
324                         }
325
326                         set { 
327                                 // FIXME: error handling
328                                 Transaction = (SqlTransaction) value; 
329                         }
330                 }
331
332                 public SqlTransaction Transaction {
333                         get { 
334                                 return trans; 
335                         }
336
337                         set { 
338                                 // FIXME: error handling
339                                 trans = value; 
340                         }
341                 }       
342
343                 [MonoTODO]
344                 public UpdateRowSource UpdatedRowSource {
345                         // FIXME: do this once DbDataAdaptor 
346                         // and DataRow are done
347                         get {           
348                                 throw new NotImplementedException (); 
349                         }
350                         set { 
351                                 throw new NotImplementedException (); 
352                         }
353                 }
354
355                 #endregion // Properties
356
357                 #region Desctructors
358 /*              
359                 [MonoTODO]
360                 [ClassInterface(ClassInterfaceType.AutoDual)]
361                 ~SqlCommand()
362                 {
363                         FIXME: need proper way to release resources
364                 }
365 */
366                 #endregion //Destructors
367         }
368 }