2 // System.Data.SqlClient.SqlDataReader.cs
5 // Rodrigo Moya (rodrigo@ximian.com)
6 // Daniel Morgan (danmorg@sc.rr.com)
8 // (C) Ximian, Inc 2002
9 // (C) Daniel Morgan 2002
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>
22 // *** uncomment #define to get debug messages, comment for production ***
23 //#define DEBUG_SqlDataReader
27 using System.Collections;
28 using System.ComponentModel;
31 namespace System.Data.SqlClient {
33 /// Provides a means of reading one or more forward-only streams
34 /// of result sets obtained by executing a command
35 /// at a SQL database.
37 //public sealed class SqlDataReader : MarshalByRefObject,
38 // IEnumerable, IDataReader, IDisposable, IDataRecord
39 public sealed class SqlDataReader : IEnumerable,
40 IDataReader, IDataRecord {
43 private SqlCommand cmd;
44 private DataTable table = null;
47 private object[] fields; // data value in a .NET type
48 private string[] types; // PostgreSQL Type
49 private bool[] isNull; // is NULL?
50 private int[] actualLength; // ActualLength of data
51 private DbType[] dbTypes; // DB data type
52 // actucalLength = -1 is variable-length
54 private bool open = false;
55 IntPtr pgResult; // PGresult
59 private int currentRow = -1; // no Read() has been done yet
65 internal SqlDataReader (SqlCommand sqlCmd) {
73 #region Public Methods
79 // free SqlDataReader resources in SqlCommand
80 // and allow SqlConnection to be used again
83 // TODO: get parameters from result
85 // clear unmanaged PostgreSQL result set
86 PostgresLibrary.PQclear (pgResult);
87 pgResult = IntPtr.Zero;
91 public DataTable GetSchemaTable() {
96 public bool NextResult() {
100 res = cmd.NextResult();
102 if(res.ResultReturned == true) {
104 pgResult = res.PgResult;
106 cols = res.FieldCount;
110 return res.ResultReturned;
119 if(currentRow < rows - 1) {
124 fields = new object[cols];
125 //dbTypes = new DbType[cols];
126 actualLength = new int[cols];
127 isNull = new bool[cols];
129 for(c = 0; c < cols; c++) {
132 dataValue = PostgresLibrary.
138 //isNull[c] = PostgresLibrary.
139 // PQgetisnull(pgResult,
143 actualLength[c] = PostgresLibrary.
144 PQgetlength(pgResult,
148 dbType = PostgresHelper.
149 TypnameToSqlDbType(types[c]);
151 if(dataValue == null) {
155 else if(dataValue.Equals("")) {
161 fields[c] = PostgresHelper.
162 ConvertDbTypeToSystem (
173 public byte GetByte(int i) {
174 throw new NotImplementedException ();
178 public long GetBytes(int i, long fieldOffset,
179 byte[] buffer, int bufferOffset,
181 throw new NotImplementedException ();
185 public char GetChar(int i) {
186 throw new NotImplementedException ();
190 public long GetChars(int i, long fieldOffset,
191 char[] buffer, int bufferOffset,
193 throw new NotImplementedException ();
197 public IDataReader GetData(int i) {
198 throw new NotImplementedException ();
202 public string GetDataTypeName(int i) {
207 public DateTime GetDateTime(int i) {
208 return (DateTime) fields[i];
212 public decimal GetDecimal(int i) {
213 return (decimal) fields[i];
217 public double GetDouble(int i) {
218 return (double) fields[i];
222 public Type GetFieldType(int i) {
224 return table.Columns[i].DataType;
228 public float GetFloat(int i) {
229 return (float) fields[i];
233 public Guid GetGuid(int i) {
234 throw new NotImplementedException ();
238 public short GetInt16(int i) {
239 return (short) fields[i];
243 public int GetInt32(int i) {
244 return (int) fields[i];
248 public long GetInt64(int i) {
249 return (long) fields[i];
253 public string GetName(int i) {
254 return table.Columns[i].ColumnName;
258 public int GetOrdinal(string name) {
260 for(i = 0; i < cols; i ++) {
261 if(table.Columns[i].ColumnName.Equals(name)) {
267 for(i = 0; i < cols; i++) {
271 ta = table.Columns[i].ColumnName.ToUpper();
279 throw new MissingFieldException("Missing field: " + name);
283 public string GetString(int i) {
284 return (string) fields[i];
288 public object GetValue(int i) {
293 public int GetValues(object[] values)
295 Array.Copy (fields, values, fields.Length);
296 return fields.Length;
300 public bool IsDBNull(int i) {
305 public bool GetBoolean(int i) {
306 return (bool) fields[i];
310 public IEnumerator GetEnumerator() {
311 throw new NotImplementedException ();
314 #endregion // Public Methods
319 public void Dispose () {
326 #endregion // Destructors
333 throw new NotImplementedException ();
337 public bool IsClosed {
347 public int RecordsAffected {
350 throw new NotImplementedException ();
354 public int FieldCount {
361 public object this[string name] {
365 for(i = 0; i < cols; i ++) {
366 if(table.Columns[i].ColumnName.Equals(name)) {
372 for(i = 0; i < cols; i++) {
376 ta = table.Columns[i].ColumnName.ToUpper();
384 throw new MissingFieldException("Missing field: " + name);
388 public object this[int i] {
395 #endregion // Properties