merging the Mainsoft branch to the trunk
[mono.git] / mcs / class / System.Data / System.Data.Common / DbDataAdapter.cs
index 56d4267492779b3028286783c6eae5a5514bfb00..a895a21cc8183bdc54710273329d2ae0a296aebb 100644 (file)
@@ -4,6 +4,7 @@
 // Author:
 //   Rodrigo Moya (rodrigo@ximian.com)
 //   Tim Coleman (tim@timcoleman.com)
+//   Sureshkumar T <tsureshkumar@novell.com>
 //
 // (C) Ximian, Inc
 // Copyright (C) Tim Coleman, 2002-2003
@@ -331,14 +332,13 @@ namespace System.Data.Common {
                        return Fill (dataSet, srcTable, command.ExecuteReader (commandBehavior), startRecord, maxRecords);
                }
 
-               private bool FillTable (DataTable dataTable, IDataReader dataReader, int startRecord, int maxRecords, ref int counter) 
+               private bool FillTable (DataTable dataTable, IDataReader dataReader, int startRecord, int maxRecords, ref int counter)
                {
-                       if (dataReader.FieldCount == 0) {
+                       if (dataReader.FieldCount == 0)
                                return false;
-                       }
 
                        int counterStart = counter;
-                       
+
                        int[] mapping = BuildSchema (dataReader, dataTable, SchemaType.Mapped);
 
                        int[] sortedMapping = new int[mapping.Length];
@@ -382,6 +382,44 @@ namespace System.Data.Common {
                        return true;
                }
 
+#if NET_2_0
+                /// <summary>
+                ///     Fills the given datatable using values from reader. if a value 
+                ///     for a column is  null, that will be filled with default value. 
+                /// </summary>
+                /// <returns>No. of rows affected </returns>
+               internal static int FillFromReader (DataTable table,
+                                                    IDataReader reader,
+                                                    int start, 
+                                                    int length,
+                                                    int [] mapping,
+                                                    LoadOption loadOption
+                                                    )
+                {
+                        if (reader.FieldCount == 0)
+                               return 0 ;
+
+                        for (int i = 0; i < start; i++)
+                                reader.Read ();
+
+                        int counter = 0;
+                        object [] values = new object [mapping.Length];
+                        while (reader.Read () &&
+                               (length == 0 || counter < length)) {
+                                
+                                for (int i = 0 ; i < mapping.Length; i++)
+                                        values [i] = mapping [i] < 0 ? null : reader [mapping [i]];
+                                        
+                                table.BeginLoadData ();
+                                table.LoadDataRow (values, loadOption);
+                                table.EndLoadData ();
+                                counter++;
+                        }
+                        return counter;
+                }
+
+#endif // NET_2_0
+
                public override DataTable[] FillSchema (DataSet dataSet, SchemaType schemaType) 
                {
                        return FillSchema (dataSet, schemaType, SelectCommand, DefaultSourceTableName, CommandBehavior.Default);
@@ -505,6 +543,22 @@ namespace System.Data.Common {
                [MonoTODO ("Test")]
                private int[] BuildSchema (IDataReader reader, DataTable table, SchemaType schemaType)
                {
+                         return BuildSchema (reader, table, schemaType, MissingSchemaAction,
+                                             MissingMappingAction, TableMappings);
+                }
+
+                /// <summary>
+                ///     Creates or Modifies the schema of the given DataTable based on the schema of
+                ///     the reader and the arguments passed.
+                /// </summary>
+                internal static int[] BuildSchema (IDataReader reader,
+                                                   DataTable table,
+                                                   SchemaType schemaType,
+                                                   MissingSchemaAction missingSchAction,
+                                                   MissingMappingAction missingMapAction,
+                                                   DataTableMappingCollection dtMapping
+                                                   )
+               {
                        int readerIndex = 0;
                        // FIXME : this fails if query has fewer columns than a table
                        int[] mapping = new int[table.Columns.Count]; // mapping the reader indexes to the datatable indexes
@@ -534,13 +588,13 @@ namespace System.Data.Common {
                                // generate DataSetColumnName from DataTableMapping, if any
                                string dsColumnName = realSourceColumnName;
                                DataTableMapping tableMapping = null;
-                               tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (TableMappings, table.TableName, table.TableName, MissingMappingAction); 
+                               tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction (dtMapping, table.TableName, table.TableName, missingMapAction); 
                                if (tableMapping != null) 
                                {
                                        
                                        table.TableName = tableMapping.DataSetTable;
                                        // check to see if the column mapping exists
-                                       DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, MissingMappingAction);
+                                       DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, realSourceColumnName, missingMapAction);
                                        if (columnMapping != null)
                                        {
                                                Type columnType = (Type)schemaRow["DataType"];
@@ -548,15 +602,15 @@ namespace System.Data.Common {
                                                        columnMapping.GetDataColumnBySchemaAction(
                                                        table ,
                                                        columnType,
-                                                       MissingSchemaAction);
+                                                       missingSchAction);
 
                                                if (col != null)
                                                {
                                                        // if the column is not in the table - add it.
                                                        if (table.Columns.IndexOf(col) == -1)
                                                        {
-                                                               if (MissingSchemaAction == MissingSchemaAction.Add 
-                                                                    || MissingSchemaAction == MissingSchemaAction.AddWithKey)
+                                                               if (missingSchAction == MissingSchemaAction.Add 
+                                                                       || missingSchAction == MissingSchemaAction.AddWithKey)
                                                                        table.Columns.Add(col);
 
                                                                int[] tmp = new int[mapping.Length + 1];
@@ -570,7 +624,7 @@ namespace System.Data.Common {
                                                        bool isKey = (bool)schemaRow["IsKey"];                                                  
                                                        bool isUnique = (bool)schemaRow["IsUnique"];
 
-                                                        if (MissingSchemaAction == MissingSchemaAction.AddWithKey) {
+                            if (missingSchAction == MissingSchemaAction.AddWithKey) {
                                                                // fill woth key info                                                           
                                                                if (isAutoIncrement && DataColumn.CanAutoIncrement(columnType)) {
                                                                        col.AutoIncrement = true;
@@ -595,10 +649,10 @@ namespace System.Data.Common {
                                                        }
 
                                                        if (isKey) {
-                                                                                primaryKey.Add (col);
+                                primaryKey.Add (col);
                                                                if (allowDBNull)
                                                                        createPrimaryKey = false;
-                                                        }
+                            }
                                                        
                                                        // add the ordinal of the column as a key and the index of the column in the datareader as a value.
                                                        mapping[col.Ordinal] = readerIndex++;
@@ -625,7 +679,8 @@ namespace System.Data.Common {
                        }
 
                        return mapping;
-               }
+                        
+                }
 
                [MonoTODO]
                object ICloneable.Clone ()