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;
47 private CommandBehavior _behavior;
48 private Boolean _isClosed;
52 // Logging related values
53 private static readonly String CLASSNAME = "NpgsqlDataReader";
55 internal NpgsqlDataReader( ArrayList resultsets, ArrayList responses, NpgsqlConnection connection, CommandBehavior behavior)
57 _resultsets = resultsets;
58 _responses = responses;
59 _connection = connection;
63 if (_resultsets.Count > 0)
64 _currentResultset = (NpgsqlResultSet)_resultsets[_resultsetIndex];
71 private Boolean CanRead()
73 //NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "CanRead");
74 /*if (_currentResultset == null)
76 return ((_currentResultset != null) &&
77 (_currentResultset.Count > 0) &&
78 (_rowIndex < _currentResultset.Count));
82 private void CheckCanRead()
85 throw new InvalidOperationException("Cannot read data");
94 /// Releases the resources used by the <see cref="Npgsql.NpgsqlCommand">NpgsqlCommand</see>.
96 protected void Dispose (bool disposing)
98 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Dispose");
108 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "Depth");
113 public Boolean IsClosed
117 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "IsClosed");
122 public Int32 RecordsAffected
126 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "RecordsAffected");
132 String[] _returnStringTokens = ((String)_responses[_resultsetIndex]).Split(null); // whitespace separator.
135 return Int32.Parse(_returnStringTokens[_returnStringTokens.Length - 1]);
137 catch (FormatException) {
146 if ((_behavior & CommandBehavior.CloseConnection) == CommandBehavior.CloseConnection)
154 public Boolean NextResult()
156 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "NextResult");
158 if((_resultsetIndex + 1) < _resultsets.Count)
162 _currentResultset = (NpgsqlResultSet)_resultsets[_resultsetIndex];
170 public Boolean Read()
172 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Read");
183 public DataTable GetSchemaTable()
185 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetSchemaTable");
187 if(_currentResultsetSchema == null)
188 _currentResultsetSchema = GetResultsetSchema();
190 return _currentResultsetSchema;
195 public Int32 FieldCount
200 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "FieldCount");
202 if (_currentResultset == null) //Executed a non return rows query.
205 return _currentResultset.RowDescription.NumFields;
212 public String GetName(Int32 i)
214 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetName");
216 if (_currentResultset == null)
219 return _currentResultset.RowDescription[i].name;
222 public String GetDataTypeName(Int32 i)
224 // FIXME: have a type name instead of the oid
225 if (_currentResultset == null)
228 return (_currentResultset.RowDescription[i].type_oid).ToString();
231 public Type GetFieldType(Int32 i)
233 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetFieldType");
236 if (_currentResultset == null)
239 return NpgsqlTypesHelper.GetSystemTypeFromTypeOid(_connection.OidToNameMapping, _currentResultset.RowDescription[i].type_oid);
242 public Object GetValue(Int32 i)
244 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetValue");
249 throw new IndexOutOfRangeException("Cannot read data. Column less than 0 specified.");
251 throw new InvalidOperationException("Cannot read data. DataReader not initialized. Maybe you forgot to call Read()?");
252 return ((NpgsqlAsciiRow)_currentResultset[_rowIndex])[i];
258 public Int32 GetValues(Object[] values)
260 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetValues");
264 // Only the number of elements in the array are filled.
265 // It's also possible to pass an array with more that FieldCount elements.
266 Int32 maxColumnIndex = (values.Length < FieldCount) ? values.Length : FieldCount;
268 for (Int32 i = 0; i < maxColumnIndex; i++)
269 values[i] = GetValue(i);
271 return maxColumnIndex;
275 public Int32 GetOrdinal(String name)
278 return _currentResultset.RowDescription.FieldIndex(name);
281 public Object this [ Int32 i ]
285 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, i);
290 public Object this [ String name ]
294 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, name);
295 return GetValue(_currentResultset.RowDescription.FieldIndex(name));
299 public Boolean GetBoolean(Int32 i)
301 // Should this be done using the GetValue directly and not by converting to String
302 // and parsing from there?
303 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBoolean");
306 return (Boolean) GetValue(i);
310 public Byte GetByte(Int32 i)
312 throw new NotImplementedException();
315 public Int64 GetBytes(Int32 i, Int64 fieldOffset, Byte[] buffer, Int32 bufferoffset, Int32 length)
320 result = (Byte[]) GetValue(i);
322 // [TODO] Implement blob support.
325 result.CopyTo(buffer, 0);
328 return result.Length;
332 public Char GetChar(Int32 i)
334 throw new NotImplementedException();
337 public Int64 GetChars(Int32 i, Int64 fieldoffset, Char[] buffer, Int32 bufferoffset, Int32 length)
345 str.ToCharArray(bufferoffset, length).CopyTo(buffer, 0);
346 return buffer.GetLength(0);
349 public Guid GetGuid(Int32 i)
351 throw new NotImplementedException();
354 public Int16 GetInt16(Int32 i)
356 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt16");
358 return (Int16) GetValue(i);
363 public Int32 GetInt32(Int32 i)
365 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt32");
367 return (Int32) GetValue(i);
372 public Int64 GetInt64(Int32 i)
374 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt64");
376 return (Int64) GetValue(i);
379 public Single GetFloat(Int32 i)
381 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetFloat");
383 return (Single) GetValue(i);
386 public Double GetDouble(Int32 i)
388 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDouble");
390 return (Double) GetValue(i);
393 public String GetString(Int32 i)
395 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetString");
397 return (String) GetValue(i);
400 public Decimal GetDecimal(Int32 i)
402 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDecimal");
404 return (Decimal) GetValue(i);
407 public DateTime GetDateTime(Int32 i)
409 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDateTime");
411 return (DateTime) GetValue(i);
414 public IDataReader GetData(Int32 i)
416 throw new NotImplementedException();
419 public Boolean IsDBNull(Int32 i)
421 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IsDBNull");
425 return ((NpgsqlAsciiRow)_currentResultset[_rowIndex]).IsDBNull(i);
428 private DataTable GetResultsetSchema()
431 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetResultsetSchema");
432 DataTable result = null;
434 NpgsqlRowDescription rd = _currentResultset.RowDescription;
435 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++)
469 row = result.NewRow();
471 row["ColumnName"] = GetName(i);
472 row["ColumnOrdinal"] = i + 1;
473 row["ColumnSize"] = (int) rd[i].type_size;
474 row["NumericPrecision"] = 0;
475 row["NumericScale"] = 0;
476 row["IsUnique"] = false;
477 row["IsKey"] = DBNull.Value;
478 row["BaseCatalogName"] = "";
479 row["BaseColumnName"] = GetName(i);
480 row["BaseSchemaName"] = "";
481 row["BaseTableName"] = "";
482 row["DataType"] = GetFieldType(i);
483 row["AllowDBNull"] = false;
484 row["ProviderType"] = (int) rd[i].type_oid;
485 row["IsAliased"] = false;
486 row["IsExpression"] = false;
487 row["IsIdentity"] = false;
488 row["IsAutoIncrement"] = false;
489 row["IsRowVersion"] = false;
490 row["IsHidden"] = false;
491 row["IsLong"] = false;
492 row["IsReadOnly"] = false;
494 result.Rows.Add(row);
504 IEnumerator IEnumerable.GetEnumerator ()
506 return new System.Data.Common.DbEnumerator (this);