Search in sources :

Example 41 with ColumnDescriptor

use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.

the class RenameConstantAction method execGutsRenameColumn.

// do necessary work for rename column at execute time.
private void execGutsRenameColumn(TableDescriptor td, Activation activation) throws StandardException {
    ColumnDescriptor columnDescriptor = null;
    int columnPosition = 0;
    ConstraintDescriptorList constraintDescriptorList;
    ConstraintDescriptor constraintDescriptor;
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();
    /* get the column descriptor for column to be renamed and
		 * using it's position in the table, set the referenced
		 * column map of the table indicating which column is being
		 * renamed. Dependency Manager uses this to find out the
		 * dependents on the column.
		 */
    columnDescriptor = td.getColumnDescriptor(oldObjectName);
    if (columnDescriptor.isAutoincrement())
        columnDescriptor.setAutoinc_create_or_modify_Start_Increment(ColumnDefinitionNode.CREATE_AUTOINCREMENT);
    columnPosition = columnDescriptor.getPosition();
    FormatableBitSet toRename = new FormatableBitSet(td.getColumnDescriptorList().size() + 1);
    toRename.set(columnPosition);
    td.setReferencedColumnMap(toRename);
    dm.invalidateFor(td, DependencyManager.RENAME, lcc);
    // look for foreign key dependency on the column.
    constraintDescriptorList = dd.getConstraintDescriptors(td);
    for (int index = 0; index < constraintDescriptorList.size(); index++) {
        constraintDescriptor = constraintDescriptorList.elementAt(index);
        int[] referencedColumns = constraintDescriptor.getReferencedColumns();
        int numRefCols = referencedColumns.length;
        for (int j = 0; j < numRefCols; j++) {
            if ((referencedColumns[j] == columnPosition) && (constraintDescriptor instanceof ReferencedKeyConstraintDescriptor))
                dm.invalidateFor(constraintDescriptor, DependencyManager.RENAME, lcc);
        }
    }
    // Drop the column
    dd.dropColumnDescriptor(td.getUUID(), oldObjectName, tc);
    columnDescriptor.setColumnName(newObjectName);
    dd.addDescriptor(columnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
    // Need to do following to reload the cache so that table
    // descriptor now has new column name
    td = dd.getTableDescriptor(td.getObjectID());
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Example 42 with ColumnDescriptor

use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.

the class MatchingClauseNode method buildFullColumnList.

/**
 * <p>
 * Build the full column list for a table.
 * </p>
 */
private ResultColumnList buildFullColumnList(TableDescriptor td) throws StandardException {
    ResultColumnList result = new ResultColumnList(getContextManager());
    ColumnDescriptorList cdl = td.getColumnDescriptorList();
    int cdlSize = cdl.size();
    for (int index = 0; index < cdlSize; index++) {
        ColumnDescriptor colDesc = cdl.elementAt(index);
        ColumnReference columnRef = new ColumnReference(colDesc.getColumnName(), null, getContextManager());
        ResultColumn resultColumn = new ResultColumn(columnRef, null, getContextManager());
        result.addResultColumn(resultColumn);
    }
    return result;
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 43 with ColumnDescriptor

use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.

the class MatchingClauseNode method buildThenColumnsForInsert.

/**
 * <p>
 * Construct the row in the temporary table which drives an INSERT action.
 * Unlike a DELETE, whose temporary row is just a list of copied columns, the
 * temporary row for INSERT may contain complex expressions which must
 * be code-generated later on.
 * </p>
 */
private void buildThenColumnsForInsert(FromList fullFromList, FromTable targetTable, ResultColumnList fullRow, ResultColumnList insertColumns, ResultColumnList insertValues) throws StandardException {
    // 
    // Don't add USAGE privilege on user-defined types just because we're
    // building the THEN columns.
    // 
    boolean wasSkippingTypePrivileges = getCompilerContext().skipTypePrivileges(true);
    TableDescriptor td = targetTable.getTableDescriptor();
    _thenColumns = fullRow.copyListAndObjects();
    // 
    for (int i = 0; i < _thenColumns.size(); i++) {
        ResultColumn origRC = _thenColumns.elementAt(i);
        String columnName = origRC.getName();
        ColumnDescriptor cd = td.getColumnDescriptor(columnName);
        boolean changed = false;
        // 
        if (!origRC.isAutoincrement() && (origRC.getExpression() instanceof VirtualColumnNode)) {
            origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
        }
        if (cd.hasGenerationClause()) {
            origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
            continue;
        }
        for (int ic = 0; ic < insertColumns.size(); ic++) {
            ResultColumn icRC = insertColumns.elementAt(ic);
            if (columnName.equals(icRC.getName())) {
                ResultColumn newRC = null;
                // replace DEFAULT for a generated or identity column
                ResultColumn valueRC = insertValues.elementAt(ic);
                if (valueRC.wasDefaultColumn() || (valueRC.getExpression() instanceof UntypedNullConstantNode)) {
                    if (!cd.isAutoincrement()) {
                        // 
                        // Eliminate column references under identity columns. They
                        // will mess up the code generation.
                        // 
                        ValueNode expr = origRC.getExpression();
                        if (expr instanceof ColumnReference) {
                            origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
                        }
                        continue;
                    }
                    newRC = makeAutoGenRC(targetTable, origRC, i + 1);
                } else {
                    newRC = valueRC.cloneMe();
                    newRC.setType(origRC.getTypeServices());
                }
                newRC.setVirtualColumnId(origRC.getVirtualColumnId());
                _thenColumns.setElementAt(newRC, i);
                changed = true;
                break;
            }
        }
        // plug in defaults if we haven't done so already
        if (!changed) {
            DefaultInfoImpl defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo();
            if ((defaultInfo != null) && !defaultInfo.isGeneratedColumn() && !cd.isAutoincrement()) {
                _thenColumns.setDefault(origRC, cd, defaultInfo);
                changed = true;
            }
        }
        // set the result column name correctly for buildThenColumnSignature()
        ResultColumn finalRC = _thenColumns.elementAt(i);
        finalRC.setName(cd.getColumnName());
    }
    // end loop through _thenColumns
    getCompilerContext().skipTypePrivileges(wasSkippingTypePrivileges);
}
Also used : ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) DefaultInfoImpl(org.apache.derby.catalog.types.DefaultInfoImpl)

Example 44 with ColumnDescriptor

use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.

the class MatchingClauseNode method buildThenColumnsForUpdate.

/**
 * <p>
 * Construct the row in the temporary table which drives an UPDATE action.
 * Unlike a DELETE, whose temporary row is just a list of copied columns, the
 * temporary row for UPDATE may contain complex expressions which must
 * be code-generated later on.
 * </p>
 */
private void buildThenColumnsForUpdate(FromList fullFromList, FromTable targetTable, ResultColumnList fullRow, ResultColumnList beforeRow, ResultColumnList afterValues) throws StandardException {
    TableDescriptor td = targetTable.getTableDescriptor();
    HashSet<String> changedColumns = getChangedColumnNames();
    HashSet<String> changedGeneratedColumns = getChangedGeneratedColumnNames(td, changedColumns);
    _thenColumns = fullRow.copyListAndObjects();
    // 
    for (int i = 0; i < _thenColumns.size(); i++) {
        ResultColumn origRC = _thenColumns.elementAt(i);
        boolean isAfterColumn = (i >= beforeRow.size());
        // skip the final RowLocation column of an UPDATE
        boolean isRowLocation = isRowLocation(origRC);
        ValueNode origExpr = origRC.getExpression();
        if (isRowLocation) {
            continue;
        }
        String columnName = origRC.getName();
        ColumnDescriptor cd = td.getColumnDescriptor(columnName);
        boolean changed = false;
        // 
        if (cd.isAutoincrement() && (origRC.getExpression() instanceof NumericConstantNode)) {
            DataValueDescriptor numericValue = ((NumericConstantNode) origRC.getExpression()).getValue();
            if (numericValue == null) {
                ResultColumn newRC = makeAutoGenRC(targetTable, origRC, i + 1);
                newRC.setVirtualColumnId(origRC.getVirtualColumnId());
                _thenColumns.setElementAt(newRC, i);
                continue;
            }
        }
        // 
        if (!origRC.isAutoincrement() && (origRC.getExpression() instanceof VirtualColumnNode)) {
            origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
        }
        // 
        if (cd.hasGenerationClause()) {
            if (isAfterColumn && changedGeneratedColumns.contains(columnName)) {
                // Set the expression to something that won't choke ResultColumnList.generateEvaluatedRow().
                // The value will be a Java null at execution time, which will cause the value
                // to be re-generated.
                origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
            } else {
                ColumnReference cr = new ColumnReference(columnName, targetTable.getTableName(), getContextManager());
                origRC.setExpression(cr);
                // remove the column descriptor in order to turn off hasGenerationClause()
                origRC.setColumnDescriptor(null, null);
            }
            continue;
        }
        if (isAfterColumn) {
            for (int ic = 0; ic < beforeRow.size(); ic++) {
                ResultColumn icRC = beforeRow.elementAt(ic);
                if (columnName.equals(icRC.getName())) {
                    ResultColumn newRC = null;
                    // replace DEFAULT for a generated or identity column
                    ResultColumn valueRC = afterValues.elementAt(ic);
                    if (valueRC.wasDefaultColumn() || (valueRC.getExpression() instanceof UntypedNullConstantNode)) {
                        if (!cd.isAutoincrement()) {
                            // 
                            // Eliminate column references under identity columns. They
                            // will mess up the code generation.
                            // 
                            ValueNode expr = origRC.getExpression();
                            if (expr instanceof ColumnReference) {
                                origRC.setExpression(new UntypedNullConstantNode(getContextManager()));
                            }
                            continue;
                        }
                        newRC = makeAutoGenRC(targetTable, origRC, i + 1);
                    } else {
                        newRC = valueRC.cloneMe();
                        newRC.setType(origRC.getTypeServices());
                    }
                    newRC.setVirtualColumnId(origRC.getVirtualColumnId());
                    _thenColumns.setElementAt(newRC, i);
                    changed = true;
                    break;
                }
            }
        }
        // plug in defaults if we haven't done so already
        if (!changed) {
            DefaultInfoImpl defaultInfo = (DefaultInfoImpl) cd.getDefaultInfo();
            if ((defaultInfo != null) && !defaultInfo.isGeneratedColumn() && !cd.isAutoincrement()) {
                _thenColumns.setDefault(origRC, cd, defaultInfo);
                changed = true;
            }
        }
        // set the result column name correctly for buildThenColumnSignature()
        ResultColumn finalRC = _thenColumns.elementAt(i);
        finalRC.setName(cd.getColumnName());
        // 
        // Turn off the autogenerated bit for identity columns so that
        // ResultColumnList.generateEvaluatedRow() doesn't try to compile
        // code to generate values for the before images in UPDATE rows.
        // This logic will probably need to be revisited as part of fixing derby-6414.
        // 
        finalRC.resetAutoincrementGenerated();
    }
// end loop through _thenColumns
}
Also used : ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) DefaultInfoImpl(org.apache.derby.catalog.types.DefaultInfoImpl) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor)

Example 45 with ColumnDescriptor

use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptor in project derby by apache.

the class FromVTI method createResultColumnsForTableFunction.

/**
 * Add result columns for a Derby-style Table Function
 */
private void createResultColumnsForTableFunction(TypeDescriptor td) throws StandardException {
    String[] columnNames = td.getRowColumnNames();
    TypeDescriptor[] types = td.getRowTypes();
    for (int i = 0; i < columnNames.length; i++) {
        String columnName = columnNames[i];
        DataTypeDescriptor dtd = DataTypeDescriptor.getType(types[i]);
        ResultColumn rc = getResultColumns().addColumn(exposedName, columnName, dtd);
        // 
        // Stuff a column descriptor into the ResultColumn. We do this so that
        // getColumnPosition() will return the column position within the
        // table function's shape. Later on, projection may remove columns
        // from the ResultColumnList. We don't want getColumnPosition() to say
        // that the column position is the index into the abbreviated ResultColumnList.
        // See DERBY-6040.
        // 
        ColumnDescriptor coldesc = new ColumnDescriptor(columnName, i + 1, dtd, (DataValueDescriptor) null, (DefaultInfo) null, (UUID) null, (UUID) null, 0L, 0L, 0L, false);
        rc.setColumnDescriptor(null, coldesc);
    }
}
Also used : TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

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