Table pkTable;
int columnCount;
int[] mainCols;
int[] refCols;
Constraint[] constraints;
Constraint constraint;
int constraintCount;
HsqlArrayList fkConstraintsList;
Object[] row;
// column number mappings
final int ipk_table_cat = 0;
final int ipk_table_schem = 1;
final int ipk_table_name = 2;
final int ipk_column_name = 3;
final int ifk_table_cat = 4;
final int ifk_table_schem = 5;
final int ifk_table_name = 6;
final int ifk_column_name = 7;
final int ikey_seq = 8;
final int iupdate_rule = 9;
final int idelete_rule = 10;
final int ifk_name = 11;
final int ipk_name = 12;
final int ideferrability = 13;
tables =
database.schemaManager.databaseObjectIterator(SchemaObject.TABLE);
// We must consider all the constraints in all the user tables, since
// this is where reference relationships are recorded. However, we
// are only concerned with Constraint.FOREIGN_KEY constraints here
// because their corresponing Constraint.MAIN entries are essentially
// duplicate data recorded in the referenced rather than the
// referencing table. Also, we skip constraints where either
// the referenced, referencing or both tables are not accessible
// relative to the session of the calling context
fkConstraintsList = new HsqlArrayList();
while (tables.hasNext()) {
table = (Table) tables.next();
if (table.isView() || !isAccessibleTable(session, table)) {
continue;
}
constraints = table.getConstraints();
constraintCount = constraints.length;
for (int i = 0; i < constraintCount; i++) {
constraint = (Constraint) constraints[i];
if (constraint.getConstraintType() == SchemaObject
.ConstraintTypes
.FOREIGN_KEY && isAccessibleTable(session, constraint
.getRef())) {
fkConstraintsList.add(constraint);
}
}
}
// Now that we have all of the desired constraints, we need to
// process them, generating one row in our ouput table for each
// imported/exported column pair of each constraint.
// Do it.
for (int i = 0; i < fkConstraintsList.size(); i++) {
constraint = (Constraint) fkConstraintsList.get(i);
pkTable = constraint.getMain();
pkTableName = pkTable.getName().name;
fkTable = constraint.getRef();
fkTableName = fkTable.getName().name;
pkTableCatalog = pkTable.getCatalogName().name;
pkTableSchema = pkTable.getSchemaName().name;
fkTableCatalog = fkTable.getCatalogName().name;
fkTableSchema = fkTable.getSchemaName().name;
mainCols = constraint.getMainColumns();
refCols = constraint.getRefColumns();
columnCount = refCols.length;
fkName = constraint.getRefName().name;
pkName = constraint.getMainName().name;
deferrability = ValuePool.getInt(constraint.getDeferability());
//pkName = constraint.getMainIndex().getName().name;
deleteRule = ValuePool.getInt(constraint.getDeleteAction());
updateRule = ValuePool.getInt(constraint.getUpdateAction());
for (int j = 0; j < columnCount; j++) {
keySequence = ValuePool.getInt(j + 1);
pkColumnName = pkTable.getColumn(mainCols[j]).getNameString();
fkColumnName = fkTable.getColumn(refCols[j]).getNameString();