Search in sources :

Example 46 with ColumnDescriptor

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);
    }
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 47 with ColumnDescriptor

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());
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 48 with ColumnDescriptor

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();
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 49 with ColumnDescriptor

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;
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 50 with ColumnDescriptor

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);
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)

Aggregations

ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)79 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)23 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)20 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)19 UUID (org.apache.derby.catalog.UUID)15 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)14 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)10 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)8 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 TransactionController (org.apache.derby.iapi.store.access.TransactionController)8 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)8 SQLLongint (org.apache.derby.iapi.types.SQLLongint)8 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)7 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)7 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)7 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)7 RowLocation (org.apache.derby.iapi.types.RowLocation)7 DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)6 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)6 DefaultDescriptor (org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)6