public static void ParameterInput(MissingMappingAction missingMapping, MissingSchemaAction missingSchema, IDataParameterCollection parameters, StatementType typeIndex, DataRow row, DataTableMapping mappings)
        {
            foreach (IDataParameter parameter in parameters)
            {
                if ((null != parameter) && (0 != (ParameterDirection.Input & parameter.Direction)))
                {
                    string columnName = parameter.SourceColumn;
                    if (!string.IsNullOrEmpty(columnName))
                    {
                        DataColumn dataColumn = mappings.GetDataColumn(sourceColumn: columnName, dataType: null, dataTable: row.Table, missingMapping, missingSchema);
                        if (null != dataColumn)
                        {
                            DataRowVersion version = GetParameterSourceVersion(typeIndex, parameter);
                            parameter.Value = row[dataColumn, version];
                        }
                        else
                        {
                            parameter.Value = null;
                        }

                        if (parameter is DbParameter p2 && p2.SourceColumnNullMapping)
                        {
                            Debug.Assert(DbType.Int32 == parameter.DbType, "unexpected DbType");
                            parameter.Value = Utility.IsNull(parameter.Value) ? ParameterValueNullValue : ParameterValueNonNullValue;
                        }
                    }
                }
            }
        }
 public DataColumn GetDataColumn(string sourceColumn,
                                 Type dataType,
                                 DataTable dataTable,
                                 MissingMappingAction mappingAction,
                                 MissingSchemaAction schemaAction)
 {
     throw new NotImplementedException();
 }
		protected DataAdapter () 
		{
			acceptChangesDuringFill = true;
			continueUpdateOnError = false;
			missingMappingAction = MissingMappingAction.Passthrough;
			missingSchemaAction = MissingSchemaAction.Add;
			tableMappings = new DataTableMappingCollection ();
		}
        /// <summary>This is the same logic as SqlDataAdapter.</summary>
        /// <param name="missingMappingAction">Get this value from <see cref="DataAdapter.MissingMappingAction"/>.</param>
        public static MissingMappingAction UpdateMappingAction(MissingMappingAction missingMappingAction)
        {
            if (MissingMappingAction.Passthrough == missingMappingAction)
            {
                return(MissingMappingAction.Passthrough);
            }

            return(MissingMappingAction.Error);
        }
 public static void ParameterOutput(MissingMappingAction missingMapping, MissingSchemaAction missingSchema, IDataParameterCollection parameters, DataRow row, DataTableMapping mappings)
 {
     foreach (IDataParameter parameter in parameters)
     {
         if (null != parameter)
         {
             ParameterOutput(parameter, row, mappings, missingMapping, missingSchema);
         }
     }
 }
Example #6
0
 protected DataAdapter()
 {
     acceptChangesDuringFill     = true;
     continueUpdateOnError       = false;
     missingMappingAction        = MissingMappingAction.Passthrough;
     missingSchemaAction         = MissingSchemaAction.Add;
     tableMappings               = new DataTableMappingCollection();
     acceptChangesDuringUpdate   = true;
     fillLoadOption              = LoadOption.OverwriteChanges;
     returnProviderSpecificTypes = false;
 }
Example #7
0
		protected DataAdapter () 
		{
			acceptChangesDuringFill = true;
			continueUpdateOnError = false;
			missingMappingAction = MissingMappingAction.Passthrough;
			missingSchemaAction = MissingSchemaAction.Add;
			tableMappings = new DataTableMappingCollection ();
			acceptChangesDuringUpdate = true;
			fillLoadOption = LoadOption.OverwriteChanges;
			returnProviderSpecificTypes = false;
		}
Example #8
0
        public int Update(DataSet dataSet, string srcTable)
        {
            MissingMappingAction mappingAction = MissingMappingAction;

            if (mappingAction == MissingMappingAction.Ignore)
            {
                mappingAction = MissingMappingAction.Error;
            }

            DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, srcTable, srcTable, mappingAction);

            DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable];

            if (dataTable == null)
            {
                throw new ArgumentException(String.Format("Missing table {0}",
                                                          srcTable));
            }

            /** Copied from another Update function **/
            if (tableMapping != null)
            {
                foreach (DataColumn col in dataTable.Columns)
                {
                    if (tableMapping.ColumnMappings.IndexOf(col.ColumnName) >= 0)
                    {
                        continue;
                    }
                    DataColumnMapping columnMapping = DataColumnMappingCollection.GetColumnMappingBySchemaAction(tableMapping.ColumnMappings, col.ColumnName, MissingMappingAction);
                    if (columnMapping == null)
                    {
                        columnMapping = new DataColumnMapping(col.ColumnName, col.ColumnName);
                    }
                    tableMapping.ColumnMappings.Add(columnMapping);
                }
            }
            else
            {
                ArrayList cmc = new ArrayList();
                foreach (DataColumn col in dataTable.Columns)
                {
                    cmc.Add(new DataColumnMapping(col.ColumnName, col.ColumnName));
                }
                tableMapping =
                    new DataTableMapping(
                        dataTable.TableName,
                        dataTable.TableName,
                        cmc.ToArray(typeof(DataColumnMapping)) as DataColumnMapping []);
            }
            /**end insert from another update**/
            return(Update(dataTable, tableMapping));
        }
Example #9
0
        // IDataAdapter.MissingMappingAction
        internal static ArgumentOutOfRangeException InvalidMissingMappingAction(MissingMappingAction value)
        {
#if DEBUG
            switch (value)
            {
            case MissingMappingAction.Passthrough:
            case MissingMappingAction.Ignore:
            case MissingMappingAction.Error:
                Debug.Assert(false, "valid MissingMappingAction " + value.ToString());
                break;
            }
#endif
            return(InvalidEnumerationValue(typeof(MissingMappingAction), (int)value));
        }
        private void CloneFrom(DataAdapter from)
        {
            _acceptChangesDuringUpdate            = from._acceptChangesDuringUpdate;
            _acceptChangesDuringUpdateAfterInsert = from._acceptChangesDuringUpdateAfterInsert;
            _continueUpdateOnError       = from._continueUpdateOnError;
            _returnProviderSpecificTypes = from._returnProviderSpecificTypes; // WebData 101795
            _acceptChangesDuringFill     = from._acceptChangesDuringFill;
            _fillLoadOption       = from._fillLoadOption;
            _missingMappingAction = from._missingMappingAction;
            _missingSchemaAction  = from._missingSchemaAction;

            if ((null != from._tableMappings) && (0 < from.TableMappings.Count))
            {
                DataTableMappingCollection parameters = this.TableMappings;
                foreach (object parameter in from.TableMappings)
                {
                    parameters.Add((parameter is ICloneable) ? ((ICloneable)parameter).Clone() : parameter);
                }
            }
        }
Example #11
0
        public int Update(DataSet dataSet, string srcTable)
        {
            MissingMappingAction mappingAction = MissingMappingAction;

            if (mappingAction == MissingMappingAction.Ignore)
            {
                mappingAction = MissingMappingAction.Error;
            }

            DataTableMapping tableMapping = DataTableMappingCollection.GetTableMappingBySchemaAction(TableMappings, srcTable, srcTable, mappingAction);

            DataTable dataTable = dataSet.Tables [tableMapping.DataSetTable];

            if (dataTable == null)
            {
                throw new ArgumentException(String.Format("Missing table {0}",
                                                          srcTable));
            }
            return(Update(dataTable, tableMapping));
        }
Example #12
0
        public static void DataAdapter()
        {
            SqlConnection  cn         = new SqlConnection();;
            SqlDataAdapter da         = new SqlDataAdapter(selectCommandText: "Select * from Aircraft", selectConnection: cn);
            var            sqlbuilder = new SqlCommandBuilder(da);
            bool           acceptChangesDuringFill   = da.AcceptChangesDuringFill;
            bool           acceptChangesDuringUpdate = da.AcceptChangesDuringUpdate;
            bool           continueUpdateOnError     = da.ContinueUpdateOnError;
            SqlCommand     sqlcmdDelete = da.DeleteCommand;
            SqlCommand     sqlcmdInsert = da.InsertCommand;
            SqlCommand     sqlcmdSelect = da.SelectCommand;
            SqlCommand     sqlcmdUpdate = da.UpdateCommand;
            LoadOption     loadOption   = da.FillLoadOption;

            MissingMappingAction mapingAction          = da.MissingMappingAction; //passthroug 1, ignore 2, error 3
            MissingSchemaAction  schemaAction          = da.MissingSchemaAction;  // add, ignore, error ,AddwithKey
            bool shouldReturnProvider                  = da.ReturnProviderSpecificTypes;
            DataTableMappingCollection dtMapCollection = da.TableMappings;

            //dtMapCollection[0].

            Console.WriteLine("*********DataAdapter**************");
            Console.WriteLine("");
            Console.WriteLine("   acceptChangesDuringFill {0,10}", acceptChangesDuringFill);
            Console.WriteLine("   acceptChangesDuringUpdate {0,10}", acceptChangesDuringUpdate);
            Console.WriteLine("   ContinueUpdateOnError {0,10}", continueUpdateOnError);
            Console.WriteLine("   DeleteCommand {0}", sqlcmdDelete);
            Console.WriteLine("   InsertCommand {0}", sqlcmdInsert);
            Console.WriteLine("   SelectCommand {0}", sqlcmdSelect);
            Console.WriteLine("   UpdateCommand {0}", sqlcmdUpdate);
            Console.WriteLine("   FillLoadOption {0}", loadOption);
            Console.WriteLine("   MissingMappingAction {0}", mapingAction);
            Console.WriteLine("   MissingSchemaAction {0}", schemaAction);
            Console.WriteLine("   ReturnProviderSpecificTypes {0}", shouldReturnProvider);
            Console.WriteLine("   TableMappings {0}", dtMapCollection);
        }
Example #13
0
        public static DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
        {
            if (null != columnMappings)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo)
                    {
                        Debug.WriteLine($"mapping match on SourceColumn \"{sourceColumn}\"");
                    }
#endif
                    return(columnMappings._items[index]);
                }
            }
            if (string.IsNullOrEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn(nameof(sourceColumn));
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceInfo)
                {
                    Debug.WriteLine($"mapping passthrough of SourceColumn \"{sourceColumn}\"");
                }
#endif
                return(new DataColumnMapping(sourceColumn, sourceColumn));

            case MissingMappingAction.Ignore:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine($"mapping filter of SourceColumn \"{sourceColumn}\"");
                }
#endif
                return(null);

            case MissingMappingAction.Error:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceError)
                {
                    Debug.WriteLine($"mapping error on SourceColumn \"{sourceColumn}\"");
                }
#endif
                throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
Example #14
0
 private void ParameterOutput(IDataParameter parameter, DataRow row, DataTableMapping mappings, MissingMappingAction missingMapping, MissingSchemaAction missingSchema)
 {
     if (0 != (ParameterDirection.Output & parameter.Direction))
     {
         object value = parameter.Value;
         if (null != value)
         {
             // null means default, meaning we leave the current DataRow value alone
             string columnName = parameter.SourceColumn;
             if (!string.IsNullOrEmpty(columnName))
             {
                 DataColumn dataColumn = mappings.GetDataColumn(columnName, null, row.Table, missingMapping, missingSchema);
                 if (null != dataColumn)
                 {
                     if (dataColumn.ReadOnly)
                     {
                         try
                         {
                             dataColumn.ReadOnly = false;
                             row[dataColumn] = value;
                         }
                         finally
                         {
                             dataColumn.ReadOnly = true;
                         }
                     }
                     else
                     {
                         row[dataColumn] = value;
                     }
                 }
             }
         }
     }
 }
Example #15
0
 public DataColumnMapping?GetColumnMappingBySchemaAction(string sourceColumn, MissingMappingAction mappingAction)
 {
     return(DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMappings, sourceColumn, mappingAction));
 }
 /// <summary>
 /// Sets the missing mapping action which is called when no mapping was previously defined.
 /// </summary>
 /// <param name="action">The missing mapping action.</param>
 public void SetMissingMappingAction(MissingMappingAction action)
 {
     this.Action = action;
 }
Example #17
0
 [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
 public DataColumn GetDataColumn(string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction) {
     return DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction);
 }
        [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]   // MDAC 69508
        static public DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
        {
            if (null != columnMappings)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo)
                    {
                        Debug.WriteLine("mapping match on SourceColumn \"" + sourceColumn + "\"");
                    }
#endif
                    return(columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction));
                }
            }
            if (ADP.IsEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn("sourceColumn");
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceInfo)
                {
                    Debug.WriteLine("mapping passthrough of SourceColumn \"" + sourceColumn + "\"");
                }
#endif
                return(DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction));

            case MissingMappingAction.Ignore:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("mapping filter of SourceColumn \"" + sourceColumn + "\"");
                }
#endif
                return(null);

            case MissingMappingAction.Error:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceError)
                {
                    Debug.WriteLine("mapping error on SourceColumn \"" + sourceColumn + "\"");
                }
#endif
                throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
 public static T CreateDeleteDataAdapter <T>([NotNull] this DbProviderFactory thisValue, IDbCommand command, MissingSchemaAction missingSchemaAction = MissingSchemaAction.AddWithKey, MissingMappingAction missingMappingAction = MissingMappingAction.Passthrough)
     where T : IDbDataAdapter
 {
     return(CreateDataAdapter <T>(thisValue, null, null, null, command, missingSchemaAction, missingMappingAction));
 }
        private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
        {
            DbSchemaRow[] sortedSchemaRows = DbSchemaRow.GetSortedSchemaRows(this._schemaTable, this._dataReader.ReturnProviderSpecificTypes);
            if (sortedSchemaRows.Length == 0)
            {
                this._dataTable = null;
                return(null);
            }
            bool flag3 = ((this._dataTable.PrimaryKey.Length == 0) && ((((LoadOption)4) <= this._loadOption) || (this._dataTable.Rows.Count == 0))) || (0 == this._dataTable.Columns.Count);

            DataColumn[] rgcol = null;
            int          len   = 0;
            bool         flag2 = true;
            string       str3  = null;
            string       str2  = null;
            bool         flag6 = false;
            bool         flag  = false;

            int[]  numArray  = null;
            bool[] flagArray = null;
            int    num3      = 0;

            object[]             objArray         = null;
            List <object>        items            = null;
            DataColumnCollection columnCollection = this._dataTable.Columns;

            try
            {
                for (int i = 0; i < sortedSchemaRows.Length; i++)
                {
                    DbSchemaRow row           = sortedSchemaRows[i];
                    int         unsortedIndex = row.UnsortedIndex;
                    bool        flag5         = false;
                    Type        dataType      = row.DataType;
                    if (null == dataType)
                    {
                        dataType = this._dataReader.GetFieldType(i);
                    }
                    if (null == dataType)
                    {
                        throw ADP.MissingDataReaderFieldType(i);
                    }
                    if (typeof(IDataReader).IsAssignableFrom(dataType))
                    {
                        if (flagArray == null)
                        {
                            flagArray = new bool[sortedSchemaRows.Length];
                        }
                        flagArray[unsortedIndex] = flag5 = true;
                        dataType = typeof(int);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(dataType))
                    {
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[sortedSchemaRows.Length];
                        }
                        this._xmlMap[i] = 1;
                    }
                    else if (typeof(XmlReader).IsAssignableFrom(dataType))
                    {
                        dataType = typeof(string);
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[sortedSchemaRows.Length];
                        }
                        this._xmlMap[i] = 2;
                    }
                    DataColumn targetColumn = null;
                    if (!row.IsHidden)
                    {
                        targetColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], dataType, this._dataTable, mappingAction, schemaAction);
                    }
                    string baseTableName = row.BaseTableName;
                    if (targetColumn == null)
                    {
                        if (numArray == null)
                        {
                            numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                        }
                        numArray[unsortedIndex] = -1;
                        if (row.IsKey && (flag6 || (row.BaseTableName == str3)))
                        {
                            flag3 = false;
                            rgcol = null;
                        }
                    }
                    else
                    {
                        if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                        {
                            if (typeof(System.Data.SqlTypes.SqlXml) == targetColumn.DataType)
                            {
                                this._xmlMap[i] = 1;
                            }
                            else if (typeof(System.Xml.XmlDocument) == targetColumn.DataType)
                            {
                                this._xmlMap[i] = 2;
                            }
                            else
                            {
                                this._xmlMap[i] = 0;
                                int num7 = 0;
                                for (int j = 0; j < this._xmlMap.Length; j++)
                                {
                                    num7 += this._xmlMap[j];
                                }
                                if (num7 == 0)
                                {
                                    this._xmlMap = null;
                                }
                            }
                        }
                        if (row.IsKey && (baseTableName != str3))
                        {
                            if (str3 == null)
                            {
                                str3 = baseTableName;
                            }
                            else
                            {
                                flag6 = true;
                            }
                        }
                        if (flag5)
                        {
                            if (targetColumn.Table != null)
                            {
                                if (!targetColumn.AutoIncrement)
                                {
                                    throw ADP.FillChapterAutoIncrement();
                                }
                            }
                            else
                            {
                                targetColumn.AllowDBNull   = false;
                                targetColumn.AutoIncrement = true;
                                targetColumn.ReadOnly      = true;
                            }
                        }
                        else
                        {
                            if ((!flag && (baseTableName != str2)) && !ADP.IsEmpty(baseTableName))
                            {
                                if (str2 == null)
                                {
                                    str2 = baseTableName;
                                }
                                else
                                {
                                    flag = true;
                                }
                            }
                            if (((LoadOption)4) <= this._loadOption)
                            {
                                if (row.IsAutoIncrement && DataColumn.IsAutoIncrementType(dataType))
                                {
                                    targetColumn.AutoIncrement = true;
                                    if (!row.AllowDBNull)
                                    {
                                        targetColumn.AllowDBNull = false;
                                    }
                                }
                                if (dataType == typeof(string))
                                {
                                    targetColumn.MaxLength = (row.Size > 0) ? row.Size : -1;
                                }
                                if (row.IsReadOnly)
                                {
                                    targetColumn.ReadOnly = true;
                                }
                                if (!row.AllowDBNull && (!row.IsReadOnly || row.IsKey))
                                {
                                    targetColumn.AllowDBNull = false;
                                }
                                if ((row.IsUnique && !row.IsKey) && !dataType.IsArray)
                                {
                                    targetColumn.Unique = true;
                                    if (!row.AllowDBNull)
                                    {
                                        targetColumn.AllowDBNull = false;
                                    }
                                }
                            }
                            else if (targetColumn.Table == null)
                            {
                                targetColumn.AutoIncrement = row.IsAutoIncrement;
                                targetColumn.AllowDBNull   = row.AllowDBNull;
                                targetColumn.ReadOnly      = row.IsReadOnly;
                                targetColumn.Unique        = row.IsUnique;
                                if ((dataType == typeof(string)) || (dataType == typeof(SqlString)))
                                {
                                    targetColumn.MaxLength = row.Size;
                                }
                            }
                        }
                        if (targetColumn.Table == null)
                        {
                            if (((LoadOption)4) > this._loadOption)
                            {
                                this.AddAdditionalProperties(targetColumn, row.DataRow);
                            }
                            this.AddItemToAllowRollback(ref items, targetColumn);
                            columnCollection.Add(targetColumn);
                        }
                        if (flag3 && row.IsKey)
                        {
                            if (rgcol == null)
                            {
                                rgcol = new DataColumn[sortedSchemaRows.Length];
                            }
                            rgcol[len++] = targetColumn;
                            if (flag2 && targetColumn.AllowDBNull)
                            {
                                flag2 = false;
                            }
                        }
                        if (numArray != null)
                        {
                            numArray[unsortedIndex] = targetColumn.Ordinal;
                        }
                        else if (unsortedIndex != targetColumn.Ordinal)
                        {
                            numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                            numArray[unsortedIndex] = targetColumn.Ordinal;
                        }
                        num3++;
                    }
                }
                bool       flag4   = false;
                DataColumn column2 = null;
                if (chapterValue != null)
                {
                    Type type = chapterValue.GetType();
                    column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
                    if (column2 != null)
                    {
                        if (column2.Table == null)
                        {
                            column2.ReadOnly    = true;
                            column2.AllowDBNull = false;
                            this.AddItemToAllowRollback(ref items, column2);
                            columnCollection.Add(column2);
                            flag4 = null != parentChapterColumn;
                        }
                        num3++;
                    }
                }
                if (0 < num3)
                {
                    if ((this._dataSet != null) && (this._dataTable.DataSet == null))
                    {
                        this.AddItemToAllowRollback(ref items, this._dataTable);
                        this._dataSet.Tables.Add(this._dataTable);
                    }
                    if (flag3 && (rgcol != null))
                    {
                        if (len < rgcol.Length)
                        {
                            rgcol = this.ResizeColumnArray(rgcol, len);
                        }
                        if (flag2)
                        {
                            this._dataTable.PrimaryKey = rgcol;
                        }
                        else
                        {
                            UniqueConstraint     constraint  = new UniqueConstraint("", rgcol);
                            ConstraintCollection constraints = this._dataTable.Constraints;
                            int count = constraints.Count;
                            for (int k = 0; k < count; k++)
                            {
                                if (constraint.Equals(constraints[k]))
                                {
                                    constraint = null;
                                    break;
                                }
                            }
                            if (constraint != null)
                            {
                                constraints.Add(constraint);
                            }
                        }
                    }
                    if ((!flag && !ADP.IsEmpty(str2)) && ADP.IsEmpty(this._dataTable.TableName))
                    {
                        this._dataTable.TableName = str2;
                    }
                    if (gettingData)
                    {
                        this._indexMap   = numArray;
                        this._chapterMap = flagArray;
                        objArray         = this.SetupMapping(sortedSchemaRows.Length, columnCollection, column2, chapterValue);
                    }
                    else
                    {
                        this._mappedMode = -1;
                    }
                }
                else
                {
                    this._dataTable = null;
                }
                if (flag4)
                {
                    this.AddRelation(parentChapterColumn, column2);
                }
            }
            catch (Exception exception)
            {
                if (ADP.IsCatchableOrSecurityExceptionType(exception))
                {
                    this.RollbackAddedItems(items);
                }
                throw;
            }
            return(objArray);
        }
        public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
        {
            if (null != columnMappings)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo)
                    {
                        Debug.WriteLine($"mapping match on SourceColumn \"{sourceColumn}\"");
                    }
#endif
                    return columnMappings._items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction);
                }
            }
            if (string.IsNullOrEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn(nameof(sourceColumn));
            }
            switch (mappingAction)
            {
                case MissingMappingAction.Passthrough:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo)
                    {
                        Debug.WriteLine($"mapping passthrough of SourceColumn \"{sourceColumn}\"");
                    }
#endif
                    return DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction);

                case MissingMappingAction.Ignore:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceWarning)
                    {
                        Debug.WriteLine($"mapping filter of SourceColumn \"{sourceColumn}\"");
                    }
#endif
                    return null;

                case MissingMappingAction.Error:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceError)
                    {
                        Debug.WriteLine($"mapping error on SourceColumn \"{sourceColumn}\"");
                    }
#endif
                    throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
Example #22
0
 public static DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
 {
     if (tableMappings.Contains(sourceTable))
     {
         return(tableMappings[sourceTable]);
     }
     if (mappingAction == MissingMappingAction.Error)
     {
         throw new InvalidOperationException(String.Format("Missing source table mapping: '{0}'",
                                                           sourceTable));
     }
     if (mappingAction == MissingMappingAction.Ignore)
     {
         return(null);
     }
     return(new DataTableMapping(sourceTable, dataSetTable));
 }
 internal static ArgumentOutOfRangeException InvalidMissingMappingAction(MissingMappingAction value)
 {
     return InvalidEnumerationValue(typeof(MissingMappingAction), (int) value);
 }
        [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
        static public DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction) {
            if (null != columnMappings) {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index) {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo) {
                        Debug.WriteLine("mapping match on SourceColumn \"" + sourceColumn + "\"");
                    }
#endif
                    return columnMappings.items[index];
                }
            }
            if (ADP.IsEmpty(sourceColumn)) {
                throw ADP.InvalidSourceColumn("sourceColumn");
            }
            switch (mappingAction) {
                case MissingMappingAction.Passthrough:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo) {
                        Debug.WriteLine("mapping passthrough of SourceColumn \"" + sourceColumn + "\"");
                    }
#endif
                    return new DataColumnMapping(sourceColumn, sourceColumn);

                case MissingMappingAction.Ignore:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceWarning) {
                        Debug.WriteLine("mapping filter of SourceColumn \"" + sourceColumn + "\"");
                    }
#endif
                    return null;

                case MissingMappingAction.Error:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceError) {
                        Debug.WriteLine("mapping error on SourceColumn \"" + sourceColumn + "\"");
                    }
#endif
                    throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
Example #25
0
        public static DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
        {
            if (null != tableMappings)
            {
                int index = tableMappings.IndexOf(sourceTable);
                if (-1 != index)
                {
                    return(tableMappings._items[index]);
                }
            }
            if (string.IsNullOrEmpty(sourceTable))
            {
                throw ADP.InvalidSourceTable(nameof(sourceTable));
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
                return(new DataTableMapping(sourceTable, dataSetTable));

            case MissingMappingAction.Ignore:
                return(null);

            case MissingMappingAction.Error:
                throw ADP.MissingTableMapping(sourceTable);

            default:
                throw ADP.InvalidMissingMappingAction(mappingAction);
            }
        }
        private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
        {
            int[]  numArray   = null;
            bool[] flagArray  = null;
            int    num3       = 0;
            int    fieldCount = this._dataReader.FieldCount;

            object[]      objArray = null;
            List <object> items    = null;

            try
            {
                DataColumnCollection columnCollection = this._dataTable.Columns;
                for (int i = 0; i < fieldCount; i++)
                {
                    bool flag      = false;
                    Type fieldType = this._dataReader.GetFieldType(i);
                    if (null == fieldType)
                    {
                        throw ADP.MissingDataReaderFieldType(i);
                    }
                    if (typeof(IDataReader).IsAssignableFrom(fieldType))
                    {
                        if (flagArray == null)
                        {
                            flagArray = new bool[fieldCount];
                        }
                        flagArray[i] = flag = true;
                        fieldType    = typeof(int);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType))
                    {
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[fieldCount];
                        }
                        this._xmlMap[i] = 1;
                    }
                    else if (typeof(XmlReader).IsAssignableFrom(fieldType))
                    {
                        fieldType = typeof(string);
                        if (this._xmlMap == null)
                        {
                            this._xmlMap = new int[fieldCount];
                        }
                        this._xmlMap[i] = 2;
                    }
                    DataColumn column = this._tableMapping.GetDataColumn(this._fieldNames[i], fieldType, this._dataTable, mappingAction, schemaAction);
                    if (column == null)
                    {
                        if (numArray == null)
                        {
                            numArray = this.CreateIndexMap(fieldCount, i);
                        }
                        numArray[i] = -1;
                    }
                    else
                    {
                        if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                        {
                            if (typeof(System.Data.SqlTypes.SqlXml) == column.DataType)
                            {
                                this._xmlMap[i] = 1;
                            }
                            else if (typeof(System.Xml.XmlDocument) == column.DataType)
                            {
                                this._xmlMap[i] = 2;
                            }
                            else
                            {
                                this._xmlMap[i] = 0;
                                int num5 = 0;
                                for (int j = 0; j < this._xmlMap.Length; j++)
                                {
                                    num5 += this._xmlMap[j];
                                }
                                if (num5 == 0)
                                {
                                    this._xmlMap = null;
                                }
                            }
                        }
                        if (column.Table == null)
                        {
                            if (flag)
                            {
                                column.AllowDBNull   = false;
                                column.AutoIncrement = true;
                                column.ReadOnly      = true;
                            }
                            this.AddItemToAllowRollback(ref items, column);
                            columnCollection.Add(column);
                        }
                        else if (flag && !column.AutoIncrement)
                        {
                            throw ADP.FillChapterAutoIncrement();
                        }
                        if (numArray != null)
                        {
                            numArray[i] = column.Ordinal;
                        }
                        else if (i != column.Ordinal)
                        {
                            numArray    = this.CreateIndexMap(fieldCount, i);
                            numArray[i] = column.Ordinal;
                        }
                        num3++;
                    }
                }
                bool       flag2   = false;
                DataColumn column2 = null;
                if (chapterValue != null)
                {
                    Type type = chapterValue.GetType();
                    column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
                    if (column2 != null)
                    {
                        if (column2.Table == null)
                        {
                            this.AddItemToAllowRollback(ref items, column2);
                            columnCollection.Add(column2);
                            flag2 = null != parentChapterColumn;
                        }
                        num3++;
                    }
                }
                if (0 < num3)
                {
                    if ((this._dataSet != null) && (this._dataTable.DataSet == null))
                    {
                        this.AddItemToAllowRollback(ref items, this._dataTable);
                        this._dataSet.Tables.Add(this._dataTable);
                    }
                    if (gettingData)
                    {
                        if (columnCollection == null)
                        {
                            columnCollection = this._dataTable.Columns;
                        }
                        this._indexMap   = numArray;
                        this._chapterMap = flagArray;
                        objArray         = this.SetupMapping(fieldCount, columnCollection, column2, chapterValue);
                    }
                    else
                    {
                        this._mappedMode = -1;
                    }
                }
                else
                {
                    this._dataTable = null;
                }
                if (flag2)
                {
                    this.AddRelation(parentChapterColumn, column2);
                }
            }
            catch (Exception exception)
            {
                if (ADP.IsCatchableOrSecurityExceptionType(exception))
                {
                    this.RollbackAddedItems(items);
                }
                throw;
            }
            return(objArray);
        }
Example #27
0
 private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
 {
     int[] array = null;
     bool[] array2 = null;
     int num = 0;
     int fieldCount = this._dataReader.FieldCount;
     object[] result = null;
     List<object> items = null;
     try
     {
         DataColumnCollection columns = this._dataTable.Columns;
         bool flag = this._dataTable.Columns.Count == 0 && (this._tableMapping.ColumnMappings == null || this._tableMapping.ColumnMappings.Count == 0) && mappingAction == MissingMappingAction.Passthrough;
         for (int i = 0; i < fieldCount; i++)
         {
             bool flag2 = false;
             Type type = this._dataReader.GetFieldType(i);
             if (null == type)
             {
                 throw new Exception("MissingDataReaderFieldType");
             }
             if (typeof(IDataReader).IsAssignableFrom(type))
             {
                 if (array2 == null)
                 {
                     array2 = new bool[fieldCount];
                 }
                 flag2 = (array2[i] = true);
                 type = typeof(int);
             }
             else
             {
                 if (typeof(SqlXml).IsAssignableFrom(type))
                 {
                     if (this._xmlMap == null)
                     {
                         this._xmlMap = new int[fieldCount];
                     }
                     this._xmlMap[i] = 1;
                 }
                 else
                 {
                     if (typeof(XmlReader).IsAssignableFrom(type))
                     {
                         type = typeof(string);
                         if (this._xmlMap == null)
                         {
                             this._xmlMap = new int[fieldCount];
                         }
                         this._xmlMap[i] = 2;
                     }
                 }
             }
             DataColumn dataColumn;
             if (flag)
             {
                 dataColumn = CreateDataColumnBySchemaAction(this._fieldNames[i], this._fieldNames[i], this._dataTable, type, schemaAction);
             }
             else
             {
                 dataColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], type, this._dataTable, mappingAction, schemaAction);
             }
             if (dataColumn == null)
             {
                 if (array == null)
                 {
                     array = this.CreateIndexMap(fieldCount, i);
                 }
                 array[i] = -1;
             }
             else
             {
                 if (this._xmlMap != null && this._xmlMap[i] != 0)
                 {
                     if (typeof(SqlXml) == dataColumn.DataType)
                     {
                         this._xmlMap[i] = 1;
                     }
                     else
                     {
                         if (typeof(XmlDocument) == dataColumn.DataType)
                         {
                             this._xmlMap[i] = 2;
                         }
                         else
                         {
                             this._xmlMap[i] = 0;
                             int num2 = 0;
                             for (int j = 0; j < this._xmlMap.Length; j++)
                             {
                                 num2 += this._xmlMap[j];
                             }
                             if (num2 == 0)
                             {
                                 this._xmlMap = null;
                             }
                         }
                     }
                 }
                 if (dataColumn.Table == null)
                 {
                     if (flag2)
                     {
                         dataColumn.AllowDBNull = false;
                         dataColumn.AutoIncrement = true;
                         dataColumn.ReadOnly = true;
                     }
                     this.AddItemToAllowRollback(ref items, dataColumn);
                     columns.Add(dataColumn);
                 }
                 else
                 {
                     if (flag2 && !dataColumn.AutoIncrement)
                     {
                         throw new Exception("FillChapterAutoIncrement");
                     }
                 }
                 if (array != null)
                 {
                     array[i] = dataColumn.Ordinal;
                 }
                 else
                 {
                     if (i != dataColumn.Ordinal)
                     {
                         array = this.CreateIndexMap(fieldCount, i);
                         array[i] = dataColumn.Ordinal;
                     }
                 }
                 num++;
             }
         }
         bool flag3 = false;
         DataColumn dataColumn2 = null;
         if (chapterValue != null)
         {
             Type type2 = chapterValue.GetType();
             dataColumn2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type2, this._dataTable, mappingAction, schemaAction);
             if (dataColumn2 != null)
             {
                 if (dataColumn2.Table == null)
                 {
                     this.AddItemToAllowRollback(ref items, dataColumn2);
                     columns.Add(dataColumn2);
                     flag3 = (null != parentChapterColumn);
                 }
                 num++;
             }
         }
         if (0 < num)
         {
             if (this._dataSet != null && this._dataTable.DataSet == null)
             {
                 this.AddItemToAllowRollback(ref items, this._dataTable);
                 this._dataSet.Tables.Add(this._dataTable);
             }
             if (gettingData)
             {
                 if (columns == null)
                 {
                     columns = this._dataTable.Columns;
                 }
                 this._indexMap = array;
                 this._chapterMap = array2;
                 result = this.SetupMapping(fieldCount, columns, dataColumn2, chapterValue);
             }
             else
             {
                 this._mappedMode = -1;
             }
         }
         else
         {
             this._dataTable = null;
         }
         if (flag3)
         {
             this.AddRelation(parentChapterColumn, dataColumn2);
         }
     }
     catch (Exception e)
     {
         this.RollbackAddedItems(items);
         throw e;
     }
     return result;
 }
        [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
        static public DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) {
            if (null != tableMappings) {
                int index = tableMappings.IndexOf(sourceTable);
                if (-1 != index) {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceWarning) {
                        Debug.WriteLine("mapping match on SourceTable \"" + sourceTable + "\"");
                    }
#endif
                    return tableMappings.items[index];
                }
            }
            if (ADP.IsEmpty(sourceTable)) {
                throw ADP.InvalidSourceTable("sourceTable");
            }
            switch (mappingAction) {
                case MissingMappingAction.Passthrough:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceInfo) {
                        Debug.WriteLine("mapping passthrough of SourceTable \"" + sourceTable + "\" -> \"" + dataSetTable + "\"");
                    }
#endif
                    return new DataTableMapping(sourceTable, dataSetTable);

                case MissingMappingAction.Ignore:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceWarning) {
                        Debug.WriteLine("mapping filter of SourceTable \"" + sourceTable + "\"");
                    }
#endif
                    return null;

                case MissingMappingAction.Error:
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceError) {
                        Debug.WriteLine("mapping error on SourceTable \"" + sourceTable + "\"");
                    }
#endif
                    throw ADP.MissingTableMapping(sourceTable);

                default:
                    throw ADP.InvalidMissingMappingAction(mappingAction);
            }
        }
Example #29
0
 public static DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
 {
     if (columnMappings.Contains(sourceColumn))
     {
         return(columnMappings[sourceColumn]);
     }
     if (mappingAction == MissingMappingAction.Ignore)
     {
         return(null);
     }
     if (mappingAction == MissingMappingAction.Error)
     {
         throw new InvalidOperationException(String.Format("Missing SourceColumn mapping for '{0}'", sourceColumn));
     }
     return(new DataColumnMapping(sourceColumn, sourceColumn));
 }
 internal DataTableMapping GetTableMappingBySchemaAction(string sourceTableName, string dataSetTableName, MissingMappingAction mappingAction)
 {
     return(DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMappings, sourceTableName, dataSetTableName, mappingAction));
 }
        private void BuildCache(bool closeConnection)
        {
            IDbCommand srcCommand = SourceCommand;

            if (null == srcCommand)
            {
                throw ADP.MissingSourceCommand();
            }

            IDbConnection connection = srcCommand.Connection;

            if (null == connection)
            {
                throw ADP.MissingSourceCommandConnection();
            }

            if (null != DataAdapter)
            {
                this.missingMapping = DataAdapter.MissingMappingAction;
                if (MissingMappingAction.Passthrough != missingMapping)
                {
                    missingMapping = MissingMappingAction.Error;
                }
            }

            if (null == this.dbSchemaTable)
            {
                if (0 == (ConnectionState.Open & connection.State))
                {
                    connection.Open();
                }
                else
                {
                    closeConnection = false; // MDAC 58710
                }
                try {                        // try-filter-finally so and catch-throw
                    try {
                        DataTable schemaTable = null;
                        using (IDataReader dataReader = srcCommand.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo)) {
                            schemaTable = dataReader.GetSchemaTable();
                        }

                        if (null != schemaTable)
                        {
#if DEBUG
                            if (AdapterSwitches.OleDbSql.TraceVerbose)
                            {
                                ADP.TraceDataTable("CommandBuilder", schemaTable);
                            }
#endif
                            BuildInformation(schemaTable);
                            this.dbSchemaTable = schemaTable;

                            int count = this.dbSchemaRows.Length;
                            sourceColumnNames = new string[count];
                            for (int i = 0; i < count; ++i)
                            {
                                if (null != dbSchemaRows[i])
                                {
                                    sourceColumnNames[i] = dbSchemaRows[i].ColumnName;
                                }
                            }
                            ADP.BuildSchemaTableInfoTableNames(sourceColumnNames);
                        }
                        else   // MDAC 66620
                        {
                            throw ADP.DynamicSQLNoTableInfo();
                        }
                    }
                    finally { // Close
                        if (closeConnection)
                        {
                            connection.Close();
                        }
                    }
                }
                catch { // MDAC 80973
                    throw;
                }
            }
        }
		public static DataColumn GetDataColumn (DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
		{
			throw new NotImplementedException ();
		}
Example #33
0
 [ EditorBrowsableAttribute(EditorBrowsableState.Advanced) ] // MDAC 69508
 public DataColumnMapping GetColumnMappingBySchemaAction(string sourceColumn, MissingMappingAction mappingAction) {
     return DataColumnMappingCollection.GetColumnMappingBySchemaAction(_columnMappings, sourceColumn, mappingAction);
 }
		public static DataTableMapping GetTableMappingBySchemaAction (DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction) 
		{
			if (tableMappings.Contains (sourceTable))
				return tableMappings[sourceTable];
			if (mappingAction == MissingMappingAction.Error)
				throw new InvalidOperationException ();
			if (mappingAction == MissingMappingAction.Ignore)
				return null;
			return new DataTableMapping (sourceTable, dataSetTable);
		}
Example #35
0
 public DataColumn?GetDataColumn(string sourceColumn, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.PublicFields)] Type?dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
 {
     return(DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction));
 }
 private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
 {
     int[] numArray = null;
     bool[] flagArray = null;
     int num3 = 0;
     int fieldCount = this._dataReader.FieldCount;
     object[] objArray = null;
     List<object> items = null;
     try
     {
         DataColumnCollection columnCollection = this._dataTable.Columns;
         for (int i = 0; i < fieldCount; i++)
         {
             bool flag = false;
             Type fieldType = this._dataReader.GetFieldType(i);
             if (null == fieldType)
             {
                 throw ADP.MissingDataReaderFieldType(i);
             }
             if (typeof(IDataReader).IsAssignableFrom(fieldType))
             {
                 if (flagArray == null)
                 {
                     flagArray = new bool[fieldCount];
                 }
                 flagArray[i] = flag = true;
                 fieldType = typeof(int);
             }
             else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType))
             {
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[fieldCount];
                 }
                 this._xmlMap[i] = 1;
             }
             else if (typeof(XmlReader).IsAssignableFrom(fieldType))
             {
                 fieldType = typeof(string);
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[fieldCount];
                 }
                 this._xmlMap[i] = 2;
             }
             DataColumn column = this._tableMapping.GetDataColumn(this._fieldNames[i], fieldType, this._dataTable, mappingAction, schemaAction);
             if (column == null)
             {
                 if (numArray == null)
                 {
                     numArray = this.CreateIndexMap(fieldCount, i);
                 }
                 numArray[i] = -1;
             }
             else
             {
                 if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                 {
                     if (typeof(System.Data.SqlTypes.SqlXml) == column.DataType)
                     {
                         this._xmlMap[i] = 1;
                     }
                     else if (typeof(System.Xml.XmlDocument) == column.DataType)
                     {
                         this._xmlMap[i] = 2;
                     }
                     else
                     {
                         this._xmlMap[i] = 0;
                         int num5 = 0;
                         for (int j = 0; j < this._xmlMap.Length; j++)
                         {
                             num5 += this._xmlMap[j];
                         }
                         if (num5 == 0)
                         {
                             this._xmlMap = null;
                         }
                     }
                 }
                 if (column.Table == null)
                 {
                     if (flag)
                     {
                         column.AllowDBNull = false;
                         column.AutoIncrement = true;
                         column.ReadOnly = true;
                     }
                     this.AddItemToAllowRollback(ref items, column);
                     columnCollection.Add(column);
                 }
                 else if (flag && !column.AutoIncrement)
                 {
                     throw ADP.FillChapterAutoIncrement();
                 }
                 if (numArray != null)
                 {
                     numArray[i] = column.Ordinal;
                 }
                 else if (i != column.Ordinal)
                 {
                     numArray = this.CreateIndexMap(fieldCount, i);
                     numArray[i] = column.Ordinal;
                 }
                 num3++;
             }
         }
         bool flag2 = false;
         DataColumn column2 = null;
         if (chapterValue != null)
         {
             Type type = chapterValue.GetType();
             column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
             if (column2 != null)
             {
                 if (column2.Table == null)
                 {
                     this.AddItemToAllowRollback(ref items, column2);
                     columnCollection.Add(column2);
                     flag2 = null != parentChapterColumn;
                 }
                 num3++;
             }
         }
         if (0 < num3)
         {
             if ((this._dataSet != null) && (this._dataTable.DataSet == null))
             {
                 this.AddItemToAllowRollback(ref items, this._dataTable);
                 this._dataSet.Tables.Add(this._dataTable);
             }
             if (gettingData)
             {
                 if (columnCollection == null)
                 {
                     columnCollection = this._dataTable.Columns;
                 }
                 this._indexMap = numArray;
                 this._chapterMap = flagArray;
                 objArray = this.SetupMapping(fieldCount, columnCollection, column2, chapterValue);
             }
             else
             {
                 this._mappedMode = -1;
             }
         }
         else
         {
             this._dataTable = null;
         }
         if (flag2)
         {
             this.AddRelation(parentChapterColumn, column2);
         }
     }
     catch (Exception exception)
     {
         if (ADP.IsCatchableOrSecurityExceptionType(exception))
         {
             this.RollbackAddedItems(items);
         }
         throw;
     }
     return objArray;
 }
Example #37
0
 DataTableMapping IAdaSchemaMappingAdapter.GetTableMappingBySchemaAction(string sourceTableName, string dataSetTableName, MissingMappingAction mappingAction)
 {
     return(DataTableMappingCollection.GetTableMappingBySchemaAction(this.TableMappings, sourceTableName, dataSetTableName, mappingAction));
 }
 /// <summary>
 /// Sets the missing mapping action which is called when no mapping was previously defined.
 /// </summary>
 /// <param name="action">The missing mapping action.</param>
 public void SetMissingMappingAction(MissingMappingAction action)
 {
     this.Action = action;
 }
 public static void ParameterOutput(IDataParameter parameter, DataRow row, DataTableMapping mappings, MissingMappingAction missingMapping, MissingSchemaAction missingSchema)
 {
     if (0 != (ParameterDirection.Output & parameter.Direction))
     {
         object value = parameter.Value;
         if (null != value)
         {
             // null means default, meaning we leave the current DataRow value alone
             string columnName = parameter.SourceColumn;
             if (!string.IsNullOrEmpty(columnName))
             {
                 DataColumn dataColumn = mappings.GetDataColumn(sourceColumn: columnName, dataType: null, dataTable: row.Table, missingMapping, missingSchema);
                 if (null != dataColumn)
                 {
                     if (dataColumn.ReadOnly)
                     {
                         try
                         {
                             dataColumn.ReadOnly = false;
                             row[dataColumn]     = value;
                         }
                         finally
                         {
                             dataColumn.ReadOnly = true;
                         }
                     }
                     else
                     {
                         row[dataColumn] = value;
                     }
                 }
             }
         }
     }
 }
        private object[] SetupSchemaWithoutKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue) {
            int[] columnIndexMap = null;
            bool[] chapterIndexMap = null;

            int mappingCount = 0;
            int count = _dataReader.FieldCount;

            object[] dataValues = null;
            List<object> addedItems = null;
            try {
                DataColumnCollection columnCollection = _dataTable.Columns;
                columnCollection.EnsureAdditionalCapacity(count + (chapterValue != null ? 1 : 0));
                // We can always just create column if there are no existing column or column mappings, and the mapping action is passthrough
                bool alwaysCreateColumns = ((_dataTable.Columns.Count == 0) && ((_tableMapping.ColumnMappings == null) || (_tableMapping.ColumnMappings.Count == 0)) && (mappingAction == MissingMappingAction.Passthrough));

                for (int i = 0; i < count; ++i) {

                    bool ischapter = false;
                    Type fieldType = _dataReader.GetFieldType(i);

                    if (null == fieldType) {
                        throw ADP.MissingDataReaderFieldType(i);
                    }

                    // if IDataReader, hierarchy exists and we will use an Int32,AutoIncrementColumn in this table
                    if (typeof(IDataReader).IsAssignableFrom(fieldType)) {
                        if (null == chapterIndexMap) {
                            chapterIndexMap = new bool[count];
                        }
                        chapterIndexMap[i] = ischapter = true;
                        fieldType = typeof(Int32);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType)) {
                        if (null == _xmlMap) { // map to DataColumn with DataType=typeof(SqlXml)
                            _xmlMap = new int[count];
                        }
                        _xmlMap[i] = SqlXml; // track its xml data
                    }
                    else if (typeof(System.Xml.XmlReader).IsAssignableFrom(fieldType)) {
                        fieldType = typeof(String); // map to DataColumn with DataType=typeof(string)
                        if (null == _xmlMap) {
                            _xmlMap = new int[count];
                        }
                        _xmlMap[i] = XmlDocument; // track its xml data
                    }

                    DataColumn dataColumn;
                    if (alwaysCreateColumns) {
                        dataColumn = DataColumnMapping.CreateDataColumnBySchemaAction(_fieldNames[i], _fieldNames[i], _dataTable, fieldType, schemaAction);
                    }
                    else {
                        dataColumn = _tableMapping.GetDataColumn(_fieldNames[i], fieldType, _dataTable, mappingAction, schemaAction);
                    }

                    if (null == dataColumn) {
                        if (null == columnIndexMap) {
                            columnIndexMap = CreateIndexMap(count, i);
                        }
                        columnIndexMap[i] = -1;
                        continue; // null means ignore (mapped to nothing)
                    }
                    else if ((null != _xmlMap) && (0 != _xmlMap[i])) {
                        if (typeof(System.Data.SqlTypes.SqlXml) == dataColumn.DataType) {
                            _xmlMap[i] = SqlXml;
                        }
                        else if (typeof(System.Xml.XmlDocument) == dataColumn.DataType) {
                            _xmlMap[i] = XmlDocument;
                        }
                        else {
                            _xmlMap[i] = 0; // datacolumn is not a specific Xml dataType, i.e. string

                            int total = 0;
                            for(int x = 0; x < _xmlMap.Length; ++x) {
                                total += _xmlMap[x];
                            }
                            if (0 == total) { // not mapping to a specific Xml datatype, get rid of the map
                                _xmlMap = null;
                            }
                        }
                    }

                    if (null == dataColumn.Table) {
                        if (ischapter) {
                            dataColumn.AllowDBNull = false;
                            dataColumn.AutoIncrement = true;
                            dataColumn.ReadOnly = true;
                        }
                        AddItemToAllowRollback(ref addedItems, dataColumn);
                        columnCollection.Add(dataColumn);
                    }
                    else if (ischapter && !dataColumn.AutoIncrement) {
                        throw ADP.FillChapterAutoIncrement();
                    }


                    if (null != columnIndexMap) {
                        columnIndexMap[i] = dataColumn.Ordinal;
                    }
                    else if (i != dataColumn.Ordinal) {
                        columnIndexMap = CreateIndexMap(count, i);
                        columnIndexMap[i] = dataColumn.Ordinal;
                    }
                    // else i == dataColumn.Ordinal and columnIndexMap can be optimized out

                    mappingCount++;
                }
                bool addDataRelation = false;
                DataColumn chapterColumn = null;
                if (null != chapterValue) { // add the extra column in the child table
                    Type fieldType = chapterValue.GetType();

                    chapterColumn = _tableMapping.GetDataColumn(_tableMapping.SourceTable, fieldType, _dataTable, mappingAction, schemaAction);
                    if (null != chapterColumn) {

                        if (null == chapterColumn.Table) {
                            AddItemToAllowRollback(ref addedItems, chapterColumn);
                            columnCollection.Add(chapterColumn);
                            addDataRelation = (null != parentChapterColumn);
                        }
                        mappingCount++;
                    }
                }

                if (0 < mappingCount) {
                    if ((null != _dataSet) && (null == _dataTable.DataSet)) {
                        // Allowed to throw exception if DataTable is from wrong DataSet
                        AddItemToAllowRollback(ref addedItems, _dataTable);
                        _dataSet.Tables.Add(_dataTable);
                    }
                    if (gettingData) {
                        if (null == columnCollection) {
                            columnCollection = _dataTable.Columns;
                        }
                        _indexMap = columnIndexMap;
                        _chapterMap = chapterIndexMap;
                        dataValues = SetupMapping(count, columnCollection, chapterColumn, chapterValue);
                    }
                    else {
                        // debug only, but for retail debug ability
                        _mappedMode = -1;
                    }
                }
                else {
                    _dataTable = null;
                }

                if (addDataRelation) {
                    AddRelation(parentChapterColumn, chapterColumn);
                }

            }
            catch (Exception e) {
                // 
                if (ADP.IsCatchableOrSecurityExceptionType(e)) {
                    RollbackAddedItems(addedItems);
                }
                throw;
            }
            return dataValues;
        }
 private DbCommand GetSelectCommand()
 {
     DbCommand selectCommand = null;
     DbDataAdapter dataAdapter = this.DataAdapter;
     if (dataAdapter != null)
     {
         if (this._missingMappingAction == ((MissingMappingAction) 0))
         {
             this._missingMappingAction = dataAdapter.MissingMappingAction;
         }
         selectCommand = dataAdapter.SelectCommand;
     }
     if (selectCommand == null)
     {
         throw ADP.MissingSourceCommand();
     }
     return selectCommand;
 }
        public static DataColumnMapping GetColumnMappingBySchemaAction(DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
        {
            if (null != columnMappings)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
                    return(columnMappings._items[index]);
                }
            }
            if (string.IsNullOrEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn(nameof(sourceColumn));
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
                return(new DataColumnMapping(sourceColumn, sourceColumn));

            case MissingMappingAction.Ignore:
                return(null);

            case MissingMappingAction.Error:
                throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
		public static DataColumnMapping GetColumnMappingBySchemaAction (DataColumnMappingCollection columnMappings, string sourceColumn, MissingMappingAction mappingAction)
		{
			if (columnMappings.Contains (sourceColumn))
				return columnMappings[sourceColumn];
			if (mappingAction == MissingMappingAction.Ignore)
				return null;
			if (mappingAction == MissingMappingAction.Error)
				throw new InvalidOperationException (String.Format ("Missing SourceColumn mapping for '{0}'", sourceColumn));
			return new DataColumnMapping (sourceColumn, sourceColumn);
		}
        private void CloneFrom(DataAdapter from) {
            _acceptChangesDuringUpdate = from._acceptChangesDuringUpdate;
            _acceptChangesDuringUpdateAfterInsert = from._acceptChangesDuringUpdateAfterInsert;
            _continueUpdateOnError = from._continueUpdateOnError;
            _returnProviderSpecificTypes = from._returnProviderSpecificTypes; // WebData 101795
            _acceptChangesDuringFill = from._acceptChangesDuringFill;
            _fillLoadOption = from._fillLoadOption;
            _missingMappingAction = from._missingMappingAction;
            _missingSchemaAction = from._missingSchemaAction;

            if ((null != from._tableMappings) && (0 < from.TableMappings.Count)) {
                DataTableMappingCollection parameters = this.TableMappings;
                foreach(object parameter in from.TableMappings) {
                    parameters.Add((parameter is ICloneable) ? ((ICloneable)parameter).Clone() : parameter);
                }
            }
        }
Example #45
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, 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 && DataColumn.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;
		}
 internal DataTableMapping GetTableMappingBySchemaAction(string sourceTableName, string dataSetTableName, MissingMappingAction mappingAction) {
     return DataTableMappingCollection.GetTableMappingBySchemaAction(_tableMappings, sourceTableName, dataSetTableName, mappingAction);
 }
 private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
 {
     DbSchemaRow[] sortedSchemaRows = DbSchemaRow.GetSortedSchemaRows(this._schemaTable, this._dataReader.ReturnProviderSpecificTypes);
     if (sortedSchemaRows.Length == 0)
     {
         this._dataTable = null;
         return null;
     }
     bool flag3 = ((this._dataTable.PrimaryKey.Length == 0) && ((((LoadOption) 4) <= this._loadOption) || (this._dataTable.Rows.Count == 0))) || (0 == this._dataTable.Columns.Count);
     DataColumn[] rgcol = null;
     int len = 0;
     bool flag2 = true;
     string str3 = null;
     string str2 = null;
     bool flag6 = false;
     bool flag = false;
     int[] numArray = null;
     bool[] flagArray = null;
     int num3 = 0;
     object[] objArray = null;
     List<object> items = null;
     DataColumnCollection columnCollection = this._dataTable.Columns;
     try
     {
         for (int i = 0; i < sortedSchemaRows.Length; i++)
         {
             DbSchemaRow row = sortedSchemaRows[i];
             int unsortedIndex = row.UnsortedIndex;
             bool flag5 = false;
             Type dataType = row.DataType;
             if (null == dataType)
             {
                 dataType = this._dataReader.GetFieldType(i);
             }
             if (null == dataType)
             {
                 throw ADP.MissingDataReaderFieldType(i);
             }
             if (typeof(IDataReader).IsAssignableFrom(dataType))
             {
                 if (flagArray == null)
                 {
                     flagArray = new bool[sortedSchemaRows.Length];
                 }
                 flagArray[unsortedIndex] = flag5 = true;
                 dataType = typeof(int);
             }
             else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(dataType))
             {
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[sortedSchemaRows.Length];
                 }
                 this._xmlMap[i] = 1;
             }
             else if (typeof(XmlReader).IsAssignableFrom(dataType))
             {
                 dataType = typeof(string);
                 if (this._xmlMap == null)
                 {
                     this._xmlMap = new int[sortedSchemaRows.Length];
                 }
                 this._xmlMap[i] = 2;
             }
             DataColumn targetColumn = null;
             if (!row.IsHidden)
             {
                 targetColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], dataType, this._dataTable, mappingAction, schemaAction);
             }
             string baseTableName = row.BaseTableName;
             if (targetColumn == null)
             {
                 if (numArray == null)
                 {
                     numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                 }
                 numArray[unsortedIndex] = -1;
                 if (row.IsKey && (flag6 || (row.BaseTableName == str3)))
                 {
                     flag3 = false;
                     rgcol = null;
                 }
             }
             else
             {
                 if ((this._xmlMap != null) && (this._xmlMap[i] != 0))
                 {
                     if (typeof(System.Data.SqlTypes.SqlXml) == targetColumn.DataType)
                     {
                         this._xmlMap[i] = 1;
                     }
                     else if (typeof(System.Xml.XmlDocument) == targetColumn.DataType)
                     {
                         this._xmlMap[i] = 2;
                     }
                     else
                     {
                         this._xmlMap[i] = 0;
                         int num7 = 0;
                         for (int j = 0; j < this._xmlMap.Length; j++)
                         {
                             num7 += this._xmlMap[j];
                         }
                         if (num7 == 0)
                         {
                             this._xmlMap = null;
                         }
                     }
                 }
                 if (row.IsKey && (baseTableName != str3))
                 {
                     if (str3 == null)
                     {
                         str3 = baseTableName;
                     }
                     else
                     {
                         flag6 = true;
                     }
                 }
                 if (flag5)
                 {
                     if (targetColumn.Table != null)
                     {
                         if (!targetColumn.AutoIncrement)
                         {
                             throw ADP.FillChapterAutoIncrement();
                         }
                     }
                     else
                     {
                         targetColumn.AllowDBNull = false;
                         targetColumn.AutoIncrement = true;
                         targetColumn.ReadOnly = true;
                     }
                 }
                 else
                 {
                     if ((!flag && (baseTableName != str2)) && !ADP.IsEmpty(baseTableName))
                     {
                         if (str2 == null)
                         {
                             str2 = baseTableName;
                         }
                         else
                         {
                             flag = true;
                         }
                     }
                     if (((LoadOption) 4) <= this._loadOption)
                     {
                         if (row.IsAutoIncrement && DataColumn.IsAutoIncrementType(dataType))
                         {
                             targetColumn.AutoIncrement = true;
                             if (!row.AllowDBNull)
                             {
                                 targetColumn.AllowDBNull = false;
                             }
                         }
                         if (dataType == typeof(string))
                         {
                             targetColumn.MaxLength = (row.Size > 0) ? row.Size : -1;
                         }
                         if (row.IsReadOnly)
                         {
                             targetColumn.ReadOnly = true;
                         }
                         if (!row.AllowDBNull && (!row.IsReadOnly || row.IsKey))
                         {
                             targetColumn.AllowDBNull = false;
                         }
                         if ((row.IsUnique && !row.IsKey) && !dataType.IsArray)
                         {
                             targetColumn.Unique = true;
                             if (!row.AllowDBNull)
                             {
                                 targetColumn.AllowDBNull = false;
                             }
                         }
                     }
                     else if (targetColumn.Table == null)
                     {
                         targetColumn.AutoIncrement = row.IsAutoIncrement;
                         targetColumn.AllowDBNull = row.AllowDBNull;
                         targetColumn.ReadOnly = row.IsReadOnly;
                         targetColumn.Unique = row.IsUnique;
                         if ((dataType == typeof(string)) || (dataType == typeof(SqlString)))
                         {
                             targetColumn.MaxLength = row.Size;
                         }
                     }
                 }
                 if (targetColumn.Table == null)
                 {
                     if (((LoadOption) 4) > this._loadOption)
                     {
                         this.AddAdditionalProperties(targetColumn, row.DataRow);
                     }
                     this.AddItemToAllowRollback(ref items, targetColumn);
                     columnCollection.Add(targetColumn);
                 }
                 if (flag3 && row.IsKey)
                 {
                     if (rgcol == null)
                     {
                         rgcol = new DataColumn[sortedSchemaRows.Length];
                     }
                     rgcol[len++] = targetColumn;
                     if (flag2 && targetColumn.AllowDBNull)
                     {
                         flag2 = false;
                     }
                 }
                 if (numArray != null)
                 {
                     numArray[unsortedIndex] = targetColumn.Ordinal;
                 }
                 else if (unsortedIndex != targetColumn.Ordinal)
                 {
                     numArray = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                     numArray[unsortedIndex] = targetColumn.Ordinal;
                 }
                 num3++;
             }
         }
         bool flag4 = false;
         DataColumn column2 = null;
         if (chapterValue != null)
         {
             Type type = chapterValue.GetType();
             column2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type, this._dataTable, mappingAction, schemaAction);
             if (column2 != null)
             {
                 if (column2.Table == null)
                 {
                     column2.ReadOnly = true;
                     column2.AllowDBNull = false;
                     this.AddItemToAllowRollback(ref items, column2);
                     columnCollection.Add(column2);
                     flag4 = null != parentChapterColumn;
                 }
                 num3++;
             }
         }
         if (0 < num3)
         {
             if ((this._dataSet != null) && (this._dataTable.DataSet == null))
             {
                 this.AddItemToAllowRollback(ref items, this._dataTable);
                 this._dataSet.Tables.Add(this._dataTable);
             }
             if (flag3 && (rgcol != null))
             {
                 if (len < rgcol.Length)
                 {
                     rgcol = this.ResizeColumnArray(rgcol, len);
                 }
                 if (flag2)
                 {
                     this._dataTable.PrimaryKey = rgcol;
                 }
                 else
                 {
                     UniqueConstraint constraint = new UniqueConstraint("", rgcol);
                     ConstraintCollection constraints = this._dataTable.Constraints;
                     int count = constraints.Count;
                     for (int k = 0; k < count; k++)
                     {
                         if (constraint.Equals(constraints[k]))
                         {
                             constraint = null;
                             break;
                         }
                     }
                     if (constraint != null)
                     {
                         constraints.Add(constraint);
                     }
                 }
             }
             if ((!flag && !ADP.IsEmpty(str2)) && ADP.IsEmpty(this._dataTable.TableName))
             {
                 this._dataTable.TableName = str2;
             }
             if (gettingData)
             {
                 this._indexMap = numArray;
                 this._chapterMap = flagArray;
                 objArray = this.SetupMapping(sortedSchemaRows.Length, columnCollection, column2, chapterValue);
             }
             else
             {
                 this._mappedMode = -1;
             }
         }
         else
         {
             this._dataTable = null;
         }
         if (flag4)
         {
             this.AddRelation(parentChapterColumn, column2);
         }
     }
     catch (Exception exception)
     {
         if (ADP.IsCatchableOrSecurityExceptionType(exception))
         {
             this.RollbackAddedItems(items);
         }
         throw;
     }
     return objArray;
 }
Example #48
0
 private DbCommand GetSelectCommand() { // V1.2.3300
     DbCommand select = null;
     DbDataAdapter adapter = DataAdapter;
     if (null != adapter) {
         if (0 == _missingMappingAction) {
             _missingMappingAction = adapter.MissingMappingAction;
         }
         select = (DbCommand)adapter.SelectCommand;
     }
     if (null == select) {
         throw ADP.MissingSourceCommand();
     }
     return select;
 }
Example #49
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 && DataColumn.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);
        }
        [EditorBrowsableAttribute(EditorBrowsableState.Advanced)]   // MDAC 69508
        static public DataTableMapping GetTableMappingBySchemaAction(DataTableMappingCollection tableMappings, string sourceTable, string dataSetTable, MissingMappingAction mappingAction)
        {
            if (null != tableMappings)
            {
                int index = tableMappings.IndexOf(sourceTable);
                if (-1 != index)
                {
#if DEBUG
                    if (AdapterSwitches.DataSchema.TraceWarning)
                    {
                        Debug.WriteLine("mapping match on SourceTable \"" + sourceTable + "\"");
                    }
#endif
                    return((DataTableMapping)tableMappings.items[index]);
                }
            }
            if (ADP.IsEmpty(sourceTable))
            {
                throw ADP.InvalidSourceTable("sourceTable");
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceInfo)
                {
                    Debug.WriteLine("mapping passthrough of SourceTable \"" + sourceTable + "\" -> \"" + dataSetTable + "\"");
                }
#endif
                return(new DataTableMapping(sourceTable, dataSetTable));

            case MissingMappingAction.Ignore:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceWarning)
                {
                    Debug.WriteLine("mapping filter of SourceTable \"" + sourceTable + "\"");
                }
#endif
                return(null);

            case MissingMappingAction.Error:
#if DEBUG
                if (AdapterSwitches.DataSchema.TraceError)
                {
                    Debug.WriteLine("mapping error on SourceTable \"" + sourceTable + "\"");
                }
#endif
                throw ADP.MissingTableMapping(sourceTable);

            default:
                throw ADP.InvalidMappingAction((int)mappingAction);
            }
        }
        public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
        {
            if (columnMappings != null)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
                    return columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction);
                }
            }
            if (ADP.IsEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn("sourceColumn");
            }
            switch (mappingAction)
            {
                case MissingMappingAction.Passthrough:
                    return DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction);

                case MissingMappingAction.Ignore:
                    return null;

                case MissingMappingAction.Error:
                    throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
Example #52
0
        public static DataColumn GetDataColumn(DataColumnMappingCollection columnMappings, string sourceColumn, Type dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
        {
            if (columnMappings != null)
            {
                int index = columnMappings.IndexOf(sourceColumn);
                if (-1 != index)
                {
                    return(columnMappings.items[index].GetDataColumnBySchemaAction(dataTable, dataType, schemaAction));
                }
            }
            if (ADP.IsEmpty(sourceColumn))
            {
                throw ADP.InvalidSourceColumn("sourceColumn");
            }
            switch (mappingAction)
            {
            case MissingMappingAction.Passthrough:
                return(DataColumnMapping.GetDataColumnBySchemaAction(sourceColumn, sourceColumn, dataTable, dataType, schemaAction));

            case MissingMappingAction.Ignore:
                return(null);

            case MissingMappingAction.Error:
                throw ADP.MissingColumnMapping(sourceColumn);
            }
            throw ADP.InvalidMissingMappingAction(mappingAction);
        }
        private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue) {
            // must sort rows from schema table by ordinal because Jet is sorted by coumn name
            DbSchemaRow[] schemaRows = DbSchemaRow.GetSortedSchemaRows(_schemaTable, _dataReader.ReturnProviderSpecificTypes); // MDAC 60609
            Debug.Assert(null != schemaRows, "SchemaSetup - null DbSchemaRow[]");
            Debug.Assert(_dataReader.FieldCount <= schemaRows.Length, "unexpected fewer rows in Schema than FieldCount");

            if (0 == schemaRows.Length) {
                _dataTable = null;
                return (object[])null;
            }

            // Everett behavior, always add a primary key if a primary key didn't exist before
            // Whidbey behavior, same as Everett unless using LoadOption then add primary key only if no columns previously existed
            bool addPrimaryKeys = (((0 == _dataTable.PrimaryKey.Length) && ((4 <= (int)_loadOption) || (0 == _dataTable.Rows.Count)))
                                    || (0 == _dataTable.Columns.Count)); // MDAC 67033

            DataColumn[] keys = null;
            int keyCount = 0;
            bool isPrimary = true; // assume key info (if any) is about a primary key

            string keyBaseTable = null;
            string commonBaseTable = null;

            bool keyFromMultiTable = false;
            bool commonFromMultiTable = false;

            int[] columnIndexMap = null;
            bool[] chapterIndexMap = null;

            int mappingCount = 0;

            object[] dataValues = null;
            List<object> addedItems = null;
            DataColumnCollection columnCollection = _dataTable.Columns;
            try {
                for(int sortedIndex = 0; sortedIndex < schemaRows.Length; ++sortedIndex) {
                    DbSchemaRow schemaRow = schemaRows[sortedIndex];

                    int unsortedIndex = schemaRow.UnsortedIndex; // MDAC 67050

                    bool ischapter = false;
                    Type fieldType = schemaRow.DataType;
                    if (null == fieldType) {
                        fieldType = _dataReader.GetFieldType(sortedIndex);
                    }
                    if (null == fieldType) {
                        throw ADP.MissingDataReaderFieldType(sortedIndex);
                    }

                    // if IDataReader, hierarchy exists and we will use an Int32,AutoIncrementColumn in this table
                    if (typeof(IDataReader).IsAssignableFrom(fieldType)) {
                        if (null == chapterIndexMap) {
                            chapterIndexMap = new bool[schemaRows.Length];
                        }
                        chapterIndexMap[unsortedIndex] = ischapter = true;
                        fieldType = typeof(Int32);
                    }
                    else if (typeof(System.Data.SqlTypes.SqlXml).IsAssignableFrom(fieldType)) {
                        if (null == _xmlMap) {
                            _xmlMap = new int[schemaRows.Length];
                        }
                        _xmlMap[sortedIndex] = SqlXml;
                    }
                    else if (typeof(System.Xml.XmlReader).IsAssignableFrom(fieldType)) {
                        fieldType = typeof(String);
                        if (null == _xmlMap) {
                            _xmlMap = new int[schemaRows.Length];
                        }
                        _xmlMap[sortedIndex] = XmlDocument;
                    }

                    DataColumn dataColumn = null;
                    if (!schemaRow.IsHidden ) {
                        dataColumn = _tableMapping.GetDataColumn(_fieldNames[sortedIndex], fieldType, _dataTable, mappingAction, schemaAction);
                    }

                    string basetable = /*schemaRow.BaseServerName+schemaRow.BaseCatalogName+schemaRow.BaseSchemaName+*/ schemaRow.BaseTableName;
                    if (null == dataColumn) {
                        if (null == columnIndexMap) {
                            columnIndexMap = CreateIndexMap(schemaRows.Length, unsortedIndex);
                        }
                        columnIndexMap[unsortedIndex] = -1;

                        // if the column is not mapped and it is a key, then don't add any key information
                        if (schemaRow.IsKey) { // MDAC 90822
#if DEBUG
                            if (AdapterSwitches.DataSchema.TraceVerbose) {
                                Debug.WriteLine("SetupSchema: partial primary key detected");
                            }
#endif
                            // if the hidden key comes from a different table - don't throw away the primary key
                            // example SELECT [T2].[ID], [T2].[ProdID], [T2].[VendorName] FROM [Vendor] AS [T2], [Prod] AS [T1] WHERE (([T1].[ProdID] = [T2].[ProdID]))
                            if (keyFromMultiTable || (schemaRow.BaseTableName == keyBaseTable)) { // WebData 100376
                                addPrimaryKeys = false; // don't add any future keys now
                                keys = null; // get rid of any keys we've seen
                            }
                        }
                        continue; // null means ignore (mapped to nothing)
                    }
                    else if ((null != _xmlMap) && (0 != _xmlMap[sortedIndex])) {
                        if (typeof(System.Data.SqlTypes.SqlXml) == dataColumn.DataType) {
                            _xmlMap[sortedIndex] = SqlXml;
                        }
                        else if (typeof(System.Xml.XmlDocument) == dataColumn.DataType) {
                            _xmlMap[sortedIndex] = XmlDocument;
                        }
                        else {
                            _xmlMap[sortedIndex] = 0; // datacolumn is not a specific Xml dataType, i.e. string

                            int total = 0;
                            for(int x = 0; x < _xmlMap.Length; ++x) {
                                total += _xmlMap[x];
                            }
                            if (0 == total) { // not mapping to a specific Xml datatype, get rid of the map
                                _xmlMap = null;
                            }
                        }
                    }

                    if (schemaRow.IsKey) {
                        if (basetable != keyBaseTable) {
                            if (null == keyBaseTable) {
                                keyBaseTable = basetable;
                            }
                            else keyFromMultiTable = true;
                        }
                    }

                    if (ischapter) {
                        if (null == dataColumn.Table) {
                            dataColumn.AllowDBNull = false;
                            dataColumn.AutoIncrement = true;
                            dataColumn.ReadOnly = true;
                        }
                        else if (!dataColumn.AutoIncrement) {
                            throw ADP.FillChapterAutoIncrement();
                        }
                    }
                    else {// MDAC 67033
                        if (!commonFromMultiTable) {
                            if ((basetable != commonBaseTable) && (!ADP.IsEmpty(basetable))) {
                                if (null == commonBaseTable) {
                                    commonBaseTable = basetable;
                                }
                                else {
                                    commonFromMultiTable = true;
                                }
                            }
                        }
                        if (4 <= (int)_loadOption) {
                            if (schemaRow.IsAutoIncrement && DataColumn.IsAutoIncrementType(fieldType)) {
                                // 

                                dataColumn.AutoIncrement = true;

                                if (!schemaRow.AllowDBNull) { // MDAC 71060
                                    dataColumn.AllowDBNull = false;
                                }
                            }

                            // setup maxLength, only for string columns since this is all the DataSet supports
                            if (fieldType == typeof(string)) {
                                //@devnote:  schemaRow.Size is count of characters for string columns, count of bytes otherwise
                                dataColumn.MaxLength = schemaRow.Size>0?schemaRow.Size:-1;
                            }

                            if (schemaRow.IsReadOnly) {
                                dataColumn.ReadOnly = true;
                            }
                            if (!schemaRow.AllowDBNull && (!schemaRow.IsReadOnly || schemaRow.IsKey)) { // MDAC 71060, 72252
                                dataColumn.AllowDBNull = false;
                            }

                            if (schemaRow.IsUnique && !schemaRow.IsKey && !fieldType.IsArray) {
                                // note, arrays are not comparable so only mark non-arrays as unique, ie timestamp columns
                                // are unique, but not comparable
                                dataColumn.Unique = true;

                                if (!schemaRow.AllowDBNull) { // MDAC 71060
                                    dataColumn.AllowDBNull = false;
                                }
                            }
                        }
                        else if (null == dataColumn.Table) {
                            dataColumn.AutoIncrement = schemaRow.IsAutoIncrement;
                            dataColumn.AllowDBNull = schemaRow.AllowDBNull;
                            dataColumn.ReadOnly = schemaRow.IsReadOnly;
                            dataColumn.Unique = schemaRow.IsUnique;

                            if (fieldType == typeof(string) || (fieldType == typeof(SqlTypes.SqlString))) {
                                //@devnote:  schemaRow.Size is count of characters for string columns, count of bytes otherwise
                                dataColumn.MaxLength = schemaRow.Size;
                            }
                        }
                    }
                    if (null == dataColumn.Table) {
                        if (4 > (int)_loadOption) {
                            AddAdditionalProperties(dataColumn, schemaRow.DataRow);
                        }
                        AddItemToAllowRollback(ref addedItems, dataColumn);
                        columnCollection.Add(dataColumn);
                    }

                    // The server sends us one key per table according to these rules.
                    //
                    // 1. If the table has a primary key, the server sends us this key.
                    // 2. If the table has a primary key and a unique key, it sends us the primary key
                    // 3. if the table has no primary key but has a unique key, it sends us the unique key
                    //
                    // In case 3, we will promote a unique key to a primary key IFF all the columns that compose
                    // that key are not nullable since no columns in a primary key can be null.  If one or more
                    // of the keys is nullable, then we will add a unique constraint.
                    //
                    if (addPrimaryKeys && schemaRow.IsKey) { // MDAC 67033
                        if (keys == null) {
                            keys = new DataColumn[schemaRows.Length];
                        }
                        keys[keyCount++] = dataColumn;
#if DEBUG
                        if (AdapterSwitches.DataSchema.TraceVerbose) {
                            Debug.WriteLine("SetupSchema: building list of " + ((isPrimary) ? "PrimaryKey" : "UniqueConstraint"));
                        }
#endif
                        // see case 3 above, we do want dataColumn.AllowDBNull not schemaRow.AllowDBNull
                        // otherwise adding PrimaryKey will change AllowDBNull to false
                        if (isPrimary && dataColumn.AllowDBNull) { // MDAC 72241
#if DEBUG
                            if (AdapterSwitches.DataSchema.TraceVerbose) {
                                Debug.WriteLine("SetupSchema: changing PrimaryKey into UniqueContraint");
                            }
#endif
                            isPrimary = false;
                        }
                    }

                    if (null != columnIndexMap) {
                        columnIndexMap[unsortedIndex] = dataColumn.Ordinal;
                    }
                    else if (unsortedIndex != dataColumn.Ordinal) {
                        columnIndexMap = CreateIndexMap(schemaRows.Length, unsortedIndex);
                        columnIndexMap[unsortedIndex] = dataColumn.Ordinal;
                    }
                    mappingCount++;
                }

                bool addDataRelation = false;
                DataColumn chapterColumn = null;
                if (null != chapterValue) { // add the extra column in the child table
                    Type fieldType = chapterValue.GetType();
                    chapterColumn = _tableMapping.GetDataColumn(_tableMapping.SourceTable, fieldType, _dataTable, mappingAction, schemaAction);
                    if (null != chapterColumn) {

                        if (null == chapterColumn.Table) {

                            chapterColumn.ReadOnly = true; // MDAC 71878
                            chapterColumn.AllowDBNull = false;

                            AddItemToAllowRollback(ref addedItems, chapterColumn);
                            columnCollection.Add(chapterColumn);
                            addDataRelation = (null != parentChapterColumn);
                        }
                        mappingCount++;
                    }
                }

                if (0 < mappingCount) {
                    if ((null != _dataSet) && null == _dataTable.DataSet) {
                        AddItemToAllowRollback(ref addedItems, _dataTable);
                        _dataSet.Tables.Add(_dataTable);
                    }
                    // setup the key
                    if (addPrimaryKeys && (null != keys)) { // MDAC 67033
                        if (keyCount < keys.Length) {
                            keys = ResizeColumnArray(keys, keyCount);
                        }

                        // MDAC 66188
                        if (isPrimary) {
#if DEBUG
                            if (AdapterSwitches.DataSchema.TraceVerbose) {
                                Debug.WriteLine("SetupSchema: set_PrimaryKey");
                            }
#endif
                            _dataTable.PrimaryKey = keys;
                        }
                        else {
                            UniqueConstraint unique = new UniqueConstraint("", keys);
                            ConstraintCollection constraints = _dataTable.Constraints;
                            int constraintCount = constraints.Count;
                            for (int i = 0; i < constraintCount; ++i) {
                                if (unique.Equals(constraints[i])) {
#if DEBUG
                                    if (AdapterSwitches.DataSchema.TraceVerbose) {
                                        Debug.WriteLine("SetupSchema: duplicate Contraint detected");
                                    }
#endif
                                    unique = null;
                                    break;
                                }
                            }
                            if (null != unique) {
#if DEBUG
                                if (AdapterSwitches.DataSchema.TraceVerbose) {
                                    Debug.WriteLine("SetupSchema: adding new UniqueConstraint");
                                }
#endif
                                constraints.Add(unique);
                            }
                        }
                    }
                    if (!commonFromMultiTable && !ADP.IsEmpty(commonBaseTable) && ADP.IsEmpty(_dataTable.TableName)) {
                        _dataTable.TableName = commonBaseTable;
                    }
                    if (gettingData) {
                        _indexMap = columnIndexMap;
                        _chapterMap = chapterIndexMap;
                        dataValues = SetupMapping(schemaRows.Length, columnCollection, chapterColumn, chapterValue);
                    }
                    else {
                        // debug only, but for retail debug ability
                        _mappedMode = -1;
                    }
                }
                else {
                    _dataTable = null;
                }
                if (addDataRelation) {
                    AddRelation(parentChapterColumn, chapterColumn);
                }
            }
            catch (Exception e) {
                if (ADP.IsCatchableOrSecurityExceptionType(e)) {
                    RollbackAddedItems(addedItems);
                }
                throw;
            }
            return dataValues;
        }
 public DataColumn? GetDataColumn(string sourceColumn, Type? dataType, DataTable dataTable, MissingMappingAction mappingAction, MissingSchemaAction schemaAction)
 {
     return DataColumnMappingCollection.GetDataColumn(_columnMappings, sourceColumn, dataType, dataTable, mappingAction, schemaAction);
 }
Example #55
0
        private object[] SetupSchemaWithKeyInfo(MissingMappingAction mappingAction, MissingSchemaAction schemaAction, bool gettingData, DataColumn parentChapterColumn, object chapterValue)
        {
            DbSchemaRow[] sortedSchemaRows = DbSchemaRow.GetSortedSchemaRows(this._schemaTable, this._dataReader.ReturnProviderSpecificTypes);
            if (sortedSchemaRows.Length == 0)
            {
                this._dataTable = null;
                return null;
            }
            bool flag = (this._dataTable.PrimaryKey.Length == 0 && this._dataTable.Rows.Count == 0) || 0 == this._dataTable.Columns.Count;
            DataColumn[] array = null;
            int num = 0;
            bool flag2 = true;
            string text = null;
            string text2 = null;
            bool flag3 = false;
            bool flag4 = false;
            int[] array2 = null;
            bool[] array3 = null;
            int num2 = 0;
            object[] result = null;
            List<object> items = null;
            DataColumnCollection columns = this._dataTable.Columns;
            try
            {
                for (int i = 0; i < sortedSchemaRows.Length; i++)
                {
                    DbSchemaRow dbSchemaRow = sortedSchemaRows[i];
                    int unsortedIndex = dbSchemaRow.UnsortedIndex;
                    bool flag5 = false;
                    Type type = dbSchemaRow.DataType;
                    if (null == type)
                    {
                        type = this._dataReader.GetFieldType(i);
                    }
                    if (null == type)
                    {
                        throw new Exception("MissingDataReaderFieldType");
                    }
                    if (typeof(IDataReader).IsAssignableFrom(type))
                    {
                        if (array3 == null)
                        {
                            array3 = new bool[sortedSchemaRows.Length];
                        }
                        flag5 = (array3[unsortedIndex] = true);
                        type = typeof(int);
                    }
                    else
                    {
                        if (typeof(SqlXml).IsAssignableFrom(type))
                        {
                            if (this._xmlMap == null)
                            {
                                this._xmlMap = new int[sortedSchemaRows.Length];
                            }
                            this._xmlMap[i] = 1;
                        }
                        else
                        {
                            if (typeof(XmlReader).IsAssignableFrom(type))
                            {
                                type = typeof(string);
                                if (this._xmlMap == null)
                                {
                                    this._xmlMap = new int[sortedSchemaRows.Length];
                                }
                                this._xmlMap[i] = 2;
                            }
                        }
                    }
                    DataColumn dataColumn = null;
                    if (!dbSchemaRow.IsHidden)
                    {
                        dataColumn = this._tableMapping.GetDataColumn(this._fieldNames[i], type, this._dataTable, mappingAction, schemaAction);
                    }
                    string baseTableName = dbSchemaRow.BaseTableName;
                    if (dataColumn == null)
                    {
                        if (array2 == null)
                        {
                            array2 = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                        }
                        array2[unsortedIndex] = -1;
                        if (dbSchemaRow.IsKey && (flag3 || dbSchemaRow.BaseTableName == text))
                        {
                            flag = false;
                            array = null;
                        }
                    }
                    else
                    {
                        if (this._xmlMap != null && this._xmlMap[i] != 0)
                        {
                            if (typeof(SqlXml) == dataColumn.DataType)
                            {
                                this._xmlMap[i] = 1;
                            }
                            else
                            {
                                if (typeof(XmlDocument) == dataColumn.DataType)
                                {
                                    this._xmlMap[i] = 2;
                                }
                                else
                                {
                                    this._xmlMap[i] = 0;
                                    int num3 = 0;
                                    for (int j = 0; j < this._xmlMap.Length; j++)
                                    {
                                        num3 += this._xmlMap[j];
                                    }
                                    if (num3 == 0)
                                    {
                                        this._xmlMap = null;
                                    }
                                }
                            }
                        }
                        if (dbSchemaRow.IsKey && baseTableName != text)
                        {
                            if (text == null)
                            {
                                text = baseTableName;
                            }
                            else
                            {
                                flag3 = true;
                            }
                        }
                        if (flag5)
                        {
                            if (dataColumn.Table == null)
                            {
                                dataColumn.AllowDBNull = false;
                                dataColumn.AutoIncrement = true;
                                dataColumn.ReadOnly = true;
                            }
                            else
                            {
                                if (!dataColumn.AutoIncrement)
                                {
                                    throw new Exception("FillChapterAutoIncrement");
                                }
                            }
                        }
                        else
                        {
                            if (!flag4 && baseTableName != text2 && !string.IsNullOrEmpty(baseTableName))
                            {
                                if (text2 == null)
                                {
                                    text2 = baseTableName;
                                }
                                else
                                {
                                    flag4 = true;
                                }
                            }
                            if (dbSchemaRow.IsAutoIncrement && IsAutoIncrementType(type))
                            {
                                dataColumn.AutoIncrement = true;
                                if (!dbSchemaRow.AllowDBNull)
                                {
                                    dataColumn.AllowDBNull = false;
                                }
                            }
                            if (type == typeof(string))
                            {
                                dataColumn.MaxLength = ((dbSchemaRow.Size > 0) ? dbSchemaRow.Size : -1);
                            }
                            if (dbSchemaRow.IsReadOnly)
                            {
                                dataColumn.ReadOnly = true;
                            }
                            if (!dbSchemaRow.AllowDBNull && (!dbSchemaRow.IsReadOnly || dbSchemaRow.IsKey))
                            {
                                dataColumn.AllowDBNull = false;
                            }
                            if (dbSchemaRow.IsUnique && !dbSchemaRow.IsKey && !type.IsArray)
                            {
                                dataColumn.Unique = true;
                                if (!dbSchemaRow.AllowDBNull)
                                {
                                    dataColumn.AllowDBNull = false;
                                }
                            }
                        }
                        if (dataColumn.Table == null)
                        {
                            this.AddAdditionalProperties(dataColumn, dbSchemaRow.DataRow);
                            this.AddItemToAllowRollback(ref items, dataColumn);
                            columns.Add(dataColumn);
                        }
                        if (flag && dbSchemaRow.IsKey)
                        {
                            if (array == null)
                            {
                                array = new DataColumn[sortedSchemaRows.Length];
                            }
                            array[num++] = dataColumn;
                            if (flag2 && dataColumn.AllowDBNull)
                            {
                                flag2 = false;
                            }
                        }
                        if (array2 != null)
                        {
                            array2[unsortedIndex] = dataColumn.Ordinal;
                        }
                        else
                        {
                            if (unsortedIndex != dataColumn.Ordinal)
                            {
                                array2 = this.CreateIndexMap(sortedSchemaRows.Length, unsortedIndex);
                                array2[unsortedIndex] = dataColumn.Ordinal;
                            }
                        }
                        num2++;
                    }
                }
                bool flag6 = false;
                DataColumn dataColumn2 = null;
                if (chapterValue != null)
                {
                    Type type2 = chapterValue.GetType();
                    dataColumn2 = this._tableMapping.GetDataColumn(this._tableMapping.SourceTable, type2, this._dataTable, mappingAction, schemaAction);
                    if (dataColumn2 != null)
                    {
                        if (dataColumn2.Table == null)
                        {
                            dataColumn2.ReadOnly = true;
                            dataColumn2.AllowDBNull = false;
                            this.AddItemToAllowRollback(ref items, dataColumn2);
                            columns.Add(dataColumn2);
                            flag6 = (null != parentChapterColumn);
                        }
                        num2++;
                    }
                }
                if (0 < num2)
                {
                    if (this._dataSet != null && this._dataTable.DataSet == null)
                    {
                        this.AddItemToAllowRollback(ref items, this._dataTable);
                        this._dataSet.Tables.Add(this._dataTable);
                    }
                    if (flag && array != null)
                    {
                        if (num < array.Length)
                        {
                            array = this.ResizeColumnArray(array, num);
                        }
                        if (flag2)
                        {
                            this._dataTable.PrimaryKey = array;
                        }
                        else
                        {
                            UniqueConstraint uniqueConstraint = new UniqueConstraint("", array);
                            ConstraintCollection constraints = this._dataTable.Constraints;
                            int count = constraints.Count;
                            for (int k = 0; k < count; k++)
                            {
                                if (uniqueConstraint.Equals(constraints[k]))
                                {
                                    uniqueConstraint = null;
                                    break;
                                }
                            }
                            if (uniqueConstraint != null)
                            {
                                constraints.Add(uniqueConstraint);
                            }
                        }
                    }
                    if (!flag4 && !string.IsNullOrEmpty(text2) && string.IsNullOrEmpty(this._dataTable.TableName))
                    {
                        this._dataTable.TableName = text2;
                    }
                    if (gettingData)
                    {
                        this._indexMap = array2;
                        this._chapterMap = array3;
                        result = this.SetupMapping(sortedSchemaRows.Length, columns, dataColumn2, chapterValue);
                    }
                    else
                    {
                        this._mappedMode = -1;
                    }
                }
                else
                {
                    this._dataTable = null;
                }
                if (flag6)
                {
                    this.AddRelation(parentChapterColumn, dataColumn2);
                }
            }
            catch (Exception e)
            {

                this.RollbackAddedItems(items);
                throw e;
            }
            return result;
        }
 private void ParameterOutput(IDataParameter parameter, DataRow row, DataTableMapping mappings, MissingMappingAction missingMapping, MissingSchemaAction missingSchema)
 {
     if ((ParameterDirection.Output & parameter.Direction) != ((ParameterDirection) 0))
     {
         object obj2 = parameter.Value;
         if (obj2 != null)
         {
             string sourceColumn = parameter.SourceColumn;
             if (!ADP.IsEmpty(sourceColumn))
             {
                 DataColumn column = mappings.GetDataColumn(sourceColumn, null, row.Table, missingMapping, missingSchema);
                 if (column != null)
                 {
                     if (column.ReadOnly)
                     {
                         try
                         {
                             column.ReadOnly = false;
                             row[column] = obj2;
                             return;
                         }
                         finally
                         {
                             column.ReadOnly = true;
                         }
                     }
                     row[column] = obj2;
                 }
             }
         }
     }
 }