use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class AlterTableConstantAction method validateNotNullConstraint.
/**
* Make sure that the columns are non null
* If any column is nullable, check that the data is null.
*
* @param columnNames names of columns to be checked
* @param nullCols true if corresponding column is nullable
* @param numRows number of rows in the table
* @param lcc language context
* @param errorMsg error message to use for exception
*
* @return true if any nullable columns found (nullable columns must have
* all non null data or exception is thrown
* @exception StandardException on error
*/
private boolean validateNotNullConstraint(String[] columnNames, boolean[] nullCols, int numRows, LanguageConnectionContext lcc, String errorMsg) throws StandardException {
boolean foundNullable = false;
StringBuilder constraintText = new StringBuilder();
/*
* Check for nullable columns and create a constraint string which can
* be used in validateConstraint to check whether any of the
* data is null.
*/
for (int colCtr = 0; colCtr < columnNames.length; colCtr++) {
ColumnDescriptor cd = td.getColumnDescriptor(columnNames[colCtr]);
if (cd == null) {
throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, columnNames[colCtr], td.getName());
}
if (cd.getType().isNullable()) {
if (numRows > 0) {
// already found a nullable column so add "AND"
if (foundNullable)
constraintText.append(" AND ");
// Delimiting the column name is important in case the
// column name uses lower case characters, spaces, or
// other unusual characters.
constraintText.append(IdUtil.normalToDelimited(columnNames[colCtr]));
constraintText.append(" IS NOT NULL ");
}
foundNullable = true;
nullCols[colCtr] = true;
}
}
/* if the table has nullable columns and isn't empty
* we need to validate the data
*/
if (foundNullable && numRows > 0) {
if (!ConstraintConstantAction.validateConstraint((String) null, constraintText.toString(), null, /* not used for not nullable constraints yet */
td, lcc, false, false)) {
if (errorMsg.equals(SQLState.LANG_NULL_DATA_IN_PRIMARY_KEY_OR_UNIQUE_CONSTRAINT)) {
// soft upgrade mode
throw StandardException.newException(SQLState.LANG_NULL_DATA_IN_PRIMARY_KEY_OR_UNIQUE_CONSTRAINT, td.getQualifiedName());
} else if (errorMsg.equals(SQLState.LANG_NULL_DATA_IN_PRIMARY_KEY)) {
// alter table add primary key
throw StandardException.newException(SQLState.LANG_NULL_DATA_IN_PRIMARY_KEY, td.getQualifiedName());
} else {
// alter table modify column not null
throw StandardException.newException(SQLState.LANG_NULL_DATA_IN_NON_NULL_COLUMN, td.getQualifiedName(), columnNames[0]);
}
}
}
return foundNullable;
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor 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.iapi.sql.dictionary.ColumnDescriptor 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);
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor 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.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class DMLWriteGeneratedColumnsResultSet method verifyAutoGeneratedColumnsNames.
/**
* Verify that the auto-generated columns list (by name) has valid
* column names for the table. If all the column names are valid,
* convert column names array to corresponding column positions array
* Save that column positions array in activation. We do this to
* simplify the rest of the logic(it only has to deal with column
* positions here after).
*
* @exception StandardException Thrown on error if invalid column
* name in the list.
*/
private void verifyAutoGeneratedColumnsNames(String[] columnNames, UUID targetUUID) throws StandardException {
int size = columnNames.length;
int[] columnPositions = new int[size];
TableDescriptor tabDesc = lcc.getDataDictionary().getTableDescriptor(targetUUID);
ColumnDescriptor cd;
for (int i = 0; i < size; i++) {
if (columnNames[i] == null) {
throw StandardException.newException(SQLState.LANG_INVALID_AUTOGEN_COLUMN_NAME, columnNames[i], tabDesc.getName());
}
cd = tabDesc.getColumnDescriptor(columnNames[i]);
if (!verifyAutoGenColumn(cd)) {
throw StandardException.newException(SQLState.LANG_INVALID_AUTOGEN_COLUMN_NAME, columnNames[i], tabDesc.getName());
}
columnPositions[i] = cd.getPosition();
}
activation.setAutoGeneratedKeysResultsetInfo(columnPositions, null);
}
Aggregations