use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class ModifyColumnNode method checkUserType.
/**
* Check the validity of a user type. Checks that
* 1. the column type is either varchar, ....
* 2. is the same type after the alter.
* 3. length is greater than the old length.
*
* @exception StandardException Thrown on error
*/
@Override
void checkUserType(TableDescriptor td) throws StandardException {
if (kind != K_MODIFY_COLUMN_TYPE) {
// nothing to do if user not changing length
return;
}
ColumnDescriptor cd = td.getColumnDescriptor(name);
if (cd == null) {
throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, name, td.getName());
}
DataTypeDescriptor oldType = cd.getType();
setNullability(oldType.isNullable());
// can't change types yet.
if (!(oldType.getTypeId().equals(getType().getTypeId()))) {
throw StandardException.newException(SQLState.LANG_MODIFY_COLUMN_CHANGE_TYPE, name);
}
// can only alter the length of varchar, bitvarying columns
String typeName = getType().getTypeName();
if (!(typeName.equals(TypeId.VARCHAR_NAME)) && !(typeName.equals(TypeId.VARBIT_NAME)) && !(typeName.equals(TypeId.BLOB_NAME)) && !(typeName.equals(TypeId.CLOB_NAME))) {
throw StandardException.newException(SQLState.LANG_MODIFY_COLUMN_INVALID_TYPE);
}
// cannot decrease the length of a column
if (getType().getMaximumWidth() < oldType.getMaximumWidth()) {
throw StandardException.newException(SQLState.LANG_MODIFY_COLUMN_INVALID_LENGTH, name);
}
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class ModifyColumnNode method validateAutoincrement.
/**
* check the validity of autoincrement values in the case that we are
* modifying an existing column (includes checking if autoincrement is set
* when making a column nullable)
*/
@Override
void validateAutoincrement(DataDictionary dd, TableDescriptor td, int tableType) throws StandardException {
ColumnDescriptor cd;
// only autoincrement columns can have their generation property changed
if ((kind == K_MODIFY_COLUMN_GENERATED_ALWAYS) || (kind == K_MODIFY_COLUMN_GENERATED_BY_DEFAULT)) {
cd = getLocalColumnDescriptor(name, td);
if (!cd.isAutoincrement()) {
throw StandardException.newException(SQLState.LANG_AI_CANNOT_ALTER_IDENTITYNESS, getColumnName());
}
if (kind == K_MODIFY_COLUMN_GENERATED_BY_DEFAULT) {
defaultInfo = createDefaultInfoOfAutoInc();
}
// nothing more to do here
return;
}
// a column that has an autoincrement default can't be made nullable
if (kind == K_MODIFY_COLUMN_CONSTRAINT) {
cd = getLocalColumnDescriptor(name, td);
if (cd.isAutoincrement()) {
throw StandardException.newException(SQLState.LANG_AI_CANNOT_NULL_AI, getColumnName());
}
}
if (autoincrementVerify) {
cd = getLocalColumnDescriptor(name, td);
if (!cd.isAutoincrement())
throw StandardException.newException(SQLState.LANG_INVALID_ALTER_TABLE_ATTRIBUTES, td.getQualifiedName(), name);
}
if (isAutoincrement == false)
return;
super.validateAutoincrement(dd, td, tableType);
if (getType().isNullable())
throw StandardException.newException(SQLState.LANG_AI_CANNOT_ADD_AI_TO_NULLABLE, getColumnName());
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class FKConstraintDefinitionNode method bind.
/**
* Bind this constraint definition. Figure out some
* information about the table we are binding against.
*
* @param dd DataDictionary
*
* @exception StandardException on error
*/
@Override
void bind(DDLStatementNode ddlNode, DataDictionary dd) throws StandardException {
super.bind(ddlNode, dd);
refTableSd = getSchemaDescriptor(refTableName.getSchemaName());
if (refTableSd.isSystemSchema()) {
throw StandardException.newException(SQLState.LANG_NO_FK_ON_SYSTEM_SCHEMA);
}
// check the referenced table, unless this is a self-referencing constraint
if (refTableName.equals(ddlNode.getObjectName()))
return;
// error when the referenced table does not exist
TableDescriptor td = getTableDescriptor(refTableName.getTableName(), refTableSd);
if (td == null)
throw StandardException.newException(SQLState.LANG_INVALID_FK_NO_REF_TAB, getConstraintMoniker(), refTableName.getTableName());
// Verify if REFERENCES_PRIV is granted to columns referenced
getCompilerContext().pushCurrentPrivType(getPrivType());
// Indicate that this statement has a dependency on the
// table which is referenced by this foreign key:
getCompilerContext().createDependency(td);
// If references clause doesn't have columnlist, get primary key info
if (refRcl.size() == 0 && (td.getPrimaryKey() != null)) {
// Get the primary key columns
int[] refCols = td.getPrimaryKey().getReferencedColumns();
for (int i = 0; i < refCols.length; i++) {
ColumnDescriptor cd = td.getColumnDescriptor(refCols[i]);
// Set tableDescriptor for this column descriptor. Needed for adding required table
// access permission. Column descriptors may not have this set already.
cd.setTableDescriptor(td);
if (isPrivilegeCollectionRequired())
getCompilerContext().addRequiredColumnPriv(cd);
}
} else {
for (ResultColumn rc : refRcl) {
ColumnDescriptor cd = td.getColumnDescriptor(rc.getName());
if (cd != null) {
// Set tableDescriptor for this column descriptor. Needed for adding required table
// access permission. Column descriptors may not have this set already.
cd.setTableDescriptor(td);
if (isPrivilegeCollectionRequired())
getCompilerContext().addRequiredColumnPriv(cd);
}
}
}
getCompilerContext().popCurrentPrivType();
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class ModifyColumnNode method getLocalColumnDescriptor.
private ColumnDescriptor getLocalColumnDescriptor(String name, TableDescriptor td) throws StandardException {
ColumnDescriptor cd;
// First verify that the column exists
cd = td.getColumnDescriptor(name);
if (cd == null) {
throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, name, td.getName());
}
return cd;
}
use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.
the class ModifyColumnNode method bindAndValidateDefault.
/**
* Check the validity of the default, if any, for this node.
*
* @param dd The DataDictionary.
* @param td The TableDescriptor.
*
* @exception StandardException Thrown on error
*/
@Override
void bindAndValidateDefault(DataDictionary dd, TableDescriptor td) throws StandardException {
ColumnDescriptor cd;
// First verify that the column exists
cd = td.getColumnDescriptor(name);
if (cd == null) {
throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, name, td.getName());
}
// Get the UUID for the old default
DefaultDescriptor defaultDescriptor = cd.getDefaultDescriptor(dd);
oldDefaultUUID = (defaultDescriptor == null) ? null : defaultDescriptor.getUUID();
// Remember the column position
columnPosition = cd.getPosition();
// No other work to do if no user specified default
if (kind != K_MODIFY_COLUMN_DEFAULT) {
return;
}
// and does not lose the other aspecs.
if (keepCurrentDefault) {
defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo();
} else {
if (cd.hasGenerationClause() || cd.isAutoincrement()) {
throw StandardException.newException(SQLState.LANG_GEN_COL_DEFAULT, cd.getColumnName());
}
}
if (autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.MODIFY_AUTOINCREMENT_RESTART_VALUE) {
autoincrementIncrement = cd.getAutoincInc();
autoincrementCycle = cd.getAutoincCycle();
}
if (autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.MODIFY_AUTOINCREMENT_INC_VALUE) {
autoincrementStart = cd.getAutoincStart();
autoincrementCycle = cd.getAutoincCycle();
}
if (autoinc_create_or_modify_Start_Increment == ColumnDefinitionNode.MODIFY_AUTOINCREMENT_CYCLE_VALUE) {
autoincrementIncrement = cd.getAutoincInc();
autoincrementStart = cd.getAutoincStart();
}
/* Fill in the DataTypeServices from the DataDictionary */
type = cd.getType();
// Now validate the default
validateDefault(dd, td);
}
Aggregations