Search in sources :

Example 6 with GroupFetchScanController

use of org.apache.derby.iapi.store.access.GroupFetchScanController in project derby by apache.

the class InsertResultSet method bulkValidateForeignKeysCore.

private void bulkValidateForeignKeysCore(TransactionController tc, ContextManager cm, FKInfo fkInfo, long fkConglom, long pkConglom, String fkConstraintName, ExecRow fullTemplate) throws StandardException {
    ExecRow template;
    GroupFetchScanController refScan = null;
    GroupFetchScanController fkScan = null;
    try {
        template = makeIndexTemplate(fkInfo, fullTemplate, cm);
        /*
				** The indexes have been dropped and recreated, so
				** we need to get the new index conglomerate number.
				*/
        fkScan = tc.openGroupFetchScan(fkConglom, // hold
        false, // read only
        0, // doesn't matter, already locked
        TransactionController.MODE_TABLE, // doesn't matter, already locked
        TransactionController.ISOLATION_READ_COMMITTED, // retrieve all fields
        (FormatableBitSet) null, // startKeyValue
        (DataValueDescriptor[]) null, // startSearchOp
        ScanController.GE, // qualifier
        null, // stopKeyValue
        (DataValueDescriptor[]) null, // stopSearchOp
        ScanController.GT);
        if (SanityManager.DEBUG) {
            /*
					** Bulk insert replace calls this method regardless
					** of whether or not any rows were inserted because
					** it has to check any referencing foreign keys
					** after the replace.  Otherwise, we
					** make sure that we actually have a row in the fk.
					** If not, we have an error because we thought that
					** since indexRows != null, we must have gotten some
					** rows.
					*/
            if (!bulkInsertReplace) {
                SanityManager.ASSERT(fkScan.next(), "No rows in fk index, even though indexRows != null");
                /*
						** Crank up the scan again.
						*/
                fkScan.reopenScan(// startKeyValue
                (DataValueDescriptor[]) null, // startSearchOp
                ScanController.GE, // qualifier
                null, // stopKeyValue
                (DataValueDescriptor[]) null, // stopSearchOp
                ScanController.GT);
            }
        }
        /*
				** Open the referenced key scan.  Use row locking on
				** the referenced table unless it is self-referencing
	 			** (in which case we don't need locks)
				*/
        refScan = tc.openGroupFetchScan(pkConglom, // hold
        false, // read only
        0, (fkConglom == pkConglom) ? TransactionController.MODE_TABLE : TransactionController.MODE_RECORD, // read committed is good enough
        TransactionController.ISOLATION_READ_COMMITTED, // retrieve all fields
        (FormatableBitSet) null, // startKeyValue
        (DataValueDescriptor[]) null, // startSearchOp
        ScanController.GE, // qualifier
        null, // stopKeyValue
        (DataValueDescriptor[]) null, // stopSearchOp
        ScanController.GT);
        /*
				** Give the scans to the bulk checker to do its
				** magic.  It will do a merge on the two indexes. 
				*/
        ExecRow firstFailedRow = template.getClone();
        RIBulkChecker riChecker = new RIBulkChecker(activation, refScan, fkScan, template, // fail on 1st failure
        true, (ConglomerateController) null, firstFailedRow, fkInfo.schemaName, fkInfo.tableName, fkInfo.fkIds[0], fkInfo.deferrable[0], fkConglom, pkConglom);
        int numFailures = riChecker.doCheck();
        if (numFailures > 0) {
            StandardException se = StandardException.newException(SQLState.LANG_FK_VIOLATION, fkConstraintName, fkInfo.tableName, StatementUtil.typeName(fkInfo.stmtType), RowUtil.toString(firstFailedRow, 0, fkInfo.colArray.length - 1));
            throw se;
        }
    } finally {
        if (fkScan != null) {
            fkScan.close();
        }
        if (refScan != null) {
            refScan.close();
        }
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) ExecRow(org.apache.derby.iapi.sql.execute.ExecRow) FormatableBitSet(org.apache.derby.iapi.services.io.FormatableBitSet) DataValueDescriptor(org.apache.derby.iapi.types.DataValueDescriptor) GroupFetchScanController(org.apache.derby.iapi.store.access.GroupFetchScanController)

Aggregations

GroupFetchScanController (org.apache.derby.iapi.store.access.GroupFetchScanController)6 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)4 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)3 ExecRow (org.apache.derby.iapi.sql.execute.ExecRow)3 ConglomerateController (org.apache.derby.iapi.store.access.ConglomerateController)3 TransactionController (org.apache.derby.iapi.store.access.TransactionController)3 StandardException (org.apache.derby.shared.common.error.StandardException)3 UUID (org.apache.derby.catalog.UUID)2 ColumnDescriptor (org.apache.derby.iapi.sql.dictionary.ColumnDescriptor)2 ConglomerateDescriptor (org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor)2 DataDictionary (org.apache.derby.iapi.sql.dictionary.DataDictionary)2 IndexRowGenerator (org.apache.derby.iapi.sql.dictionary.IndexRowGenerator)2 StatisticsDescriptor (org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor)2 ExecIndexRow (org.apache.derby.iapi.sql.execute.ExecIndexRow)2 RowLocation (org.apache.derby.iapi.types.RowLocation)2 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1 StatisticsImpl (org.apache.derby.catalog.types.StatisticsImpl)1 ClassFactory (org.apache.derby.iapi.services.loader.ClassFactory)1 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)1