Search in sources :

Example 21 with ColumnDescriptorList

use of org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList 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 22 with ColumnDescriptorList

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

the class CreateTriggerNode method forbidActionsOnGenCols.

/*
     * Forbid references to generated columns in the actions of BEFORE triggers.
     * This is DERBY-3948, enforcing the following section of the SQL standard:
     * part 2, section 11.39 (<trigger definition>), syntax rule 12c:
     *
     * <blockquote>
     *    12) If BEFORE is specified, then:
     * :
     * c) The <triggered action> shall not contain a <field reference> that
     * references a field in the new transition variable corresponding to a
     * generated column of T. 
     * </blockquote>
     */
private void forbidActionsOnGenCols() throws StandardException {
    ColumnDescriptorList generatedColumns = triggerTableDescriptor.getGeneratedColumns();
    int genColCount = generatedColumns.size();
    if (genColCount == 0) {
        return;
    }
    CollectNodesVisitor<ColumnReference> visitor = new CollectNodesVisitor<ColumnReference>(ColumnReference.class);
    actionNode.accept(visitor);
    if (whenClause != null) {
        whenClause.accept(visitor);
    }
    for (ColumnReference cr : visitor.getList()) {
        String colRefName = cr.getColumnName();
        String tabRefName = cr.getTableName();
        for (int gc_idx = 0; gc_idx < genColCount; gc_idx++) {
            String genColName = generatedColumns.elementAt(gc_idx).getColumnName();
            if (genColName.equals(colRefName) && equals(newTableName, tabRefName)) {
                throw StandardException.newException(SQLState.LANG_GEN_COL_BEFORE_TRIG, genColName);
            }
        }
    }
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)

Example 23 with ColumnDescriptorList

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

the class CurrentOfNode method bindNonVTITables.

// 
// FromTable interface
// 
/**
 * Binding this FromTable means finding the prepared statement
 * for the cursor and creating the result columns (the columns
 * updatable on that cursor).
 *
 * We expect someone else to verify that the target table
 * of the positioned update or delete is the table under this cursor.
 *
 * @param dataDictionary	The DataDictionary to use for binding
 * @param fromListParam		FromList to use/append to.
 *
 * @return	ResultSetNode		Returns this.
 *
 * @exception StandardException		Thrown on error
 */
@Override
ResultSetNode bindNonVTITables(DataDictionary dataDictionary, FromList fromListParam) throws StandardException {
    // verify that the cursor exists
    preStmt = getCursorStatement();
    if (preStmt == null) {
        throw StandardException.newException(SQLState.LANG_CURSOR_NOT_FOUND, cursorName);
    }
    preStmt.rePrepare(getLanguageConnectionContext());
    // for checking that the right columns are updatable)
    if (preStmt.getUpdateMode() != CursorNode.UPDATE) {
        String printableString = (cursorName == null) ? "" : cursorName;
        throw StandardException.newException(SQLState.LANG_CURSOR_NOT_UPDATABLE, printableString);
    }
    ExecCursorTableReference refTab = preStmt.getTargetTable();
    String schemaName = refTab.getSchemaName();
    exposedTableName = makeTableName(null, refTab.getExposedName());
    baseTableName = makeTableName(schemaName, refTab.getBaseName());
    SchemaDescriptor tableSchema = getSchemaDescriptor(refTab.getSchemaName());
    /*
		** This will only happen when we are binding against a publication
		** dictionary w/o the schema we are interested in.
		*/
    if (tableSchema == null) {
        throw StandardException.newException(SQLState.LANG_SCHEMA_DOES_NOT_EXIST, refTab.getSchemaName());
    }
    /* Create dependency on target table, in case table not named in 
		 * positioned update/delete.  Make sure we find the table descriptor,
		 * we may fail to find it if we are binding a publication.
		 */
    TableDescriptor td = getTableDescriptor(refTab.getBaseName(), tableSchema);
    if (td == null) {
        throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND, refTab.getBaseName());
    }
    /*
		** Add all the result columns from the target table.
		** For now, all updatable cursors have all columns
		** from the target table.  In the future, we should
		** relax this so that the cursor may do a partial
		** read and then the current of should make sure that
		** it can go to the base table to get all of the 
		** columns needed by the referencing positioned
		** DML.  In the future, we'll probably need to get
		** the result columns from preparedStatement and
		** turn them into an RCL that we can run with.
		*/
    setResultColumns(new ResultColumnList(getContextManager()));
    ColumnDescriptorList cdl = td.getColumnDescriptorList();
    int cdlSize = cdl.size();
    for (int index = 0; index < cdlSize; index++) {
        /* Build a ResultColumn/BaseColumnNode pair for the column */
        ColumnDescriptor colDesc = cdl.elementAt(index);
        BaseColumnNode bcn = new BaseColumnNode(colDesc.getColumnName(), exposedTableName, colDesc.getType(), getContextManager());
        ResultColumn rc = new ResultColumn(colDesc, bcn, getContextManager());
        /* Build the ResultColumnList to return */
        getResultColumns().addResultColumn(rc);
    }
    /* Assign the tableNumber */
    if (// allow re-bind, in which case use old number
    tableNumber == -1)
        tableNumber = getCompilerContext().getNextTableNumber();
    return this;
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) ExecCursorTableReference(org.apache.derby.iapi.sql.execute.ExecCursorTableReference) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 24 with ColumnDescriptorList

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

the class DMLModStatementNode method generateFKInfo.

/**
 * Generate the FKInfo structures used during code generation.
 * For each constraint that isn't a check constraint, add another
 * one of these FKInfo structures and then package them up into
 * a single array.
 *
 * @param cdl				The constraint descriptor list
 * @param dd				The DataDictionary
 * @param td				The TableDescriptor
 * @param readColsBitSet 	columns read
 *
 * @exception StandardException		Thrown on failure
 */
private void generateFKInfo(ConstraintDescriptorList cdl, DataDictionary dd, TableDescriptor td, FormatableBitSet readColsBitSet) throws StandardException {
    ArrayList<FKInfo> fkList = new ArrayList<FKInfo>();
    int type;
    UUID[] uuids;
    long[] conglomNumbers;
    String[] fkNames;
    ConstraintDescriptorList fkcdl;
    ReferencedKeyConstraintDescriptor refcd;
    boolean[] isSelfReferencingFK;
    ConstraintDescriptorList activeList = dd.getActiveConstraintDescriptors(cdl);
    int[] rowMap = getRowMap(readColsBitSet, td);
    int[] raRules;
    boolean[] deferrable;
    UUID[] fkIds;
    ArrayList<String> refSchemaNames = new ArrayList<String>(1);
    ArrayList<String> refTableNames = new ArrayList<String>(1);
    ArrayList<Long> refIndexConglomNum = new ArrayList<Long>(1);
    ArrayList<Integer> refActions = new ArrayList<Integer>(1);
    ArrayList<ColumnDescriptorList> refColDescriptors = new ArrayList<ColumnDescriptorList>(1);
    ArrayList<int[]> fkColMap = new ArrayList<int[]>(1);
    int activeSize = activeList.size();
    for (int index = 0; index < activeSize; index++) {
        ConstraintDescriptor cd = activeList.elementAt(index);
        if (cd instanceof ForeignKeyConstraintDescriptor) {
            /*
				** We are saving information for checking the
				** primary/unique key that is referenced by this
				** foreign key, so type is FOREIGN KEY.
				*/
            type = FKInfo.FOREIGN_KEY;
            refcd = ((ForeignKeyConstraintDescriptor) cd).getReferencedConstraint();
            uuids = new UUID[1];
            deferrable = new boolean[1];
            fkIds = new UUID[1];
            conglomNumbers = new long[1];
            fkNames = new String[1];
            isSelfReferencingFK = new boolean[1];
            raRules = new int[1];
            fkSetupArrays(dd, (ForeignKeyConstraintDescriptor) cd, 0, uuids, conglomNumbers, fkNames, isSelfReferencingFK, raRules, deferrable, fkIds);
            // oops, get the right constraint name -- for error
            // handling we want the FK name, not refcd name
            fkNames[0] = cd.getConstraintName();
        } else if (cd instanceof ReferencedKeyConstraintDescriptor) {
            refcd = (ReferencedKeyConstraintDescriptor) cd;
            /*
				** We are saving information for checking the
				** foreign key(s) that is dependent on this referenced
				** key, so type is REFERENCED KEY.
				*/
            type = FKInfo.REFERENCED_KEY;
            fkcdl = dd.getActiveConstraintDescriptors(((ReferencedKeyConstraintDescriptor) cd).getForeignKeyConstraints(ConstraintDescriptor.ENABLED));
            int size = fkcdl.size();
            if (size == 0) {
                continue;
            }
            uuids = new UUID[size];
            deferrable = new boolean[size];
            fkIds = new UUID[size];
            fkNames = new String[size];
            conglomNumbers = new long[size];
            isSelfReferencingFK = new boolean[size];
            raRules = new int[size];
            TableDescriptor fktd;
            ColumnDescriptorList coldl;
            int[] refColumns;
            ColumnDescriptor cold;
            int[] colArray = remapReferencedColumns(cd, rowMap);
            for (int inner = 0; inner < size; inner++) {
                ForeignKeyConstraintDescriptor fkcd = (ForeignKeyConstraintDescriptor) fkcdl.elementAt(inner);
                fkSetupArrays(dd, fkcd, inner, uuids, conglomNumbers, fkNames, isSelfReferencingFK, raRules, deferrable, fkIds);
                if ((raRules[inner] == StatementType.RA_CASCADE) || (raRules[inner] == StatementType.RA_SETNULL)) {
                    // find  the referencing  table Name
                    fktd = fkcd.getTableDescriptor();
                    refSchemaNames.add(fktd.getSchemaName());
                    refTableNames.add(fktd.getName());
                    refActions.add(Integer.valueOf(raRules[inner]));
                    // find the referencing column name required for update null.
                    refColumns = fkcd.getReferencedColumns();
                    coldl = fktd.getColumnDescriptorList();
                    ColumnDescriptorList releventColDes = new ColumnDescriptorList();
                    for (int i = 0; i < refColumns.length; i++) {
                        cold = coldl.elementAt(refColumns[i] - 1);
                        releventColDes.add(cold);
                    }
                    refColDescriptors.add(releventColDes);
                    refIndexConglomNum.add(Long.valueOf(conglomNumbers[inner]));
                    fkColMap.add(colArray);
                }
            }
        } else {
            continue;
        }
        final TableDescriptor pktd = refcd.getTableDescriptor();
        final UUID pkIndexId = refcd.getIndexId();
        final ConglomerateDescriptor pkIndexConglom = pktd.getConglomerateDescriptor(pkIndexId);
        final TableDescriptor refTd = cd.getTableDescriptor();
        fkList.add(new FKInfo(// foreign key names
        fkNames, cd.getSchemaDescriptor().getSchemaName(), // table being modified
        refTd.getName(), // INSERT|UPDATE|DELETE
        statementType, // FOREIGN_KEY|REFERENCED_KEY
        type, // referenced backing index uuid
        pkIndexId, pkIndexConglom.getConglomerateNumber(), // referenced backing index conglom
        refcd.getUUID(), // referenced constraint is
        refcd.deferrable(), // fk backing index uuids
        uuids, // fk backing index congloms
        conglomNumbers, // is self ref array of bool
        isSelfReferencingFK, remapReferencedColumns(cd, rowMap), // columns referenced by key
        dd.getRowLocationTemplate(getLanguageConnectionContext(), refTd), // referential action rules
        raRules, // deferrable flags
        deferrable, // UUID of fks
        fkIds));
    }
    // Now convert the list into an array.
    if (!fkList.isEmpty()) {
        fkInfo = fkList.toArray(new FKInfo[fkList.size()]);
    }
    // Convert the ref action info lists to arrays.
    int size = refActions.size();
    if (size > 0) {
        fkTableNames = new String[size];
        fkSchemaNames = new String[size];
        fkRefActions = new int[size];
        fkColDescriptors = new ColumnDescriptorList[size];
        fkIndexConglomNumbers = new long[size];
        fkColArrays = new int[size][];
        for (int i = 0; i < size; i++) {
            fkTableNames[i] = refTableNames.get(i);
            fkSchemaNames[i] = refSchemaNames.get(i);
            fkRefActions[i] = (refActions.get(i)).intValue();
            fkColDescriptors[i] = refColDescriptors.get(i);
            fkIndexConglomNumbers[i] = (refIndexConglomNum.get(i)).longValue();
            fkColArrays[i] = (fkColMap.get(i));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) UUID(org.apache.derby.catalog.UUID) FKInfo(org.apache.derby.impl.sql.execute.FKInfo) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)

Example 25 with ColumnDescriptorList

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

the class FromBaseTable method addColsToList.

/**
 * Augment the RCL to include the columns in the FormatableBitSet.
 * If the column is already there, don't add it twice.
 * Column is added as a ResultColumn pointing to a
 * ColumnReference.
 *
 * @param inputRcl			The original list
 * @param colsWeWant		bit set of cols we want
 *
 * @return ResultColumnList the rcl
 *
 * @exception StandardException		Thrown on error
 */
ResultColumnList addColsToList(ResultColumnList inputRcl, FormatableBitSet colsWeWant) throws StandardException {
    ResultColumn resultColumn;
    TableName exposedName;
    /* Cache exposed name for this table.
		 * The exposed name becomes the qualifier for each column
		 * in the expanded list.
		 */
    exposedName = getExposedTableName();
    /* Add all of the columns in the table */
    ResultColumnList newRcl = new ResultColumnList((getContextManager()));
    ColumnDescriptorList cdl = tableDescriptor.getColumnDescriptorList();
    int cdlSize = cdl.size();
    for (int index = 0; index < cdlSize; index++) {
        /* Build a ResultColumn/BaseColumnNode pair for the column */
        ColumnDescriptor cd = cdl.elementAt(index);
        int position = cd.getPosition();
        if (!colsWeWant.get(position)) {
            continue;
        }
        if ((resultColumn = inputRcl.getResultColumn(position)) == null) {
            ColumnReference cr = new ColumnReference(cd.getColumnName(), exposedName, getContextManager());
            if ((getMergeTableID() != ColumnReference.MERGE_UNKNOWN)) {
                cr.setMergeTableID(getMergeTableID());
            }
            resultColumn = new ResultColumn(cd, cr, getContextManager());
        }
        /* Build the ResultColumnList to return */
        newRcl.addResultColumn(resultColumn);
    }
    return newRcl;
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Aggregations

ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)27 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)23 UUID (org.apache.derby.catalog.UUID)9 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)9 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)7 TransactionController (org.apache.derby.iapi.store.access.TransactionController)7 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)6 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)6 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)6 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)5 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)5 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)4 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)4 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)4 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 ForeignKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor)3 ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)3 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)3