2 // Npgsql.NpgsqlDataReader.cs
5 // Francisco Jr. (fxjrlists@yahoo.com.br)
7 // Copyright (C) 2002 The Npgsql Development Team
8 // npgsql-general@gborg.postgresql.org
9 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
12 // This library is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU Lesser General Public
14 // License as published by the Free Software Foundation; either
15 // version 2.1 of the License, or (at your option) any later version.
17 // This library is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 using System.Collections;
35 public class NpgsqlDataReader : IDataReader, IEnumerable
40 private NpgsqlConnection _connection;
41 private ArrayList _resultsets;
42 private ArrayList _responses;
43 private Int32 _rowIndex;
44 private Int32 _resultsetIndex;
45 private NpgsqlResultSet _currentResultset;
46 private DataTable _currentResultsetSchema;
49 // Logging related values
50 private static readonly String CLASSNAME = "NpgsqlDataReader";
52 internal NpgsqlDataReader( ArrayList resultsets, ArrayList responses, NpgsqlConnection connection)
54 _resultsets = resultsets;
55 _responses = responses;
56 _connection = connection;
59 if (_resultsets.Count > 0)
60 _currentResultset = (NpgsqlResultSet)_resultsets[_resultsetIndex];
66 private Boolean CanRead()
68 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CanRead");
69 /*if (_currentResultset == null)
71 return ((_currentResultset != null) && (_currentResultset.Count > 0));
75 private void CheckCanRead()
78 throw new InvalidOperationException("Cannot read data");
89 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Depth");
94 public Boolean IsClosed
98 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsClosed");
103 public Int32 RecordsAffected
107 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "RecordsAffected");
109 /*if (_currentResultset == null)
110 return 0; //[FIXME] Get the actual number of rows deleted, updated or inserted.
117 String[] ret_string_tokens = ((String)_responses[_resultsetIndex]).Split(null); // whitespace separator.
119 return Int32.Parse(ret_string_tokens[ret_string_tokens.Length - 1]);
129 public Boolean NextResult()
131 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "NextResult");
132 //throw new NotImplementedException();
134 //[FIXME] Should the currentResultset not be modified
135 // in case there aren't any more resultsets?
136 // SqlClient modify to a invalid resultset and throws exceptions
137 // when trying to access any data.
140 if((_resultsetIndex + 1) < _resultsets.Count)
144 _currentResultset = (NpgsqlResultSet)_resultsets[_resultsetIndex];
152 public Boolean Read()
154 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Read");
160 return (_rowIndex < _currentResultset.Count);
163 public DataTable GetSchemaTable()
165 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetSchemaTable");
167 if(_currentResultsetSchema == null)
168 _currentResultsetSchema = GetResultsetSchema();
170 return _currentResultsetSchema;
175 public Int32 FieldCount
180 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "FieldCount");
181 //return ((_currentResultset == null) ? 0 : _currentResultset.RowDescription.NumFields);
184 return _currentResultset.RowDescription.NumFields;
188 if (_currentResultset == null) //Executed a non return rows query.
191 return _currentResultset.RowDescription.NumFields;
198 public String GetName(Int32 i)
200 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetName");
202 if (_currentResultset == null)
205 return _currentResultset.RowDescription[i].name;
208 public String GetDataTypeName(Int32 i)
210 // FIXME: have a type name instead of the oid
211 if (_currentResultset == null)
214 return (_currentResultset.RowDescription[i].type_oid).ToString();
217 public Type GetFieldType(Int32 i)
219 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetFieldType");
222 if (_currentResultset == null)
225 return NpgsqlTypesHelper.GetSystemTypeFromTypeOid(_connection.OidToNameMapping, _currentResultset.RowDescription[i].type_oid);
228 public Object GetValue(Int32 i)
230 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetValue");
234 if (i < 0 || _rowIndex < 0)
235 throw new InvalidOperationException("Cannot read data.");
236 return ((NpgsqlAsciiRow)_currentResultset[_rowIndex])[i];
242 public Int32 GetValues(Object[] values)
244 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetValues");
248 // Only the number of elements in the array are filled.
249 // It's also possible to pass an array with more that FieldCount elements.
250 Int32 maxColumnIndex = (values.Length < FieldCount) ? values.Length : FieldCount;
252 for (Int32 i = 0; i < maxColumnIndex; i++)
253 values[i] = GetValue(i);
255 return maxColumnIndex;
259 public Int32 GetOrdinal(String name)
262 return _currentResultset.RowDescription.FieldIndex(name);
265 public Object this [ Int32 i ]
269 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, i);
274 public Object this [ String name ]
278 //throw new NotImplementedException();
279 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, name);
280 return GetValue(_currentResultset.RowDescription.FieldIndex(name));
284 public Boolean GetBoolean(Int32 i)
286 // Should this be done using the GetValue directly and not by converting to String
287 // and parsing from there?
288 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBoolean");
291 return (Boolean) GetValue(i);
295 public Byte GetByte(Int32 i)
297 throw new NotImplementedException();
300 public Int64 GetBytes(Int32 i, Int64 fieldOffset, Byte[] buffer, Int32 bufferoffset, Int32 length)
305 result = (Byte[]) GetValue(i);
307 // [TODO] Implement blob support.
310 result.CopyTo(buffer, 0);
313 return result.Length;
317 public Char GetChar(Int32 i)
319 throw new NotImplementedException();
322 public Int64 GetChars(Int32 i, Int64 fieldoffset, Char[] buffer, Int32 bufferoffset, Int32 length)
330 str.ToCharArray(bufferoffset, length).CopyTo(buffer, 0);
331 return buffer.GetLength(0);
334 public Guid GetGuid(Int32 i)
336 throw new NotImplementedException();
339 public Int16 GetInt16(Int32 i)
341 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt16");
343 return (Int16) GetValue(i);
348 public Int32 GetInt32(Int32 i)
350 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt32");
352 return (Int32) GetValue(i);
357 public Int64 GetInt64(Int32 i)
359 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt64");
361 return (Int64) GetValue(i);
364 public Single GetFloat(Int32 i)
366 // Should this be done using the GetValue directly and not by converting to String
367 // and parsing from there?
368 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetFloat");
371 return Single.Parse((String) this[i]);
372 } catch (System.FormatException)
374 throw new System.InvalidCastException();
376 return (Single) GetValue(i);
379 public Double GetDouble(Int32 i)
381 // Should this be done using the GetValue directly and not by converting to String
382 // and parsing from there?
383 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDouble");
386 return Double.Parse((String) this[i]);
387 } catch (System.FormatException)
389 throw new System.InvalidCastException();
391 return (Double) GetValue(i);
394 public String GetString(Int32 i)
396 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetString");
398 return (String) GetValue(i);
401 public Decimal GetDecimal(Int32 i)
403 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDecimal");
405 return (Decimal) GetValue(i);
408 public DateTime GetDateTime(Int32 i)
410 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDateTime");
412 return (DateTime) GetValue(i);
415 public IDataReader GetData(Int32 i)
417 throw new NotImplementedException();
420 public Boolean IsDBNull(Int32 i)
422 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IsDBNull");
426 return ((NpgsqlAsciiRow)_currentResultset[_rowIndex]).IsNull(i);
429 private DataTable GetResultsetSchema()
432 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetResultsetSchema");
433 DataTable result = null;
435 NpgsqlRowDescription rd = _currentResultset.RowDescription;
436 Int16 numFields = rd.NumFields;
438 result = new DataTable("SchemaTable");
440 result.Columns.Add ("ColumnName", typeof (string));
441 result.Columns.Add ("ColumnOrdinal", typeof (int));
442 result.Columns.Add ("ColumnSize", typeof (int));
443 result.Columns.Add ("NumericPrecision", typeof (int));
444 result.Columns.Add ("NumericScale", typeof (int));
445 result.Columns.Add ("IsUnique", typeof (bool));
446 result.Columns.Add ("IsKey", typeof (bool));
447 DataColumn dc = result.Columns["IsKey"];
448 dc.AllowDBNull = true; // IsKey can have a DBNull
449 result.Columns.Add ("BaseCatalogName", typeof (string));
450 result.Columns.Add ("BaseColumnName", typeof (string));
451 result.Columns.Add ("BaseSchemaName", typeof (string));
452 result.Columns.Add ("BaseTableName", typeof (string));
453 result.Columns.Add ("DataType", typeof(Type));
454 result.Columns.Add ("AllowDBNull", typeof (bool));
455 result.Columns.Add ("ProviderType", typeof (int));
456 result.Columns.Add ("IsAliased", typeof (bool));
457 result.Columns.Add ("IsExpression", typeof (bool));
458 result.Columns.Add ("IsIdentity", typeof (bool));
459 result.Columns.Add ("IsAutoIncrement", typeof (bool));
460 result.Columns.Add ("IsRowVersion", typeof (bool));
461 result.Columns.Add ("IsHidden", typeof (bool));
462 result.Columns.Add ("IsLong", typeof (bool));
463 result.Columns.Add ("IsReadOnly", typeof (bool));
467 for (Int16 i = 0; i < numFields; i++) {
468 row = result.NewRow();
470 row["ColumnName"] = GetName(i);
471 row["ColumnOrdinal"] = i + 1;
472 row["ColumnSize"] = (int) rd[i].type_size;
473 row["NumericPrecision"] = 0;
474 row["NumericScale"] = 0;
475 row["IsUnique"] = false;
476 row["IsKey"] = DBNull.Value;
477 row["BaseCatalogName"] = "";
478 row["BaseColumnName"] = GetName(i);
479 row["BaseSchemaName"] = "";
480 row["BaseTableName"] = "";
481 row["DataType"] = GetFieldType(i);
482 row["AllowDBNull"] = false;
483 row["ProviderType"] = (int) rd[i].type_oid;
484 row["IsAliased"] = false;
485 row["IsExpression"] = false;
486 row["IsIdentity"] = false;
487 row["IsAutoIncrement"] = false;
488 row["IsRowVersion"] = false;
489 row["IsHidden"] = false;
490 row["IsLong"] = false;
491 row["IsReadOnly"] = false;
493 result.Rows.Add(row);
503 IEnumerator IEnumerable.GetEnumerator ()
505 return new System.Data.Common.DbEnumerator (this);