Search in sources :

Example 6 with ColumnDescriptorList

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

the class ResultColumnList method expandToAll.

/**
 * Expand this ResultColumnList by adding all columns from the given
 * table that are not in this list.  The result is sorted by column
 * position.
 *
 * @param td	The TableDescriptor for the table in question
 * @param tableName	The name of the table as given in the query
 *
 * @return	A new ResultColumnList expanded to include all columns in
 *			the given table.
 *
 * @exception StandardException		Thrown on error
 */
ResultColumnList expandToAll(TableDescriptor td, TableName tableName) throws StandardException {
    ResultColumn rc;
    ColumnDescriptor cd;
    ResultColumnList retval;
    ResultColumn[] originalRCS;
    int posn;
    /* Get a new ResultColumnList */
    retval = new ResultColumnList(getContextManager());
    /*
		** Form a sorted array of the ResultColumns
		*/
    originalRCS = getSortedByPosition();
    posn = 0;
    /* Iterate through the ColumnDescriptors for the given table */
    ColumnDescriptorList cdl = td.getColumnDescriptorList();
    int cdlSize = cdl.size();
    for (int index = 0; index < cdlSize; index++) {
        cd = cdl.elementAt(index);
        if ((posn < originalRCS.length) && (cd.getPosition() == originalRCS[posn].getColumnPosition())) {
            rc = originalRCS[posn];
            posn++;
        } else {
            /* Build a ResultColumn/ColumnReference pair for the column */
            rc = makeColumnReferenceFromName(tableName, cd.getColumnName());
            /* Bind the new ResultColumn */
            rc.bindResultColumnByPosition(td, cd.getPosition());
        }
        /* Add the ResultColumn to the list */
        retval.addResultColumn(rc);
    }
    if (SanityManager.DEBUG)
        SanityManager.ASSERT(posn == originalRCS.length, "ResultColumns in original list not added to expanded ResultColumnList");
    return retval;
}
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 7 with ColumnDescriptorList

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

the class RenameNode method renameColumnBind.

// do any checking needs to be done at bind time for rename column
private void renameColumnBind(DataDictionary dd) throws StandardException {
    ColumnDescriptor columnDescriptor = td.getColumnDescriptor(oldObjectName);
    /* Verify that old column name does exist in the table */
    if (columnDescriptor == null)
        throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, oldObjectName, getFullName());
    /* Verify that new column name does not exist in the table */
    ColumnDescriptor cd = td.getColumnDescriptor(newObjectName);
    if (cd != null)
        throw descriptorExistsException(cd, td);
    // 
    // You cannot rename a column which is referenced by the generation
    // clause of a generated column.
    // 
    ColumnDescriptorList generatedColumns = td.getGeneratedColumns();
    int generatedColumnCount = generatedColumns.size();
    for (int i = 0; i < generatedColumnCount; i++) {
        ColumnDescriptor gc = generatedColumns.elementAt(i);
        String[] referencedColumns = gc.getDefaultInfo().getReferencedColumnNames();
        int refColCount = referencedColumns.length;
        for (int j = 0; j < refColCount; j++) {
            String refName = referencedColumns[j];
            if (oldObjectName.equals(refName)) {
                throw StandardException.newException(SQLState.LANG_GEN_COL_BAD_RENAME, oldObjectName, gc.getColumnName());
            }
        }
    }
    /* Verify that there are no check constraints using the column being renamed */
    ConstraintDescriptorList constraintDescriptorList = dd.getConstraintDescriptors(td);
    int size = constraintDescriptorList == null ? 0 : constraintDescriptorList.size();
    ConstraintDescriptor constraintDescriptor;
    ColumnDescriptorList checkConstraintCDL;
    int checkConstraintCDLSize;
    // go through all the constraints defined on the table
    for (int index = 0; index < size; index++) {
        constraintDescriptor = constraintDescriptorList.elementAt(index);
        // renamed is not used in it's sql
        if (constraintDescriptor.getConstraintType() == DataDictionary.CHECK_CONSTRAINT) {
            checkConstraintCDL = constraintDescriptor.getColumnDescriptors();
            checkConstraintCDLSize = checkConstraintCDL.size();
            for (int index2 = 0; index2 < checkConstraintCDLSize; index2++) if (checkConstraintCDL.elementAt(index2) == columnDescriptor)
                throw StandardException.newException(SQLState.LANG_RENAME_COLUMN_WILL_BREAK_CHECK_CONSTRAINT, oldObjectName, constraintDescriptor.getConstraintName());
        }
    }
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)

Example 8 with ColumnDescriptorList

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

the class FromBaseTable method genResultColList.

/**
 * Build a ResultColumnList based on all of the columns in this FromBaseTable.
 * NOTE - Since the ResultColumnList generated is for the FromBaseTable,
 * ResultColumn.expression will be a BaseColumnNode.
 *
 * @return ResultColumnList representing all referenced columns
 *
 * @exception StandardException		Thrown on error
 */
ResultColumnList genResultColList() throws StandardException {
    ResultColumn resultColumn;
    ValueNode valueNode;
    /* Cache exposed name for this table.
		 * The exposed name becomes the qualifier for each column
		 * in the expanded list.
		 */
    TableName exposedName = getExposedTableName();
    /* Add all of the columns in the table */
    ResultColumnList rcList = 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 colDesc = cdl.elementAt(index);
        // A ColumnDescriptor instantiated through SYSCOLUMNSRowFactory only has
        // the uuid set on it and no table descriptor set on it. Since we know here
        // that this columnDescriptor is tied to tableDescriptor, set it so using
        // setTableDescriptor method. ColumnDescriptor's table descriptor is used
        // to get ResultSetMetaData.getTableName & ResultSetMetaData.getSchemaName
        colDesc.setTableDescriptor(tableDescriptor);
        valueNode = new BaseColumnNode(colDesc.getColumnName(), exposedName, colDesc.getType(), getContextManager());
        resultColumn = new ResultColumn(colDesc, valueNode, getContextManager());
        /* Build the ResultColumnList to return */
        rcList.addResultColumn(resultColumn);
    }
    // add a row location column as necessary
    if (rowLocationColumnName != null) {
        CurrentRowLocationNode rowLocationNode = new CurrentRowLocationNode(getContextManager());
        ResultColumn rowLocationColumn = new ResultColumn(rowLocationColumnName, rowLocationNode, getContextManager());
        rowLocationColumn.markGenerated();
        rowLocationNode.bindExpression(null, null, null);
        rowLocationColumn.bindResultColumnToExpression();
        rcList.addResultColumn(rowLocationColumn);
    }
    return rcList;
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 9 with ColumnDescriptorList

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

the class FromVTI method genResultColList.

private ResultColumnList genResultColList(TableDescriptor td) throws StandardException {
    /* Add all of the columns in the table */
    ResultColumnList rcList = 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);
        ValueNode valueNode = new BaseColumnNode(colDesc.getColumnName(), exposedName, colDesc.getType(), getContextManager());
        ResultColumn resultColumn = new ResultColumn(colDesc, valueNode, getContextManager());
        /* Build the ResultColumnList to return */
        rcList.addResultColumn(resultColumn);
    }
    return rcList;
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)

Example 10 with ColumnDescriptorList

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

the class TableElementList method findIllegalGenerationReferences.

/**
 * Complain if a generation clause references other generated columns. This
 * is required by the SQL Standard, part 2, section 4.14.8.
 *
 * @param fromList		The FromList in question.
 * @param baseTable  Table descriptor if this is an ALTER TABLE statement.
 * @exception StandardException		Thrown on error
 */
void findIllegalGenerationReferences(FromList fromList, TableDescriptor baseTable) throws StandardException {
    ArrayList<ColumnDefinitionNode> generatedColumns = new ArrayList<ColumnDefinitionNode>();
    HashSet<String> names = new HashSet<String>();
    // add in existing generated columns if this is an ALTER TABLE statement
    if (baseTable != null) {
        ColumnDescriptorList cdl = baseTable.getGeneratedColumns();
        int count = cdl.size();
        for (int i = 0; i < count; i++) {
            names.add(cdl.elementAt(i).getColumnName());
        }
    }
    // find all of the generated columns
    for (TableElementNode element : this) {
        ColumnDefinitionNode cdn;
        if (!(element instanceof ColumnDefinitionNode)) {
            continue;
        }
        cdn = (ColumnDefinitionNode) element;
        if (!cdn.hasGenerationClause()) {
            continue;
        }
        generatedColumns.add(cdn);
        names.add(cdn.getColumnName());
    }
    // now look at their generation clauses to see if they reference one
    // another
    int count = generatedColumns.size();
    for (int i = 0; i < count; i++) {
        ColumnDefinitionNode cdn = generatedColumns.get(i);
        GenerationClauseNode generationClauseNode = cdn.getGenerationClauseNode();
        List<ColumnReference> referencedColumns = generationClauseNode.findReferencedColumns();
        int refCount = referencedColumns.size();
        for (int j = 0; j < refCount; j++) {
            String name = referencedColumns.get(j).getColumnName();
            if (name != null) {
                if (names.contains(name)) {
                    throw StandardException.newException(SQLState.LANG_CANT_REFERENCE_GENERATED_COLUMN, cdn.getColumnName());
                }
            }
        }
    }
}
Also used : ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

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