use of org.apache.derby.catalog.UUID in project derby by apache.
the class CreateTableConstantAction method executeConstantAction.
// INTERFACE METHODS
/**
* This is the guts of the Execution-time logic for CREATE TABLE.
*
* @see ConstantAction#executeConstantAction
*
* @exception StandardException Thrown on failure
*/
public void executeConstantAction(Activation activation) throws StandardException {
TableDescriptor td;
UUID toid;
SchemaDescriptor schemaDescriptor;
ColumnDescriptor columnDescriptor;
ExecRow template;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
/* Mark the activation as being for create table */
activation.setForCreateTable();
// setup for create conglomerate call:
// o create row template to tell the store what type of rows this
// table holds.
// o create array of collation id's to tell collation id of each
// column in table.
template = RowUtil.getEmptyValueRow(columnInfo.length, lcc);
int[] collation_ids = new int[columnInfo.length];
for (int ix = 0; ix < columnInfo.length; ix++) {
ColumnInfo col_info = columnInfo[ix];
if (col_info.defaultValue != null) {
/* If there is a default value, use it, otherwise use null */
template.setColumn(ix + 1, col_info.defaultValue);
} else {
template.setColumn(ix + 1, col_info.dataType.getNull());
}
// get collation info for each column.
collation_ids[ix] = col_info.dataType.getCollationType();
}
/* create the conglomerate to hold the table's rows
* RESOLVE - If we ever have a conglomerate creator
* that lets us specify the conglomerate number then
* we will need to handle it here.
*/
long conglomId = tc.createConglomerate(// we're requesting a heap conglomerate
"heap", // row template
template.getRowArray(), // column sort order - not required for heap
null, collation_ids, // properties
properties, tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE ? (TransactionController.IS_TEMPORARY | TransactionController.IS_KEPT) : TransactionController.IS_DEFAULT);
/*
** 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.
*/
if (tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE)
dd.startWriting(lcc);
SchemaDescriptor sd;
if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE)
sd = dd.getSchemaDescriptor(schemaName, tc, true);
else
sd = DDLConstantAction.getSchemaDescriptorForCreate(dd, activation, schemaName);
//
// Create a new table descriptor.
//
DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
if (tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
td = ddg.newTableDescriptor(tableName, sd, tableType, lockGranularity);
dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, false, tc);
} else {
td = ddg.newTableDescriptor(tableName, sd, tableType, onCommitDeleteRows, onRollbackDeleteRows);
td.setUUID(dd.getUUIDFactory().createUUID());
}
toid = td.getUUID();
// Save the TableDescriptor off in the Activation
activation.setDDLTableDescriptor(td);
/* NOTE: We must write the columns out to the system
* tables before any of the conglomerates, including
* the heap, since we read the columns before the
* conglomerates when building a TableDescriptor.
* This will hopefully reduce the probability of
* a deadlock involving those system tables.
*/
// for each column, stuff system.column
int index = 1;
ColumnDescriptor[] cdlArray = new ColumnDescriptor[columnInfo.length];
for (int ix = 0; ix < columnInfo.length; ix++) {
UUID defaultUUID = columnInfo[ix].newDefaultUUID;
/* Generate a UUID for the default, if one exists
* and there is no default id yet.
*/
if (columnInfo[ix].defaultInfo != null && defaultUUID == null) {
defaultUUID = dd.getUUIDFactory().createUUID();
}
if (// dealing with autoinc column
columnInfo[ix].autoincInc != 0) {
columnDescriptor = new ColumnDescriptor(columnInfo[ix].name, index++, columnInfo[ix].dataType, columnInfo[ix].defaultValue, columnInfo[ix].defaultInfo, td, defaultUUID, columnInfo[ix].autoincStart, columnInfo[ix].autoincInc, columnInfo[ix].autoinc_create_or_modify_Start_Increment, columnInfo[ix].autoincCycle);
//
if (dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null)) {
CreateSequenceConstantAction csca = makeCSCA(columnInfo[ix], TableDescriptor.makeSequenceName(toid));
csca.executeConstantAction(activation);
}
} else {
columnDescriptor = new ColumnDescriptor(columnInfo[ix].name, index++, columnInfo[ix].dataType, columnInfo[ix].defaultValue, columnInfo[ix].defaultInfo, td, defaultUUID, columnInfo[ix].autoincStart, columnInfo[ix].autoincInc, columnInfo[ix].autoincCycle);
}
cdlArray[ix] = columnDescriptor;
}
if (tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
dd.addDescriptorArray(cdlArray, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
}
// now add the column descriptors to the table.
ColumnDescriptorList cdl = td.getColumnDescriptorList();
for (int i = 0; i < cdlArray.length; i++) cdl.add(cdlArray[i]);
//
// Create a conglomerate desciptor with the conglomId filled in and
// add it.
//
// RESOLVE: Get information from the conglomerate descriptor which
// was provided.
//
ConglomerateDescriptor cgd = ddg.newConglomerateDescriptor(conglomId, null, false, null, false, null, toid, sd.getUUID());
if (tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
dd.addDescriptor(cgd, sd, DataDictionary.SYSCONGLOMERATES_CATALOG_NUM, false, tc);
}
// add the newly added conglomerate to the table descriptor
ConglomerateDescriptorList conglomList = td.getConglomerateDescriptorList();
conglomList.add(cgd);
/* Create any constraints */
if (constraintActions != null) {
/*
** Do everything but FK constraints first,
** then FK constraints on 2nd pass.
*/
for (int conIndex = 0; conIndex < constraintActions.length; conIndex++) {
// skip fks
if (!constraintActions[conIndex].isForeignKeyConstraint()) {
constraintActions[conIndex].executeConstantAction(activation);
}
}
for (int conIndex = 0; conIndex < constraintActions.length; conIndex++) {
// only foreign keys
if (constraintActions[conIndex].isForeignKeyConstraint()) {
constraintActions[conIndex].executeConstantAction(activation);
}
}
}
//
for (int ix = 0; ix < columnInfo.length; ix++) {
addColumnDependencies(lcc, dd, td, columnInfo[ix]);
}
//
// The table itself can depend on the user defined types of its columns.
//
adjustUDTDependencies(lcc, dd, td, columnInfo, false);
if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
lcc.addDeclaredGlobalTempTable(td);
}
// Indicate that the CREATE TABLE statement itself depends on the
// table it is creating. Normally such statement dependencies are
// added during compilation, but here we have a bootstrapping issue
// because the table doesn't exist until the CREATE TABLE statement
// has been executed, so we had to defer the creation of this
// dependency until now. (DERBY-4479)
dd.getDependencyManager().addDependency(activation.getPreparedStatement(), td, lcc.getContextManager());
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class CreateTriggerConstantAction method executeConstantAction.
/**
* This is the guts of the Execution-time logic for CREATE TRIGGER.
*
* @see ConstantAction#executeConstantAction
*
* @exception StandardException Thrown on failure
*/
public void executeConstantAction(Activation activation) throws StandardException {
SPSDescriptor whenspsd = null;
SPSDescriptor actionspsd;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
/*
** Indicate that we are about to modify the data dictionary.
**
** We tell the data dictionary we're done writing at the end of
** the transaction.
*/
dd.startWriting(lcc);
SchemaDescriptor triggerSd = getSchemaDescriptorForCreate(dd, activation, triggerSchemaName);
if (spsCompSchemaId == null) {
SchemaDescriptor def = lcc.getDefaultSchema();
if (def.getUUID() == null) {
// Descriptor for default schema is stale,
// look it up in the dictionary
def = dd.getSchemaDescriptor(def.getDescriptorName(), tc, false);
}
/*
** It is possible for spsCompSchemaId to be null. For instance,
** the current schema may not have been physically created yet but
** it exists "virtually". In this case, its UUID will have the
** value of null meaning that it is not persistent. e.g.:
**
** CONNECT 'db;create=true' user 'ernie';
** CREATE TABLE bert.t1 (i INT);
** CREATE TRIGGER bert.tr1 AFTER INSERT ON bert.t1
** FOR EACH STATEMENT MODE DB2SQL
** SELECT * FROM SYS.SYSTABLES;
**
** Note that in the above case, the trigger action statement have a
** null compilation schema. A compilation schema with null value
** indicates that the trigger action statement text does not have
** any dependencies with the CURRENT SCHEMA. This means:
**
** o It is safe to compile this statement in any schema since
** there is no dependency with the CURRENT SCHEMA. i.e.: All
** relevent identifiers are qualified with a specific schema.
**
** o The statement cache mechanism can utilize this piece of
** information to enable better statement plan sharing across
** connections in different schemas; thus, avoiding unnecessary
** statement compilation.
*/
if (def != null)
spsCompSchemaId = def.getUUID();
}
String tabName;
if (triggerTable != null) {
triggerTableId = triggerTable.getUUID();
tabName = triggerTable.getName();
} else
tabName = "with UUID " + triggerTableId;
/* We need to get table descriptor again. We simply can't trust the
* one we got at compile time, the lock on system table was released
* when compile was done, and the table might well have been dropped.
*/
triggerTable = dd.getTableDescriptor(triggerTableId);
if (triggerTable == null) {
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tabName);
}
/* Lock the table for DDL. Otherwise during our execution, the table
* might be changed, even dropped. Beetle 4269
*/
lockTableForDDL(tc, triggerTable.getHeapConglomerateId(), true);
/* get triggerTable again for correctness, in case it's changed before
* the lock is aquired
*/
triggerTable = dd.getTableDescriptor(triggerTableId);
if (triggerTable == null) {
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tabName);
}
/*
** Send an invalidate on the table from which
** the triggering event emanates. This it
** to make sure that DML statements on this table
** will be recompiled. Do this before we create
** our trigger spses lest we invalidate them just
** after creating them.
*/
dm.invalidateFor(triggerTable, DependencyManager.CREATE_TRIGGER, lcc);
/*
** Lets get our trigger id up front, we'll use it when
** we create our spses.
*/
UUID tmpTriggerId = dd.getUUIDFactory().createUUID();
actionSPSId = (actionSPSId == null) ? dd.getUUIDFactory().createUUID() : actionSPSId;
if (whenSPSId == null && whenText != null) {
whenSPSId = dd.getUUIDFactory().createUUID();
}
DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
/*
** Create the trigger descriptor first so the trigger action
** compilation can pick up the relevant trigger especially in
** the case of self triggering.
*/
TriggerDescriptor triggerd = ddg.newTriggerDescriptor(triggerSd, tmpTriggerId, triggerName, eventMask, isBefore, isRow, isEnabled, triggerTable, whenSPSId, actionSPSId, makeCreationTimestamp(dd), referencedCols, referencedColsInTriggerAction, originalActionText, referencingOld, referencingNew, oldReferencingName, newReferencingName, originalWhenText);
dd.addDescriptor(triggerd, triggerSd, DataDictionary.SYSTRIGGERS_CATALOG_NUM, false, tc);
/*
** If we have a WHEN action we create it now.
*/
if (whenText != null) {
// The WHEN clause is just a search condition and not a full
// SQL statement. Turn in into a VALUES statement.
String whenValuesStmt = "VALUES " + whenText;
whenspsd = createSPS(lcc, ddg, dd, tc, tmpTriggerId, triggerSd, whenSPSId, spsCompSchemaId, whenValuesStmt, true, triggerTable);
}
/*
** Create the trigger action
*/
actionspsd = createSPS(lcc, ddg, dd, tc, tmpTriggerId, triggerSd, actionSPSId, spsCompSchemaId, actionText, false, triggerTable);
/*
** Make underlying spses dependent on the trigger.
*/
if (whenspsd != null) {
dm.addDependency(triggerd, whenspsd, lcc.getContextManager());
}
dm.addDependency(triggerd, actionspsd, lcc.getContextManager());
dm.addDependency(triggerd, triggerTable, lcc.getContextManager());
// from the triggered statement or the WHEN clause.
for (ProviderInfo info : providerInfo) {
Provider provider = (Provider) info.getDependableFinder().getDependable(dd, info.getObjectId());
dm.addDependency(triggerd, provider, lcc.getContextManager());
}
// store trigger's dependency on various privileges in the dependeny system
storeViewTriggerDependenciesOnPrivileges(activation, triggerd);
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class AlterTableConstantAction method modifyColumnDefault.
/**
* Workhorse for modifying the default value of a column.
*
* @param ix the index of the column specfication in the ALTER
* statement-- currently we allow only one.
* @exception StandardException, thrown on error.
*/
private void modifyColumnDefault(int ix) throws StandardException {
ColumnDescriptor columnDescriptor = td.getColumnDescriptor(columnInfo[ix].name);
int columnPosition = columnDescriptor.getPosition();
// Clean up after the old default, if non-null
if (columnDescriptor.hasNonNullDefault()) {
// Invalidate off of the old default
DefaultDescriptor defaultDescriptor = new DefaultDescriptor(dd, columnInfo[ix].oldDefaultUUID, td.getUUID(), columnPosition);
dm.invalidateFor(defaultDescriptor, DependencyManager.MODIFY_COLUMN_DEFAULT, lcc);
// Drop any dependencies
dm.clearDependencies(lcc, defaultDescriptor);
}
UUID defaultUUID = columnInfo[ix].newDefaultUUID;
/* Generate a UUID for the default, if one exists
* and there is no default id yet.
*/
if (columnInfo[ix].defaultInfo != null && defaultUUID == null) {
defaultUUID = dd.getUUIDFactory().createUUID();
}
/* Get a ColumnDescriptor reflecting the new default */
columnDescriptor = new ColumnDescriptor(columnInfo[ix].name, columnPosition, columnInfo[ix].dataType, columnInfo[ix].defaultValue, columnInfo[ix].defaultInfo, td, defaultUUID, columnInfo[ix].autoincStart, columnInfo[ix].autoincInc, columnInfo[ix].autoinc_create_or_modify_Start_Increment, columnInfo[ix].autoincCycle);
// Update the ColumnDescriptor with new default info
dd.dropColumnDescriptor(td.getUUID(), columnInfo[ix].name, tc);
dd.addDescriptor(columnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) {
// adding an autoincrement default-- calculate the maximum value
// of the autoincrement column.
long maxValue = getColumnMax(td, columnInfo[ix].name, columnInfo[ix].autoincInc);
dd.setAutoincrementValue(tc, td.getUUID(), columnInfo[ix].name, maxValue, true);
} else if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_RESTART) {
dd.setAutoincrementValue(tc, td.getUUID(), columnInfo[ix].name, columnInfo[ix].autoincStart, false);
}
if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_RESTART) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
//
if (dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null)) {
Long currentValue = null;
// are just changing the increment. see DERBY-6579.
if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
currentValue = dd.peekAtIdentity(td.getSchemaName(), td.getName());
}
if (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE) {
if (columnInfo[ix].autoincCycle) {
// ALTER TABLE ALTER COLUMN $columnName SET CYCLE
if (currentValue == null) {
//
// If the current value is NULL, then the sequence generator
// is exhausted and it must have been a NO CYCLE generator,
// which we are changing to CYCLE.
// According to the 2016 SQL Standard, section 4.27.2
// (Operations involving sequence generators),
// the next value of the sequence generator should be the minimum value
// (for an ascending sequence generator) or the maximum value
// (for a descending sequence generator). See DERBY-6961.
// This logic will have to change in the future if we
// let users configure the maximum and minimum values of identity columns.
//
int topOrBottom = (columnInfo[ix].autoincInc > 0) ? RANGE_BOTTOM : RANGE_TOP;
currentValue = getRangeBound(columnInfo[ix].dataType, topOrBottom);
}
} else {
// ALTER TABLE ALTER COLUMN $columnName SET NO CYCLE
//
// If we are just about to issue the rollover value,
// set it to NULL in order to prevent cycling.
int topOrBottom = (columnInfo[ix].autoincInc > 0) ? RANGE_BOTTOM : RANGE_TOP;
Long rolloverValue = getRangeBound(columnInfo[ix].dataType, topOrBottom);
if ((currentValue != null) && (currentValue.equals(rolloverValue))) {
currentValue = null;
}
}
}
DropTableConstantAction.dropIdentitySequence(dd, td, activation);
// recreate the sequence
String sequenceName = TableDescriptor.makeSequenceName(td.getUUID());
CreateSequenceConstantAction csca = CreateTableConstantAction.makeCSCA(columnInfo[ix], sequenceName);
csca.executeConstantAction(activation);
// reset the current value of the sequence generator as necessary
if ((columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_INCREMENT) || (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_DEFAULT_CYCLE)) {
SequenceDescriptor sequence = dd.getSequenceDescriptor(dd.getSystemSchemaDescriptor(), sequenceName);
RowLocation[] rowLocation = new RowLocation[1];
SequenceDescriptor[] sequenceDescriptor = new SequenceDescriptor[1];
dd.computeSequenceRowLocation(tc, sequence.getUUID().toString(), rowLocation, sequenceDescriptor);
dd.updateCurrentSequenceValue(tc, rowLocation[0], true, null, currentValue);
}
}
}
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class AlterTableConstantAction method modifyIdentityState.
/**
* Change an identity from ALWAYS to BY DEFAULT (or vice versa)
*
* @param ix the index of the column specfication in the ALTER
* statement-- currently we allow only one.
* @exception StandardException, thrown on error.
*/
private void modifyIdentityState(int ix) throws StandardException {
ColumnDescriptor oldColumnDescriptor = td.getColumnDescriptor(columnInfo[ix].name);
int columnPosition = oldColumnDescriptor.getPosition();
boolean wasGeneratedAlways = oldColumnDescriptor.isAutoincAlways();
boolean willBeGeneratedAlways = (columnInfo[ix].action == ColumnInfo.MODIFY_COLUMN_GENERATED_ALWAYS);
// nothing to do if the identity column already behaves the right way
if (wasGeneratedAlways == willBeGeneratedAlways) {
return;
}
UUID defaultUUID = willBeGeneratedAlways ? null : dd.getUUIDFactory().createUUID();
/* Get a ColumnDescriptor reflecting the new default */
ColumnDescriptor newColumnDescriptor = new ColumnDescriptor(columnInfo[ix].name, columnPosition, oldColumnDescriptor.getType(), columnInfo[ix].defaultValue, columnInfo[ix].defaultInfo, td, defaultUUID, oldColumnDescriptor.getAutoincStart(), oldColumnDescriptor.getAutoincInc(), ColumnDefinitionNode.MODIFY_AUTOINCREMENT_ALWAYS_VS_DEFAULT, oldColumnDescriptor.getAutoincCycle());
// Update the ColumnDescriptor with new default info
dd.dropColumnDescriptor(td.getUUID(), columnInfo[ix].name, tc);
dd.addDescriptor(newColumnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
}
use of org.apache.derby.catalog.UUID in project derby by apache.
the class RenameConstantAction method executeConstantAction.
// INTERFACE METHODS
/**
* The guts of the Execution-time logic for RENAME TABLE/COLUMN/INDEX.
*
* @see ConstantAction#executeConstantAction
*
* @exception StandardException Thrown on failure
*/
public void executeConstantAction(Activation activation) throws StandardException {
TableDescriptor td;
UUID tableID;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
/*
** 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);
td = dd.getTableDescriptor(tableId);
if (td == null) {
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);
}
/*
** 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.
*/
if (sd == null) {
sd = getAndCheckSchemaDescriptor(dd, schemaId, "RENAME TABLE");
}
long heapId = td.getHeapConglomerateId();
/* need to lock table, beetle 4271
*/
lockTableForDDL(tc, heapId, true);
/* need to get td again, in case it's changed before lock acquired
*/
td = dd.getTableDescriptor(tableId);
if (td == null) {
throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);
}
switch(renamingWhat) {
case StatementType.RENAME_TABLE:
execGutsRenameTable(td, activation);
break;
case StatementType.RENAME_COLUMN:
execGutsRenameColumn(td, activation);
break;
case StatementType.RENAME_INDEX:
execGutsRenameIndex(td, activation);
break;
default:
if (SanityManager.DEBUG) {
SanityManager.THROWASSERT("Unexpected rename action in RenameConstantAction");
}
break;
}
}
Aggregations