use of org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor in project derby by apache.
the class DataDictionaryImpl method dropSubKeyConstraint.
/**
* Drop the matching row from syskeys when dropping a primary key
* or unique constraint.
*
* @param constraint the constraint
* @param tc The TransactionController
*
* @exception StandardException Thrown on failure
*/
private void dropSubKeyConstraint(ConstraintDescriptor constraint, TransactionController tc) throws StandardException {
ExecIndexRow keyRow1 = null;
DataValueDescriptor constraintIdOrderable;
TabInfoImpl ti;
int baseNum;
int indexNum;
if (constraint.getConstraintType() == DataDictionary.FOREIGNKEY_CONSTRAINT) {
baseNum = SYSFOREIGNKEYS_CATALOG_NUM;
indexNum = SYSFOREIGNKEYSRowFactory.SYSFOREIGNKEYS_INDEX1_ID;
/*
** If we have a foreign key, we need to decrement the
** reference count of the contraint that this FK references.
** We need to do this *before* we drop the foreign key
** because of the way FK.getReferencedConstraint() works.
*/
if (constraint.getConstraintType() == DataDictionary.FOREIGNKEY_CONSTRAINT) {
ReferencedKeyConstraintDescriptor refDescriptor = (ReferencedKeyConstraintDescriptor) getConstraintDescriptor(((ForeignKeyConstraintDescriptor) constraint).getReferencedConstraintId());
if (refDescriptor != null) {
refDescriptor.decrementReferenceCount();
int[] colsToSet = new int[1];
colsToSet[0] = SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_REFERENCECOUNT;
updateConstraintDescriptor(refDescriptor, refDescriptor.getUUID(), colsToSet, tc);
}
}
} else {
baseNum = SYSKEYS_CATALOG_NUM;
indexNum = SYSKEYSRowFactory.SYSKEYS_INDEX1_ID;
}
ti = getNonCoreTI(baseNum);
/* Use constraintIdOrderable in both start
* and stop position for index 1 scan.
*/
constraintIdOrderable = getIDValueAsCHAR(constraint.getUUID());
/* Set up the start/stop position for the scan */
keyRow1 = (ExecIndexRow) exFactory.getIndexableRow(1);
keyRow1.setColumn(1, constraintIdOrderable);
ti.deleteRow(tc, keyRow1, indexNum);
}
use of org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor 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;
}
use of org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor 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());
}
use of org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor in project derby by apache.
the class RenameConstantAction method execGutsRenameTable.
// do necessary work for rename table at execute time.
private void execGutsRenameTable(TableDescriptor td, Activation activation) throws StandardException {
ConstraintDescriptorList constraintDescriptorList;
ConstraintDescriptor constraintDescriptor;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
dm.invalidateFor(td, DependencyManager.RENAME, lcc);
/* look for foreign key dependency on the table. If found any,
use dependency manager to pass the rename action to the
dependents. */
constraintDescriptorList = dd.getConstraintDescriptors(td);
for (int index = 0; index < constraintDescriptorList.size(); index++) {
constraintDescriptor = constraintDescriptorList.elementAt(index);
if (constraintDescriptor instanceof ReferencedKeyConstraintDescriptor)
dm.invalidateFor(constraintDescriptor, DependencyManager.RENAME, lcc);
}
// Drop the table
dd.dropTableDescriptor(td, sd, tc);
// Change the table name of the table descriptor
td.setTableName(newTableName);
// add the table descriptor with new name
dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, false, tc);
}
use of org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor in project derby by apache.
the class DropTableConstantAction method dropAllConstraintDescriptors.
private void dropAllConstraintDescriptors(TableDescriptor td, Activation activation) throws StandardException {
ConstraintDescriptor cd;
ConstraintDescriptorList cdl;
ConstraintDescriptor fkcd;
ConstraintDescriptorList fkcdl;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
cdl = dd.getConstraintDescriptors(td);
/* The current element will be deleted underneath
* the loop, so we only increment the counter when
* skipping an element. (HACK!)
*/
for (int index = 0; index < cdl.size(); ) {
cd = cdl.elementAt(index);
if (cd instanceof ReferencedKeyConstraintDescriptor) {
index++;
continue;
}
dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc);
dropConstraint(cd, td, activation, lcc, true);
}
/* The current element will be deleted underneath
* the loop. (HACK!)
*/
while (cdl.size() > 0) {
cd = cdl.elementAt(0);
if (SanityManager.DEBUG) {
if (!(cd instanceof ReferencedKeyConstraintDescriptor)) {
SanityManager.THROWASSERT("Constraint descriptor not an instance of " + "ReferencedKeyConstraintDescriptor as expected. Is a " + cd.getClass().getName());
}
}
/*
** Drop the referenced constraint (after we got
** the primary keys) now. Do this prior to
** droping the referenced keys to avoid performing
** a lot of extra work updating the referencedcount
** field of sys.sysconstraints.
**
** Pass in false to dropConstraintsAndIndex so it
** doesn't clear dependencies, we'll do that ourselves.
*/
dropConstraint(cd, td, activation, lcc, false);
/*
** If we are going to cascade, get all the
** referencing foreign keys and zap them first.
*/
if (cascade) {
/*
** Go to the system tables to get the foreign keys
** to be safe
*/
fkcdl = dd.getForeignKeys(cd.getUUID());
/*
** For each FK that references this key, drop
** it.
*/
for (int inner = 0; inner < fkcdl.size(); inner++) {
fkcd = (ConstraintDescriptor) fkcdl.elementAt(inner);
dm.invalidateFor(fkcd, DependencyManager.DROP_CONSTRAINT, lcc);
dropConstraint(fkcd, td, activation, lcc, true);
activation.addWarning(StandardException.newWarning(SQLState.LANG_CONSTRAINT_DROPPED, fkcd.getConstraintName(), fkcd.getTableDescriptor().getName()));
}
}
/*
** Now that we got rid of the fks (if we were cascading), it is
** ok to do an invalidate for.
*/
dm.invalidateFor(cd, DependencyManager.DROP_CONSTRAINT, lcc);
dm.clearDependencies(lcc, cd);
}
}
Aggregations