Search in sources :

Example 6 with DefaultInfoImpl

use of org.apache.derby.catalog.types.DefaultInfoImpl 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 7 with DefaultInfoImpl

use of org.apache.derby.catalog.types.DefaultInfoImpl 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 8 with DefaultInfoImpl

use of org.apache.derby.catalog.types.DefaultInfoImpl in project derby by apache.

the class SYSCOLUMNSRowFactory method buildDescriptor.

// /////////////////////////////////////////////////////////////////////////
// 
// ABSTRACT METHODS TO BE IMPLEMENTED BY CHILDREN OF CatalogRowFactory
// 
// /////////////////////////////////////////////////////////////////////////
/**
 * Make a ColumnDescriptor out of a SYSCOLUMNS row
 *
 * @param row 					a SYSCOLUMNS row
 * @param parentTupleDescriptor	The UniqueTupleDescriptor for the object that is tied
 *								to this column
 * @param dd 					dataDictionary
 *
 * @return	a column descriptor equivalent to a SYSCOLUMNS row
 *
 * @exception   StandardException thrown on failure
 */
public TupleDescriptor buildDescriptor(ExecRow row, TupleDescriptor parentTupleDescriptor, DataDictionary dd) throws StandardException {
    if (SanityManager.DEBUG) {
        int expectedCols = dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_14, null) ? SYSCOLUMNS_COLUMN_COUNT : (SYSCOLUMNS_COLUMN_COUNT - 1);
        SanityManager.ASSERT(row.nColumns() == expectedCols, "Wrong number of columns for a SYSCOLUMNS row");
    }
    int columnNumber;
    String columnName;
    String defaultID;
    DefaultInfoImpl defaultInfo = null;
    ColumnDescriptor colDesc;
    DataValueDescriptor defaultValue = null;
    UUID defaultUUID = null;
    UUID uuid = null;
    UUIDFactory uuidFactory = getUUIDFactory();
    long autoincStart, autoincInc, autoincValue;
    boolean autoincCycle = false;
    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    /*
		** We're going to be getting the UUID for this sucka
		** so make sure it is a UniqueTupleDescriptor.
		*/
    if (parentTupleDescriptor != null) {
        if (SanityManager.DEBUG) {
            if (!(parentTupleDescriptor instanceof UniqueTupleDescriptor)) {
                SanityManager.THROWASSERT(parentTupleDescriptor.getClass().getName() + " not instanceof UniqueTupleDescriptor");
            }
        }
        uuid = ((UniqueTupleDescriptor) parentTupleDescriptor).getUUID();
    } else {
        /* 1st column is REFERENCEID (char(36)) */
        uuid = uuidFactory.recreateUUID(row.getColumn(SYSCOLUMNS_REFERENCEID).getString());
    }
    /* NOTE: We get columns 5 and 6 next in order to work around 
		 * a 1.3.0 HotSpot bug.  (#4361550)
		 */
    // 5th column is COLUMNDEFAULT (serialiazable)
    Object object = row.getColumn(SYSCOLUMNS_COLUMNDEFAULT).getObject();
    if (object instanceof DataValueDescriptor) {
        defaultValue = (DataValueDescriptor) object;
    } else if (object instanceof DefaultInfoImpl) {
        defaultInfo = (DefaultInfoImpl) object;
        defaultValue = defaultInfo.getDefaultValue();
    }
    /* 6th column is DEFAULTID (char(36)) */
    defaultID = row.getColumn(SYSCOLUMNS_COLUMNDEFAULTID).getString();
    if (defaultID != null) {
        defaultUUID = uuidFactory.recreateUUID(defaultID);
    }
    /* 2nd column is COLUMNNAME (varchar(128)) */
    columnName = row.getColumn(SYSCOLUMNS_COLUMNNAME).getString();
    /* 3rd column is COLUMNNUMBER (int) */
    columnNumber = row.getColumn(SYSCOLUMNS_COLUMNNUMBER).getInt();
    /* 4th column is COLUMNDATATYPE */
    /*
		** What is stored in the column is a TypeDescriptorImpl, which
		** points to a BaseTypeIdImpl.  These are simple types that are
		** intended to be movable to the client, so they don't have
		** the entire implementation.  We need to wrap them in DataTypeServices
		** and TypeId objects that contain the full implementations for
		** language processing.
		*/
    TypeDescriptor catalogType = (TypeDescriptor) row.getColumn(SYSCOLUMNS_COLUMNDATATYPE).getObject();
    DataTypeDescriptor dataTypeServices = DataTypeDescriptor.getType(catalogType);
    /* 7th column is AUTOINCREMENTVALUE (long) */
    autoincValue = row.getColumn(SYSCOLUMNS_AUTOINCREMENTVALUE).getLong();
    /* 8th column is AUTOINCREMENTSTART (long) */
    autoincStart = row.getColumn(SYSCOLUMNS_AUTOINCREMENTSTART).getLong();
    /* 9th column is AUTOINCREMENTINC (long) */
    autoincInc = row.getColumn(SYSCOLUMNS_AUTOINCREMENTINC).getLong();
    if (row.nColumns() >= 10) {
        DataValueDescriptor col = row.getColumn(SYSCOLUMNS_AUTOINCREMENTINCCYCLE);
        autoincCycle = col.getBoolean();
    }
    DataValueDescriptor col = row.getColumn(SYSCOLUMNS_AUTOINCREMENTSTART);
    autoincStart = col.getLong();
    col = row.getColumn(SYSCOLUMNS_AUTOINCREMENTINC);
    autoincInc = col.getLong();
    // Hard upgraded tables <=10.13 come with a false autoincCyle before they are first
    // explicitly set with cycle or no cycle command.
    colDesc = new ColumnDescriptor(columnName, columnNumber, dataTypeServices, defaultValue, defaultInfo, uuid, defaultUUID, autoincStart, autoincInc, autoincValue, autoincCycle);
    return colDesc;
}
Also used : UUIDFactory(org.apache.derby.iapi.services.uuid.UUIDFactory) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DefaultInfoImpl(org.apache.derby.catalog.types.DefaultInfoImpl) DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) TypeDescriptor(org.apache.derby.catalog.TypeDescriptor) UUID(org.apache.derby.catalog.UUID) UniqueTupleDescriptor(org.apache.derby.iapi.sql.dictionary.UniqueTupleDescriptor)

Aggregations

DefaultInfoImpl (org.apache.derby.catalog.types.DefaultInfoImpl)8 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)6 ResultColumnDescriptor (org.apache.derby.iapi.sql.ResultColumnDescriptor)4 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)3 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)2 ProviderList (org.apache.derby.iapi.sql.depend.ProviderList)2 DefaultDescriptor (org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)2 DataTypeDescriptor (org.apache.derby.iapi.types.DataTypeDescriptor)2 TypeId (org.apache.derby.iapi.types.TypeId)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TypeDescriptor (org.apache.derby.catalog.TypeDescriptor)1 UUID (org.apache.derby.catalog.UUID)1 UUIDFactory (org.apache.derby.iapi.services.uuid.UUIDFactory)1 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)1 UniqueTupleDescriptor (org.apache.derby.iapi.sql.dictionary.UniqueTupleDescriptor)1 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)1