* import other accessible tables' primary key and/or unique
* constraint columns
*/
final Table SYSTEM_CROSSREFERENCE(Session session) {
Table t = sysTables[SYSTEM_CROSSREFERENCE];
if (t == null) {
t = createBlankTable(sysTableHsqlNames[SYSTEM_CROSSREFERENCE]);
addColumn(t, "PKTABLE_CAT", SQL_IDENTIFIER);
addColumn(t, "PKTABLE_SCHEM", SQL_IDENTIFIER);
addColumn(t, "PKTABLE_NAME", SQL_IDENTIFIER); // not null
addColumn(t, "PKCOLUMN_NAME", SQL_IDENTIFIER); // not null
addColumn(t, "FKTABLE_CAT", SQL_IDENTIFIER);
addColumn(t, "FKTABLE_SCHEM", SQL_IDENTIFIER);
addColumn(t, "FKTABLE_NAME", SQL_IDENTIFIER); // not null
addColumn(t, "FKCOLUMN_NAME", SQL_IDENTIFIER); // not null
addColumn(t, "KEY_SEQ", Type.SQL_SMALLINT); // not null
addColumn(t, "UPDATE_RULE", Type.SQL_SMALLINT); // not null
addColumn(t, "DELETE_RULE", Type.SQL_SMALLINT); // not null
addColumn(t, "FK_NAME", SQL_IDENTIFIER);
addColumn(t, "PK_NAME", SQL_IDENTIFIER);
addColumn(t, "DEFERRABILITY", Type.SQL_SMALLINT); // not null
// order: FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ
// added for unique: FK_NAME
// false PK, as FKTABLE_CAT, FKTABLE_SCHEM and/or FK_NAME
// may be null
HsqlName name = HsqlNameManager.newInfoSchemaObjectName(
sysTableHsqlNames[SYSTEM_CROSSREFERENCE].name, false,
SchemaObject.INDEX);
t.createPrimaryKeyConstraint(name, new int[] {
4, 5, 6, 8, 11
}, false);
return t;
}
PersistentStore store = session.sessionData.getRowStore(t);
// calculated column values
String pkTableCatalog;
String pkTableSchema;
String pkTableName;
String pkColumnName;
String fkTableCatalog;
String fkTableSchema;
String fkTableName;
String fkColumnName;
Integer keySequence;
Integer updateRule;
Integer deleteRule;
String fkName;
String pkName;
Integer deferrability;
// Intermediate holders
Iterator tables;
Table table;
Table fkTable;
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();
row = t.getEmptyRowData();
row[ipk_table_cat] = pkTableCatalog;
row[ipk_table_schem] = pkTableSchema;
row[ipk_table_name] = pkTableName;