// If we're not in a DataSet relations collection, we need to verify on every property get that we're // still a good relation object. internal override void CheckState() { if (_DataSet == null) { // Make sure columns arrays are valid parentKey.CheckState(); childKey.CheckState(); if (parentKey.Table.DataSet != childKey.Table.DataSet) { throw ExceptionBuilder.TablesInDifferentSets(); } for (int i = 0; i < parentKey.Columns.Length; i++) { if (parentKey.Columns[i].DataType != childKey.Columns[i].DataType) { throw ExceptionBuilder.ColumnsTypeMismatch(); } } if (childKey.ColumnsEqual(parentKey)) { throw ExceptionBuilder.KeyColumnsIdentical(); } } }
internal override bool CanBeRemovedFromCollection(ConstraintCollection constraints, bool fThrowException) { if (Equals(constraints.Table._primaryKey)) { Debug.Assert(constraints.Table._primaryKey == this, "If the primary key and this are 'Equal', they should also be '=='"); if (!fThrowException) { return(false); } else { throw ExceptionBuilder.RemovePrimaryKey(constraints.Table); } } for (ParentForeignKeyConstraintEnumerator cs = new ParentForeignKeyConstraintEnumerator(Table.DataSet, Table); cs.GetNext();) { ForeignKeyConstraint constraint = cs.GetForeignKeyConstraint(); if (!_key.ColumnsEqual(constraint.ParentKey)) { continue; } if (!fThrowException) { return(false); } else { throw ExceptionBuilder.NeededForForeignKeyConstraint(this, constraint); } } return(true); }
// If we're not in a dataSet relations collection, we need to verify on every property get that we're // still a good relation object. internal void CheckState() { if (_dataSet == null) { _parentKey.CheckState(); _childKey.CheckState(); if (_parentKey.Table.DataSet != _childKey.Table.DataSet) { throw ExceptionBuilder.RelationDataSetMismatch(); } if (_childKey.ColumnsEqual(_parentKey)) { throw ExceptionBuilder.KeyColumnsIdentical(); } for (int i = 0; i < _parentKey.ColumnsReference.Length; i++) { if ((_parentKey.ColumnsReference[i].DataType != _childKey.ColumnsReference[i].DataType) || ((_parentKey.ColumnsReference[i].DataType == typeof(DateTime)) && (_parentKey.ColumnsReference[i].DateTimeMode != _childKey.ColumnsReference[i].DateTimeMode) && ((_parentKey.ColumnsReference[i].DateTimeMode & _childKey.ColumnsReference[i].DateTimeMode) != DataSetDateTime.Unspecified))) { // allow unspecified and unspecifiedlocal throw ExceptionBuilder.ColumnsTypeMismatch(); } } } }
private void NonVirtualCheckState() { if (_DataSet == null) { // Make sure columns arrays are valid _parentKey.CheckState(); _childKey.CheckState(); if (_parentKey.Table.DataSet != _childKey.Table.DataSet) { throw ExceptionBuilder.TablesInDifferentSets(); } for (int i = 0; i < _parentKey.ColumnsReference.Length; i++) { if (_parentKey.ColumnsReference[i].DataType != _childKey.ColumnsReference[i].DataType || ((_parentKey.ColumnsReference[i].DataType == typeof(DateTime)) && (_parentKey.ColumnsReference[i].DateTimeMode != _childKey.ColumnsReference[i].DateTimeMode) && ((_parentKey.ColumnsReference[i].DateTimeMode & _childKey.ColumnsReference[i].DateTimeMode) != DataSetDateTime.Unspecified))) { throw ExceptionBuilder.ColumnsTypeMismatch(); } } if (_childKey.ColumnsEqual(_parentKey)) { throw ExceptionBuilder.KeyColumnsIdentical(); } } }
// If we're not in a dataSet relations collection, we need to verify on every property get that we're // still a good relation object. internal void CheckState() { if (dataSet == null) { parentKey.CheckState(); childKey.CheckState(); if (parentKey.Table.DataSet != childKey.Table.DataSet) { throw ExceptionBuilder.RelationDataSetMismatch(); } if (childKey.ColumnsEqual(parentKey)) { throw ExceptionBuilder.KeyColumnsIdentical(); } for (int i = 0; i < parentKey.Columns.Length; i++) { if (parentKey.Columns[i].DataType != childKey.Columns[i].DataType) { throw ExceptionBuilder.ColumnsTypeMismatch(); } } } }
private void AssertConstraintAndKeyIndexes() { Debug.Assert(null != _constraintIndex, "null UniqueConstraint index"); // ideally, we would like constraintIndex and key.GetSortIndex to share the same index underneath: Debug.Assert(_constraintIndex == key.GetSortIndex) // but, there is a scenario where constraint and key indexes are built from the same list of columns but in a different order DataColumn[] sortIndexColumns = new DataColumn[_constraintIndex._indexFields.Length]; for (int i = 0; i < sortIndexColumns.Length; i++) { sortIndexColumns[i] = _constraintIndex._indexFields[i].Column; } Debug.Assert(DataKey.ColumnsEqual(_key.ColumnsReference, sortIndexColumns), "UniqueConstraint index columns do not match the key sort index"); }
protected override void AddCore(DataRelation relation) { base.AddCore(relation); if ((relation.ChildTable.DataSet != this.dataSet) || (relation.ParentTable.DataSet != this.dataSet)) { throw ExceptionBuilder.ForeignRelation(); } relation.CheckState(); if (relation.Nested) { relation.CheckNestedRelations(); } if (relation.relationName.Length == 0) { relation.relationName = base.AssignName(); } else { base.RegisterName(relation.relationName); } DataKey childKey = relation.ChildKey; for (int i = 0; i < this.relations.Count; i++) { if (childKey.ColumnsEqual(((DataRelation)this.relations[i]).ChildKey) && relation.ParentKey.ColumnsEqual(((DataRelation)this.relations[i]).ParentKey)) { throw ExceptionBuilder.RelationAlreadyExists(); } } this.relations.Add(relation); ((DataRelationCollection.DataTableRelationCollection)relation.ParentTable.ChildRelations).Add(relation); ((DataRelationCollection.DataTableRelationCollection)relation.ChildTable.ParentRelations).Add(relation); relation.SetDataSet(this.dataSet); relation.ChildKey.GetSortIndex().AddRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } ForeignKeyConstraint constraint = relation.ChildTable.Constraints.FindForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference); if (relation.createConstraints && (constraint == null)) { relation.ChildTable.Constraints.Add(constraint = new ForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference)); try { constraint.ConstraintName = relation.RelationName; } catch (Exception exception) { if (!ADP.IsCatchableExceptionType(exception)) { throw; } ExceptionBuilder.TraceExceptionWithoutRethrow(exception); } } UniqueConstraint constraint2 = relation.ParentTable.Constraints.FindKeyConstraint(relation.ParentColumnsReference); relation.SetParentKeyConstraint(constraint2); relation.SetChildKeyConstraint(constraint); }
protected override void AddCore(DataRelation relation) { base.AddCore(relation); if (relation.ChildTable.DataSet != _dataSet || relation.ParentTable.DataSet != _dataSet) { throw ExceptionBuilder.ForeignRelation(); } relation.CheckState(); if (relation.Nested) { relation.CheckNestedRelations(); } if (relation._relationName.Length == 0) { relation._relationName = AssignName(); } else { RegisterName(relation._relationName); } DataKey childKey = relation.ChildKey; for (int i = 0; i < _relations.Count; i++) { if (childKey.ColumnsEqual(((DataRelation)_relations[i]).ChildKey)) { if (relation.ParentKey.ColumnsEqual(((DataRelation)_relations[i]).ParentKey)) { throw ExceptionBuilder.RelationAlreadyExists(); } } } _relations.Add(relation); ((DataTableRelationCollection)(relation.ParentTable.ChildRelations)).Add(relation); // Caching in ParentTable -> ChildRelations ((DataTableRelationCollection)(relation.ChildTable.ParentRelations)).Add(relation); // Caching in ChildTable -> ParentRelations relation.SetDataSet(_dataSet); relation.ChildKey.GetSortIndex().AddRef(); if (relation.Nested) { relation.ChildTable.CacheNestedParent(); } ForeignKeyConstraint foreignKey = relation.ChildTable.Constraints.FindForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference); if (relation._createConstraints) { if (foreignKey == null) { relation.ChildTable.Constraints.Add(foreignKey = new ForeignKeyConstraint(relation.ParentColumnsReference, relation.ChildColumnsReference)); // try to name the fk constraint the same as the parent relation: try { foreignKey.ConstraintName = relation.RelationName; } catch (Exception e) when(Common.ADP.IsCatchableExceptionType(e)) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } } } UniqueConstraint key = relation.ParentTable.Constraints.FindKeyConstraint(relation.ParentColumnsReference); relation.SetParentKeyConstraint(key); relation.SetChildKeyConstraint(foreignKey); }