示例#1
0
        public void AddForeignKey(Constraint c)
        {
            this.CheckModifyTable();
            this.CheckCreateForeignKey(c);
            Constraint uniqueConstraintForColumns = c.Core.MainTable.GetUniqueConstraintForColumns(c.Core.MainCols, c.Core.RefCols);
            Index      mainIndex = uniqueConstraintForColumns.GetMainIndex();

            uniqueConstraintForColumns.CheckReferencedRows(this._session, this._table, c.Core.RefCols);
            bool forward = false;

            if (c.Core.MainTable.GetSchemaName() == this._table.GetSchemaName())
            {
                int tableIndex = this._database.schemaManager.GetTableIndex(this._table);
                if ((tableIndex != -1) && (tableIndex < this._database.schemaManager.GetTableIndex(c.Core.MainTable)))
                {
                    forward = true;
                }
            }
            else
            {
                forward = true;
            }
            QNameManager.QName name = this._database.NameManager.NewAutoName("IDX", this._table.GetSchemaName(), this._table.GetName(), 20);
            Index index             = this._table.CreateIndexStructure(name, c.Core.RefCols, null, null, false, true, forward);

            QNameManager.QName name2 = this._database.NameManager.NewAutoName("REF", c.GetName().Name, this._table.GetSchemaName(), this._table.GetName(), 20);
            c.Core.UniqueName = uniqueConstraintForColumns.GetName();
            c.Core.MainName   = name2;
            c.Core.MainIndex  = mainIndex;
            c.Core.RefTable   = this._table;
            c.Core.RefName    = c.GetName();
            c.Core.RefIndex   = index;
            c.IsForward       = forward;
            Table newTable = this._table.MoveDefinition(this._session, this._table.TableType, null, c, index, -1, 0, this.EmptySetQName, this.EmptySetQName);

            this.MoveData(this._table, newTable, -1, 0);
            this._database.schemaManager.AddSchemaObject(c);
            this.SetNewTableInSchema(newTable);
            this._database.schemaManager.GetTable(this._session, c.Core.MainTable.GetName().Name, c.Core.MainTable.GetSchemaName().Name).AddConstraint(new Constraint(name2, c));
            this.UpdateConstraints(newTable, this.EmptySetQName);
            this._database.schemaManager.RecompileDependentObjects(newTable);
            this._table = newTable;
        }