Search in sources :

Example 11 with ReferencedKeyConstraintDescriptor

use of org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor in project derby by apache.

the class DMLModStatementNode method createConstraintDependencies.

/**
 * Get all of our dependents due to a constraint.
 *
 * Makes the calling object (usually a Statement) dependent on all the constraints.
 *
 * @param dd				The data dictionary
 * @param cdl				The constraint descriptor list
 * @param dependent			Parent object that will depend on all the constraints
 *							that we look up. If this argument is null, then we
 *							use the default dependent (the statement being compiled).
 *
 * @exception StandardException		Thrown on failure
 */
private void createConstraintDependencies(DataDictionary dd, ConstraintDescriptorList cdl, Dependent dependent) throws StandardException {
    CompilerContext compilerContext = getCompilerContext();
    int cdlSize = cdl.size();
    for (int index = 0; index < cdlSize; index++) {
        ConstraintDescriptor cd = cdl.elementAt(index);
        /*
			** The dependent now depends on this constraint. 
			** the default dependent is the statement 
			** being compiled.
			*/
        if (dependent == null) {
            compilerContext.createDependency(cd);
        } else {
            compilerContext.createDependency(dependent, cd);
        }
        /*
			** We are also dependent on all referencing keys --
			** if one of them is deleted, we'll have to recompile.
			** Also, if there is a BULK_INSERT on the table
			** we are going to scan to validate the constraint,
			** the index number will change, so we'll add a
			** dependency on all tables we will scan.
			*/
        if (cd instanceof ReferencedKeyConstraintDescriptor) {
            ConstraintDescriptorList fkcdl = dd.getActiveConstraintDescriptors(((ReferencedKeyConstraintDescriptor) cd).getForeignKeyConstraints(ConstraintDescriptor.ENABLED));
            int fklSize = fkcdl.size();
            for (int inner = 0; inner < fklSize; inner++) {
                ConstraintDescriptor fkcd = fkcdl.elementAt(inner);
                if (dependent == null) {
                    compilerContext.createDependency(fkcd);
                    compilerContext.createDependency(fkcd.getTableDescriptor());
                } else {
                    compilerContext.createDependency(dependent, fkcd);
                    compilerContext.createDependency(dependent, fkcd.getTableDescriptor());
                }
            }
        } else if (cd instanceof ForeignKeyConstraintDescriptor) {
            ForeignKeyConstraintDescriptor fkcd = (ForeignKeyConstraintDescriptor) cd;
            if (dependent == null) {
                compilerContext.createDependency(fkcd.getReferencedConstraint().getTableDescriptor());
            } else {
                compilerContext.createDependency(dependent, fkcd.getReferencedConstraint().getTableDescriptor());
            }
        }
    }
}
Also used : CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor)

Example 12 with ReferencedKeyConstraintDescriptor

use of org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor in project derby by apache.

the class DataDictionaryImpl method addSubKeyConstraint.

/**
 * Add the matching row to syskeys when adding a unique or primary key constraint
 *
 * @param descriptor	The KeyConstraintDescriptor for the constraint.
 * @param tc			The TransactionController
 *
 * @exception StandardException		Thrown on failure
 */
private void addSubKeyConstraint(KeyConstraintDescriptor descriptor, TransactionController tc) throws StandardException {
    ExecRow row;
    TabInfoImpl ti;
    /*
		** Foreign keys get a row in SYSFOREIGNKEYS, and
		** all others get a row in SYSKEYS.
		*/
    if (descriptor.getConstraintType() == DataDictionary.FOREIGNKEY_CONSTRAINT) {
        ForeignKeyConstraintDescriptor fkDescriptor = (ForeignKeyConstraintDescriptor) descriptor;
        if (SanityManager.DEBUG) {
            if (!(descriptor instanceof ForeignKeyConstraintDescriptor)) {
                SanityManager.THROWASSERT("descriptor not an fk descriptor, is " + descriptor.getClass().getName());
            }
        }
        ti = getNonCoreTI(SYSFOREIGNKEYS_CATALOG_NUM);
        SYSFOREIGNKEYSRowFactory fkkeysRF = (SYSFOREIGNKEYSRowFactory) ti.getCatalogRowFactory();
        row = fkkeysRF.makeRow(fkDescriptor, null);
        /*
			** Now we need to bump the reference count of the
			** constraint that this FK references
			*/
        ReferencedKeyConstraintDescriptor refDescriptor = fkDescriptor.getReferencedConstraint();
        refDescriptor.incrementReferenceCount();
        int[] colsToSet = new int[1];
        colsToSet[0] = SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_REFERENCECOUNT;
        updateConstraintDescriptor(refDescriptor, refDescriptor.getUUID(), colsToSet, tc);
    } else {
        ti = getNonCoreTI(SYSKEYS_CATALOG_NUM);
        SYSKEYSRowFactory keysRF = (SYSKEYSRowFactory) ti.getCatalogRowFactory();
        // build the row to be stuffed into SYSKEYS
        row = keysRF.makeRow(descriptor, null);
    }
    // insert row into catalog and all its indices
    ti.insertRow(row, tc);
}
Also used : ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor)

Aggregations

ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)12 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)9 ForeignKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor)9 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)7 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)5 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)5 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)5 TransactionController (org.apache.derby.iapi.store.access.TransactionController)5 UUID (org.apache.derby.catalog.UUID)4 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)3 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)3 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)3 ArrayList (java.util.ArrayList)2 ReferencedColumnsDescriptorImpl (org.apache.derby.catalog.types.ReferencedColumnsDescriptorImpl)2 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)2 CheckConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor)2 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)2 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)2 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)2 TriggerDescriptor (org.apache.derby.iapi.sql.dictionary.TriggerDescriptor)2