2002-08-16 Rodrigo Moya <rodrigo@ximian.com>
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbDataReader.cs
1 //
2 // System.Data.OleDb.OleDbDataReader
3 //
4 // Author:
5 //   Rodrigo Moya (rodrigo@ximian.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Rodrigo Moya, 2002
9 // Copyright (C) Tim Coleman, 2002
10 //
11
12 using System.Collections;
13 using System.ComponentModel;
14 using System.Data;
15 using System.Data.Common;
16
17 namespace System.Data.OleDb
18 {
19         public sealed class OleDbDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
20         {
21                 #region Fields
22                 
23                 private OleDbCommand command;
24                 private bool open;
25                 private ArrayList gdaResults;
26                 private int currentResult;
27                 private int currentRow;
28
29                 #endregion
30
31                 #region Constructors
32
33                 internal OleDbDataReader (OleDbCommand command, ArrayList results) 
34                 {
35                         this.command = command;
36                         open = true;
37                         if (results != null)
38                                 gdaResults = results;
39                         else
40                                 gdaResults = new ArrayList ();
41                         currentResult = -1;
42                         currentRow = -1;
43                 }
44
45                 #endregion
46
47                 #region Properties
48
49                 public int Depth {
50                         [MonoTODO]
51                         get {
52                                 throw new NotImplementedException ();
53                         }
54                 }
55
56                 public int FieldCount {
57                         get {
58                                 if (currentResult < 0 ||
59                                     currentResult >= gdaResults.Count)
60                                         return 0;
61
62                                 return libgda.gda_data_model_get_n_columns (
63                                         (IntPtr) gdaResults[currentResult]);
64                         }
65                 }
66
67                 public bool IsClosed {
68                         get {
69                                 return !open;
70                         }
71                 }
72
73                 public object this[string name] {
74                         [MonoTODO]
75                         get {
76                                 throw new NotImplementedException ();
77                         }
78                 }
79
80                 public object this[int index] {
81                         get {
82                                 if (currentResult < 0 ||
83                                     currentResult >= gdaResults.Count)
84                                         return null;
85                                 
86                                 return libgda.gda_data_model_get_value_at (
87                                                 (IntPtr) gdaResults[currentResult],
88                                                 index,
89                                                 currentRow);
90                         }
91                 }
92
93                 public int RecordsAffected {
94                         get {
95                                 int total_rows;
96                                 
97                                 if (currentResult < 0 ||
98                                     currentResult >= gdaResults.Count)
99                                         return 0;
100
101                                 total_rows = libgda.gda_data_model_get_n_rows (
102                                         (IntPtr) gdaResults[currentResult]);
103                                 if (total_rows > 0) {
104                                         if (FieldCount > 0) {
105                                                 // It's a SELECT statement
106                                                 return -1;
107                                         }
108                                 }
109
110                                 return FieldCount > 0 ? -1 : total_rows;
111                         }
112                 }
113
114                 #endregion
115
116                 #region Methods
117
118                 [MonoTODO]
119                 public void Close ()
120                 {
121                         throw new NotImplementedException ();
122                 }
123
124                 [MonoTODO]
125                 ~OleDbDataReader ()
126                 {
127                         throw new NotImplementedException ();
128                 }
129
130                 public bool GetBoolean (int ordinal)
131                 {
132                         IntPtr value;
133
134                         if (currentResult == -1)
135                                 throw new InvalidCastException ();
136
137                         value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
138                                                                     ordinal, currentRow);
139                         if (value == IntPtr.Zero)
140                                 throw new InvalidCastException ();
141                         
142                         if (libgda.gda_value_get_vtype (value) != GdaValueType.Boolean)
143                                 throw new InvalidCastException ();
144                         return libgda.gda_value_get_boolean (value);
145                 }
146
147                 public byte GetByte (int ordinal)
148                 {
149                         IntPtr value;
150
151                         if (currentResult == -1)
152                                 throw new InvalidCastException ();
153
154                         value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
155                                                                     ordinal, currentRow);
156                         if (value == IntPtr.Zero)
157                                 throw new InvalidCastException ();
158                         
159                         if (libgda.gda_value_get_vtype (value) != GdaValueType.Tinyint)
160                                 throw new InvalidCastException ();
161                         return libgda.gda_value_get_tinyint (value);
162                 }
163
164                 [MonoTODO]
165                 public long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
166                 {
167                         throw new NotImplementedException ();
168                 }
169                 
170                 public char GetChar (int ordinal)
171                 {
172                         IntPtr value;
173
174                         if (currentResult == -1)
175                                 throw new InvalidCastException ();
176
177                         value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
178                                                                     ordinal, currentRow);
179                         if (value == IntPtr.Zero)
180                                 throw new InvalidCastException ();
181                         
182                         if (libgda.gda_value_get_vtype (value) != GdaValueType.Tinyint)
183                                 throw new InvalidCastException ();
184                         return (char) libgda.gda_value_get_tinyint (value);
185                 }
186
187                 [MonoTODO]
188                 public long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
189                 {
190                         throw new NotImplementedException ();
191                 }
192
193                 [MonoTODO]
194                 public OleDbDataReader GetData (int ordinal)
195                 {
196                         throw new NotImplementedException ();
197                 }
198
199                 public string GetDataTypeName (int index)
200                 {
201                         IntPtr value;
202
203                         if (currentResult == -1)
204                                 return "unknown";
205
206                         value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
207                                                                     index, currentRow);
208                         if (value == IntPtr.Zero)
209                                 return "unknown";
210
211                         return libgda.gda_type_to_string (libgda.gda_value_get_vtype (value));
212                 }
213
214                 [MonoTODO]
215                 public DateTime GetDateTime (int ordinal)
216                 {
217                         throw new NotImplementedException ();
218                 }
219
220                 [MonoTODO]
221                 public decimal GetDecimal (int ordinal)
222                 {
223                         throw new NotImplementedException ();
224                 }
225
226                 [MonoTODO]
227                 public double GetDouble (int ordinal)
228                 {
229                         throw new NotImplementedException ();
230                 }
231
232                 [MonoTODO]
233                 public Type GetFieldType (int index)
234                 {
235                         throw new NotImplementedException ();
236                 }
237
238                 [MonoTODO]
239                 public float GetFloat (int ordinal)
240                 {
241                         throw new NotImplementedException ();
242                 }
243
244                 [MonoTODO]
245                 public Guid GetGuid (int ordinal)
246                 {
247                         throw new NotImplementedException ();
248                 }
249
250                 [MonoTODO]
251                 public short GetInt16 (int ordinal)
252                 {
253                         throw new NotImplementedException ();
254                 }
255
256                 [MonoTODO]
257                 public int GetInt32 (int ordinal)
258                 {
259                         throw new NotImplementedException ();
260                 }
261
262                 [MonoTODO]
263                 public long GetInt64 (int ordinal)
264                 {
265                         throw new NotImplementedException ();
266                 }
267
268                 [MonoTODO]
269                 public string GetName (int index)
270                 {
271                         throw new NotImplementedException ();
272                 }
273
274                 [MonoTODO]
275                 public int GetOrdinal (string name)
276                 {
277                         throw new NotImplementedException ();
278                 }
279
280                 [MonoTODO]
281                 public DataTable GetSchemaTable ()
282                 {
283                         throw new NotImplementedException ();
284                 }
285
286                 [MonoTODO]
287                 public string GetString (int ordinal)
288                 {
289                         throw new NotImplementedException ();
290                 }
291
292                 [MonoTODO]
293                 public TimeSpan GetTimeSpan (int ordinal)
294                 {
295                         throw new NotImplementedException ();
296                 }
297
298                 public object GetValue (int ordinal)
299                 {
300                         IntPtr value;
301                         GdaValueType type;
302
303                         if (currentResult == -1)
304                                 throw new IndexOutOfRangeException ();
305
306                         value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
307                                                                     ordinal, currentRow);
308                         if (value == IntPtr.Zero)
309                                 throw new IndexOutOfRangeException ();
310
311                         type = libgda.gda_value_get_vtype (value);
312                         // FIXME: return correct type
313
314                         return (object) libgda.gda_value_stringify (value);
315                 }
316
317                 [MonoTODO]
318                 public int GetValues (object[] values)
319                 {
320                         throw new NotImplementedException ();
321                 }
322
323                 [MonoTODO]
324                 IDataReader IDataRecord.GetData (int ordinal)
325                 {
326                         throw new NotImplementedException ();
327                 }
328
329                 [MonoTODO]
330                 void IDisposable.Dispose ()
331                 {
332                         throw new NotImplementedException ();
333                 }
334
335                 [MonoTODO]
336                 IEnumerator IEnumerable.GetEnumerator ()
337                 {
338                         throw new NotImplementedException ();
339                 }
340
341                 [MonoTODO]
342                 public bool IsDBNull (int ordinal)
343                 {
344                         throw new NotImplementedException ();
345                 }
346
347                 public bool NextResult ()
348                 {
349                         int i = currentResult + 1;
350                         if (i >= 0 && i < gdaResults.Count) {
351                                 currentResult++;
352                                 return true;
353                         }
354
355                         return false;
356                 }
357
358                 public bool Read ()
359                 {
360                         if (currentResult < 0 ||
361                             currentResult >= gdaResults.Count)
362                                 return false;
363
364                         currentRow++;
365                         if (currentRow <
366                             libgda.gda_data_model_get_n_rows ((IntPtr) gdaResults[currentResult]))
367                                 return true;
368
369                         return false;
370                 }
371
372                 #endregion
373         }
374 }