Search in sources :

Example 86 with DataDictionary

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

the class CreateViewNode method bindStatement.

// We inherit the generate() method from DDLStatementNode.
/**
 * Bind this CreateViewNode.  This means doing any static error
 * checking that can be done before actually creating the table.
 * For example, verifying that the ResultColumnList does not
 * contain any duplicate column names.
 *
 * @exception StandardException		Thrown on error
 */
@Override
public void bindStatement() throws StandardException {
    CompilerContext cc = getCompilerContext();
    DataDictionary dataDictionary = getDataDictionary();
    ResultColumnList qeRCL;
    String duplicateColName;
    // bind the query expression
    providerInfos = bindViewDefinition(dataDictionary, cc, getLanguageConnectionContext(), getOptimizerFactory(), queryExpression, getContextManager());
    qeRCL = queryExpression.getResultColumns();
    /* If there is an RCL for the view definition then
		 * copy the names to the queryExpression's RCL after verifying
		 * that they both have the same size.
		 */
    if (resultColumns != null) {
        if (resultColumns.size() != qeRCL.visibleSize()) {
            throw StandardException.newException(SQLState.LANG_VIEW_DEFINITION_R_C_L_MISMATCH, getFullName());
        }
        qeRCL.copyResultColumnNames(resultColumns);
    }
    /* Check to make sure the queryExpression's RCL has unique names. If target column
		 * names not specified, raise error if there are any un-named columns to match DB2
		 */
    duplicateColName = qeRCL.verifyUniqueNames((resultColumns == null) ? true : false);
    if (duplicateColName != null) {
        throw StandardException.newException(SQLState.LANG_DUPLICATE_COLUMN_NAME_CREATE_VIEW, duplicateColName);
    }
    /* Only 5000 columns allowed per view */
    if (queryExpression.getResultColumns().size() > Limits.DB2_MAX_COLUMNS_IN_VIEW) {
        throw StandardException.newException(SQLState.LANG_TOO_MANY_COLUMNS_IN_TABLE_OR_VIEW, String.valueOf(queryExpression.getResultColumns().size()), getRelativeName(), String.valueOf(Limits.DB2_MAX_COLUMNS_IN_VIEW));
    }
    // for each column, stuff system.column
    // System columns should only include visible columns DERBY-4230
    colInfos = new ColumnInfo[queryExpression.getResultColumns().visibleSize()];
    genColumnInfos(colInfos);
}
Also used : CompilerContext(org.apache.derby.iapi.sql.compile.CompilerContext) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary)

Example 87 with DataDictionary

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

the class AlterTableNode method bindStatement.

// We inherit the generate() method from DDLStatementNode.
/**
 * Bind this AlterTableNode.  This means doing any static error
 * checking that can be done before actually creating the table.
 * For example, verifying that the user is not trying to add a
 * non-nullable column.
 *
 * @exception StandardException		Thrown on error
 */
@Override
public void bindStatement() throws StandardException {
    DataDictionary dd = getDataDictionary();
    int numCheckConstraints = 0;
    int numReferenceConstraints = 0;
    int numGenerationClauses = 0;
    int numBackingIndexes = 0;
    /*
		** Get the table descriptor.  Checks the schema
		** and the table.
		*/
    if (compressTable && (purge || defragment || truncateEndOfTable)) {
        // We are dealing with inplace compress here and inplace compress is
        // allowed on system schemas. In order to support inplace compress
        // on user as well as system tables, we need to use special
        // getTableDescriptor(boolean) call to get TableDescriptor. This
        // getTableDescriptor(boolean) allows getting TableDescriptor for
        // system tables without throwing an exception.
        baseTable = getTableDescriptor(false);
    } else
        baseTable = getTableDescriptor();
    // throw an exception if user is attempting to alter a temporary table
    if (baseTable.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
        throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE);
    }
    /* Statement is dependent on the TableDescriptor */
    getCompilerContext().createDependency(baseTable);
    // The collation derivation of such a column would be "implicit".
    if (changeType == ADD_TYPE) {
        // the action is of type add.
        if (tableElementList != null) {
            // check if is is add column
            for (int i = 0; i < tableElementList.size(); i++) {
                if (tableElementList.elementAt(i) instanceof ColumnDefinitionNode) {
                    ColumnDefinitionNode cdn = (ColumnDefinitionNode) tableElementList.elementAt(i);
                    if (cdn.hasGenerationClause() && (cdn.getType() == null)) {
                        continue;
                    }
                    if (cdn.getType() == null) {
                        throw StandardException.newException(SQLState.LANG_NEEDS_DATATYPE, cdn.getColumnName());
                    }
                    if (cdn.getType().getTypeId().isStringTypeId()) {
                        // we found what we are looking for. Set the
                        // collation type of this column to be the same as
                        // schema descriptor's collation. Set the collation
                        // derivation as implicit
                        cdn.setCollationType(schemaDescriptor.getCollationType());
                    }
                }
            }
        }
    }
    if (tableElementList != null) {
        tableElementList.validate(this, dd, baseTable);
        /* Only 1012 columns allowed per table */
        if ((tableElementList.countNumberOfColumns() + baseTable.getNumberOfColumns()) > Limits.DB2_MAX_COLUMNS_IN_TABLE) {
            throw StandardException.newException(SQLState.LANG_TOO_MANY_COLUMNS_IN_TABLE_OR_VIEW, String.valueOf(tableElementList.countNumberOfColumns() + baseTable.getNumberOfColumns()), getRelativeName(), String.valueOf(Limits.DB2_MAX_COLUMNS_IN_TABLE));
        }
        /* Number of backing indexes in the alter table statment */
        numBackingIndexes = tableElementList.countConstraints(DataDictionary.PRIMARYKEY_CONSTRAINT) + tableElementList.countConstraints(DataDictionary.FOREIGNKEY_CONSTRAINT) + tableElementList.countConstraints(DataDictionary.UNIQUE_CONSTRAINT);
        /* Check the validity of all check constraints */
        numCheckConstraints = tableElementList.countConstraints(DataDictionary.CHECK_CONSTRAINT);
        numReferenceConstraints = tableElementList.countConstraints(DataDictionary.FOREIGNKEY_CONSTRAINT);
        numGenerationClauses = tableElementList.countGenerationClauses();
    }
    // so far is more than 32767, then we need to throw an exception
    if ((numBackingIndexes + baseTable.getTotalNumberOfIndexes()) > Limits.DB2_MAX_INDEXES_ON_TABLE) {
        throw StandardException.newException(SQLState.LANG_TOO_MANY_INDEXES_ON_TABLE, String.valueOf(numBackingIndexes + baseTable.getTotalNumberOfIndexes()), getRelativeName(), String.valueOf(Limits.DB2_MAX_INDEXES_ON_TABLE));
    }
    if ((numCheckConstraints > 0) || (numGenerationClauses > 0) || (numReferenceConstraints > 0)) {
        /* In order to check the validity of the check constraints and
			 * generation clauses
			 * we must goober up a FromList containing a single table, 
			 * the table being alter, with an RCL containing the existing and
			 * new columns and their types.  This will allow us to
			 * bind the constraint definition trees against that
			 * FromList.  When doing this, we verify that there are
			 * no nodes which can return non-deterministic results.
			 */
        FromList fromList = makeFromList(dd, tableElementList, false);
        FormatableBitSet generatedColumns = baseTable.makeColumnMap(baseTable.getGeneratedColumns());
        /* Now that we've finally goobered stuff up, bind and validate
			 * the check constraints and generation clauses.
			 */
        if (numGenerationClauses > 0) {
            tableElementList.bindAndValidateGenerationClauses(schemaDescriptor, fromList, generatedColumns, baseTable);
        }
        if (numCheckConstraints > 0) {
            tableElementList.bindAndValidateCheckConstraints(fromList);
        }
        if (numReferenceConstraints > 0) {
            tableElementList.validateForeignKeysOnGenerationClauses(fromList, generatedColumns);
        }
    }
    // must be done after resolving the datatypes of the generation clauses
    if (tableElementList != null) {
        tableElementList.validatePrimaryKeyNullability();
    }
    // index. If yes, then verify that the indexname provided is a valid one.
    if ((updateStatistics && !updateStatisticsAll) || (dropStatistics && !dropStatisticsAll)) {
        ConglomerateDescriptor cd = null;
        if (schemaDescriptor.getUUID() != null)
            cd = dd.getConglomerateDescriptor(indexNameForStatistics, schemaDescriptor, false);
        if (cd == null) {
            throw StandardException.newException(SQLState.LANG_INDEX_NOT_FOUND, schemaDescriptor.getSchemaName() + "." + indexNameForStatistics);
        }
    }
    /* Unlike most other DDL, we will make this ALTER TABLE statement
		 * dependent on the table being altered.  In general, we try to
		 * avoid this for DDL, but we are already requiring the table to
		 * exist at bind time (not required for create index) and we don't
		 * want the column ids to change out from under us before
		 * execution.
		 */
    getCompilerContext().createDependency(baseTable);
}
Also used : FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)

Example 88 with DataDictionary

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

the class AlterTableNode method prepConstantAction.

/**
 *	Generate arguments to constant action. Called by makeConstantAction() in this class and in
 *	our subclass RepAlterTableNode.
 *
 * @exception StandardException		Thrown on failure
 */
private void prepConstantAction() throws StandardException {
    if (tableElementList != null) {
        genColumnInfo();
    }
    if (numConstraints > 0) {
        conActions = new ConstraintConstantAction[numConstraints];
        tableElementList.genConstraintActions(false, conActions, getRelativeName(), schemaDescriptor, getDataDictionary());
        for (int conIndex = 0; conIndex < conActions.length; conIndex++) {
            ConstraintConstantAction cca = conActions[conIndex];
            if (cca instanceof CreateConstraintConstantAction) {
                int constraintType = cca.getConstraintType();
                if (constraintType == DataDictionary.PRIMARYKEY_CONSTRAINT) {
                    DataDictionary dd = getDataDictionary();
                    // Check to see if a constraint of the same type
                    // already exists
                    ConstraintDescriptorList cdl = dd.getConstraintDescriptors(baseTable);
                    if (cdl.getPrimaryKey() != null) {
                        throw StandardException.newException(SQLState.LANG_ADD_PRIMARY_KEY_FAILED1, baseTable.getQualifiedName());
                    }
                }
            }
        }
    }
}
Also used : ConstraintConstantAction(org.apache.derby.impl.sql.execute.ConstraintConstantAction) CreateConstraintConstantAction(org.apache.derby.impl.sql.execute.CreateConstraintConstantAction) ConstraintDescriptorList(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) CreateConstraintConstantAction(org.apache.derby.impl.sql.execute.CreateConstraintConstantAction)

Example 89 with DataDictionary

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

the class CallStatementNode method optimizeStatement.

/**
 * Optimize a DML statement (which is the only type of statement that
 * should need optimizing, I think). This method over-rides the one
 * in QueryTreeNode.
 *
 * This method takes a bound tree, and returns an optimized tree.
 * It annotates the bound tree rather than creating an entirely
 * new tree.
 *
 * Throws an exception if the tree is not bound, or if the binding
 * is out of date.
 *
 * @exception StandardException		Thrown on error
 */
@Override
public void optimizeStatement() throws StandardException {
    DataDictionary dd = getDataDictionary();
    if (SanityManager.DEBUG)
        SanityManager.ASSERT((dd != null), "Failed to get data dictionary");
    /* Preprocess the method call tree */
    methodCall = (JavaToSQLValueNode) methodCall.preprocess(getCompilerContext().getNumTables(), new FromList(getOptimizerFactory().doJoinOrderOptimization(), getContextManager()), (SubqueryList) null, (PredicateList) null);
}
Also used : DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary)

Example 90 with DataDictionary

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

the class RoutinePrivilegeInfo method executeGrantRevoke.

/**
 *	This is the guts of the Execution-time logic for GRANT/REVOKE of a routine execute privilege
 *
 * @param activation
 * @param grant true if grant, false if revoke
 * @param grantees a list of authorization ids (strings)
 *
 * @exception StandardException		Thrown on failure
 */
public void executeGrantRevoke(Activation activation, boolean grant, List grantees) throws StandardException {
    // Check that the current user has permission to grant the privileges.
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    String currentUser = lcc.getCurrentUserId(activation);
    TransactionController tc = lcc.getTransactionExecute();
    // Check that the current user has permission to grant the privileges.
    checkOwnership(currentUser, aliasDescriptor, dd.getSchemaDescriptor(aliasDescriptor.getSchemaUUID(), tc), dd);
    DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
    RoutinePermsDescriptor routinePermsDesc = ddg.newRoutinePermsDescriptor(aliasDescriptor, currentUser);
    dd.startWriting(lcc);
    for (Iterator itr = grantees.iterator(); itr.hasNext(); ) {
        // Keep track to see if any privileges are revoked by a revoke
        // statement. If a privilege is not revoked, we need to raise a
        // warning.
        boolean privileges_revoked = false;
        String grantee = (String) itr.next();
        if (dd.addRemovePermissionsDescriptor(grant, routinePermsDesc, grantee, tc)) {
            privileges_revoked = true;
            // Derby currently supports only restrict form of revoke execute
            // privilege and that is why, we are sending invalidation action
            // as REVOKE_PRIVILEGE_RESTRICT rather than REVOKE_PRIVILEGE
            dd.getDependencyManager().invalidateFor(routinePermsDesc, DependencyManager.REVOKE_PRIVILEGE_RESTRICT, lcc);
            // When revoking a privilege from a Routine we need to
            // invalidate all GPSs refering to it. But GPSs aren't
            // Dependents of RoutinePermsDescr, but of the
            // AliasDescriptor itself, so we must send
            // INTERNAL_RECOMPILE_REQUEST to the AliasDescriptor's
            // Dependents.
            dd.getDependencyManager().invalidateFor(aliasDescriptor, DependencyManager.INTERNAL_RECOMPILE_REQUEST, lcc);
        }
        addWarningIfPrivilegeNotRevoked(activation, grant, privileges_revoked, grantee);
    }
}
Also used : DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) Iterator(java.util.Iterator) RoutinePermsDescriptor(org.apache.derby.iapi.sql.dictionary.RoutinePermsDescriptor) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TransactionController(org.apache.derby.iapi.store.access.TransactionController)

Aggregations

DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)102 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)57 TransactionController (org.apache.derby.iapi.store.access.TransactionController)40 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)33 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)23 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)22 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)17 StandardException (org.apache.derby.shared.common.error.StandardException)16 UUID (org.apache.derby.catalog.UUID)15 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)15 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)13 CompilerContext (org.apache.derby.iapi.sql.compile.CompilerContext)10 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)9 RoleGrantDescriptor (org.apache.derby.iapi.sql.dictionary.RoleGrantDescriptor)8 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)7 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)7 ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)7 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)6 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)6 Iterator (java.util.Iterator)5