private void MergeRelation(DataRelation relation) { Debug.Assert(_dataSet != null); Debug.Assert(MissingSchemaAction.Error == _missingSchemaAction || MissingSchemaAction.Add == _missingSchemaAction, "Unexpected value of MissingSchemaAction parameter : " + _missingSchemaAction.ToString()); DataRelation?destRelation = null; // try to find given relation in this dataSet int iDest = _dataSet.Relations.InternalIndexOf(relation.RelationName); if (iDest >= 0) { // check the columns and Relation properties.. destRelation = _dataSet.Relations[iDest]; if (relation.ParentKey.ColumnsReference.Length != destRelation.ParentKey.ColumnsReference.Length) { _dataSet.RaiseMergeFailed(null, SR.Format(SR.DataMerge_MissingDefinition, relation.RelationName), _missingSchemaAction); } for (int i = 0; i < relation.ParentKey.ColumnsReference.Length; i++) { DataColumn dest = destRelation.ParentKey.ColumnsReference[i]; DataColumn src = relation.ParentKey.ColumnsReference[i]; if (0 != string.Compare(dest.ColumnName, src.ColumnName, false, dest.Table !.Locale)) { _dataSet.RaiseMergeFailed(null, SR.Format(SR.DataMerge_ReltionKeyColumnsMismatch, relation.RelationName), _missingSchemaAction); } dest = destRelation.ChildKey.ColumnsReference[i]; src = relation.ChildKey.ColumnsReference[i]; if (0 != string.Compare(dest.ColumnName, src.ColumnName, false, dest.Table !.Locale)) { _dataSet.RaiseMergeFailed(null, SR.Format(SR.DataMerge_ReltionKeyColumnsMismatch, relation.RelationName), _missingSchemaAction); } } } else { if (MissingSchemaAction.Add == _missingSchemaAction) { // create identical realtion in the current dataset DataTable parent = _IgnoreNSforTableLookup ? _dataSet.Tables[relation.ParentTable.TableName] ! : _dataSet.Tables[relation.ParentTable.TableName, relation.ParentTable.Namespace] !; DataTable child = _IgnoreNSforTableLookup ? _dataSet.Tables[relation.ChildTable.TableName] ! : _dataSet.Tables[relation.ChildTable.TableName, relation.ChildTable.Namespace] !; DataColumn[] parentColumns = new DataColumn[relation.ParentKey.ColumnsReference.Length]; DataColumn[] childColumns = new DataColumn[relation.ParentKey.ColumnsReference.Length]; for (int i = 0; i < relation.ParentKey.ColumnsReference.Length; i++) { parentColumns[i] = parent.Columns[relation.ParentKey.ColumnsReference[i].ColumnName] !; childColumns[i] = child.Columns[relation.ChildKey.ColumnsReference[i].ColumnName] !; } try { destRelation = new DataRelation(relation.RelationName, parentColumns, childColumns, relation._createConstraints); destRelation.Nested = relation.Nested; _dataSet.Relations.Add(destRelation); } catch (Exception e) when(Common.ADP.IsCatchableExceptionType(e)) { ExceptionBuilder.TraceExceptionForCapture(e); _dataSet.RaiseMergeFailed(null, e.Message, _missingSchemaAction); // TODO: destRelation may be null, causing an NRE below } } else { Debug.Assert(MissingSchemaAction.Error == _missingSchemaAction, "Unexpected value of MissingSchemaAction parameter : " + _missingSchemaAction.ToString()); throw ExceptionBuilder.MergeMissingDefinition(relation.RelationName); } } // TODO: destRelation may be null, see comment above MergeExtendedProperties(relation.ExtendedProperties, destRelation !.ExtendedProperties); }
private static T ValueField(object value) => value == DBNull.Value ? throw DataSetUtil.InvalidCast(SR.Format(SR.DataSetLinq_NonNullableCast, typeof(T))) : (T)value;
private DataTable?MergeSchema(DataTable table) { DataTable?targetTable = null; if (!_isStandAlonetable) { if (_dataSet !.Tables.Contains(table.TableName, true)) { if (_IgnoreNSforTableLookup) { targetTable = _dataSet.Tables[table.TableName]; } else { targetTable = _dataSet.Tables[table.TableName, table.Namespace]; } } } else { targetTable = _dataTable; } if (targetTable == null) { // in case of standalone table, we make sure that targetTable is not null, so if this check passes, it will be when it is called via detaset if (MissingSchemaAction.Add == _missingSchemaAction) { targetTable = table.Clone(table.DataSet); // if we are here mainly we are called from DataSet.Merge at this point we don't set //expression columns, since it might have refer to other columns via relation, so it won't find the table and we get exception; // do it after adding relations. _dataSet !.Tables.Add(targetTable); } else if (MissingSchemaAction.Error == _missingSchemaAction) { throw ExceptionBuilder.MergeMissingDefinition(table.TableName); } } else { if (MissingSchemaAction.Ignore != _missingSchemaAction) { // Do the columns int oldCount = targetTable.Columns.Count; for (int i = 0; i < table.Columns.Count; i++) { DataColumn src = table.Columns[i]; DataColumn?dest = (targetTable.Columns.Contains(src.ColumnName, true)) ? targetTable.Columns[src.ColumnName] : null; if (dest == null) { if (MissingSchemaAction.Add == _missingSchemaAction) { dest = src.Clone(); targetTable.Columns.Add(dest); } else { if (!_isStandAlonetable) { _dataSet !.RaiseMergeFailed(targetTable, SR.Format(SR.DataMerge_MissingColumnDefinition, table.TableName, src.ColumnName), _missingSchemaAction); } else { throw ExceptionBuilder.MergeFailed(SR.Format(SR.DataMerge_MissingColumnDefinition, table.TableName, src.ColumnName)); } } } else { if (dest.DataType != src.DataType || ((dest.DataType == typeof(DateTime)) && (dest.DateTimeMode != src.DateTimeMode) && ((dest.DateTimeMode & src.DateTimeMode) != DataSetDateTime.Unspecified))) { if (!_isStandAlonetable) { _dataSet !.RaiseMergeFailed(targetTable, SR.Format(SR.DataMerge_DataTypeMismatch, src.ColumnName), MissingSchemaAction.Error); } else { throw ExceptionBuilder.MergeFailed(SR.Format(SR.DataMerge_DataTypeMismatch, src.ColumnName)); } } MergeExtendedProperties(src.ExtendedProperties, dest.ExtendedProperties); } } // Set DataExpression if (_isStandAlonetable) { for (int i = oldCount; i < targetTable.Columns.Count; i++) { targetTable.Columns[i].CopyExpressionFrom(table.Columns[targetTable.Columns[i].ColumnName] !); } } // check the PrimaryKey DataColumn[] targetPKey = targetTable.PrimaryKey; DataColumn[] tablePKey = table.PrimaryKey; if (targetPKey.Length != tablePKey.Length) { // special case when the target table does not have the PrimaryKey if (targetPKey.Length == 0) { DataColumn[] key = new DataColumn[tablePKey.Length]; for (int i = 0; i < tablePKey.Length; i++) { key[i] = targetTable.Columns[tablePKey[i].ColumnName] !; } targetTable.PrimaryKey = key; } else if (tablePKey.Length != 0) { _dataSet !.RaiseMergeFailed(targetTable, SR.DataMerge_PrimaryKeyMismatch, _missingSchemaAction); } } else { for (int i = 0; i < targetPKey.Length; i++) { if (string.Compare(targetPKey[i].ColumnName, tablePKey[i].ColumnName, false, targetTable.Locale) != 0) { _dataSet !.RaiseMergeFailed(table, SR.Format(SR.DataMerge_PrimaryKeyColumnsMismatch, targetPKey[i].ColumnName, tablePKey[i].ColumnName), _missingSchemaAction); } } } } MergeExtendedProperties(table.ExtendedProperties, targetTable.ExtendedProperties); } return(targetTable); }