2005-08-16 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / class / System.Data / System.Data.OleDb / OleDbDataReader.cs
index 4375949eb9deb5ad8ac89002533beeaedfa4cea4..cc19391d813b6f20a5f8abca21b21f21009216d1 100644 (file)
@@ -9,6 +9,29 @@
 // Copyright (C) Tim Coleman, 2002
 //
 
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System.Collections;
 using System.ComponentModel;
 using System.Data;
@@ -26,6 +49,7 @@ namespace System.Data.OleDb
                private ArrayList gdaResults;
                private int currentResult;
                private int currentRow;
+               private bool disposed = false;
 
                #endregion
 
@@ -48,9 +72,8 @@ namespace System.Data.OleDb
                #region Properties
 
                public int Depth {
-                       [MonoTODO]
                        get {
-                               throw new NotImplementedException ();
+                               return 0; // no nested selects supported
                        }
                }
 
@@ -72,9 +95,19 @@ namespace System.Data.OleDb
                }
 
                public object this[string name] {
-                       [MonoTODO]
                        get {
-                               throw new NotImplementedException ();
+                               int pos;
+
+                               if (currentResult == -1)
+                                       throw new InvalidOperationException ();
+
+                               pos = libgda.gda_data_model_get_column_position (
+                                       (IntPtr) gdaResults[currentResult],
+                                       name);
+                               if (pos == -1)
+                                       throw new IndexOutOfRangeException ();
+
+                               return this[pos];
                        }
                }
 
@@ -104,6 +137,13 @@ namespace System.Data.OleDb
                                return FieldCount > 0 ? -1 : total_rows;
                        }
                }
+               
+               [MonoTODO]
+               public bool HasRows {
+                       get {
+                               throw new NotImplementedException ();
+                       }
+               }
 
                #endregion
 
@@ -114,22 +154,16 @@ namespace System.Data.OleDb
                        for (int i = 0; i < gdaResults.Count; i++) {
                                IntPtr obj = (IntPtr) gdaResults[i];
                                libgda.FreeObject (obj);
-                               gdaResults = null;
                        }
 
                        gdaResults.Clear ();
+                       gdaResults = null;
                        
                        open = false;
                        currentResult = -1;
                        currentRow = -1;
                }
 
-               ~OleDbDataReader ()
-               {
-                       if (open)
-                               Close ();
-               }
-
                public bool GetBoolean (int ordinal)
                {
                        IntPtr value;
@@ -142,7 +176,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Boolean)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Boolean)
                                throw new InvalidCastException ();
                        return libgda.gda_value_get_boolean (value);
                }
@@ -159,7 +193,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Tinyint)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Tinyint)
                                throw new InvalidCastException ();
                        return libgda.gda_value_get_tinyint (value);
                }
@@ -170,6 +204,7 @@ namespace System.Data.OleDb
                        throw new NotImplementedException ();
                }
                
+               [EditorBrowsableAttribute (EditorBrowsableState.Never)]
                public char GetChar (int ordinal)
                {
                        IntPtr value;
@@ -182,7 +217,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Tinyint)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Tinyint)
                                throw new InvalidCastException ();
                        return (char) libgda.gda_value_get_tinyint (value);
                }
@@ -232,19 +267,19 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) == GdaValueType.Date) {
+                       if (libgda.gda_value_get_type (value) == GdaValueType.Date) {
                                GdaDate gdt;
 
                                gdt = (GdaDate) Marshal.PtrToStructure (libgda.gda_value_get_date (value),
                                                                        typeof (GdaDate));
                                return new DateTime ((int) gdt.year, (int) gdt.month, (int) gdt.day);
-                       } else if (libgda.gda_value_get_vtype (value) == GdaValueType.Time) {
+                       } else if (libgda.gda_value_get_type (value) == GdaValueType.Time) {
                                GdaTime gdt;
 
                                gdt = (GdaTime) Marshal.PtrToStructure (libgda.gda_value_get_time (value),
                                                                        typeof (GdaTime));
                                return new DateTime (0, 0, 0, (int) gdt.hour, (int) gdt.minute, (int) gdt.second, 0);
-                       } else if (libgda.gda_value_get_vtype (value) == GdaValueType.Timestamp) {
+                       } else if (libgda.gda_value_get_type (value) == GdaValueType.Timestamp) {
                                GdaTimestamp gdt;
                                
                                gdt = (GdaTimestamp) Marshal.PtrToStructure (libgda.gda_value_get_timestamp (value),
@@ -276,7 +311,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Double)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Double)
                                throw new InvalidCastException ();
                        return libgda.gda_value_get_double (value);
                }
@@ -284,7 +319,33 @@ namespace System.Data.OleDb
                [MonoTODO]
                public Type GetFieldType (int index)
                {
-                       throw new NotImplementedException ();
+                       IntPtr value;
+                       GdaValueType type;
+
+                       if (currentResult == -1)
+                               throw new IndexOutOfRangeException ();
+
+                       value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
+                               index, currentRow);
+                       if (value == IntPtr.Zero)
+                               throw new IndexOutOfRangeException ();
+
+                       type = libgda.gda_value_get_type (value);
+                       switch (type) {
+                       case GdaValueType.Bigint : return typeof (long);
+                       case GdaValueType.Boolean : return typeof (bool);
+                       case GdaValueType.Date : return typeof (DateTime);
+                       case GdaValueType.Double : return typeof (double);
+                       case GdaValueType.Integer : return typeof (int);
+                       case GdaValueType.Single : return typeof (float);
+                       case GdaValueType.Smallint : return typeof (byte);
+                       case GdaValueType.String : return typeof (string);
+                       case GdaValueType.Time : return typeof (DateTime);
+                       case GdaValueType.Timestamp : return typeof (DateTime);
+                       case GdaValueType.Tinyint : return typeof (byte);
+                       }
+
+                       return typeof(string); // default
                }
 
                public float GetFloat (int ordinal)
@@ -299,7 +360,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Single)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Single)
                                throw new InvalidCastException ();
                        return libgda.gda_value_get_single (value);
                }
@@ -322,7 +383,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Smallint)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Smallint)
                                throw new InvalidCastException ();
                        return (short) libgda.gda_value_get_smallint (value);
                }
@@ -339,7 +400,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Integer)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Integer)
                                throw new InvalidCastException ();
                        return libgda.gda_value_get_integer (value);
                }
@@ -356,7 +417,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.Bigint)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.Bigint)
                                throw new InvalidCastException ();
                        return libgda.gda_value_get_bigint (value);
                }
@@ -383,10 +444,128 @@ namespace System.Data.OleDb
                        throw new IndexOutOfRangeException ();
                }
 
-               [MonoTODO]
                public DataTable GetSchemaTable ()
                {
-                       throw new NotImplementedException ();
+                       DataTable dataTableSchema = null;
+                       // Only Results from SQL SELECT Queries 
+                       // get a DataTable for schema of the result
+                       // otherwise, DataTable is null reference
+                       if(this.FieldCount > 0) {
+
+                               IntPtr attrs;
+                               GdaValueType gdaType;
+                               long columnSize = 0;
+
+                               if (currentResult == -1) {
+                                       // FIXME: throw an exception?
+#if DEBUG_OleDbDataReader
+                                       Console.WriteLine("Error: current result -1");
+#endif
+                                       return null;
+                               }
+                                               
+                               dataTableSchema = new DataTable ();
+                               
+                               dataTableSchema.Columns.Add ("ColumnName", typeof (string));
+                               dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
+                               dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
+                               dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
+                               dataTableSchema.Columns.Add ("NumericScale", typeof (int));
+                               dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsKey", typeof (bool));
+                               DataColumn dc = dataTableSchema.Columns["IsKey"];
+                               dc.AllowDBNull = true; // IsKey can have a DBNull
+                               dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
+                               dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
+                               dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
+                               dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
+                               dataTableSchema.Columns.Add ("DataType", typeof(Type));
+                               dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
+                               dataTableSchema.Columns.Add ("ProviderType", typeof (int));
+                               dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsLong", typeof (bool));
+                               dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
+\r
+                               DataRow schemaRow;
+                               DbType dbType;
+                               Type typ;
+                                                               
+                               for (int i = 0; i < this.FieldCount; i += 1 ) {
+                                       
+                                       schemaRow = dataTableSchema.NewRow ();
+
+                                       attrs = libgda.gda_data_model_describe_column ((IntPtr) gdaResults[currentResult],
+                                               i);
+                                       if (attrs == IntPtr.Zero){
+                                               // FIXME: throw exception
+#if DEBUG_OleDbDataReader
+                                               Console.WriteLine("Error: attrs null");
+#endif
+                                               return null;
+                                       }
+
+                                       gdaType = libgda.gda_field_attributes_get_gdatype (attrs);
+                                       columnSize = libgda.gda_field_attributes_get_defined_size (attrs);
+                                       libgda.gda_field_attributes_free (attrs);
+                                                                               
+                                       schemaRow["ColumnName"] = this.GetName(i);
+                                       schemaRow["ColumnOrdinal"] = i + 1;
+                                       
+                                       schemaRow["ColumnSize"] = (int) columnSize;
+                                       schemaRow["NumericPrecision"] = 0;
+                                       schemaRow["NumericScale"] = 0;
+                                       // TODO: need to get KeyInfo
+                                       //if((cmdBehavior & CommandBehavior.KeyInfo) == CommandBehavior.KeyInfo) {
+                                               // bool IsUnique, IsKey;
+                                               // GetKeyInfo(field[i].Name, out IsUnique, out IsKey);
+                                       //}
+                                       //else {
+                                               schemaRow["IsUnique"] = false;
+                                               schemaRow["IsKey"] = DBNull.Value;
+                                       //}
+                                       schemaRow["BaseCatalogName"] = "";
+                                       
+                                       schemaRow["BaseColumnName"] = this.GetName(i);
+                                       schemaRow["BaseSchemaName"] = "";
+                                       schemaRow["BaseTableName"] = "";
+
+                                       schemaRow["DataType"] = this.GetFieldType(i);\r
+
+                                       schemaRow["AllowDBNull"] = false;
+                                       
+                                       schemaRow["ProviderType"] = (int) gdaType;
+                                       schemaRow["IsAliased"] = false;
+                                       schemaRow["IsExpression"] = false;
+                                       schemaRow["IsIdentity"] = false;
+                                       schemaRow["IsAutoIncrement"] = false;
+                                       schemaRow["IsRowVersion"] = false;
+                                       schemaRow["IsHidden"] = false;
+                                       schemaRow["IsLong"] = false;
+                                       schemaRow["IsReadOnly"] = false;
+                                       
+                                       schemaRow.AcceptChanges();
+                                       
+                                       dataTableSchema.Rows.Add (schemaRow);
+                               }
+                               
+#if DEBUG_OleDbDataReader
+                               Console.WriteLine("********** DEBUG Table Schema BEGIN ************");
+                               foreach (DataRow myRow in dataTableSchema.Rows) {\r
+                                       foreach (DataColumn myCol in dataTableSchema.Columns)\r
+                                               Console.WriteLine(myCol.ColumnName + " = " + myRow[myCol]);\r
+                                       Console.WriteLine();\r
+                               }
+                               Console.WriteLine("********** DEBUG Table Schema END ************");
+#endif // DEBUG_OleDbDataReader
+
+                       }
+                       
+                       return dataTableSchema;
                }
 
                public string GetString (int ordinal)
@@ -401,7 +580,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new InvalidCastException ();
                        
-                       if (libgda.gda_value_get_vtype (value) != GdaValueType.String)
+                       if (libgda.gda_value_get_type (value) != GdaValueType.String)
                                throw new InvalidCastException ();
                        return libgda.gda_value_get_string (value);
                }
@@ -425,7 +604,7 @@ namespace System.Data.OleDb
                        if (value == IntPtr.Zero)
                                throw new IndexOutOfRangeException ();
 
-                       type = libgda.gda_value_get_vtype (value);
+                       type = libgda.gda_value_get_type (value);
                        switch (type) {
                        case GdaValueType.Bigint : return GetInt64 (ordinal);
                        case GdaValueType.Boolean : return GetBoolean (ordinal);
@@ -455,22 +634,24 @@ namespace System.Data.OleDb
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
-               void IDisposable.Dispose ()
-               {
-                       throw new NotImplementedException ();
-               }
-
-               [MonoTODO]
                IEnumerator IEnumerable.GetEnumerator ()
                {
-                       throw new NotImplementedException ();
+                       return new DbEnumerator (this);
                }
 
-               [MonoTODO]
                public bool IsDBNull (int ordinal)
                {
-                       throw new NotImplementedException ();
+                       IntPtr value;
+
+                       if (currentResult == -1)
+                               throw new IndexOutOfRangeException ();
+
+                       value = libgda.gda_data_model_get_value_at ((IntPtr) gdaResults[currentResult],
+                                                                   ordinal, currentRow);
+                       if (value == IntPtr.Zero)
+                               throw new IndexOutOfRangeException ();
+
+                       return libgda.gda_value_is_null (value);
                }
 
                public bool NextResult ()
@@ -499,5 +680,38 @@ namespace System.Data.OleDb
                }
 
                #endregion
+
+               #region Destructors
+
+               private void Dispose (bool disposing) {
+                       if (!this.disposed) {
+                               if (disposing) {
+                                       // release any managed resources
+                                       command = null;
+                               }
+                               // release any unmanaged resources
+                               if (gdaResults != null) {
+                                       gdaResults.Clear ();
+                                       gdaResults = null;
+                               }
+
+                               // close any handles
+                               if (open)
+                                       Close ();
+
+                               this.disposed = true;   
+                       }
+               }
+
+               void IDisposable.Dispose() {
+                       Dispose (true);
+               }
+
+               ~OleDbDataReader() {
+                       Dispose (false);
+               }
+
+               #endregion // Destructors
+
        }
 }