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.
134 return Int32.Parse(_returnStringTokens[_returnStringTokens.Length - 1]);
141 if ((_behavior & CommandBehavior.CloseConnection) == CommandBehavior.CloseConnection)
149 public Boolean NextResult()
151 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "NextResult");
153 if((_resultsetIndex + 1) < _resultsets.Count)
157 _currentResultset = (NpgsqlResultSet)_resultsets[_resultsetIndex];
165 public Boolean Read()
167 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "Read");
178 public DataTable GetSchemaTable()
180 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetSchemaTable");
182 if(_currentResultsetSchema == null)
183 _currentResultsetSchema = GetResultsetSchema();
185 return _currentResultsetSchema;
190 public Int32 FieldCount
195 NpgsqlEventLog.LogPropertyGet(LogLevel.Debug, CLASSNAME, "FieldCount");
197 if (_currentResultset == null) //Executed a non return rows query.
200 return _currentResultset.RowDescription.NumFields;
207 public String GetName(Int32 i)
209 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetName");
211 if (_currentResultset == null)
214 return _currentResultset.RowDescription[i].name;
217 public String GetDataTypeName(Int32 i)
219 // FIXME: have a type name instead of the oid
220 if (_currentResultset == null)
223 return (_currentResultset.RowDescription[i].type_oid).ToString();
226 public Type GetFieldType(Int32 i)
228 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetFieldType");
231 if (_currentResultset == null)
234 return NpgsqlTypesHelper.GetSystemTypeFromTypeOid(_connection.OidToNameMapping, _currentResultset.RowDescription[i].type_oid);
237 public Object GetValue(Int32 i)
239 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetValue");
244 throw new InvalidOperationException("Cannot read data. Column less than 0 specified.");
246 throw new InvalidOperationException("Cannot read data. DataReader not initialized. Maybe you forgot to call Read()?");
247 return ((NpgsqlAsciiRow)_currentResultset[_rowIndex])[i];
253 public Int32 GetValues(Object[] values)
255 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetValues");
259 // Only the number of elements in the array are filled.
260 // It's also possible to pass an array with more that FieldCount elements.
261 Int32 maxColumnIndex = (values.Length < FieldCount) ? values.Length : FieldCount;
263 for (Int32 i = 0; i < maxColumnIndex; i++)
264 values[i] = GetValue(i);
266 return maxColumnIndex;
270 public Int32 GetOrdinal(String name)
273 return _currentResultset.RowDescription.FieldIndex(name);
276 public Object this [ Int32 i ]
280 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, i);
285 public Object this [ String name ]
289 //throw new NotImplementedException();
290 NpgsqlEventLog.LogIndexerGet(LogLevel.Debug, CLASSNAME, name);
291 return GetValue(_currentResultset.RowDescription.FieldIndex(name));
295 public Boolean GetBoolean(Int32 i)
297 // Should this be done using the GetValue directly and not by converting to String
298 // and parsing from there?
299 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetBoolean");
302 return (Boolean) GetValue(i);
306 public Byte GetByte(Int32 i)
308 throw new NotImplementedException();
311 public Int64 GetBytes(Int32 i, Int64 fieldOffset, Byte[] buffer, Int32 bufferoffset, Int32 length)
316 result = (Byte[]) GetValue(i);
318 // [TODO] Implement blob support.
321 result.CopyTo(buffer, 0);
324 return result.Length;
328 public Char GetChar(Int32 i)
330 throw new NotImplementedException();
333 public Int64 GetChars(Int32 i, Int64 fieldoffset, Char[] buffer, Int32 bufferoffset, Int32 length)
341 str.ToCharArray(bufferoffset, length).CopyTo(buffer, 0);
342 return buffer.GetLength(0);
345 public Guid GetGuid(Int32 i)
347 throw new NotImplementedException();
350 public Int16 GetInt16(Int32 i)
352 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt16");
354 return (Int16) GetValue(i);
359 public Int32 GetInt32(Int32 i)
361 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt32");
363 return (Int32) GetValue(i);
368 public Int64 GetInt64(Int32 i)
370 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetInt64");
372 return (Int64) GetValue(i);
375 public Single GetFloat(Int32 i)
377 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetFloat");
379 return (Single) GetValue(i);
382 public Double GetDouble(Int32 i)
384 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDouble");
386 return (Double) GetValue(i);
389 public String GetString(Int32 i)
391 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetString");
393 return (String) GetValue(i);
396 public Decimal GetDecimal(Int32 i)
398 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDecimal");
400 return (Decimal) GetValue(i);
403 public DateTime GetDateTime(Int32 i)
405 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetDateTime");
407 return (DateTime) GetValue(i);
410 public IDataReader GetData(Int32 i)
412 throw new NotImplementedException();
415 public Boolean IsDBNull(Int32 i)
417 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "IsDBNull");
421 return ((NpgsqlAsciiRow)_currentResultset[_rowIndex]).IsDBNull(i);
424 private DataTable GetResultsetSchema()
427 NpgsqlEventLog.LogMethodEnter(LogLevel.Debug, CLASSNAME, "GetResultsetSchema");
428 DataTable result = null;
430 NpgsqlRowDescription rd = _currentResultset.RowDescription;
431 Int16 numFields = rd.NumFields;
434 result = new DataTable("SchemaTable");
436 result.Columns.Add ("ColumnName", typeof (string));
437 result.Columns.Add ("ColumnOrdinal", typeof (int));
438 result.Columns.Add ("ColumnSize", typeof (int));
439 result.Columns.Add ("NumericPrecision", typeof (int));
440 result.Columns.Add ("NumericScale", typeof (int));
441 result.Columns.Add ("IsUnique", typeof (bool));
442 result.Columns.Add ("IsKey", typeof (bool));
443 DataColumn dc = result.Columns["IsKey"];
444 dc.AllowDBNull = true; // IsKey can have a DBNull
445 result.Columns.Add ("BaseCatalogName", typeof (string));
446 result.Columns.Add ("BaseColumnName", typeof (string));
447 result.Columns.Add ("BaseSchemaName", typeof (string));
448 result.Columns.Add ("BaseTableName", typeof (string));
449 result.Columns.Add ("DataType", typeof(Type));
450 result.Columns.Add ("AllowDBNull", typeof (bool));
451 result.Columns.Add ("ProviderType", typeof (int));
452 result.Columns.Add ("IsAliased", typeof (bool));
453 result.Columns.Add ("IsExpression", typeof (bool));
454 result.Columns.Add ("IsIdentity", typeof (bool));
455 result.Columns.Add ("IsAutoIncrement", typeof (bool));
456 result.Columns.Add ("IsRowVersion", typeof (bool));
457 result.Columns.Add ("IsHidden", typeof (bool));
458 result.Columns.Add ("IsLong", typeof (bool));
459 result.Columns.Add ("IsReadOnly", typeof (bool));
463 for (Int16 i = 0; i < numFields; i++)
465 row = result.NewRow();
467 row["ColumnName"] = GetName(i);
468 row["ColumnOrdinal"] = i + 1;
469 row["ColumnSize"] = (int) rd[i].type_size;
470 row["NumericPrecision"] = 0;
471 row["NumericScale"] = 0;
472 row["IsUnique"] = false;
473 row["IsKey"] = DBNull.Value;
474 row["BaseCatalogName"] = "";
475 row["BaseColumnName"] = GetName(i);
476 row["BaseSchemaName"] = "";
477 row["BaseTableName"] = "";
478 row["DataType"] = GetFieldType(i);
479 row["AllowDBNull"] = false;
480 row["ProviderType"] = (int) rd[i].type_oid;
481 row["IsAliased"] = false;
482 row["IsExpression"] = false;
483 row["IsIdentity"] = false;
484 row["IsAutoIncrement"] = false;
485 row["IsRowVersion"] = false;
486 row["IsHidden"] = false;
487 row["IsLong"] = false;
488 row["IsReadOnly"] = false;
490 result.Rows.Add(row);
500 IEnumerator IEnumerable.GetEnumerator ()
502 return new System.Data.Common.DbEnumerator (this);