use of org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor in project derby by apache.
the class DataDictionaryImpl method addConstraintDescriptor.
/**
* Adds the given ConstraintDescriptor to the data dictionary,
* associated with the given table and constraint type.
*
* @param descriptor The descriptor to add
* @param tc The transaction controller
*
* @exception StandardException Thrown on error
*/
public void addConstraintDescriptor(ConstraintDescriptor descriptor, TransactionController tc) throws StandardException {
int type = descriptor.getConstraintType();
if (SanityManager.DEBUG) {
if (!(type == DataDictionary.PRIMARYKEY_CONSTRAINT || type == DataDictionary.FOREIGNKEY_CONSTRAINT || type == DataDictionary.UNIQUE_CONSTRAINT || type == DataDictionary.CHECK_CONSTRAINT)) {
SanityManager.THROWASSERT("constraint type (" + type + ") is unexpected value");
}
}
addDescriptor(descriptor, descriptor.getSchemaDescriptor(), SYSCONSTRAINTS_CATALOG_NUM, false, tc);
switch(type) {
case DataDictionary.PRIMARYKEY_CONSTRAINT:
case DataDictionary.FOREIGNKEY_CONSTRAINT:
case DataDictionary.UNIQUE_CONSTRAINT:
if (SanityManager.DEBUG) {
if (!(descriptor instanceof KeyConstraintDescriptor)) {
SanityManager.THROWASSERT("descriptor expected to be instanceof KeyConstraintDescriptor, " + "not, " + descriptor.getClass().getName());
}
}
addSubKeyConstraint((KeyConstraintDescriptor) descriptor, tc);
break;
case DataDictionary.CHECK_CONSTRAINT:
if (SanityManager.DEBUG) {
if (!(descriptor instanceof CheckConstraintDescriptor)) {
SanityManager.THROWASSERT("descriptor expected " + "to be instanceof CheckConstraintDescriptorImpl, " + "not, " + descriptor.getClass().getName());
}
}
addDescriptor(descriptor, null, SYSCHECKS_CATALOG_NUM, true, tc);
break;
}
}
use of org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor in project derby by apache.
the class SYSKEYSRowFactory method makeRow.
// ///////////////////////////////////////////////////////////////////////////
//
// METHODS
//
// ///////////////////////////////////////////////////////////////////////////
/**
* Make a SYSKEYS row
*
* @return Row suitable for inserting into SYSKEYS.
*
* @exception StandardException thrown on failure
*/
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
DataValueDescriptor col;
ExecRow row;
UUID oid;
String constraintID = null;
String conglomerateID = null;
if (td != null) {
KeyConstraintDescriptor constraint = (KeyConstraintDescriptor) td;
/*
** We only allocate a new UUID if the descriptor doesn't already have one.
** For descriptors replicated from a Source system, we already have an UUID.
*/
oid = constraint.getUUID();
constraintID = oid.toString();
conglomerateID = constraint.getIndexUUIDString();
}
/* Insert info into syskeys */
/* RESOLVE - It would be nice to require less knowledge about syskeys
* and have this be more table driven.
*/
/* Build the row to insert */
row = getExecutionFactory().getValueRow(SYSKEYS_COLUMN_COUNT);
/* 1st column is CONSTRAINTID (UUID - char(36)) */
row.setColumn(SYSKEYS_CONSTRAINTID, new SQLChar(constraintID));
/* 2nd column is CONGLOMERATEID (UUID - char(36)) */
row.setColumn(SYSKEYS_CONGLOMERATEID, new SQLChar(conglomerateID));
return row;
}
use of org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor in project derby by apache.
the class ModifyColumnNode method checkExistingConstraints.
/**
* Check if the the column can be modified, and throw error if not.
*
* If the type of a column is being changed (for instance if the length
* of the column is being increased) then make sure that this does not
* violate any key constraints;
* the column being altered is
* 1. part of foreign key constraint
* ==> ERROR. This references a Primary Key constraint and the
* type and lengths of the pkey/fkey must match exactly.
* 2. part of a unique/primary key constraint
* ==> OK if no fkey references this constraint.
* ==> ERROR if any fkey in the system references this constraint.
*
* @param td The Table Descriptor on which the ALTER is being done.
*
* @exception StandardException Thrown on Error.
*/
void checkExistingConstraints(TableDescriptor td) throws StandardException {
if ((kind != K_MODIFY_COLUMN_TYPE) && (kind != K_MODIFY_COLUMN_CONSTRAINT) && (kind != K_MODIFY_COLUMN_CONSTRAINT_NOT_NULL))
return;
DataDictionary dd = getDataDictionary();
ConstraintDescriptorList cdl = dd.getConstraintDescriptors(td);
int[] intArray = new int[1];
intArray[0] = columnPosition;
for (int index = 0; index < cdl.size(); index++) {
ConstraintDescriptor existingConstraint = cdl.elementAt(index);
if (!(existingConstraint instanceof KeyConstraintDescriptor))
continue;
if (!existingConstraint.columnIntersects(intArray))
continue;
int constraintType = existingConstraint.getConstraintType();
// and fkey columns.
if ((constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT) && (kind == K_MODIFY_COLUMN_TYPE)) {
throw StandardException.newException(SQLState.LANG_MODIFY_COLUMN_FKEY_CONSTRAINT, name, existingConstraint.getConstraintName());
} else {
if (!dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_4, null)) {
// made nullable in soft upgrade mode from a pre-10.4 db.
if (kind == K_MODIFY_COLUMN_CONSTRAINT && (existingConstraint.getConstraintType() == DataDictionary.UNIQUE_CONSTRAINT)) {
throw StandardException.newException(SQLState.LANG_MODIFY_COLUMN_EXISTING_CONSTRAINT, name);
}
}
// is being made nullable; can't be done.
if ((kind == K_MODIFY_COLUMN_CONSTRAINT) && ((existingConstraint.getConstraintType() == DataDictionary.PRIMARYKEY_CONSTRAINT))) {
String errorState = (getLanguageConnectionContext().getDataDictionary().checkVersion(DataDictionary.DD_VERSION_DERBY_10_4, null)) ? SQLState.LANG_MODIFY_COLUMN_EXISTING_PRIMARY_KEY : SQLState.LANG_MODIFY_COLUMN_EXISTING_CONSTRAINT;
throw StandardException.newException(errorState, name);
}
// unique key or primary key.
ConstraintDescriptorList refcdl = dd.getForeignKeys(existingConstraint.getUUID());
if (refcdl.size() > 0) {
throw StandardException.newException(SQLState.LANG_MODIFY_COLUMN_REFERENCED, name, refcdl.elementAt(0).getConstraintName());
}
// Make the statement dependent on the primary key constraint.
getCompilerContext().createDependency(existingConstraint);
}
}
}
use of org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor in project derby by apache.
the class DDLSingleTableConstantAction method dropConstraint.
/**
* See "dropConstraint(...") above.
*
* @param newConglomActions Optional List. If non-null then
* for each ConglomerateDescriptor for which we skip the
* "create new conglomerate" processing we will add a
* ConstantAction to this list. The constant action can
* then be executed later (esp. by the caller) to create the
* new conglomerate, if needed. If this argument is null and
* we skip creation of a new conglomerate, the new conglomerate
* is effectively ignored (which may be fine in some cases--
* ex. when dropping a table).
*/
void dropConstraint(ConstraintDescriptor consDesc, TableDescriptor skipCreate, List<ConstantAction> newConglomActions, Activation activation, LanguageConnectionContext lcc, boolean clearDeps) throws StandardException {
/* Get the properties on the old backing conglomerate before
* dropping the constraint, since we can't get them later if
* dropping the constraint causes us to drop the backing
* conglomerate.
*/
Properties ixProps = null;
if (consDesc instanceof KeyConstraintDescriptor) {
ixProps = new Properties();
loadIndexProperties(lcc, ((KeyConstraintDescriptor) consDesc).getIndexConglomerateDescriptor(lcc.getDataDictionary()), ixProps);
}
ConglomerateDescriptor newBackingConglomCD = consDesc.drop(lcc, clearDeps);
/* If we don't need a new conglomerate then there's nothing
* else to do.
*/
if (newBackingConglomCD == null)
return;
/* Only create the new conglomerate if it is NOT for the table
* described by skipCreate.
*/
if ((skipCreate != null) && skipCreate.getUUID().equals(consDesc.getTableDescriptor().getUUID())) {
/* We're skipping the "create new conglom" phase; if we have
* a list in which to store the ConstantAction, then store it;
* otherwise, the new conglomerate is effectively ignored.
*/
if (newConglomActions != null) {
newConglomActions.add(getConglomReplacementAction(newBackingConglomCD, consDesc.getTableDescriptor(), ixProps));
}
} else {
executeConglomReplacement(getConglomReplacementAction(newBackingConglomCD, consDesc.getTableDescriptor(), ixProps), activation);
}
return;
}
Aggregations