Search in sources :

Example 41 with TableDescriptor

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

the class AlterConstraintConstantAction method executeConstantAction.

/**
 *  This is the guts of the Execution-time logic for ALTER CONSTRAINT.
 *
 *  @see ConstantAction#executeConstantAction
 *
 * @exception StandardException     Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    final LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    final DataDictionary dd = lcc.getDataDictionary();
    final DependencyManager dm = dd.getDependencyManager();
    final TransactionController tc = lcc.getTransactionExecute();
    /*
        ** Inform the data dictionary that we are about to write to it.
        ** There are several calls to data dictionary "get" methods here
        ** that might be done in "read" mode in the data dictionary, but
        ** it seemed safer to do this whole operation in "write" mode.
        **
        ** We tell the data dictionary we're done writing at the end of
        ** the transaction.
        */
    dd.startWriting(lcc);
    final TableDescriptor td = dd.getTableDescriptor(tableId);
    if (td == null) {
        throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, tableName);
    }
    /* Table gets locked in AlterTableConstantAction */
    /*
        ** If the schema descriptor is null, then
        ** we must have just read ourselves in.
        ** So we will get the corresponding schema
        ** descriptor from the data dictionary.
        */
    SchemaDescriptor tdSd = td.getSchemaDescriptor();
    SchemaDescriptor constraintSd = constraintSchemaName == null ? tdSd : dd.getSchemaDescriptor(constraintSchemaName, tc, true);
    /* Get the constraint descriptor for the index, along
         * with an exclusive row lock on the row in sys.sysconstraints
         * in order to ensure that no one else compiles against the
         * index.
         */
    final ConstraintDescriptor conDesc = dd.getConstraintDescriptorByName(td, constraintSd, constraintName, true);
    if (conDesc == null) {
        throw StandardException.newException(SQLState.LANG_DROP_OR_ALTER_NON_EXISTING_CONSTRAINT, constraintSd.getSchemaName() + "." + constraintName, td.getQualifiedName());
    }
    if (characteristics[2] != ConstraintDefinitionNode.ENFORCED_DEFAULT) {
        dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, "DEFERRED CONSTRAINTS");
        if (constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT || constraintType == DataDictionary.NOTNULL_CONSTRAINT || !characteristics[2]) /* not enforced */
        {
            // Remove when feature DERBY-532 is completed
            if (!PropertyUtil.getSystemProperty("derby.constraintsTesting", "false").equals("true")) {
                throw StandardException.newException(SQLState.NOT_IMPLEMENTED, "non-default enforcement");
            }
        }
    }
    // The first two characteristics are unused during ALTER CONSTRAINT; only
    // enforcement can change.
    conDesc.setEnforced(characteristics[2]);
    int[] colsToSet = new int[1];
    colsToSet[0] = SYSCONSTRAINTSRowFactory.SYSCONSTRAINTS_STATE;
    dd.updateConstraintDescriptor(conDesc, conDesc.getUUID(), colsToSet, tc);
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ForeignKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor) ReferencedKeyConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor) ConstraintDescriptor(org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TransactionController(org.apache.derby.iapi.store.access.TransactionController) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 42 with TableDescriptor

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

the class GenericLanguageConnectionContext method tempTablesXApostCommit.

private void tempTablesXApostCommit() throws StandardException {
    TransactionController tc = getTransactionExecute();
    // transaction boundary.
    for (int i = 0; i < allDeclaredGlobalTempTables.size(); i++) {
        // remove all temp tables from this context.
        TableDescriptor td = allDeclaredGlobalTempTables.get(i).getTableDescriptor();
        // remove the conglomerate created for this temp table
        tc.dropConglomerate(td.getHeapConglomerateId());
        // remove it from the list of temp tables
        allDeclaredGlobalTempTables.remove(i);
    }
    tc.commit();
}
Also used : TransactionController(org.apache.derby.iapi.store.access.TransactionController) XATransactionController(org.apache.derby.iapi.store.access.XATransactionController) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 43 with TableDescriptor

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

the class GenericLanguageConnectionContext method tempTablesAndRollback.

/**
 * do the necessary work at rollback time for temporary tables
 * 1)If a temp table was declared in the UOW, then drop it and remove it
 *   from list of temporary tables.
 * 2)If a temp table was declared and dropped in the UOW, then remove it
 *   from list of temporary tables.
 * 3)If an existing temp table was dropped in the UOW, then recreate it
 *   with no data.
 * 4)If an existing temp table was modified in the UOW, then get rid of
 *   all the rows from the table.
 */
private void tempTablesAndRollback() throws StandardException {
    for (int i = allDeclaredGlobalTempTables.size() - 1; i >= 0; i--) {
        TempTableInfo tempTableInfo = allDeclaredGlobalTempTables.get(i);
        if (tempTableInfo.getDeclaredInSavepointLevel() >= currentSavepointLevel) {
            if (tempTableInfo.getDroppedInSavepointLevel() == -1) {
                // the table was declared but not dropped in the unit of
                // work getting rolled back and hence we will remove it
                // from valid list of temporary tables and drop the
                // conglomerate associated with it
                TableDescriptor td = tempTableInfo.getTableDescriptor();
                invalidateCleanupDroppedTable(td);
                // remove the conglomerate created for this temp table
                tran.dropConglomerate(td.getHeapConglomerateId());
                // remove it from the list of temp tables
                allDeclaredGlobalTempTables.remove(i);
            } else if (tempTableInfo.getDroppedInSavepointLevel() >= currentSavepointLevel) {
                // the table was declared and dropped in the unit of work
                // getting rolled back
                allDeclaredGlobalTempTables.remove(i);
            }
        } else if (tempTableInfo.getDroppedInSavepointLevel() >= currentSavepointLevel) {
            // this means the table was declared in an earlier savepoint
            // unit / transaction and then dropped in current UOW
            // restore the old definition of temp table because drop is
            // being rolledback
            TableDescriptor td = tempTableInfo.getTableDescriptor();
            td = cleanupTempTableOnCommitOrRollback(td, false);
            // In order to store the old conglomerate information for the
            // temp table, we need to replace the existing table descriptor
            // with the old table descriptor which has the old conglomerate
            // information
            tempTableInfo.setTableDescriptor(td);
            tempTableInfo.setDroppedInSavepointLevel(-1);
            // following will mark the table as not modified. This is
            // because the table data has been deleted as part of the
            // current rollback
            tempTableInfo.setModifiedInSavepointLevel(-1);
            allDeclaredGlobalTempTables.set(i, tempTableInfo);
        } else if (tempTableInfo.getModifiedInSavepointLevel() >= currentSavepointLevel) {
            // this means the table was declared in an earlier savepoint
            // unit / transaction and modified in current UOW
            // following will mark the table as not modified. This is
            // because the table data will be deleted as part of the
            // current rollback
            tempTableInfo.setModifiedInSavepointLevel(-1);
            TableDescriptor td = tempTableInfo.getTableDescriptor();
            invalidateCleanupDroppedTable(td);
        }
    // there is no else here because there is no special processing
    // required for temp tables declares in earlier work of
    // unit/transaction and not modified
    }
    if (allDeclaredGlobalTempTables.isEmpty()) {
        allDeclaredGlobalTempTables = null;
    }
}
Also used : TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor)

Example 44 with TableDescriptor

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

the class BasicDependencyManager method coreInvalidateFor.

/**
 * A version of invalidateFor that does not provide synchronization among
 * invalidators.
 *
 * @param p the provider
 * @param action the action causing the invalidation
 * @param lcc language connection context
 *
 * @throws StandardException if something goes wrong
 */
private void coreInvalidateFor(Provider p, int action, LanguageConnectionContext lcc) throws StandardException {
    List<Dependency> list = getDependents(p);
    if (list.isEmpty()) {
        return;
    }
    // affectedCols is passed in from table descriptor provider to indicate
    // which columns it cares; subsetCols is affectedCols' intersection
    // with column bit map found in the provider of SYSDEPENDS line to
    // find out which columns really matter.  If SYSDEPENDS line's
    // dependent is view (or maybe others), provider is table, yet it
    // doesn't have column bit map because the view was created in a
    // previous version of server which doesn't support column dependency,
    // and we really want it to have (such as in drop column), in any case
    // if we passed in table descriptor to this function with a bit map,
    // we really need this, we generate the bitmaps on the fly and update
    // SYSDEPENDS
    // 
    // Note: Since the "previous version of server" mentioned above must
    // be a version that predates Derby, and we don't support upgrade from
    // those versions, we no longer have code to generate the column
    // dependency list on the fly. Instead, an assert has been added to
    // verify that we always have a column bitmap in SYSDEPENDS if the
    // affectedCols bitmap is non-null.
    FormatableBitSet affectedCols = null, subsetCols = null;
    if (p instanceof TableDescriptor) {
        affectedCols = ((TableDescriptor) p).getReferencedColumnMap();
        if (affectedCols != null)
            subsetCols = new FormatableBitSet(affectedCols.getLength());
    }
    {
        StandardException noInvalidate = null;
        // entries from this list.
        for (int ei = list.size() - 1; ei >= 0; ei--) {
            if (ei >= list.size())
                continue;
            Dependency dependency = list.get(ei);
            Dependent dep = dependency.getDependent();
            if (affectedCols != null) {
                TableDescriptor td = (TableDescriptor) dependency.getProvider();
                FormatableBitSet providingCols = td.getReferencedColumnMap();
                if (providingCols == null) {
                    if (dep instanceof ViewDescriptor) {
                        // this code was removed as part of DERBY-6169.
                        if (SanityManager.DEBUG) {
                            SanityManager.THROWASSERT("Expected view to " + "have referenced column bitmap");
                        }
                    } else
                        // if dep instanceof ViewDescriptor
                        ((TableDescriptor) p).setReferencedColumnMap(null);
                } else // if providingCols == null
                {
                    subsetCols.copyFrom(affectedCols);
                    subsetCols.and(providingCols);
                    if (subsetCols.anySetBit() == -1)
                        continue;
                    ((TableDescriptor) p).setReferencedColumnMap(subsetCols);
                }
            }
            // generate a list of invalidations that fail.
            try {
                dep.prepareToInvalidate(p, action, lcc);
            } catch (StandardException sqle) {
                if (noInvalidate == null) {
                    noInvalidate = sqle;
                } else {
                    try {
                        sqle.initCause(noInvalidate);
                        noInvalidate = sqle;
                    } catch (IllegalStateException ise) {
                    // We weren't able to chain the exceptions. That's
                    // OK, since we always have the first exception we
                    // caught. Just skip the current exception.
                    }
                }
            }
            if (noInvalidate == null) {
                if (affectedCols != null)
                    ((TableDescriptor) p).setReferencedColumnMap(affectedCols);
                // REVISIT: future impl will want to mark the individual
                // dependency as invalid as well as the dependent...
                dep.makeInvalid(action, lcc);
            }
        }
        if (noInvalidate != null)
            throw noInvalidate;
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) Dependency(org.apache.derby.iapi.sql.depend.Dependency) Dependent(org.apache.derby.iapi.sql.depend.Dependent) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) ViewDescriptor(org.apache.derby.iapi.sql.dictionary.ViewDescriptor)

Example 45 with TableDescriptor

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

the class CreateAliasConstantAction method executeConstantAction.

// INTERFACE METHODS
/**
 *	This is the guts of the Execution-time logic for
 *  CREATE FUNCTION, PROCEDURE, SYNONYM, and TYPE.
 *  <P>
 *  A function, procedure, or udt is represented as:
 *  <UL>
 *  <LI> AliasDescriptor
 *  </UL>
 *  Routine dependencies are created as:
 *  <UL>
 *  <LI> None
 *  </UL>
 *
 *  <P>
 *  A synonym is represented as:
 *  <UL>
 *  <LI> AliasDescriptor
 *  <LI> TableDescriptor
 *  </UL>
 *  Synonym dependencies are created as:
 *  <UL>
 *  <LI> None
 *  </UL>
 *
 *  In both cases a SchemaDescriptor will be created if
 *  needed. No dependency is created on the SchemaDescriptor.
 *
 * @see ConstantAction#executeConstantAction
 * @see AliasDescriptor
 * @see TableDescriptor
 * @see SchemaDescriptor
 *
 * @exception StandardException		Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    TransactionController tc = lcc.getTransactionExecute();
    // For routines no validity checking is made
    // on the Java method, that is checked when the
    // routine is executed.
    /*
		** Inform the data dictionary that we are about to write to it.
		** There are several calls to data dictionary "get" methods here
		** that might be done in "read" mode in the data dictionary, but
		** it seemed safer to do this whole operation in "write" mode.
		**
		** We tell the data dictionary we're done writing at the end of
		** the transaction.
		*/
    dd.startWriting(lcc);
    SchemaDescriptor sd = DDLConstantAction.getSchemaDescriptorForCreate(dd, activation, schemaName);
    // 
    // Create a new alias descriptor with aliasID filled in.
    // 
    UUID aliasID = dd.getUUIDFactory().createUUID();
    AliasDescriptor ads = new AliasDescriptor(dd, aliasID, aliasName, sd.getUUID(), javaClassName, aliasType, nameSpace, false, aliasInfo, null);
    // perform duplicate rule checking
    switch(aliasType) {
        case AliasInfo.ALIAS_TYPE_AGGREGATE_AS_CHAR:
            AliasDescriptor duplicateAlias = dd.getAliasDescriptor(sd.getUUID().toString(), aliasName, nameSpace);
            if (duplicateAlias != null) {
                throw StandardException.newException(SQLState.LANG_OBJECT_ALREADY_EXISTS, ads.getDescriptorType(), aliasName);
            }
            // also don't want to collide with 1-arg functions by the same name
            List<AliasDescriptor> funcList = dd.getRoutineList(sd.getUUID().toString(), aliasName, AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR);
            for (int i = 0; i < funcList.size(); i++) {
                AliasDescriptor func = funcList.get(i);
                RoutineAliasInfo funcInfo = (RoutineAliasInfo) func.getAliasInfo();
                if (funcInfo.getParameterCount() == 1) {
                    throw StandardException.newException(SQLState.LANG_BAD_UDA_OR_FUNCTION_NAME, schemaName, aliasName);
                }
            }
            break;
        case AliasInfo.ALIAS_TYPE_UDT_AS_CHAR:
            AliasDescriptor duplicateUDT = dd.getAliasDescriptor(sd.getUUID().toString(), aliasName, nameSpace);
            if (duplicateUDT != null) {
                throw StandardException.newException(SQLState.LANG_OBJECT_ALREADY_EXISTS, ads.getDescriptorType(), aliasName);
            }
            break;
        case AliasInfo.ALIAS_TYPE_PROCEDURE_AS_CHAR:
            vetRoutine(dd, sd, ads);
            break;
        case AliasInfo.ALIAS_TYPE_FUNCTION_AS_CHAR:
            vetRoutine(dd, sd, ads);
            // if this is a 1-arg function, make sure there isn't an aggregate
            // by the same qualified name
            int paramCount = ((RoutineAliasInfo) aliasInfo).getParameterCount();
            if (paramCount == 1) {
                AliasDescriptor aliasCollision = dd.getAliasDescriptor(sd.getUUID().toString(), aliasName, AliasInfo.ALIAS_NAME_SPACE_AGGREGATE_AS_CHAR);
                if (aliasCollision != null) {
                    throw StandardException.newException(SQLState.LANG_BAD_UDA_OR_FUNCTION_NAME, schemaName, aliasName);
                }
            }
            break;
        case AliasInfo.ALIAS_TYPE_SYNONYM_AS_CHAR:
            // If target table/view exists already, error.
            TableDescriptor targetTD = dd.getTableDescriptor(aliasName, sd, tc);
            if (targetTD != null) {
                throw StandardException.newException(SQLState.LANG_OBJECT_ALREADY_EXISTS, targetTD.getDescriptorType(), targetTD.getDescriptorName());
            }
            // Detect synonym cycles, if present.
            String nextSynTable = ((SynonymAliasInfo) aliasInfo).getSynonymTable();
            String nextSynSchema = ((SynonymAliasInfo) aliasInfo).getSynonymSchema();
            SchemaDescriptor nextSD;
            for (; ; ) {
                nextSD = dd.getSchemaDescriptor(nextSynSchema, tc, false);
                if (nextSD == null)
                    break;
                AliasDescriptor nextAD = dd.getAliasDescriptor(nextSD.getUUID().toString(), nextSynTable, nameSpace);
                if (nextAD == null)
                    break;
                SynonymAliasInfo info = (SynonymAliasInfo) nextAD.getAliasInfo();
                nextSynTable = info.getSynonymTable();
                nextSynSchema = info.getSynonymSchema();
                if (aliasName.equals(nextSynTable) && schemaName.equals(nextSynSchema))
                    throw StandardException.newException(SQLState.LANG_SYNONYM_CIRCULAR, aliasName, ((SynonymAliasInfo) aliasInfo).getSynonymTable());
            }
            // If synonym final target is not present, raise a warning
            if (nextSD != null)
                targetTD = dd.getTableDescriptor(nextSynTable, nextSD, tc);
            if (nextSD == null || targetTD == null)
                activation.addWarning(StandardException.newWarning(SQLState.LANG_SYNONYM_UNDEFINED, aliasName, nextSynSchema + "." + nextSynTable));
            // To prevent any possible deadlocks with SYSTABLES, we insert a row into
            // SYSTABLES also for synonyms. This also ensures tables/views/synonyms share
            // same namespace
            TableDescriptor td;
            DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator();
            td = ddg.newTableDescriptor(aliasName, sd, TableDescriptor.SYNONYM_TYPE, TableDescriptor.DEFAULT_LOCK_GRANULARITY);
            dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, false, tc);
            break;
        default:
            break;
    }
    dd.addDescriptor(ads, null, DataDictionary.SYSALIASES_CATALOG_NUM, false, tc);
    adjustUDTDependencies(lcc, dd, ads, true);
}
Also used : SchemaDescriptor(org.apache.derby.iapi.sql.dictionary.SchemaDescriptor) RoutineAliasInfo(org.apache.derby.catalog.types.RoutineAliasInfo) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) DataDescriptorGenerator(org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) AliasDescriptor(org.apache.derby.iapi.sql.dictionary.AliasDescriptor) SynonymAliasInfo(org.apache.derby.catalog.types.SynonymAliasInfo) TransactionController(org.apache.derby.iapi.store.access.TransactionController) UUID(org.apache.derby.catalog.UUID)

Aggregations

TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)80 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)27 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)23 UUID (org.apache.derby.catalog.UUID)22 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)20 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)20 TransactionController (org.apache.derby.iapi.store.access.TransactionController)20 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)19 DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)11 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)11 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)9 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)9 StandardException (org.apache.derby.shared.common.error.StandardException)9 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)7 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)7 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)7 ArrayList (java.util.ArrayList)6 AliasDescriptor (org.apache.derby.iapi.sql.dictionary.AliasDescriptor)6 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)6 ViewDescriptor (org.apache.derby.iapi.sql.dictionary.ViewDescriptor)6