use of org.apache.derby.iapi.sql.dictionary.SequenceDescriptor in project derby by apache.
the class DataDictionaryImpl method createIdentitySequence.
/**
* Create a sequence generator for an identity column on upgrade to 10.11.
*/
private void createIdentitySequence(TableDescriptor td, // the identity column
ColumnDescriptor cd, TransactionController tc) throws StandardException {
DataTypeDescriptor dtd = cd.getType();
long[] bounds = dtd.getNumericBounds();
long currentValue = cd.getAutoincValue();
long initialValue = cd.getAutoincStart();
long minValue = bounds[DataTypeDescriptor.MIN_VALUE_IDX];
long maxValue = bounds[DataTypeDescriptor.MAX_VALUE_IDX];
long stepValue = cd.getAutoincInc();
SchemaDescriptor sd = getSystemSchemaDescriptor();
SequenceDescriptor seqDef = getDataDescriptorGenerator().newSequenceDescriptor(sd, getUUIDFactory().createUUID(), TableDescriptor.makeSequenceName(td.getUUID()), dtd, currentValue, initialValue, minValue, maxValue, stepValue, // whether the sequence can wrap-around
false);
addDescriptor(seqDef, // parent
null, DataDictionary.SYSSEQUENCES_CATALOG_NUM, // duplicatesAllowed
false, tc);
}
use of org.apache.derby.iapi.sql.dictionary.SequenceDescriptor in project derby by apache.
the class SYSSEQUENCESRowFactory method makeRow.
/**
* Make a SYSSEQUENCES row
*
* @param td a sequence descriptor
* @param parent unused
* @return Row suitable for inserting into SYSSEQUENCES.
* @throws org.apache.derby.shared.common.error.StandardException
* thrown on failure
*/
public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException {
ExecRow row;
String oidString = null;
String sequenceName = null;
String schemaIdString = null;
TypeDescriptor typeDesc = null;
Long currentValue = null;
long startValue = 0;
long minimumValue = 0;
long maximumValue = 0;
long increment = 0;
boolean canCycle = false;
if (td != null) {
SequenceDescriptor sd = (SequenceDescriptor) td;
UUID oid = sd.getUUID();
oidString = oid.toString();
sequenceName = sd.getSequenceName();
UUID schemaId = sd.getSchemaId();
schemaIdString = schemaId.toString();
typeDesc = sd.getDataType().getCatalogType();
currentValue = sd.getCurrentValue();
startValue = sd.getStartValue();
minimumValue = sd.getMinimumValue();
maximumValue = sd.getMaximumValue();
increment = sd.getIncrement();
canCycle = sd.canCycle();
}
/* Build the row to insert */
row = getExecutionFactory().getValueRow(SYSSEQUENCES_COLUMN_COUNT);
/* 1st column is UUID */
row.setColumn(SYSSEQUENCES_SEQUENCEID, new SQLChar(oidString));
/* 2nd column is SEQUENCENAME */
row.setColumn(SYSSEQUENCES_SEQUENCENAME, new SQLVarchar(sequenceName));
/* 3nd column is SCHEMAID */
row.setColumn(SYSSEQUENCES_SCHEMAID, new SQLChar(schemaIdString));
/* 4th column is SEQUENCEDATATYPE */
row.setColumn(SYSSEQUENCES_SEQUENCEDATATYPE, new UserType(typeDesc));
/* 5th column is CURRENTVALUE */
SQLLongint curVal;
if (currentValue == null) {
curVal = new SQLLongint();
} else {
curVal = new SQLLongint(currentValue.longValue());
}
row.setColumn(SYSSEQUENCES_CURRENT_VALUE, curVal);
/* 6th column is STARTVALUE */
row.setColumn(SYSSEQUENCES_START_VALUE, new SQLLongint(startValue));
/* 7th column is MINIMUMVALUE */
row.setColumn(SYSSEQUENCES_MINIMUM_VALUE, new SQLLongint(minimumValue));
/* 8th column is MAXIMUMVALUE */
row.setColumn(SYSSEQUENCES_MAXIMUM_VALUE, new SQLLongint(maximumValue));
/* 9th column is INCREMENT */
row.setColumn(SYSSEQUENCES_INCREMENT, new SQLLongint(increment));
/* 10th column is CYCLEOPTION */
row.setColumn(SYSSEQUENCES_CYCLE_OPTION, new SQLChar(canCycle ? "Y" : "N"));
return row;
}
use of org.apache.derby.iapi.sql.dictionary.SequenceDescriptor in project derby by apache.
the class DropSequenceNode method bindStatement.
/**
* Bind this DropSequenceNode.
*
* @throws StandardException Thrown on error
*/
@Override
public void bindStatement() throws StandardException {
DataDictionary dataDictionary = getDataDictionary();
String sequenceName = getRelativeName();
SequenceDescriptor seqDesc = null;
SchemaDescriptor sd = getSchemaDescriptor();
if (sd.getUUID() != null) {
seqDesc = dataDictionary.getSequenceDescriptor(sd, sequenceName);
}
if (seqDesc == null) {
throw StandardException.newException(SQLState.LANG_OBJECT_DOES_NOT_EXIST, statementToString(), sequenceName);
}
// Statement is dependent on the SequenceDescriptor
getCompilerContext().createDependency(seqDesc);
}
use of org.apache.derby.iapi.sql.dictionary.SequenceDescriptor in project derby by apache.
the class CreateSequenceConstantAction method executeConstantAction.
// INTERFACE METHODS
/**
* This is the guts of the Execution-time logic for CREATE SEQUENCE.
*
* @throws org.apache.derby.shared.common.error.StandardException
* Thrown on failure
* @see org.apache.derby.iapi.sql.execute.ConstantAction#executeConstantAction
*/
public void executeConstantAction(Activation activation) throws StandardException {
SchemaDescriptor schemaDescriptor;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
TransactionController tc = lcc.getTransactionExecute();
DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
dd.startWriting(lcc);
schemaDescriptor = DDLConstantAction.getSchemaDescriptorForCreate(dd, activation, _schemaName);
//
// Check if this sequence already exists. If it does, throw.
//
SequenceDescriptor seqDef = dd.getSequenceDescriptor(schemaDescriptor, _sequenceName);
if (seqDef != null) {
throw StandardException.newException(SQLState.LANG_OBJECT_ALREADY_EXISTS, seqDef.getDescriptorType(), _sequenceName);
}
seqDef = ddg.newSequenceDescriptor(schemaDescriptor, dd.getUUIDFactory().createUUID(), _sequenceName, _dataType, // current value
_initialValue, _initialValue, _minValue, _maxValue, _stepValue, // whether the sequence can wrap-around
_cycle);
dd.addDescriptor(seqDef, // parent
null, DataDictionary.SYSSEQUENCES_CATALOG_NUM, // duplicatesAllowed
false, tc);
}
use of org.apache.derby.iapi.sql.dictionary.SequenceDescriptor 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);
}
}
}
}
Aggregations