/// <include file='doc\ConstraintCollection.uex' path='docs/doc[@for="ConstraintCollection.Add"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Adds the constraint to the collection.</para>
        /// </devdoc>
        public void Add(Constraint constraint)
        {
            if (constraint == null)
            {
                throw ExceptionBuilder.ArgumentNull("constraint");
            }

            // It is an error if we find an equivalent constraint already in collection
            if (FindConstraint(constraint) != null)
            {
                throw ExceptionBuilder.DuplicateConstraint(FindConstraint(constraint).ConstraintName);
            }

            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint).bPrimaryKey)
                {
                    if (Table.primaryKey != null)
                    {
                        throw ExceptionBuilder.AddPrimaryKeyConstraint();
                    }
                }
                AddUniqueConstraint((UniqueConstraint)constraint);
            }
            else if (constraint is ForeignKeyConstraint)
            {
                ForeignKeyConstraint fk  = (ForeignKeyConstraint)constraint;
                UniqueConstraint     key = fk.RelatedTable.Constraints.FindKeyConstraint(fk.RelatedColumns);
                if (key == null)
                {
                    if (constraint.ConstraintName.Length == 0)
                    {
                        constraint.ConstraintName = AssignName();
                    }
                    else
                    {
                        RegisterName(constraint.ConstraintName);
                    }

                    key = new UniqueConstraint(fk.RelatedColumns);
                    fk.RelatedTable.Constraints.Add(key);
                }
                AddForeignKeyConstraint((ForeignKeyConstraint)constraint);
            }

            BaseAdd(constraint);
            ArrayAdd(constraint);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, constraint));


            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint).bPrimaryKey)
                {
                    Table.PrimaryKey = ((UniqueConstraint)constraint).Columns;
                }
            }
        }
Esempio n. 2
0
 internal void Add(Constraint constraint, bool addUniqueWhenAddingForeign)
 {
     if (constraint == null)
     {
         throw ExceptionBuilder.ArgumentNull("constraint");
     }
     if (this.FindConstraint(constraint) != null)
     {
         throw ExceptionBuilder.DuplicateConstraint(this.FindConstraint(constraint).ConstraintName);
     }
     if ((1 < this.table.NestedParentRelations.Length) && !this.AutoGenerated(constraint))
     {
         throw ExceptionBuilder.CantAddConstraintToMultipleNestedTable(this.table.TableName);
     }
     if (constraint is UniqueConstraint)
     {
         if (((UniqueConstraint)constraint).bPrimaryKey && (this.Table.primaryKey != null))
         {
             throw ExceptionBuilder.AddPrimaryKeyConstraint();
         }
         this.AddUniqueConstraint((UniqueConstraint)constraint);
     }
     else if (constraint is ForeignKeyConstraint)
     {
         ForeignKeyConstraint constraint2 = (ForeignKeyConstraint)constraint;
         if (addUniqueWhenAddingForeign && (constraint2.RelatedTable.Constraints.FindKeyConstraint(constraint2.RelatedColumnsReference) == null))
         {
             if (constraint.ConstraintName.Length == 0)
             {
                 constraint.ConstraintName = this.AssignName();
             }
             else
             {
                 this.RegisterName(constraint.ConstraintName);
             }
             UniqueConstraint constraint3 = new UniqueConstraint(constraint2.RelatedColumnsReference);
             constraint2.RelatedTable.Constraints.Add(constraint3);
         }
         this.AddForeignKeyConstraint((ForeignKeyConstraint)constraint);
     }
     this.BaseAdd(constraint);
     this.ArrayAdd(constraint);
     this.OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, constraint));
     if ((constraint is UniqueConstraint) && ((UniqueConstraint)constraint).bPrimaryKey)
     {
         this.Table.PrimaryKey = ((UniqueConstraint)constraint).ColumnsReference;
     }
 }
Esempio n. 3
0
        internal void FinishInitConstraints()
        {
            if (_delayLoadingConstraints == null)
            {
                return;
            }

            int colCount;

            DataColumn[] parents, childs;
            for (int i = 0; i < _delayLoadingConstraints.Length; i++)
            {
                if (_delayLoadingConstraints[i] is UniqueConstraint)
                {
                    if (_fLoadForeignKeyConstraintsOnly)
                    {
                        continue;
                    }

                    UniqueConstraint constr = (UniqueConstraint)_delayLoadingConstraints[i];
                    if (constr._columnNames == null)
                    {
                        Add(constr);
                        continue;
                    }
                    colCount = constr._columnNames.Length;
                    parents  = new DataColumn[colCount];
                    for (int j = 0; j < colCount; j++)
                    {
                        parents[j] = _table.Columns[constr._columnNames[j]];
                    }
                    if (constr._bPrimaryKey)
                    {
                        if (_table._primaryKey != null)
                        {
                            throw ExceptionBuilder.AddPrimaryKeyConstraint();
                        }
                        else
                        {
                            Add(constr.ConstraintName, parents, true);
                        }
                        continue;
                    }
                    UniqueConstraint newConstraint = new UniqueConstraint(constr._constraintName, parents);
                    if (FindConstraint(newConstraint) == null)
                    {
                        Add(newConstraint);
                    }
                }
                else
                {
                    ForeignKeyConstraint constr = (ForeignKeyConstraint)_delayLoadingConstraints[i];
                    if (constr._parentColumnNames == null || constr._childColumnNames == null)
                    {
                        Add(constr);
                        continue;
                    }

                    if (_table.DataSet == null)
                    {
                        _fLoadForeignKeyConstraintsOnly = true;
                        continue;
                    }

                    colCount = constr._parentColumnNames.Length;
                    parents  = new DataColumn[colCount];
                    childs   = new DataColumn[colCount];
                    for (int j = 0; j < colCount; j++)
                    {
                        if (constr._parentTableNamespace == null)
                        {
                            parents[j] = _table.DataSet.Tables[constr._parentTableName].Columns[constr._parentColumnNames[j]];
                        }
                        else
                        {
                            parents[j] = _table.DataSet.Tables[constr._parentTableName, constr._parentTableNamespace].Columns[constr._parentColumnNames[j]];
                        }
                        childs[j] = _table.Columns[constr._childColumnNames[j]];
                    }
                    ForeignKeyConstraint newConstraint = new ForeignKeyConstraint(constr._constraintName, parents, childs);
                    newConstraint.AcceptRejectRule = constr._acceptRejectRule;
                    newConstraint.DeleteRule       = constr._deleteRule;
                    newConstraint.UpdateRule       = constr._updateRule;
                    Add(newConstraint);
                }
            }

            if (!_fLoadForeignKeyConstraintsOnly)
            {
                _delayLoadingConstraints = null;
            }
        }
Esempio n. 4
0
        // To add foreign key constraint without adding any unique constraint for internal use. Main purpose : Binary Remoting
        internal void Add(Constraint constraint, bool addUniqueWhenAddingForeign)
        {
            if (constraint == null)
            {
                throw ExceptionBuilder.ArgumentNull(nameof(constraint));
            }

            // It is an error if we find an equivalent constraint already in collection
            if (FindConstraint(constraint) != null)
            {
                throw ExceptionBuilder.DuplicateConstraint(FindConstraint(constraint).ConstraintName);
            }

            if (1 < _table.NestedParentRelations.Length)
            {
                if (!AutoGenerated(constraint))
                {
                    throw ExceptionBuilder.CantAddConstraintToMultipleNestedTable(_table.TableName);
                }
            }

            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint)._bPrimaryKey)
                {
                    if (Table._primaryKey != null)
                    {
                        throw ExceptionBuilder.AddPrimaryKeyConstraint();
                    }
                }
                AddUniqueConstraint((UniqueConstraint)constraint);
            }
            else if (constraint is ForeignKeyConstraint)
            {
                ForeignKeyConstraint fk = (ForeignKeyConstraint)constraint;
                if (addUniqueWhenAddingForeign)
                {
                    UniqueConstraint key = fk.RelatedTable.Constraints.FindKeyConstraint(fk.RelatedColumnsReference);
                    if (key == null)
                    {
                        if (constraint.ConstraintName.Length == 0)
                        {
                            constraint.ConstraintName = AssignName();
                        }
                        else
                        {
                            RegisterName(constraint.ConstraintName);
                        }

                        key = new UniqueConstraint(fk.RelatedColumnsReference);
                        fk.RelatedTable.Constraints.Add(key);
                    }
                }
                AddForeignKeyConstraint((ForeignKeyConstraint)constraint);
            }
            BaseAdd(constraint);
            ArrayAdd(constraint);
            OnCollectionChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, constraint));

            if (constraint is UniqueConstraint)
            {
                if (((UniqueConstraint)constraint)._bPrimaryKey)
                {
                    Table.PrimaryKey = ((UniqueConstraint)constraint).ColumnsReference;
                }
            }
        }
Esempio n. 5
0
 internal void FinishInitConstraints()
 {
     if (this.delayLoadingConstraints != null)
     {
         for (int i = 0; i < this.delayLoadingConstraints.Length; i++)
         {
             DataColumn[] columnArray;
             int          length;
             if (this.delayLoadingConstraints[i] is UniqueConstraint)
             {
                 if (!this.fLoadForeignKeyConstraintsOnly)
                 {
                     UniqueConstraint constraint2 = (UniqueConstraint)this.delayLoadingConstraints[i];
                     if (constraint2.columnNames == null)
                     {
                         this.Add(constraint2);
                     }
                     else
                     {
                         length      = constraint2.columnNames.Length;
                         columnArray = new DataColumn[length];
                         for (int j = 0; j < length; j++)
                         {
                             columnArray[j] = this.table.Columns[constraint2.columnNames[j]];
                         }
                         if (constraint2.bPrimaryKey)
                         {
                             if (this.table.primaryKey != null)
                             {
                                 throw ExceptionBuilder.AddPrimaryKeyConstraint();
                             }
                             this.Add(constraint2.ConstraintName, columnArray, true);
                         }
                         else
                         {
                             UniqueConstraint constraint4 = new UniqueConstraint(constraint2.constraintName, columnArray);
                             if (this.FindConstraint(constraint4) == null)
                             {
                                 this.Add(constraint4);
                             }
                         }
                     }
                 }
             }
             else
             {
                 ForeignKeyConstraint constraint = (ForeignKeyConstraint)this.delayLoadingConstraints[i];
                 if ((constraint.parentColumnNames == null) || (constraint.childColumnNames == null))
                 {
                     this.Add(constraint);
                 }
                 else if (this.table.DataSet == null)
                 {
                     this.fLoadForeignKeyConstraintsOnly = true;
                 }
                 else
                 {
                     length      = constraint.parentColumnNames.Length;
                     columnArray = new DataColumn[length];
                     DataColumn[] childColumns = new DataColumn[length];
                     for (int k = 0; k < length; k++)
                     {
                         if (constraint.parentTableNamespace == null)
                         {
                             columnArray[k] = this.table.DataSet.Tables[constraint.parentTableName].Columns[constraint.parentColumnNames[k]];
                         }
                         else
                         {
                             columnArray[k] = this.table.DataSet.Tables[constraint.parentTableName, constraint.parentTableNamespace].Columns[constraint.parentColumnNames[k]];
                         }
                         childColumns[k] = this.table.Columns[constraint.childColumnNames[k]];
                     }
                     ForeignKeyConstraint constraint3 = new ForeignKeyConstraint(constraint.constraintName, columnArray, childColumns)
                     {
                         AcceptRejectRule = constraint.acceptRejectRule,
                         DeleteRule       = constraint.deleteRule,
                         UpdateRule       = constraint.updateRule
                     };
                     this.Add(constraint3);
                 }
             }
         }
         if (!this.fLoadForeignKeyConstraintsOnly)
         {
             this.delayLoadingConstraints = null;
         }
     }
 }