2002-05-04 Daniel Morgan <danmorg@sc.rr.com>
[mono.git] / mcs / class / System.Data / System.Data.SqlClient / SqlDataReader.cs
1 //
2 // System.Data.SqlClient.SqlDataReader.cs
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Daniel Morgan (danmorg@sc.rr.com)
7 //
8 // (C) Ximian, Inc 2002
9 // (C) Daniel Morgan 2002
10 //
11 // Credits:
12 //    SQL and concepts were used from libgda 0.8.190 (GNOME Data Access)\r
13 //    http://www.gnome-db.org/\r
14 //    with permission from the authors of the\r
15 //    PostgreSQL provider in libgda:\r
16 //        Michael Lausch <michael@lausch.at>
17 //        Rodrigo Moya <rodrigo@gnome-db.org>
18 //        Vivien Malerba <malerba@gnome-db.org>
19 //        Gonzalo Paniagua Javier <gonzalo@gnome-db.org>
20 //
21
22 using System;
23 using System.Collections;
24 using System.ComponentModel;
25 using System.Data;
26
27 namespace System.Data.SqlClient {
28         /// <summary>
29         /// Provides a means of reading one or more forward-only streams
30         /// of result sets obtained by executing a command 
31         /// at a SQL database.
32         /// </summary>
33         //public sealed class SqlDataReader : MarshalByRefObject,
34         //      IEnumerable, IDataReader, IDisposable, IDataRecord
35         public sealed class SqlDataReader : IEnumerable, 
36                 IDataReader, IDataRecord {
37                 #region Fields
38
39                 private SqlCommand cmd;
40                 private DataTable table;
41
42                 private object[] fields;
43                 private string[] types; // PostgreSQL Type
44                 private bool[] isNull;
45                                 
46                 private bool open = false;
47                 IntPtr pgResult; // PGresult
48                 private int rows;
49                 private int cols;
50
51                 private int currentRow = -1; // no Read() has been done yet
52
53                 #endregion // Fields
54
55                 #region Constructors
56
57                 internal SqlDataReader (SqlCommand sqlCmd, 
58                         DataTable dataTableSchema, IntPtr pg_result,
59                         int rowCount, int fieldCount, string[] pgtypes) {
60
61                         cmd = sqlCmd;
62                         table = dataTableSchema;
63                         pgResult = pg_result;
64                         rows = rowCount;
65                         cols = fieldCount;
66                         types = pgtypes;
67                         open = true;
68                 }
69
70                 #endregion
71
72                 #region Public Methods
73
74                 [MonoTODO]
75                 public void Close() {
76                         // close result set
77                         PostgresLibrary.PQclear (pgResult);
78                         open = false;
79                         // TODO: change busy state on SqlConnection to not busy
80                 }
81
82                 [MonoTODO]
83                 public DataTable GetSchemaTable() {
84                         return table;
85                 }
86
87                 [MonoTODO]
88                 public bool NextResult() {
89                         throw new NotImplementedException ();
90                 }
91
92                 [MonoTODO]
93                 public bool Read() {
94                         string value;
95                         fields = new object[cols]; // re-init row
96                         DbType dbType;
97
98                         if(currentRow < rows - 1)  {
99                                 currentRow++;
100                                 int c;
101                                 for(c = 0; c < cols; c++) {
102
103                                         // get data value
104                                         value = PostgresLibrary.
105                                                 PQgetvalue(
106                                                 pgResult,
107                                                 currentRow, c);
108
109                                         int columnIsNull;
110                                         // is column NULL?
111                                         columnIsNull = PostgresLibrary.
112                                                 PQgetisnull(pgResult,
113                                                 currentRow, c);
114
115                                         int actualLength;
116                                         // get Actual Length
117                                         actualLength = PostgresLibrary.
118                                                 PQgetlength(pgResult,
119                                                 currentRow, c);
120                                                 
121                                         dbType = PostgresHelper.
122                                                 TypnameToSqlDbType(types[c]);
123
124                                         fields[c] = PostgresHelper.
125                                                 ConvertDbTypeToSystem (
126                                                         dbType,
127                                                         value);
128                                 }
129                                 return true;
130                         }
131                         return false; // EOF
132                 }
133
134                 [MonoTODO]
135                 public byte GetByte(int i) {
136                         throw new NotImplementedException ();
137                 }
138
139                 [MonoTODO]
140                 public long GetBytes(int i, long fieldOffset, 
141                         byte[] buffer, int bufferOffset, 
142                         int length) {
143                         throw new NotImplementedException ();
144                 }
145
146                 [MonoTODO]
147                 public char GetChar(int i) {
148                         throw new NotImplementedException ();
149                 }
150
151                 [MonoTODO]
152                 public long GetChars(int i, long fieldOffset, 
153                         char[] buffer, int bufferOffset, 
154                         int length) {
155                         throw new NotImplementedException ();
156                 }
157
158                 [MonoTODO]
159                 public IDataReader GetData(int i) {
160                         throw new NotImplementedException ();
161                 }
162
163                 [MonoTODO]
164                 public string GetDataTypeName(int i) {
165                         throw new NotImplementedException ();
166                 }
167
168                 [MonoTODO]
169                 public DateTime GetDateTime(int i) {
170                         throw new NotImplementedException ();
171                 }
172
173                 [MonoTODO]
174                 public decimal GetDecimal(int i) {
175                         return (decimal) fields[i];
176                 }
177
178                 [MonoTODO]
179                 public double GetDouble(int i) {
180                         return (double) fields[i];
181                 }
182
183                 [MonoTODO]
184                 public Type GetFieldType(int i) {
185                         throw new NotImplementedException ();
186                 }
187
188                 [MonoTODO]
189                 public float GetFloat(int i) {
190                         return (float) fields[i];
191                 }
192
193                 [MonoTODO]
194                 public Guid GetGuid(int i) {
195                         throw new NotImplementedException ();
196                 }
197
198                 [MonoTODO]
199                 public short GetInt16(int i) {
200                         return (short) fields[i];
201                 }
202
203                 [MonoTODO]
204                 public int GetInt32(int i) {
205                         return (int) fields[i];
206                 }
207
208                 [MonoTODO]
209                 public long GetInt64(int i) {
210                         return (long) fields[i];
211                 }
212
213                 [MonoTODO]
214                 public string GetName(int i) {
215                         return table.Columns[i].ColumnName;
216                 }
217
218                 [MonoTODO]
219                 public int GetOrdinal(string name) {
220                         throw new NotImplementedException ();
221                 }
222
223                 [MonoTODO]
224                 public string GetString(int i) {
225                         return (string) fields[i];
226                 }
227
228                 [MonoTODO]
229                 public object GetValue(int i) {
230                         return fields[i];
231                 }
232
233                 [MonoTODO]
234                 public int GetValues(object[] values) {
235                         throw new NotImplementedException ();
236                 }
237
238                 [MonoTODO]
239                 public bool IsDBNull(int i) {
240                         throw new NotImplementedException ();
241                 }
242
243                 [MonoTODO]
244                 public bool GetBoolean(int i) {
245                         return (bool) fields[i];
246                 }
247
248                 [MonoTODO]
249                 public IEnumerator GetEnumerator() {
250                         throw new NotImplementedException ();
251                 }
252
253                 #endregion // Public Methods
254
255                 #region Destructors
256
257                 [MonoTODO]
258                 public void Dispose () {
259                 }
260
261                 [MonoTODO]
262                 ~SqlDataReader() {
263                 }
264
265                 #endregion // Destructors
266
267                 #region Properties
268
269                 public int Depth {
270                         [MonoTODO]
271                         get { 
272                                 throw new NotImplementedException (); 
273                         }
274                 }
275
276                 public bool IsClosed {
277                         [MonoTODO]
278                         get {
279                                 if(open == false)
280                                         return true;
281                                 else
282                                         return false;
283                         }
284                 }
285
286                 public int RecordsAffected {
287                         [MonoTODO]
288                         get { 
289                                 throw new NotImplementedException (); 
290                         }
291                 }
292         
293                 public int FieldCount {
294                         [MonoTODO]
295                         get { 
296                                 return cols;
297                         }
298                 }
299
300                 public object this[string name] {
301                         [MonoTODO]
302                         get { 
303                                 int i;
304                                 for(i = 0; i < cols; i ++) {
305                                         if(table.Columns[i].ColumnName.Equals(name)) {
306                                                 return fields[i];
307                                         }
308
309                                 }
310         
311                                 for(i = 0; i < cols; i++) {
312                                         string ta;
313                                         string n;
314                                                 
315                                         ta = table.Columns[i].ColumnName.ToUpper();
316                                         n = name.ToUpper();
317                                                 
318                                         if(ta.Equals(n)) {
319                                                 return fields[i];
320                                         }
321                                 }
322                         
323                                 throw new MissingFieldException("Missing field: " + name);
324                         }
325                 }
326
327                 public object this[int i] {
328                         [MonoTODO]
329                         get { 
330                                 return fields[i];
331                         }
332                 }
333
334                 #endregion // Properties
335         }
336 }