Search in sources :

Example 11 with ConstraintDescriptor

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

the class BaseJoinStrategy method fillInScanArgs2.

final void fillInScanArgs2(MethodBuilder mb, Optimizable innerTable, int bulkFetch, int colRefItem, int indexColItem, int lockMode, boolean tableLocked, int isolationLevel) throws StandardException {
    mb.push(innerTable.getBaseTableName());
    // run time statistics.
    if (innerTable.getProperties() != null)
        mb.push(PropertyUtil.sortProperties(innerTable.getProperties()));
    else
        mb.pushNull("java.lang.String");
    ConglomerateDescriptor cd = innerTable.getTrulyTheBestAccessPath().getConglomerateDescriptor();
    if (cd.isConstraint()) {
        DataDictionary dd = innerTable.getDataDictionary();
        TableDescriptor td = innerTable.getTableDescriptor();
        ConstraintDescriptor constraintDesc = dd.getConstraintDescriptor(td, cd.getUUID());
        mb.push(constraintDesc.getConstraintName());
    } else if (cd.isIndex()) {
        mb.push(cd.getConglomerateName());
    } else {
        mb.pushNull("java.lang.String");
    }
    // Whether or not the conglomerate is the backing index for a constraint
    mb.push(cd.isConstraint());
    // tell it whether it's to open for update, which we should do if
    // it's an update or delete statement, or if it's the target
    // table of an updatable cursor.
    mb.push(innerTable.forUpdate());
    mb.push(colRefItem);
    mb.push(indexColItem);
    mb.push(lockMode);
    mb.push(tableLocked);
    mb.push(isolationLevel);
    if (bulkFetch > 0) {
        mb.push(bulkFetch);
        // If the table references LOBs, we want to disable bulk fetching
        // when the cursor is holdable. Otherwise, a commit could close
        // LOBs before they have been returned to the user.
        mb.push(innerTable.hasLargeObjectColumns());
    }
    /* 1 row scans (avoiding 2nd next()) are
 		 * only meaningful for some join strategies.
		 * (Only an issue for outer table, which currently
		 * can only be nested loop, as avoidance of 2nd next
		 * on inner table already factored in to join node.)
		 */
    if (validForOutermostTable()) {
        mb.push(innerTable.isOneRowScan());
    }
    mb.push(innerTable.getTrulyTheBestAccessPath().getCostEstimate().rowCount());
    mb.push(innerTable.getTrulyTheBestAccessPath().getCostEstimate().getEstimatedCost());
}
Also used : ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 12 with ConstraintDescriptor

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

the class TableElementList method validate.

/**
 * Validate this TableElementList.  This includes checking for
 * duplicate columns names, and checking that user types really exist.
 *
 * @param ddlStmt	DDLStatementNode which contains this list
 * @param dd		DataDictionary to use
 * @param td		TableDescriptor for table, if existing table.
 *
 * @exception StandardException		Thrown on error
 */
void validate(DDLStatementNode ddlStmt, DataDictionary dd, TableDescriptor td) throws StandardException {
    this.td = td;
    int numAutoCols = 0;
    int size = size();
    HashSet<String> columnNames = new HashSet<String>(size + 2, 0.999f);
    HashSet<String> constraintNames = new HashSet<String>(size + 2, 0.999f);
    // all the primary key/unique key constraints for this table
    ArrayList<Object> constraints = new ArrayList<Object>();
    // special case for alter table (td is not null in case of alter table)
    if (td != null) {
        // In case of alter table, get the already existing primary key and unique
        // key constraints for this table. And then we will compare them with  new
        // primary key/unique key constraint column lists.
        ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
        ConstraintDescriptor cd;
        if (// table does have some pre-existing constraints defined on it
        cdl != null) {
            for (int i = 0; i < cdl.size(); i++) {
                cd = cdl.elementAt(i);
                // if the constraint type is not primary key or unique key, ignore it.
                if (cd.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT || cd.getConstraintType() == DataDictionary.UNIQUE_CONSTRAINT) {
                    constraints.add(cd);
                }
            }
        }
    }
    int tableType = TableDescriptor.BASE_TABLE_TYPE;
    if (ddlStmt instanceof CreateTableNode)
        tableType = ((CreateTableNode) ddlStmt).tableType;
    for (TableElementNode tableElement : this) {
        if (tableElement instanceof ColumnDefinitionNode) {
            ColumnDefinitionNode cdn = (ColumnDefinitionNode) tableElement;
            if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE && (cdn.getType().getTypeId().isLongConcatableTypeId() || cdn.getType().getTypeId().isUserDefinedTypeId())) {
                throw StandardException.newException(SQLState.LANG_LONG_DATA_TYPE_NOT_ALLOWED, cdn.getColumnName());
            }
            checkForDuplicateColumns(ddlStmt, columnNames, cdn.getColumnName());
            cdn.checkUserType(td);
            cdn.bindAndValidateDefault(dd, td);
            cdn.validateAutoincrement(dd, td, tableType);
            if (tableElement instanceof ModifyColumnNode) {
                ModifyColumnNode mcdn = (ModifyColumnNode) cdn;
                mcdn.checkExistingConstraints(td);
                mcdn.useExistingCollation(td);
            } else if (cdn.isAutoincrementColumn()) {
                numAutoCols++;
            }
        } else if (tableElement.getElementType() == TableElementNode.AT_DROP_COLUMN) {
            String colName = tableElement.getName();
            if (td.getColumnDescriptor(colName) == null) {
                throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, colName, td.getQualifiedName());
            }
            break;
        }
        /* The rest of this method deals with validating constraints */
        if (!(tableElement.hasConstraint())) {
            continue;
        }
        ConstraintDefinitionNode cdn = (ConstraintDefinitionNode) tableElement;
        cdn.bind(ddlStmt, dd);
        // If constraint is primary key or unique key, add it to the list.
        if (cdn.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT || cdn.getConstraintType() == DataDictionary.UNIQUE_CONSTRAINT) {
            /* In case of create table, the list can have only ConstraintDefinitionNode
				* elements. In case of alter table, it can have both ConstraintDefinitionNode
				* (for new constraints) and ConstraintDescriptor(for pre-existing constraints).
				*/
            Object destConstraint;
            String destName = null;
            String[] destColumnNames = null;
            for (int i = 0; i < constraints.size(); i++) {
                destConstraint = constraints.get(i);
                if (destConstraint instanceof ConstraintDefinitionNode) {
                    ConstraintDefinitionNode destCDN = (ConstraintDefinitionNode) destConstraint;
                    destName = destCDN.getConstraintMoniker();
                    destColumnNames = destCDN.getColumnList().getColumnNames();
                } else if (destConstraint instanceof ConstraintDescriptor) {
                    // will come here only for pre-existing constraints in case of alter table
                    ConstraintDescriptor destCD = (ConstraintDescriptor) destConstraint;
                    destName = destCD.getConstraintName();
                    destColumnNames = destCD.getColumnDescriptors().getColumnNames();
                }
                // check if there are multiple constraints with same set of columns
                if (columnsMatch(cdn.getColumnList().getColumnNames(), destColumnNames))
                    throw StandardException.newException(SQLState.LANG_MULTIPLE_CONSTRAINTS_WITH_SAME_COLUMNS, cdn.getConstraintMoniker(), destName);
            }
            constraints.add(cdn);
        }
        /* Make sure that there are no duplicate constraint names in the list */
        checkForDuplicateConstraintNames(ddlStmt, constraintNames, cdn.getConstraintMoniker());
        /* Make sure that the constraint we are trying to drop exists */
        if (cdn.getConstraintType() == DataDictionary.DROP_CONSTRAINT || cdn.getConstraintType() == DataDictionary.MODIFY_CONSTRAINT) {
            /*
				** If no schema descriptor, then must be an invalid
				** schema name.
				*/
            String dropConstraintName = cdn.getConstraintMoniker();
            if (dropConstraintName != null) {
                String dropSchemaName = cdn.getDropSchemaName();
                SchemaDescriptor sd = dropSchemaName == null ? td.getSchemaDescriptor() : getSchemaDescriptor(dropSchemaName);
                ConstraintDescriptor cd = dd.getConstraintDescriptorByName(td, sd, dropConstraintName, false);
                if (cd == null) {
                    throw StandardException.newException(SQLState.LANG_DROP_OR_ALTER_NON_EXISTING_CONSTRAINT, (sd.getSchemaName() + "." + dropConstraintName), td.getQualifiedName());
                }
                /* Statement is dependendent on the ConstraintDescriptor */
                getCompilerContext().createDependency(cd);
            }
        }
        // validation of primary key nullability moved to validatePrimaryKeyNullability().
        if (cdn.hasPrimaryKeyConstraint()) {
            // for PRIMARY KEY, check that columns are unique
            verifyUniqueColumnList(ddlStmt, cdn);
        } else if (cdn.hasUniqueKeyConstraint()) {
            // for UNIQUE, check that columns are unique
            verifyUniqueColumnList(ddlStmt, cdn);
            // disallow until database hard upgraded at least to 10.4.
            if (!dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_4, null)) {
                checkForNullColumns(cdn, td);
            }
        } else if (cdn.hasForeignKeyConstraint()) {
            // for FOREIGN KEY, check that columns are unique
            verifyUniqueColumnList(ddlStmt, cdn);
        }
    }
    // with ALTER TABLE ADD COLUMN.
    if (numAutoCols > 1 || (numAutoCols > 0 && td != null && td.tableHasAutoincrement())) {
        throw StandardException.newException(SQLState.LANG_MULTIPLE_AUTOINCREMENT_COLUMNS);
    }
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) ArrayList(java.util.ArrayList) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) HashSet(java.util.HashSet)

Example 13 with ConstraintDescriptor

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

the class CreateConstraintConstantAction method executeConstantAction.

// INTERFACE METHODS
/**
 *	This is the guts of the Execution-time logic for CREATE CONSTRAINT.
 *  <P>
 *  A constraint is represented as:
 *  <UL>
 *  <LI> ConstraintDescriptor.
 *  </UL>
 *  If a backing index is required then the index will
 *  be created through an CreateIndexConstantAction setup
 *  by the compiler.
 *  <BR>
 *  Dependencies are created as:
 *  <UL>
 *  <LI> ConstraintDescriptor depends on all the providers collected
 *  at compile time and passed into the constructor.
 *  <LI> For a FOREIGN KEY constraint ConstraintDescriptor depends
 *  on the ConstraintDescriptor for the referenced constraints
 *  and the privileges required to create the constraint.
 *  </UL>
 *
 *  @see ConstraintDescriptor
 *  @see CreateIndexConstantAction
 *	@see ConstantAction#executeConstantAction
 *
 * @exception StandardException		Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    ConglomerateDescriptor conglomDesc = null;
    ConglomerateDescriptor[] conglomDescs = null;
    ConstraintDescriptor conDesc = null;
    TableDescriptor td = null;
    UUID indexId = null;
    String uniqueName;
    String backingIndexName;
    /* RESOLVE - blow off not null constraints for now (and probably for ever) */
    if (constraintType == DataDictionary.NOTNULL_CONSTRAINT) {
        return;
    }
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();
    cf = lcc.getLanguageConnectionFactory().getClassFactory();
    /*
		** Inform the data dictionary that we are about to write to it.
		** There are several calls to data dictionary "get" methods here
		** that might be done in "read" mode in the data dictionary, but
		** it seemed safer to do this whole operation in "write" mode.
		**
		** We tell the data dictionary we're done writing at the end of
		** the transaction.
		*/
    dd.startWriting(lcc);
    /* Table gets locked in AlterTableConstantAction */
    /*
		** If the schema descriptor is null, then
		** we must have just read ourselves in.  
		** So we will get the corresponding schema
		** descriptor from the data dictionary.
		*/
    SchemaDescriptor sd = dd.getSchemaDescriptor(schemaName, tc, true);
    /* Try to get the TableDescriptor from
		 * the Activation. We will go to the
		 * DD if not there. (It should always be
		 * there except when in a target.)
		 */
    td = activation.getDDLTableDescriptor();
    if (td == null) {
        /* tableId will be non-null if adding a
			 * constraint to an existing table.
			 */
        if (tableId != null) {
            td = dd.getTableDescriptor(tableId);
        } else {
            td = dd.getTableDescriptor(tableName, sd, tc);
        }
        if (td == null) {
            throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName);
        }
        activation.setDDLTableDescriptor(td);
    }
    /* Generate the UUID for the backing index.  This will become the
		 * constraint's name, if no name was specified.
		 */
    UUIDFactory uuidFactory = dd.getUUIDFactory();
    UUID constrId = uuidFactory.createUUID();
    /* Create the index, if there's one for this constraint */
    if (indexAction != null) {
        if (indexAction.getIndexName() == null) {
            /* Set the index name */
            backingIndexName = uuidFactory.createUUID().toString();
            indexAction.setIndexName(backingIndexName);
        } else {
            backingIndexName = indexAction.getIndexName();
        }
        indexAction.setConstraintID(constrId);
        /* Create the index */
        indexAction.executeConstantAction(activation);
        /* Get the conglomerate descriptor for the backing index */
        conglomDescs = td.getConglomerateDescriptors();
        for (int index = 0; index < conglomDescs.length; index++) {
            conglomDesc = conglomDescs[index];
            /* Check for conglomerate being an index first, since
				 * name is null for heap.
				 */
            if (conglomDesc.isIndex() && backingIndexName.equals(conglomDesc.getConglomerateName())) {
                break;
            }
        }
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(conglomDesc != null, "conglomDesc is expected to be non-null after search for backing index");
            SanityManager.ASSERT(conglomDesc.isIndex(), "conglomDesc is expected to be indexable after search for backing index");
            SanityManager.ASSERT(conglomDesc.getConglomerateName().equals(backingIndexName), "conglomDesc name expected to be the same as backing index name after search for backing index");
        }
        indexId = conglomDesc.getUUID();
    }
    boolean[] defaults = new boolean[] { ConstraintDefinitionNode.DEFERRABLE_DEFAULT, ConstraintDefinitionNode.INITIALLY_DEFERRED_DEFAULT, ConstraintDefinitionNode.ENFORCED_DEFAULT };
    for (int i = 0; i < characteristics.length; i++) {
        if (characteristics[i] != defaults[i]) {
            dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, "DEFERRED CONSTRAINTS");
            if (constraintType == DataDictionary.NOTNULL_CONSTRAINT || !characteristics[2]) /* not enforced */
            {
                // Remove when feature DERBY-532 is completed
                if (!PropertyUtil.getSystemProperty("derby.constraintsTesting", "false").equals("true")) {
                    throw StandardException.newException(SQLState.NOT_IMPLEMENTED, "non-default constraint characteristics");
                }
            }
        }
    }
    /* Now, lets create the constraint descriptor */
    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    switch(constraintType) {
        case DataDictionary.PRIMARYKEY_CONSTRAINT:
            conDesc = ddg.newPrimaryKeyConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], // int[],
            genColumnPositions(td, false), constrId, indexId, sd, characteristics[2], // referenceCount
            0);
            dd.addConstraintDescriptor(conDesc, tc);
            break;
        case DataDictionary.UNIQUE_CONSTRAINT:
            conDesc = ddg.newUniqueConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], // int[],
            genColumnPositions(td, false), constrId, indexId, sd, characteristics[2], // referenceCount
            0);
            dd.addConstraintDescriptor(conDesc, tc);
            break;
        case DataDictionary.CHECK_CONSTRAINT:
            conDesc = ddg.newCheckConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], constrId, constraintText, // int[],
            new ReferencedColumnsDescriptorImpl(genColumnPositions(td, false)), sd, characteristics[2]);
            dd.addConstraintDescriptor(conDesc, tc);
            storeConstraintDependenciesOnPrivileges(activation, conDesc, null, providerInfo);
            break;
        case DataDictionary.FOREIGNKEY_CONSTRAINT:
            ReferencedKeyConstraintDescriptor referencedConstraint = DDUtils.locateReferencedConstraint(dd, td, constraintName, columnNames, otherConstraintInfo);
            DDUtils.validateReferentialActions(dd, td, constraintName, otherConstraintInfo, columnNames);
            conDesc = ddg.newForeignKeyConstraintDescriptor(td, constraintName, // deferable,
            characteristics[0], // initiallyDeferred,
            characteristics[1], // int[],
            genColumnPositions(td, false), constrId, indexId, sd, referencedConstraint, characteristics[2], otherConstraintInfo.getReferentialActionDeleteRule(), otherConstraintInfo.getReferentialActionUpdateRule());
            // try to create the constraint first, because it
            // is expensive to do the bulk check, find obvious
            // errors first
            dd.addConstraintDescriptor(conDesc, tc);
            /* No need to do check if we're creating a 
				 * table.
				 */
            if ((!forCreateTable) && dd.activeConstraint(conDesc)) {
                validateFKConstraint(activation, tc, dd, (ForeignKeyConstraintDescriptor) conDesc, referencedConstraint, ((CreateIndexConstantAction) indexAction).getIndexTemplateRow());
            }
            /* Create stored dependency on the referenced constraint */
            dm.addDependency(conDesc, referencedConstraint, lcc.getContextManager());
            // store constraint's dependency on REFERENCES privileges in the dependeny system
            storeConstraintDependenciesOnPrivileges(activation, conDesc, referencedConstraint.getTableId(), providerInfo);
            break;
        case DataDictionary.MODIFY_CONSTRAINT:
            throw StandardException.newException(SQLState.NOT_IMPLEMENTED, "ALTER CONSTRAINT");
        default:
            if (SanityManager.DEBUG) {
                SanityManager.THROWASSERT("contraintType (" + constraintType + ") has unexpected value");
            }
            break;
    }
    /* Create stored dependencies for each provider */
    if (providerInfo != null) {
        for (int ix = 0; ix < providerInfo.length; ix++) {
            Provider provider = null;
            /* We should always be able to find the Provider */
            provider = (Provider) providerInfo[ix].getDependableFinder().getDependable(dd, providerInfo[ix].getObjectId());
            dm.addDependency(conDesc, provider, lcc.getContextManager());
        }
    }
    /* Finally, invalidate off of the table descriptor(s)
		 * to ensure that any dependent statements get
		 * re-compiled.
		 */
    if (!forCreateTable) {
        dm.invalidateFor(td, DependencyManager.CREATE_CONSTRAINT, lcc);
    }
    if (constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT) {
        if (SanityManager.DEBUG) {
            SanityManager.ASSERT(conDesc != null, "conDesc expected to be non-null");
            if (!(conDesc instanceof ForeignKeyConstraintDescriptor)) {
                SanityManager.THROWASSERT("conDesc expected to be instance of ForeignKeyConstraintDescriptor, not " + conDesc.getClass().getName());
            }
        }
        dm.invalidateFor(((ForeignKeyConstraintDescriptor) conDesc).getReferencedConstraint().getTableDescriptor(), DependencyManager.CREATE_CONSTRAINT, lcc);
    }
    this.constraintId = constrId;
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) UUIDFactory(org.apache.derby.iapi.services.uuid.UUIDFactory) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) ReferencedColumnsDescriptorImpl(org.apache.derby.catalog.types.ReferencedColumnsDescriptorImpl) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) Provider(org.apache.derby.iapi.sql.depend.Provider) DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) 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) UUID(org.apache.derby.catalog.UUID) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 14 with ConstraintDescriptor

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

the class AlterTableConstantAction method modifyColumnConstraint.

/**
 * Workhorse for modifying column level constraints.
 * Right now it is restricted to modifying a null constraint to a not null
 * constraint.
 */
private void modifyColumnConstraint(String colName, boolean nullability) throws StandardException {
    ColumnDescriptor columnDescriptor = td.getColumnDescriptor(colName);
    // Get the type and change the nullability
    DataTypeDescriptor dataType = columnDescriptor.getType().getNullabilityType(nullability);
    // check if there are any unique constraints to update
    ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
    int columnPostion = columnDescriptor.getPosition();
    for (int i = 0; i < cdl.size(); i++) {
        ConstraintDescriptor cd = cdl.elementAt(i);
        if (cd.getConstraintType() == DataDictionary.UNIQUE_CONSTRAINT) {
            ColumnDescriptorList columns = cd.getColumnDescriptors();
            for (int count = 0; count < columns.size(); count++) {
                if (columns.elementAt(count).getPosition() != columnPostion)
                    break;
                // get backing index
                ConglomerateDescriptor desc = td.getConglomerateDescriptor(cd.getConglomerateId());
                // not null ie is backed by unique index
                if (!(desc.getIndexDescriptor().isUnique() || desc.getIndexDescriptor().hasDeferrableChecking())) {
                    break;
                }
                // replace backing index with a unique when not null index.
                recreateUniqueConstraintBackingIndexAsUniqueWhenNotNull(desc, td, activation, lcc);
            }
        }
    }
    ColumnDescriptor newColumnDescriptor = new ColumnDescriptor(colName, columnDescriptor.getPosition(), dataType, columnDescriptor.getDefaultValue(), columnDescriptor.getDefaultInfo(), td, columnDescriptor.getDefaultUUID(), columnDescriptor.getAutoincStart(), columnDescriptor.getAutoincInc(), columnDescriptor.getAutoincCycle());
    // Update the ColumnDescriptor with new default info
    dd.dropColumnDescriptor(td.getUUID(), colName, tc);
    dd.addDescriptor(newColumnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) CheckConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor) 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) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

Example 15 with ConstraintDescriptor

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

the class RenameConstantAction method execGutsRenameColumn.

// do necessary work for rename column at execute time.
private void execGutsRenameColumn(TableDescriptor td, Activation activation) throws StandardException {
    ColumnDescriptor columnDescriptor = null;
    int columnPosition = 0;
    ConstraintDescriptorList constraintDescriptorList;
    ConstraintDescriptor constraintDescriptor;
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();
    /* get the column descriptor for column to be renamed and
		 * using it's position in the table, set the referenced
		 * column map of the table indicating which column is being
		 * renamed. Dependency Manager uses this to find out the
		 * dependents on the column.
		 */
    columnDescriptor = td.getColumnDescriptor(oldObjectName);
    if (columnDescriptor.isAutoincrement())
        columnDescriptor.setAutoinc_create_or_modify_Start_Increment(ColumnDefinitionNode.CREATE_AUTOINCREMENT);
    columnPosition = columnDescriptor.getPosition();
    FormatableBitSet toRename = new FormatableBitSet(td.getColumnDescriptorList().size() + 1);
    toRename.set(columnPosition);
    td.setReferencedColumnMap(toRename);
    dm.invalidateFor(td, DependencyManager.RENAME, lcc);
    // look for foreign key dependency on the column.
    constraintDescriptorList = dd.getConstraintDescriptors(td);
    for (int index = 0; index < constraintDescriptorList.size(); index++) {
        constraintDescriptor = constraintDescriptorList.elementAt(index);
        int[] referencedColumns = constraintDescriptor.getReferencedColumns();
        int numRefCols = referencedColumns.length;
        for (int j = 0; j < numRefCols; j++) {
            if ((referencedColumns[j] == columnPosition) && (constraintDescriptor instanceof ReferencedKeyConstraintDescriptor))
                dm.invalidateFor(constraintDescriptor, DependencyManager.RENAME, lcc);
        }
    }
    // Drop the column
    dd.dropColumnDescriptor(td.getUUID(), oldObjectName, tc);
    columnDescriptor.setColumnName(newObjectName);
    dd.addDescriptor(columnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
    // Need to do following to reload the cache so that table
    // descriptor now has new column name
    td = dd.getTableDescriptor(td.getObjectID());
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Aggregations

ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)34 ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)16 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)15 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)15 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)12 ForeignKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor)12 UUID (org.apache.derby.catalog.UUID)10 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)10 TransactionController (org.apache.derby.iapi.store.access.TransactionController)10 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)9 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)9 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)9 CheckConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.CheckConstraintDescriptor)8 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)7 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)7 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)7 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)6 SubCheckConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.SubCheckConstraintDescriptor)5 SubConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.SubConstraintDescriptor)5 SubKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.SubKeyConstraintDescriptor)5