Exemple #1
0
        protected override int Fill(DataTable[] dataTables, IDataReader dataReader, int startRecord, int maxRecords)
        {
            int rowsAffected = 0;

            if (!dataReader.IsClosed)
            {
                bool        moreResults         = true;
                IEnumerator dataTableEnumerator = dataTables.GetEnumerator();

                while (moreResults && dataTableEnumerator.MoveNext())
                {
                    DataTable        dataTable    = (DataTable)dataTableEnumerator.Current;
                    DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, "Table", dataTable.TableName, MissingMappingAction);

                    if (tableMapping != null)
                    {
                        rowsAffected += Fill(dataTable, tableMapping, dataReader, startRecord, maxRecords);
                    }

                    moreResults = dataReader.NextResult();
                }
            }

            return(rowsAffected);
        }
Exemple #2
0
 public void GetTableMappingBySchemaActionException1()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMapCollection, "sourceCustomers", "dataSetCustomers", MissingMappingAction.Error);
     });
 }
Exemple #3
0
 // <Snippet1>
 public void FindDataTableMapping()
 {
     // ...
     // create mappings and mapping
     // ...
     if (mappings.Contains("Categories"))
     {
         mapping = DataTableMappingCollection.GetTableMappingBySchemaAction
                       (mappings, "Categories", "", MissingMappingAction.Ignore);
     }
 }
Exemple #4
0
        public void GetTableMappingBySchemaAction()
        {
            var collection = new DataTableMappingCollection();

            Assert.Throws <ArgumentException>(() => DataTableMappingCollection.GetTableMappingBySchemaAction(collection, "", "", MissingMappingAction.Ignore));
            Assert.Throws <InvalidOperationException>(() => DataTableMappingCollection.GetTableMappingBySchemaAction(collection, "source", "", MissingMappingAction.Error));
            Assert.Null(DataTableMappingCollection.GetTableMappingBySchemaAction(collection, "source", "", MissingMappingAction.Ignore));
            Assert.Throws <ArgumentOutOfRangeException>(() => DataTableMappingCollection.GetTableMappingBySchemaAction(collection, "source", "", default(MissingMappingAction)));
            DataTableMapping mapping = DataTableMappingCollection.GetTableMappingBySchemaAction(collection, "source", "", MissingMappingAction.Passthrough);

            Assert.NotNull(mapping);
            Assert.Equal("source", mapping.SourceTable);
        }
Exemple #5
0
        internal string SetupSchema(SchemaType schemaType, string sourceTableName)
        {
            DataTableMapping tableMapping = null;

            if (schemaType == SchemaType.Mapped)
            {
                tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, sourceTableName, sourceTableName, MissingMappingAction);
                if (tableMapping != null)
                {
                    return(tableMapping.DataSetTable);
                }
                return(null);
            }
            else
            {
                return(sourceTableName);
            }
        }
Exemple #6
0
        protected override int Fill(DataSet dataSet, string srcTable, IDataReader dataReader, int startRecord, int maxRecords)
        {
            if (!dataReader.IsClosed)
            {
                DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, srcTable, srcTable, MissingMappingAction);

                if (tableMapping != null)
                {
                    DataTable dataTable = tableMapping.GetDataTableBySchemaAction(dataSet, MissingSchemaAction);

                    if (dataTable != null)
                    {
                        return(Fill(dataTable, tableMapping, dataReader, startRecord, maxRecords));
                    }
                }
            }

            return(0);
        }
Exemple #7
0
        public void GetTableMappingBySchemaAction()
        {
            _tableMapCollection.AddRange(_tabs);
            bool             eq;
            DataTableMapping tab1;

            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMapCollection, "sourceCustomers", "dataSetCustomers", MissingMappingAction.Passthrough);
            eq   = (tab1.DataSetTable.Equals("dataSetCustomers") && tab1.SourceTable.Equals("sourceCustomers"));
            Assert.True(eq);
            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMapCollection, "sourceEmployees", "dataSetEmployees", MissingMappingAction.Passthrough);
            eq   = (tab1.DataSetTable.Equals("dataSetEmployees") && tab1.SourceTable.Equals("sourceEmployees"));
            Assert.True(eq);

            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMapCollection, "sourceData", "dataSetData", MissingMappingAction.Passthrough);
            eq   = (tab1.DataSetTable.Equals("sourceData") && tab1.SourceTable.Equals("dataSetData"));
            Assert.False(eq);
            eq = _tableMapCollection.Contains(tab1);
            Assert.False(eq);
            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMapCollection, "sourceData", "dataSetData", MissingMappingAction.Ignore);
            Assert.Null(tab1);
        }
        public void GetTableMappingBySchemaAction()
        {
            tableMapCollection.AddRange(tabs);
            bool             eq;
            DataTableMapping tab1;

            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection, "sourceCustomers", "dataSetCustomers", MissingMappingAction.Passthrough);
            eq   = (tab1.DataSetTable.Equals("dataSetCustomers") && tab1.SourceTable.Equals("sourceCustomers"));
            Assert.AreEqual(true, eq, "test1");
            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection, "sourceEmployees", "dataSetEmployees", MissingMappingAction.Passthrough);
            eq   = (tab1.DataSetTable.Equals("dataSetEmployees") && tab1.SourceTable.Equals("sourceEmployees"));
            Assert.AreEqual(true, eq, "test2");

            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection, "sourceData", "dataSetData", MissingMappingAction.Passthrough);
            eq   = (tab1.DataSetTable.Equals("sourceData") && tab1.SourceTable.Equals("dataSetData"));
            Assert.AreEqual(false, eq, "test3");
            eq = tableMapCollection.Contains(tab1);
            Assert.AreEqual(false, eq, "test4");
            tab1 = DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection, "sourceData", "dataSetData", MissingMappingAction.Ignore);
            Assert.AreEqual(null, tab1, "test5");
        }
 internal DataTableMapping GetTableMappingBySchemaAction(string sourceTableName, string dataSetTableName, MissingMappingAction mappingAction)
 {
     return(DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMappings, sourceTableName, dataSetTableName, mappingAction));
 }
        internal SchemaMapping(DataAdapter adapter, DataSet dataset, System.Data.DataTable datatable, DataReaderContainer dataReader, bool keyInfo, SchemaType schemaType, string sourceTableName, bool gettingData, DataColumn parentChapterColumn, object parentChapterValue)
        {
            MissingMappingAction missingMappingAction;
            MissingSchemaAction  missingSchemaAction;

            this._dataSet    = dataset;
            this._dataTable  = datatable;
            this._adapter    = adapter;
            this._dataReader = dataReader;
            if (keyInfo)
            {
                this._schemaTable = dataReader.GetSchemaTable();
            }
            if (adapter.ShouldSerializeFillLoadOption())
            {
                this._loadOption = adapter.FillLoadOption;
            }
            else if (adapter.AcceptChangesDuringFill)
            {
                this._loadOption = (LoadOption)4;
            }
            else
            {
                this._loadOption = (LoadOption)5;
            }
            if (SchemaType.Mapped == schemaType)
            {
                missingMappingAction = this._adapter.MissingMappingAction;
                missingSchemaAction  = this._adapter.MissingSchemaAction;
                if (ADP.IsEmpty(sourceTableName))
                {
                    if (this._dataTable != null)
                    {
                        int num2 = this._adapter.IndexOfDataSetTable(this._dataTable.TableName);
                        if (-1 == num2)
                        {
                            switch (missingMappingAction)
                            {
                            case MissingMappingAction.Passthrough:
                                this._tableMapping = new DataTableMapping(this._dataTable.TableName, this._dataTable.TableName);
                                goto Label_01DB;

                            case MissingMappingAction.Ignore:
                                this._tableMapping = null;
                                goto Label_01DB;

                            case MissingMappingAction.Error:
                                throw ADP.MissingTableMappingDestination(this._dataTable.TableName);
                            }
                            throw ADP.InvalidMissingMappingAction(missingMappingAction);
                        }
                        this._tableMapping = this._adapter.TableMappings[num2];
                    }
                }
                else
                {
                    this._tableMapping = this._adapter.GetTableMappingBySchemaAction(sourceTableName, sourceTableName, missingMappingAction);
                }
            }
            else
            {
                if (SchemaType.Source != schemaType)
                {
                    throw ADP.InvalidSchemaType(schemaType);
                }
                missingMappingAction = MissingMappingAction.Passthrough;
                missingSchemaAction  = MissingSchemaAction.Add;
                if (!ADP.IsEmpty(sourceTableName))
                {
                    this._tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(null, sourceTableName, sourceTableName, missingMappingAction);
                }
                else if (this._dataTable != null)
                {
                    int num = this._adapter.IndexOfDataSetTable(this._dataTable.TableName);
                    if (-1 != num)
                    {
                        this._tableMapping = this._adapter.TableMappings[num];
                    }
                    else
                    {
                        this._tableMapping = new DataTableMapping(this._dataTable.TableName, this._dataTable.TableName);
                    }
                }
            }
Label_01DB:
            if (this._tableMapping != null)
            {
                if (this._dataTable == null)
                {
                    this._dataTable = this._tableMapping.GetDataTableBySchemaAction(this._dataSet, missingSchemaAction);
                }
                if (this._dataTable != null)
                {
                    this._fieldNames = GenerateFieldNames(dataReader);
                    if (this._schemaTable == null)
                    {
                        this._readerDataValues = this.SetupSchemaWithoutKeyInfo(missingMappingAction, missingSchemaAction, gettingData, parentChapterColumn, parentChapterValue);
                        return;
                    }
                    this._readerDataValues = this.SetupSchemaWithKeyInfo(missingMappingAction, missingSchemaAction, gettingData, parentChapterColumn, parentChapterValue);
                }
            }
        }
Exemple #11
0
        /// <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

            for (int i = 0; i < mapping.Length; i++)
            {
                mapping[i] = -1;
            }

            ArrayList primaryKey       = new ArrayList();
            ArrayList sourceColumns    = new ArrayList();
            bool      createPrimaryKey = true;

            DataTable schemaTable = reader.GetSchemaTable();

            DataColumn ColumnNameCol      = schemaTable.Columns["ColumnName"];
            DataColumn DataTypeCol        = schemaTable.Columns["DataType"];
            DataColumn IsAutoIncrementCol = schemaTable.Columns["IsAutoIncrement"];
            DataColumn AllowDBNullCol     = schemaTable.Columns["AllowDBNull"];
            DataColumn IsReadOnlyCol      = schemaTable.Columns["IsReadOnly"];
            DataColumn IsKeyCol           = schemaTable.Columns["IsKey"];
            DataColumn IsUniqueCol        = schemaTable.Columns["IsUnique"];
            DataColumn ColumnSizeCol      = schemaTable.Columns["ColumnSize"];

            foreach (DataRow schemaRow in schemaTable.Rows)
            {
                // generate a unique column name in the source table.
                string sourceColumnName;
                string realSourceColumnName;
                if (ColumnNameCol == null || schemaRow.IsNull(ColumnNameCol) ||
                    (string)schemaRow [ColumnNameCol] == String.Empty)
                {
                    sourceColumnName     = DefaultSourceColumnName;
                    realSourceColumnName = DefaultSourceColumnName + "1";
                }
                else
                {
                    sourceColumnName     = (string)schemaRow [ColumnNameCol];
                    realSourceColumnName = sourceColumnName;
                }

                for (int i = 1; sourceColumns.Contains(realSourceColumnName); i += 1)
                {
                    realSourceColumnName = String.Format("{0}{1}", sourceColumnName, i);
                }
                sourceColumns.Add(realSourceColumnName);

                // generate DataSetColumnName from DataTableMapping, if any
                DataTableMapping tableMapping = null;

                //FIXME : The sourcetable name shud get passed as a parameter..
                int    index    = dtMapping.IndexOfDataSetTable(table.TableName);
                string srcTable = (index != -1 ? dtMapping[index].SourceTable : table.TableName);
                tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(dtMapping, ADP.IsEmpty(srcTable) ? " " : srcTable, 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, missingMapAction);
                    if (columnMapping != null)
                    {
                        Type       columnType = schemaRow[DataTypeCol] as Type;
                        DataColumn col        = columnType != null?columnMapping.GetDataColumnBySchemaAction(
                            table,
                            columnType,
                            missingSchAction) : null;

                        if (col != null)
                        {
                            // if the column is not in the table - add it.
                            if (table.Columns.IndexOf(col) == -1)
                            {
                                if (missingSchAction == MissingSchemaAction.Add ||
                                    missingSchAction == MissingSchemaAction.AddWithKey)
                                {
                                    table.Columns.Add(col);
                                }

                                int[] tmp = new int[mapping.Length + 1];
                                Array.Copy(mapping, 0, tmp, 0, col.Ordinal);
                                Array.Copy(mapping, col.Ordinal, tmp, col.Ordinal + 1, mapping.Length - col.Ordinal);
                                mapping = tmp;
                            }

                            if (missingSchAction == MissingSchemaAction.AddWithKey)
                            {
                                object value       = (AllowDBNullCol != null) ? schemaRow[AllowDBNullCol] : null;
                                bool   allowDBNull = value is bool?(bool)value : true;

                                value = (IsKeyCol != null) ? schemaRow[IsKeyCol] : null;
                                bool isKey = value is bool?(bool)value : false;

                                value = (IsAutoIncrementCol != null) ? schemaRow[IsAutoIncrementCol] : null;
                                bool isAutoIncrement = value is bool?(bool)value : false;

                                value = (IsReadOnlyCol != null) ? schemaRow[IsReadOnlyCol] : null;
                                bool isReadOnly = value is bool?(bool)value : false;

                                value = (IsUniqueCol != null) ? schemaRow[IsUniqueCol] : null;
                                bool isUnique = value is bool?(bool)value : false;

                                col.AllowDBNull = allowDBNull;
                                // fill woth key info
                                if (isAutoIncrement && CanAutoIncrement(columnType))
                                {
                                    col.AutoIncrement = true;
                                    if (!allowDBNull)
                                    {
                                        col.AllowDBNull = false;
                                    }
                                }

                                if (columnType == DbTypes.TypeOfString)
                                {
                                    col.MaxLength = (ColumnSizeCol != null) ? (int)schemaRow[ColumnSizeCol] : 0;
                                }

                                if (isReadOnly)
                                {
                                    col.ReadOnly = true;
                                }

                                if (!allowDBNull && (!isReadOnly || isKey))
                                {
                                    col.AllowDBNull = false;
                                }
                                if (isUnique && !isKey && !columnType.IsArray)
                                {
                                    col.Unique = true;
                                    if (!allowDBNull)
                                    {
                                        col.AllowDBNull = false;
                                    }
                                }

                                // This might not be set by all DataProviders
                                bool isHidden = false;
                                if (schemaTable.Columns.Contains("IsHidden"))
                                {
                                    value    = schemaRow["IsHidden"];
                                    isHidden = ((value is bool) ? (bool)value : false);
                                }

                                if (isKey && !isHidden)
                                {
                                    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++;
                        }
                    }
                }
            }
            if (primaryKey.Count > 0)
            {
                DataColumn[] colKey = (DataColumn[])(primaryKey.ToArray(typeof(DataColumn)));
                if (createPrimaryKey)
                {
                    table.PrimaryKey = colKey;
                }
                else
                {
                    UniqueConstraint uConstraint = new UniqueConstraint(colKey);
                    for (int i = 0; i < table.Constraints.Count; i++)
                    {
                        if (table.Constraints[i].Equals(uConstraint))
                        {
                            uConstraint = null;
                            break;
                        }
                    }

                    if (uConstraint != null)
                    {
                        table.Constraints.Add(uConstraint);
                    }
                }
            }
            return(mapping);
        }
 public void GetTableMappingBySchemaActionException1()
 {
     DataTableMappingCollection.GetTableMappingBySchemaAction(tableMapCollection, "sourceCustomers", "dataSetCustomers", MissingMappingAction.Error);
 }
Exemple #13
0
 DataTableMapping IAdaSchemaMappingAdapter.GetTableMappingBySchemaAction(string sourceTableName, string dataSetTableName, MissingMappingAction mappingAction)
 {
     return(DataTableMappingCollection.GetTableMappingBySchemaAction(this.TableMappings, sourceTableName, dataSetTableName, mappingAction));
 }