Search in sources :

Example 31 with DependencyManager

use of org.apache.derby.iapi.sql.depend.DependencyManager in project derby by apache.

the class DropTableConstantAction method executeConstantAction.

// INTERFACE METHODS
/**
 *	This is the guts of the Execution-time logic for DROP TABLE.
 *
 *	@see ConstantAction#executeConstantAction
 *
 * @exception StandardException		Thrown on failure
 */
public void executeConstantAction(Activation activation) throws StandardException {
    TableDescriptor td;
    UUID tableID;
    ConglomerateDescriptor[] cds;
    LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
    DataDictionary dd = lcc.getDataDictionary();
    DependencyManager dm = dd.getDependencyManager();
    TransactionController tc = lcc.getTransactionExecute();
    if ((sd != null) && sd.getSchemaName().equals(SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME)) {
        // check if this is a temp table before checking data dictionary
        td = lcc.getTableDescriptorForDeclaredGlobalTempTable(tableName);
        if (// td null here means it is not a temporary table. Look for table in physical SESSION schema
        td == null)
            td = dd.getTableDescriptor(tableName, sd, tc);
        if (// td null means tableName is not a temp table and it is not a physical table in SESSION schema
        td == null) {
            throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);
        }
        if (td.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) {
            dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc);
            tc.dropConglomerate(td.getHeapConglomerateId());
            lcc.dropDeclaredGlobalTempTable(tableName);
            return;
        }
    }
    /* Lock the table before we access the data dictionary
		 * to prevent deadlocks.
		 *
		 * Note that for DROP TABLE replayed at Targets during REFRESH,
		 * the conglomerateNumber will be 0. That's ok. During REFRESH,
		 * we don't need to lock the conglomerate.
		 */
    if (conglomerateNumber != 0) {
        lockTableForDDL(tc, conglomerateNumber, true);
    }
    /*
		** 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);
    /* Get the table descriptor. */
    td = dd.getTableDescriptor(tableId);
    if (td == null) {
        throw StandardException.newException(SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, fullTableName);
    }
    /* Get an exclusive table lock on the table. */
    long heapId = td.getHeapConglomerateId();
    lockTableForDDL(tc, heapId, true);
    /* Drop the triggers */
    for (TriggerDescriptor trd : dd.getTriggerDescriptors(td)) {
        trd.drop(lcc);
    }
    /* Drop all defaults */
    ColumnDescriptorList cdl = td.getColumnDescriptorList();
    for (ColumnDescriptor cd : cdl) {
        // 
        if (cd.isAutoincrement() && dd.checkVersion(DataDictionary.DD_VERSION_DERBY_10_11, null)) {
            dropIdentitySequence(dd, td, activation);
        }
        // any dependencies
        if (cd.getDefaultInfo() != null) {
            DefaultDescriptor defaultDesc = cd.getDefaultDescriptor(dd);
            dm.clearDependencies(lcc, defaultDesc);
        }
    }
    /* Drop the columns */
    dd.dropAllColumnDescriptors(tableId, tc);
    /* Drop all table and column permission descriptors */
    dd.dropAllTableAndColPermDescriptors(tableId, tc);
    /* Drop the constraints */
    dropAllConstraintDescriptors(td, activation);
    /*
		** Drop all the conglomerates.  Drop the heap last, because the
		** store needs it for locking the indexes when they are dropped.
		*/
    cds = td.getConglomerateDescriptors();
    long[] dropped = new long[cds.length - 1];
    int numDropped = 0;
    for (int index = 0; index < cds.length; index++) {
        ConglomerateDescriptor cd = cds[index];
        /* if it's for an index, since similar indexes share one
			 * conglomerate, we only drop the conglomerate once
			 */
        if (cd.getConglomerateNumber() != heapId) {
            long thisConglom = cd.getConglomerateNumber();
            int i;
            for (i = 0; i < numDropped; i++) {
                if (dropped[i] == thisConglom)
                    break;
            }
            if (// not dropped
            i == numDropped) {
                dropped[numDropped++] = thisConglom;
                tc.dropConglomerate(thisConglom);
                dd.dropStatisticsDescriptors(td.getUUID(), cd.getUUID(), tc);
            }
        }
    }
    /* Prepare all dependents to invalidate.  (This is there chance
		 * to say that they can't be invalidated.  For example, an open
		 * cursor referencing a table/view that the user is attempting to
		 * drop.) If no one objects, then invalidate any dependent objects.
		 * We check for invalidation before we drop the table descriptor
		 * since the table descriptor may be looked up as part of
		 * decoding tuples in SYSDEPENDS.
		 */
    dm.invalidateFor(td, DependencyManager.DROP_TABLE, lcc);
    // 
    // The table itself can depend on the user defined types of its columns.
    // Drop all of those dependencies now.
    // 
    adjustUDTDependencies(lcc, dd, td, null, true);
    /* Drop the table */
    dd.dropTableDescriptor(td, sd, tc);
    /* Drop the conglomerate descriptors */
    dd.dropAllConglomerateDescriptors(td, tc);
    /* Drop the store element at last, to prevent dangling reference
		 * for open cursor, beetle 4393.
		 */
    tc.dropConglomerate(heapId);
}
Also used : ColumnDescriptor(org.apache.derby.iapi.sql.dictionary.ColumnDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary) ConglomerateDescriptor(org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor) TableDescriptor(org.apache.derby.iapi.sql.dictionary.TableDescriptor) TriggerDescriptor(org.apache.derby.iapi.sql.dictionary.TriggerDescriptor) LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) ColumnDescriptorList(org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList) UUID(org.apache.derby.catalog.UUID) TransactionController(org.apache.derby.iapi.store.access.TransactionController) DefaultDescriptor(org.apache.derby.iapi.sql.dictionary.DefaultDescriptor)

Example 32 with DependencyManager

use of org.apache.derby.iapi.sql.depend.DependencyManager in project derby by apache.

the class JarUtil method drop.

/**
 *	  Drop a jar file from the current connection's database.
 *
 *	  <P> The reason for dropping  the jar file in this private instance
 *	  method is that it allows us to share set up logic with add and
 *	  replace.
 *
 *	  @exception StandardException Opps
 */
private void drop() throws StandardException {
    // 
    // Like create table we say we are writing before we read the dd
    dd.startWriting(lcc);
    FileInfoDescriptor fid = getInfo();
    if (fid == null)
        throw StandardException.newException(SQLState.LANG_FILE_DOES_NOT_EXIST, sqlName, schemaName);
    String dbcp_s = PropertyUtil.getServiceProperty(lcc.getTransactionExecute(), Property.DATABASE_CLASSPATH);
    if (dbcp_s != null) {
        String[][] dbcp = IdUtil.parseDbClassPath(dbcp_s);
        boolean found = false;
        // a database classpath that is stored in the propert congomerate.
        for (int ix = 0; ix < dbcp.length; ix++) if (dbcp.length == 2 && dbcp[ix][0].equals(schemaName) && dbcp[ix][1].equals(sqlName))
            found = true;
        if (found)
            throw StandardException.newException(SQLState.LANG_CANT_DROP_JAR_ON_DB_CLASS_PATH_DURING_EXECUTION, IdUtil.mkQualifiedName(schemaName, sqlName), dbcp_s);
    }
    try {
        notifyLoader(false);
        dd.invalidateAllSPSPlans();
        DependencyManager dm = dd.getDependencyManager();
        dm.invalidateFor(fid, DependencyManager.DROP_JAR, lcc);
        UUID id = fid.getUUID();
        dd.dropFileInfoDescriptor(fid);
        fr.remove(JarUtil.mkExternalName(id, schemaName, sqlName, fr.getSeparatorChar()), fid.getGenerationId());
    } finally {
        notifyLoader(true);
    }
}
Also used : FileInfoDescriptor(org.apache.derby.iapi.sql.dictionary.FileInfoDescriptor) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) UUID(org.apache.derby.catalog.UUID)

Example 33 with DependencyManager

use of org.apache.derby.iapi.sql.depend.DependencyManager in project derby by apache.

the class T_ConsistencyChecker method countDependencies.

/**
 * Check to make sure that there are no active dependencies (stored or
 * in memory).
 *
 * @return String       If an inconsistency is found, and if DEBUG is on,
 *                      then a string will be returned with more info.
 *                      If DEBUG is off, then a simple string will be
 *                      returned stating whether or not there are open scans.
 *
 * @exception StandardException     Thrown on error
 * @exception java.sql.SQLException     Thrown on error
 */
public static String countDependencies() throws StandardException, java.sql.SQLException {
    int numDependencies = 0;
    DataDictionary dd;
    DependencyManager dm;
    StringBuffer debugBuf = new StringBuffer();
    LanguageConnectionContext lcc = (LanguageConnectionContext) getContext(LanguageConnectionContext.CONTEXT_ID);
    dd = lcc.getDataDictionary();
    dm = dd.getDependencyManager();
    numDependencies = dm.countDependencies();
    if (numDependencies > 0) {
        debugBuf.append(numDependencies + " dependencies found");
    } else {
        debugBuf.append("No outstanding dependencies.\n");
    }
    return debugBuf.toString();
}
Also used : LanguageConnectionContext(org.apache.derby.iapi.sql.conn.LanguageConnectionContext) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) DataDictionary(org.apache.derby.iapi.sql.dictionary.DataDictionary)

Example 34 with DependencyManager

use of org.apache.derby.iapi.sql.depend.DependencyManager 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 35 with DependencyManager

use of org.apache.derby.iapi.sql.depend.DependencyManager in project derby by apache.

the class GenericLanguageConnectionContext method verifyNoOpenResultSets.

/**
 * Verify that there are no activations with open result sets
 * on the specified prepared statement.
 *
 * @param pStmt     The prepared Statement
 * @param provider  The object precipitating a possible invalidation
 * @param action    The action causing the possible invalidation
 *
 * @return Nothing.
 *
 * @exception StandardException thrown on failure
 */
public boolean verifyNoOpenResultSets(PreparedStatement pStmt, Provider provider, int action) throws StandardException {
    /*
        ** It is not a problem to create an index when there is an open
        ** result set, since it doesn't invalidate the access path that was
        ** chosen for the result set.
        */
    boolean seenOpenResultSets = false;
    // in this list, thus invalidating the Enumeration
    for (int i = acts.size() - 1; i >= 0; i--) {
        Activation a = acts.get(i);
        if (!a.isInUse()) {
            continue;
        }
        /* for this prepared statement */
        if (pStmt == a.getPreparedStatement()) {
            ResultSet rs = a.getResultSet();
            /* is there an open result set? */
            if (rs != null && !rs.isClosed()) {
                if (!rs.returnsRows())
                    continue;
                seenOpenResultSets = true;
                break;
            }
        }
    }
    if (!seenOpenResultSets)
        return false;
    // There may be open ResultSet's that are yet to be garbage collected
    // let's try and force these out rather than throw an error
    System.gc();
    System.runFinalization();
    // in this list, thus invalidating the Enumeration
    for (int i = acts.size() - 1; i >= 0; i--) {
        Activation a = acts.get(i);
        if (!a.isInUse()) {
            continue;
        }
        /* for this prepared statement */
        if (pStmt == a.getPreparedStatement()) {
            ResultSet rs = a.getResultSet();
            /* is there an open result set? */
            if (rs != null && !rs.isClosed()) {
                if ((provider != null) && rs.returnsRows()) {
                    DependencyManager dmgr = getDataDictionary().getDependencyManager();
                    throw StandardException.newException(SQLState.LANG_CANT_INVALIDATE_OPEN_RESULT_SET, dmgr.getActionString(action), provider.getObjectName());
                }
                return true;
            }
        }
    }
    return false;
}
Also used : ResultSet(org.apache.derby.iapi.sql.ResultSet) DependencyManager(org.apache.derby.iapi.sql.depend.DependencyManager) Activation(org.apache.derby.iapi.sql.Activation) CursorActivation(org.apache.derby.iapi.sql.execute.CursorActivation)

Aggregations

DependencyManager (org.apache.derby.iapi.sql.depend.DependencyManager)55 TransactionController (org.apache.derby.iapi.store.access.TransactionController)23 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)22 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)20 UUID (org.apache.derby.catalog.UUID)11 TableDescriptor (org.apache.derby.iapi.sql.dictionary.TableDescriptor)11 SchemaDescriptor (org.apache.derby.iapi.sql.dictionary.SchemaDescriptor)8 ContextManager (org.apache.derby.iapi.services.context.ContextManager)7 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)7 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)7 ConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptor)7 ProviderInfo (org.apache.derby.iapi.sql.depend.ProviderInfo)6 ReferencedKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor)6 Provider (org.apache.derby.iapi.sql.depend.Provider)5 DataDescriptorGenerator (org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator)5 ProviderList (org.apache.derby.iapi.sql.depend.ProviderList)4 ColumnDescriptorList (org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList)4 ConstraintDescriptorList (org.apache.derby.iapi.sql.dictionary.ConstraintDescriptorList)4 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)3 ForeignKeyConstraintDescriptor (org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor)3