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();
}
}
}
Aggregations