| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- //
- // System.Data.SqlClient.SqlDataReader.cs
- //
- // Author:
- // Rodrigo Moya ([email protected])
- // Daniel Morgan ([email protected])
- //
- // (C) Ximian, Inc 2002
- // (C) Daniel Morgan 2002
- //
- // Credits:
- // SQL and concepts were used from libgda 0.8.190 (GNOME Data Access)
- // http://www.gnome-db.org/
- // with permission from the authors of the
- // PostgreSQL provider in libgda:
- // Michael Lausch <[email protected]>
- // Rodrigo Moya <[email protected]>
- // Vivien Malerba <[email protected]>
- // Gonzalo Paniagua Javier <[email protected]>
- //
- // *** uncomment #define to get debug messages, comment for production ***
- //#define DEBUG_SqlDataReader
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Data;
- namespace System.Data.SqlClient {
- /// <summary>
- /// Provides a means of reading one or more forward-only streams
- /// of result sets obtained by executing a command
- /// at a SQL database.
- /// </summary>
- //public sealed class SqlDataReader : MarshalByRefObject,
- // IEnumerable, IDataReader, IDisposable, IDataRecord
- public sealed class SqlDataReader : IEnumerable,
- IDataReader, IDataRecord {
- #region Fields
- private SqlCommand cmd;
- private DataTable table = null;
- // columns in a row
- private object[] fields; // data value in a .NET type
- private string[] types; // PostgreSQL Type
- private bool[] isNull; // is NULL?
- private int[] actualLength; // ActualLength of data
- private DbType[] dbTypes; // DB data type
- // actucalLength = -1 is variable-length
-
- private bool open = false;
- IntPtr pgResult; // PGresult
- private int rows;
- private int cols;
- private int currentRow = -1; // no Read() has been done yet
- #endregion // Fields
- #region Constructors
- internal SqlDataReader (SqlCommand sqlCmd) {
- cmd = sqlCmd;
- open = true;
- }
- #endregion
- #region Public Methods
- [MonoTODO]
- public void Close() {
- open = false;
-
- // free SqlDataReader resources in SqlCommand
- // and allow SqlConnection to be used again
- cmd.CloseReader();
- // TODO: get parameters from result
- // clear unmanaged PostgreSQL result set
- PostgresLibrary.PQclear (pgResult);
- pgResult = IntPtr.Zero;
- }
- [MonoTODO]
- public DataTable GetSchemaTable() {
- return table;
- }
- [MonoTODO]
- public bool NextResult() {
- SqlResult res;
- currentRow = -1;
-
- res = cmd.NextResult();
- if(res.ResultReturned == true) {
- table = res.Table;
- pgResult = res.PgResult;
- rows = res.RowCount;
- cols = res.FieldCount;
- types = res.PgTypes;
- }
- return res.ResultReturned;
- }
- [MonoTODO]
- public bool Read() {
-
- string dataValue;
- int c = 0;
-
- if(currentRow < rows - 1) {
-
- currentRow++;
-
- // re-init row
- fields = new object[cols];
- //dbTypes = new DbType[cols];
- actualLength = new int[cols];
- isNull = new bool[cols];
-
- for(c = 0; c < cols; c++) {
- // get data value
- dataValue = PostgresLibrary.
- PQgetvalue(
- pgResult,
- currentRow, c);
- // is column NULL?
- //isNull[c] = PostgresLibrary.
- // PQgetisnull(pgResult,
- // currentRow, c);
- // get Actual Length
- actualLength[c] = PostgresLibrary.
- PQgetlength(pgResult,
- currentRow, c);
- DbType dbType;
- dbType = PostgresHelper.
- TypnameToSqlDbType(types[c]);
- if(dataValue == null) {
- fields[c] = null;
- isNull[c] = true;
- }
- else if(dataValue.Equals("")) {
- fields[c] = null;
- isNull[c] = true;
- }
- else {
- isNull[c] = false;
- fields[c] = PostgresHelper.
- ConvertDbTypeToSystem (
- dbType,
- dataValue);
- }
- }
- return true;
- }
- return false; // EOF
- }
- [MonoTODO]
- public byte GetByte(int i) {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public long GetBytes(int i, long fieldOffset,
- byte[] buffer, int bufferOffset,
- int length) {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public char GetChar(int i) {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public long GetChars(int i, long fieldOffset,
- char[] buffer, int bufferOffset,
- int length) {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public IDataReader GetData(int i) {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public string GetDataTypeName(int i) {
- return types[i];
- }
- [MonoTODO]
- public DateTime GetDateTime(int i) {
- return (DateTime) fields[i];
- }
- [MonoTODO]
- public decimal GetDecimal(int i) {
- return (decimal) fields[i];
- }
- [MonoTODO]
- public double GetDouble(int i) {
- return (double) fields[i];
- }
- [MonoTODO]
- public Type GetFieldType(int i) {
- return table.Columns[i].DataType;
- }
- [MonoTODO]
- public float GetFloat(int i) {
- return (float) fields[i];
- }
- [MonoTODO]
- public Guid GetGuid(int i) {
- throw new NotImplementedException ();
- }
- [MonoTODO]
- public short GetInt16(int i) {
- return (short) fields[i];
- }
- [MonoTODO]
- public int GetInt32(int i) {
- return (int) fields[i];
- }
- [MonoTODO]
- public long GetInt64(int i) {
- return (long) fields[i];
- }
- [MonoTODO]
- public string GetName(int i) {
- return table.Columns[i].ColumnName;
- }
- [MonoTODO]
- public int GetOrdinal(string name) {
- int i;
- for(i = 0; i < cols; i ++) {
- if(table.Columns[i].ColumnName.Equals(name)) {
- return i;
- }
- }
-
- for(i = 0; i < cols; i++) {
- string ta;
- string n;
-
- ta = table.Columns[i].ColumnName.ToUpper();
- n = name.ToUpper();
-
- if(ta.Equals(n)) {
- return i;
- }
- }
-
- throw new MissingFieldException("Missing field: " + name);
- }
- [MonoTODO]
- public string GetString(int i) {
- return (string) fields[i];
- }
- [MonoTODO]
- public object GetValue(int i) {
- return fields[i];
- }
- [MonoTODO]
- public int GetValues(object[] values)
- {
- Array.Copy (fields, values, fields.Length);
- return fields.Length;
- }
- [MonoTODO]
- public bool IsDBNull(int i) {
- return isNull[i];
- }
- [MonoTODO]
- public bool GetBoolean(int i) {
- return (bool) fields[i];
- }
- [MonoTODO]
- public IEnumerator GetEnumerator() {
- throw new NotImplementedException ();
- }
- #endregion // Public Methods
- #region Destructors
- [MonoTODO]
- public void Dispose () {
- }
- [MonoTODO]
- ~SqlDataReader() {
- }
- #endregion // Destructors
- #region Properties
- public int Depth {
- [MonoTODO]
- get {
- throw new NotImplementedException ();
- }
- }
- public bool IsClosed {
- [MonoTODO]
- get {
- if(open == false)
- return true;
- else
- return false;
- }
- }
- public int RecordsAffected {
- [MonoTODO]
- get {
- throw new NotImplementedException ();
- }
- }
-
- public int FieldCount {
- [MonoTODO]
- get {
- return cols;
- }
- }
- public object this[string name] {
- [MonoTODO]
- get {
- int i;
- for(i = 0; i < cols; i ++) {
- if(table.Columns[i].ColumnName.Equals(name)) {
- return fields[i];
- }
- }
-
- for(i = 0; i < cols; i++) {
- string ta;
- string n;
-
- ta = table.Columns[i].ColumnName.ToUpper();
- n = name.ToUpper();
-
- if(ta.Equals(n)) {
- return fields[i];
- }
- }
-
- throw new MissingFieldException("Missing field: " + name);
- }
- }
- public object this[int i] {
- [MonoTODO]
- get {
- return fields[i];
- }
- }
- #endregion // Properties
- }
- }
|