Search in sources :

Example 51 with ColumnDescriptor

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

the class UpdateNode method addGeneratedColumns.

/**
 * Add generated columns to the update list as necessary. We add
 * any column whose generation clause mentions columns already
 * in the update list. We fill in a list of all generated columns affected
 * by this update. We also fill in a list of all generated columns which we
 * added to the update list.
 */
private void addGeneratedColumns(TableDescriptor baseTable, ResultSetNode updateSet, ColumnDescriptorList affectedGeneratedColumns, ColumnDescriptorList addedGeneratedColumns) throws StandardException {
    ResultColumnList updateColumnList = updateSet.getResultColumns();
    ColumnDescriptorList generatedColumns = baseTable.getGeneratedColumns();
    HashSet<String> updatedColumns = new HashSet<String>();
    UUID tableID = baseTable.getObjectID();
    for (ResultColumn rc : updateColumnList) {
        updatedColumns.add(rc.getName());
    }
    for (ColumnDescriptor gc : generatedColumns) {
        DefaultInfo defaultInfo = gc.getDefaultInfo();
        String[] mentionedColumnNames = defaultInfo.getReferencedColumnNames();
        int mentionedColumnCount = mentionedColumnNames.length;
        // literal
        if (updatedColumns.contains(gc.getColumnName())) {
            affectedGeneratedColumns.add(tableID, gc);
        }
        // update
        for (String mcn : mentionedColumnNames) {
            if (updatedColumns.contains(mcn)) {
                // Yes, we are updating one of the columns mentioned in
                // this generation clause.
                affectedGeneratedColumns.add(tableID, gc);
                // add it.
                if (!updatedColumns.contains(gc.getColumnName())) {
                    addedGeneratedColumns.add(tableID, gc);
                    // we will fill in the real value later on in parseAndBindGenerationClauses();
                    ValueNode dummy = new UntypedNullConstantNode(getContextManager());
                    ResultColumn newResultColumn = new ResultColumn(gc.getType(), dummy, getContextManager());
                    newResultColumn.setColumnDescriptor(baseTable, gc);
                    newResultColumn.setName(gc.getColumnName());
                    updateColumnList.addResultColumn(newResultColumn);
                }
                break;
            }
        }
    // done looping through mentioned columns
    }
// done looping through generated columns
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) DefaultInfo(org.apache.derby.catalog.DefaultInfo) UUID(org.apache.derby.catalog.UUID) HashSet(java.util.HashSet)

Example 52 with ColumnDescriptor

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

the class ResultColumn method bindResultColumnByName.

/**
 * Bind this result column by its name and set the VirtualColumnId.
 * This is useful for update statements, and for INSERT statements
 * like "insert into t (a, b, c) values (1, 2, 3)" where the user
 * specified a column list.
 * An exception is thrown when a columnDescriptor cannot be found for a
 * given name.  (There is no column with that name.)
 *
 * NOTE: We must set the VirtualColumnId here because INSERT does not
 * construct the ResultColumnList in the usual way.
 *
 * @param tableDescriptor	The descriptor for the table being
 *				updated or inserted into
 * @param columnId		The ordinal position of the column
 *						in the table, starting at 1. (Used to
 *						set the VirtualColumnId.)
 *
 * @exception StandardException		Thrown on error
 */
void bindResultColumnByName(TableDescriptor tableDescriptor, int columnId) throws StandardException {
    ColumnDescriptor colDesc;
    colDesc = tableDescriptor.getColumnDescriptor(_derivedColumnName);
    if (colDesc == null) {
        String errorString;
        String schemaName;
        errorString = "";
        schemaName = tableDescriptor.getSchemaName();
        if (schemaName != null)
            errorString += schemaName + ".";
        errorString += tableDescriptor.getName();
        throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, _derivedColumnName, errorString);
    }
    setColumnDescriptor(tableDescriptor, colDesc);
    setVirtualColumnId(columnId);
    if (isPrivilegeCollectionRequired())
        getCompilerContext().addRequiredColumnPriv(colDesc);
}
Also used : ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 53 with ColumnDescriptor

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

the class ResultColumn method bindResultColumnByPosition.

/**
 * Bind this result column by ordinal position and set the VirtualColumnId.
 * This is useful for INSERT statements like "insert into t values (1, 2, 3)",
 * where the user did not specify a column list.
 * If a columnDescriptor is not found for a given position, then
 * the user has specified more values than the # of columns in
 * the table and an exception is thrown.
 *
 * NOTE: We must set the VirtualColumnId here because INSERT does not
 * construct the ResultColumnList in the usual way.
 *
 * @param tableDescriptor	The descriptor for the table being
 *				inserted into
 * @param columnId		The ordinal position of the column
 *						in the table, starting at 1.
 *
 * @exception StandardException		Thrown on error
 */
void bindResultColumnByPosition(TableDescriptor tableDescriptor, int columnId) throws StandardException {
    ColumnDescriptor colDesc;
    colDesc = tableDescriptor.getColumnDescriptor(columnId);
    if (colDesc == null) {
        String errorString;
        String schemaName;
        errorString = "";
        schemaName = tableDescriptor.getSchemaName();
        if (schemaName != null)
            errorString += schemaName + ".";
        errorString += tableDescriptor.getName();
        throw StandardException.newException(SQLState.LANG_TOO_MANY_RESULT_COLUMNS, errorString);
    }
    setColumnDescriptor(tableDescriptor, colDesc);
    setVirtualColumnId(columnId);
}
Also used : ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 54 with ColumnDescriptor

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

the class ResultColumnList method bindResultColumnsByName.

/**
 * Bind the result columns by their names.  This is useful for update
 * VTI statements, and for INSERT statements like "insert into new t() (a, b, c)
 * values (1, 2, 3)" where the user specified a column list.
 * Also, verify that the result column list does not contain any duplicates.
 * NOTE: We pass the ResultColumns position in the ResultColumnList so
 * that the VirtualColumnId gets set.
 *
 * @param fullRCL	The full RCL for the target table
 * @param statement			DMLStatementNode containing this list
 *
 * @exception StandardException		Thrown on error
 */
void bindResultColumnsByName(ResultColumnList fullRCL, FromVTI targetVTI, DMLStatementNode statement) throws StandardException {
    int size = size();
    HashSet<String> seenNames = new HashSet<String>(size + 2, 0.999f);
    for (int index = 0; index < size; index++) {
        ResultColumn matchRC;
        ResultColumn rc = elementAt(index);
        /* Verify that this column's name is unique within the list */
        String colName = rc.getName();
        boolean alreadySeen = !seenNames.add(colName);
        if (alreadySeen) {
            if (SanityManager.DEBUG) {
                SanityManager.ASSERT((statement instanceof UpdateNode) || (statement instanceof InsertNode), "statement is expected to be instanceof UpdateNode or InsertNode");
            }
            if (statement instanceof UpdateNode) {
                throw StandardException.newException(SQLState.LANG_DUPLICATE_COLUMN_NAME_UPDATE, colName);
            } else {
                throw StandardException.newException(SQLState.LANG_DUPLICATE_COLUMN_NAME_INSERT, colName);
            }
        }
        matchRC = fullRCL.getResultColumn(null, rc.getName());
        if (matchRC == null) {
            throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, rc.getName(), targetVTI.getMethodCall().getJavaClassName());
        }
        /* We have a match.  We need to create a dummy ColumnDescriptor
			 * since calling code expects one to get column info.
			 */
        ColumnDescriptor cd = new ColumnDescriptor(rc.getName(), matchRC.getVirtualColumnId(), matchRC.getType(), null, null, (TableDescriptor) null, null, 0, 0, false);
        rc.setColumnDescriptor(null, cd);
        rc.setVirtualColumnId(index + 1);
    }
}
Also used : ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) HashSet(java.util.HashSet)

Example 55 with ColumnDescriptor

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

the class ResultColumnList method buildEmptyIndexRow.

/**
 *	Build an empty index row for the given conglomerate.
 *
 *	@return	an empty row of the correct size and shape.
 * @exception StandardException		Thrown on error
 */
public ExecRow buildEmptyIndexRow(TableDescriptor td, ConglomerateDescriptor cd, StoreCostController scc, DataDictionary dd) throws StandardException {
    ResultColumn rc;
    if (SanityManager.DEBUG) {
        if (!cd.isIndex()) {
            SanityManager.THROWASSERT("ConglomerateDescriptor expected to be for index: " + cd);
        }
    }
    int[] baseCols = cd.getIndexDescriptor().baseColumnPositions();
    ExecRow row = getExecutionFactory().getValueRow(baseCols.length + 1);
    for (int i = 0; i < baseCols.length; i++) {
        ColumnDescriptor coldes = td.getColumnDescriptor(baseCols[i]);
        DataTypeDescriptor dataType = coldes.getType();
        // rc = getResultColumn(baseCols[i]);
        // rc = (ResultColumn) at(baseCols[i] - 1);
        // dataType = rc.getTypeServices();
        DataValueDescriptor dataValue = dataType.getNull();
        row.setColumn(i + 1, dataValue);
    }
    RowLocation rlTemplate = scc.newRowLocationTemplate();
    row.setColumn(baseCols.length + 1, rlTemplate);
    return row;
}
Also used : DataTypeDescriptor(org.apache.derby.iapi.types.DataTypeDescriptor) ResultColumnDescriptor(org.apache.derby.iapi.sql.ResultColumnDescriptor) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) RowLocation(org.apache.derby.iapi.types.RowLocation)

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