use of org.hsqldb_voltpatches.persist.PersistentStore in project voltdb by VoltDB.
the class DatabaseInformationFull method USAGE_PRIVILEGES.
/**
* The USAGE_PRIVILEGES view has one row for each usage privilege
* descriptor. <p>
*
* It effectively contains a representation of the usage privilege
* descriptors. <p>
*
* <b>Definition:</b> <p>
*
* <pre class="SqlCodeExample">
* CREATE TABLE SYSTEM_USAGE_PRIVILEGES (
* GRANTOR VARCHAR NOT NULL,
* GRANTEE VARCHAR NOT NULL,
* OBJECT_CATALOG VARCHAR NULL,
* OBJECT_SCHEMA VARCHAR NULL,
* OBJECT_NAME VARCHAR NOT NULL,
* OBJECT_TYPE VARCHAR NOT NULL
*
* CHECK ( OBJECT_TYPE IN (
* 'DOMAIN',
* 'CHARACTER SET',
* 'COLLATION',
* 'TRANSLATION',
* 'SEQUENCE' ) ),
*
* IS_GRANTABLE VARCHAR NOT NULL
*
* CHECK ( IS_GRANTABLE IN ( 'YES', 'NO' ) ),
*
* UNIQUE( GRANTOR, GRANTEE, OBJECT_CATALOG,
* OBJECT_SCHEMA, OBJECT_NAME, OBJECT_TYPE )
* )
* </pre>
*
* <b>Description:</b><p>
*
* <ol>
* <li> The value of GRANTOR is the <authorization identifier> of the
* user or role who granted usage privileges on the object of the type
* identified by OBJECT_TYPE that is identified by OBJECT_CATALOG,
* OBJECT_SCHEMA, and OBJECT_NAME, to the user or role identified by the
* value of GRANTEE forthe usage privilege being described. <p>
*
* <li> The value of GRANTEE is the <authorization identifier> of some
* user or role, or PUBLIC to indicate all users, to whom the usage
* privilege being described is granted. <p>
*
* <li> The values of OBJECT_CATALOG, OBJECT_SCHEMA, and OBJECT_NAME are the
* catalog name, unqualified schema name, and qualified identifier,
* respectively, of the object to which the privilege applies. <p>
*
* <li> The values of OBJECT_TYPE have the following meanings: <p>
*
* <table border cellpadding="3">
* <tr>
* <td nowrap>DOMAIN</td>
* <td nowrap>The object to which the privilege applies is
* a domain.</td>
* <tr>
* <tr>
* <td nowrap>CHARACTER SET</td>
* <td nowrap>The object to which the privilege applies is a
* character set.</td>
* <tr>
* <tr>
* <td nowrap>COLLATION</td>
* <td nowrap>The object to which the privilege applies is a
* collation.</td>
* <tr>
* <tr>
* <td nowrap>TRANSLATION</td>
* <td nowrap>The object to which the privilege applies is a
* transliteration.</td>
* <tr>
* <tr>
* <td nowrap>SEQUENCE</td>
* <td nowrap>The object to which the privilege applies is a
* sequence generator.</td>
* <tr>
* </table> <p>
*
* <li> The values of IS_GRANTABLE have the following meanings: <p>
*
* <table border cellpadding="3">
* <tr>
* <td nowrap>YES</td>
* <td nowrap>The privilege being described was granted
* WITH GRANT OPTION and is thus grantable.</td>
* <tr>
* <tr>
* <td nowrap>NO</td>
* <td nowrap>The privilege being described was not granted
* WITH GRANT OPTION and is thus not grantable.</td>
* <tr>
* </table> <p>
* <ol>
*
* @return Table
*/
Table USAGE_PRIVILEGES() {
Table t = sysTables[USAGE_PRIVILEGES];
if (t == null) {
t = createBlankTable(sysTableHsqlNames[USAGE_PRIVILEGES]);
// not null
addColumn(t, "GRANTOR", SQL_IDENTIFIER);
// not null
addColumn(t, "GRANTEE", SQL_IDENTIFIER);
addColumn(t, "OBJECT_CATALOG", SQL_IDENTIFIER);
addColumn(t, "OBJECT_SCHEMA", SQL_IDENTIFIER);
// not null
addColumn(t, "OBJECT_NAME", SQL_IDENTIFIER);
// not null
addColumn(t, "OBJECT_TYPE", CHARACTER_DATA);
addColumn(t, "PRIVILEGE_TYPE", CHARACTER_DATA);
// not null
addColumn(t, "IS_GRANTABLE", YES_OR_NO);
// order: COLUMN_NAME, PRIVILEGE
// for unique: GRANTEE, GRANTOR, TABLE_NAME, TABLE_SCHEM, TABLE_CAT
// false PK, as TABLE_SCHEM and/or TABLE_CAT may be null
HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[USAGE_PRIVILEGES].name, false, SchemaObject.INDEX);
t.createPrimaryKey(name, new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }, false);
return t;
}
//
Object[] row;
//
final int grantor = 0;
final int grantee = 1;
final int object_catalog = 2;
final int object_schema = 3;
final int object_name = 4;
final int object_type = 5;
final int privilege_type = 6;
final int is_grantable = 7;
PersistentStore store = database.persistentStoreCollection.getStore(t);
Iterator objects = new WrapperIterator(database.schemaManager.databaseObjectIterator(SchemaObject.SEQUENCE), database.schemaManager.databaseObjectIterator(SchemaObject.COLLATION));
objects = new WrapperIterator(objects, database.schemaManager.databaseObjectIterator(SchemaObject.CHARSET));
objects = new WrapperIterator(objects, database.schemaManager.databaseObjectIterator(SchemaObject.DOMAIN));
/*
objects = new WrapperIterator(
objects,
database.schemaManager.databaseObjectIterator(SchemaObject.TYPE));
*/
OrderedHashSet grantees = session.getGrantee().getGranteeAndAllRolesWithPublic();
while (objects.hasNext()) {
SchemaObject object = (SchemaObject) objects.next();
for (int i = 0; i < grantees.size(); i++) {
Grantee granteeObject = (Grantee) grantees.get(i);
OrderedHashSet rights = granteeObject.getAllDirectPrivileges(object);
OrderedHashSet grants = granteeObject.getAllGrantedPrivileges(object);
if (!grants.isEmpty()) {
grants.addAll(rights);
rights = grants;
}
for (int j = 0; j < rights.size(); j++) {
Right right = (Right) rights.get(j);
Right grantableRight = right.getGrantableRights();
row = t.getEmptyRowData();
row[grantor] = right.getGrantor().getName().name;
row[grantee] = right.getGrantee().getName().name;
row[object_catalog] = database.getCatalogName().name;
row[object_schema] = object.getSchemaName().name;
row[object_name] = object.getName().name;
row[object_type] = SchemaObjectSet.getName(object.getName().type);
row[privilege_type] = Tokens.T_USAGE;
row[is_grantable] = right.getGrantee() == object.getOwner() || grantableRight.isFull() ? Tokens.T_YES : Tokens.T_NO;
;
try {
t.insertSys(store, row);
} catch (HsqlException e) {
}
}
}
}
return t;
}
use of org.hsqldb_voltpatches.persist.PersistentStore in project voltdb by VoltDB.
the class DatabaseInformationFull method PARAMETERS.
Table PARAMETERS() {
Table t = sysTables[PARAMETERS];
if (t == null) {
t = createBlankTable(sysTableHsqlNames[PARAMETERS]);
addColumn(t, "SPECIFIC_CATALOG", SQL_IDENTIFIER);
addColumn(t, "SPECIFIC_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "SPECIFIC_NAME", SQL_IDENTIFIER);
addColumn(t, "ORDINAL_POSITION", CARDINAL_NUMBER);
addColumn(t, "IS_RESULT", YES_OR_NO);
addColumn(t, "AS_LOCATOR", YES_OR_NO);
addColumn(t, "PARAMETER_NAME", SQL_IDENTIFIER);
//
addColumn(t, "FROM_SQL_SPECIFIC_CATALOG", SQL_IDENTIFIER);
addColumn(t, "FROM_SQL_SPECIFIC_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "FROM_SQL_SPECIFIC_NAME", SQL_IDENTIFIER);
//
addColumn(t, "TO_SQL_SPECIFIC_CATALOG", SQL_IDENTIFIER);
addColumn(t, "TO_SQL_SPECIFIC_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "TO_SQL_SPECIFIC_NAME", SQL_IDENTIFIER);
//
addColumn(t, "DATA_TYPE", CHARACTER_DATA);
addColumn(t, "CHARACTER_MAXIMUM_LENGTH", CARDINAL_NUMBER);
addColumn(t, "CHARACTER_OCTET_LENGTH", CARDINAL_NUMBER);
addColumn(t, "CHARACTER_SET_CATALOG", CHARACTER_DATA);
addColumn(t, "CHARACTER_SET_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "CHARACTER_SET_NAME", SQL_IDENTIFIER);
addColumn(t, "COLLATION_CATALOG", SQL_IDENTIFIER);
addColumn(t, "COLLATION_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "COLLATION_NAME", SQL_IDENTIFIER);
addColumn(t, "NUMERIC_PRECISION", CARDINAL_NUMBER);
addColumn(t, "NUMERIC_PRECISION_RADIX", CARDINAL_NUMBER);
addColumn(t, "NUMERIC_SCALE", CARDINAL_NUMBER);
addColumn(t, "DATETIME_PRECISION", CARDINAL_NUMBER);
addColumn(t, "INTERVAL_TYPE", CHARACTER_DATA);
addColumn(t, "INTERVAL_PRECISION", CARDINAL_NUMBER);
addColumn(t, "UDT_CATALOG", SQL_IDENTIFIER);
addColumn(t, "UDT_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "UDT_NAME", SQL_IDENTIFIER);
addColumn(t, "SCOPE_CATALOG", SQL_IDENTIFIER);
addColumn(t, "SCOPE_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "SCOPE_NAME", SQL_IDENTIFIER);
// NULL
addColumn(t, "MAXIMUM_CARDINALITY", CARDINAL_NUMBER);
addColumn(t, "DTD_IDENTIFIER", SQL_IDENTIFIER);
addColumn(t, "DECLARED_DATA_TYPE", CHARACTER_DATA);
addColumn(t, "DECLARED_NUMERIC_PRECISION", CARDINAL_NUMBER);
addColumn(t, "DECLARED_NUMERIC_SCALE", CARDINAL_NUMBER);
HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[PARAMETERS].name, false, SchemaObject.INDEX);
t.createPrimaryKey(name, new int[] { 0, 1, 2 }, false);
return t;
}
PersistentStore store = database.persistentStoreCollection.getStore(t);
// column number mappings
final int specific_cat = 0;
final int specific_schem = 1;
final int specific_name = 2;
final int ordinal_position = 3;
final int is_result = 4;
final int as_locator = 5;
final int parameter_name = 6;
final int from_specific_catalog = 7;
final int from_specific_schema = 8;
final int from_specific_name = 9;
final int to_specific_catalog = 10;
final int to_specific_schema = 11;
final int to_specific_name = 12;
final int data_type = 13;
final int character_maximum_length = 14;
final int character_octet_length = 15;
final int character_set_catalog = 16;
final int character_set_schema = 17;
final int character_set_name = 18;
final int collation_catalog = 19;
final int collation_schema = 20;
final int collation_name = 21;
final int numeric_precision = 22;
final int numeric_precision_radix = 23;
final int numeric_scale = 24;
final int datetime_precision = 25;
final int interval_type = 26;
final int interval_precision = 27;
final int udt_catalog = 28;
final int udt_schema = 29;
final int udt_name = 30;
final int scope_catalog = 31;
final int scope_schema = 32;
final int scope_name = 33;
final int maximum_cardinality = 34;
final int dtd_identifier = 35;
final int declared_data_type = 36;
final int declared_numeric_precision = 37;
final int declared_numeric_scale = 38;
return t;
}
use of org.hsqldb_voltpatches.persist.PersistentStore in project voltdb by VoltDB.
the class DatabaseInformationMain method SYSTEM_USERS.
/**
* Retrieves a <code>Table</code> object describing the
* visible <code>Users</code> defined within this database.
* @return table containing information about the users defined within
* this database
*/
Table SYSTEM_USERS() {
Table t = sysTables[SYSTEM_USERS];
if (t == null) {
t = createBlankTable(sysTableHsqlNames[SYSTEM_USERS]);
addColumn(t, "USER_NAME", SQL_IDENTIFIER);
addColumn(t, "ADMIN", Type.SQL_BOOLEAN);
addColumn(t, "INITIAL_SCHEMA", SQL_IDENTIFIER);
// order: USER
// true PK
HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[SYSTEM_USERS].name, false, SchemaObject.INDEX);
t.createPrimaryKey(name, new int[] { 0 }, true);
return t;
}
PersistentStore store = database.persistentStoreCollection.getStore(t);
// Intermediate holders
HsqlArrayList users;
User user;
Object[] row;
HsqlName initialSchema;
// Initialization
users = database.getUserManager().listVisibleUsers(session);
// Do it.
for (int i = 0; i < users.size(); i++) {
row = t.getEmptyRowData();
user = (User) users.get(i);
initialSchema = user.getInitialSchema();
row[0] = user.getNameString();
row[1] = ValuePool.getBoolean(user.isAdmin());
row[2] = ((initialSchema == null) ? null : initialSchema.name);
t.insertSys(store, row);
}
return t;
}
use of org.hsqldb_voltpatches.persist.PersistentStore in project voltdb by VoltDB.
the class DatabaseInformationMain method SEQUENCES.
/**
* The SEQUENCES view has one row for each external sequence
* generator. <p>
*
* <b>Definition:</b> <p>
*
* <pre class="SqlCodeExample">
*
* SEQUENCE_CATALOG VARCHAR NULL,
* SEQUENCE_SCHEMA VARCHAR NULL,
* SEQUENCE_NAME VARCHAR NOT NULL,
* DATA_TYPE CHARACTER_DATA
* DATA_TYPE CHARACTER_DATA
* NUMERIC_PRECISION CARDINAL_NUMBER
* NUMERIC_PRECISION_RADIX CARDINAL_NUMBER
* NUMERIC_SCALE CARDINAL_NUMBER
* MAXIMUM_VALUE VARCHAR NOT NULL,
* MINIMUM_VALUE VARCHAR NOT NULL,
* INCREMENT VARCHAR NOT NULL,
* CYCLE_OPTION VARCHAR {'YES', 'NO'},
* START_WITH VARCHAR NOT NULL,
* DECLARED_DATA_TYPE CHARACTER_DATA
* DECLARED_NUMERIC_PRECISION CARDINAL_NUMBER
* DECLARED_NUMERIC_SCLAE CARDINAL_NUMBER
*
* </pre>
*
* <b>DESCRIPTION:</b><p>
*
* <ol>
* <li> The values of SEQUENCE_CATALOG, SEQUENCE_SCHEMA, and
* SEQUENCE_NAME are the catalog name, unqualified schema name,
* and qualified identifier, respectively, of the sequence generator
* being described. <p>
*
* <li> The values of SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME, and
* DTD_IDENTIFIER are the values of OBJECT_CATALOG, OBJECT_SCHEMA,
* OBJECT_NAME, and DTD_IDENTIFIER, respectively, of the row in
* DATA_TYPE_DESCRIPTOR (not yet implemented) that describes the data
* type of the sequence generator. <p>
*
* <li> The values of MAXIMUM_VALUE, MINIMUM_VALUE, and INCREMENT are the
* character representations of maximum value, minimum value,
* and increment, respectively, of the sequence generator being
* described. <p>
*
* <li> The values of CYCLE_OPTION have the following meanings: <p>
*
* <table border cellpadding="3">
* <tr>
* <td nowrap>YES</td>
* <td nowrap>The cycle option of the sequence generator
* is CYCLE.</td>
* <tr>
* <td nowrap>NO</td>
* <td nowrap>The cycle option of the sequence generator is
* NO CYCLE.</td>
* </tr>
* </table> <p>
*
* <li> The value of START_WITH is HSQLDB-specific (not in the SQL 200n
* spec). <p>
*
* It is the character representation of the START WITH value. <p>
*
* <li> The value of NEXT_VALUE is HSQLDB-specific (not in the SQL 200n)<p>
* This is the character representation of the value that
* would be generated by NEXT VALUE FOR when this sequence
* is materialized in an SQL statement. <p>
* </ol>
*
* @return Table
*/
final Table SEQUENCES() {
Table t = sysTables[SEQUENCES];
if (t == null) {
t = createBlankTable(sysTableHsqlNames[SEQUENCES]);
addColumn(t, "SEQUENCE_CATALOG", SQL_IDENTIFIER);
addColumn(t, "SEQUENCE_SCHEMA", SQL_IDENTIFIER);
addColumn(t, "SEQUENCE_NAME", SQL_IDENTIFIER);
addColumn(t, "DATA_TYPE", CHARACTER_DATA);
addColumn(t, "NUMERIC_PRECISION", CARDINAL_NUMBER);
addColumn(t, "NUMERIC_PRECISION_RADIX", CARDINAL_NUMBER);
addColumn(t, "NUMERIC_SCALE", CARDINAL_NUMBER);
addColumn(t, "MAXIMUM_VALUE", CHARACTER_DATA);
addColumn(t, "MINIMUM_VALUE", CHARACTER_DATA);
addColumn(t, "INCREMENT", CHARACTER_DATA);
addColumn(t, "CYCLE_OPTION", YES_OR_NO);
addColumn(t, "DECLARED_DATA_TYPE", CHARACTER_DATA);
addColumn(t, "DECLARED_NUMERIC_PRECISION", CARDINAL_NUMBER);
addColumn(t, "DECLARED_NUMERIC_SCLAE", CARDINAL_NUMBER);
// HSQLDB-specific
addColumn(t, "START_WITH", CHARACTER_DATA);
addColumn(t, "NEXT_VALUE", CHARACTER_DATA);
// order SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME
// false PK, as CATALOG may be null
HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[SEQUENCES].name, false, SchemaObject.INDEX);
t.createPrimaryKey(name, new int[] { 0, 1, 2 }, false);
return t;
}
PersistentStore store = database.persistentStoreCollection.getStore(t);
//
final int sequence_catalog = 0;
final int sequence_schema = 1;
final int sequence_name = 2;
final int data_type = 3;
final int numeric_precision = 4;
final int numeric_precision_radix = 5;
final int numeric_scale = 6;
final int maximum_value = 7;
final int minimum_value = 8;
final int increment = 9;
final int cycle_option = 10;
final int declared_data_type = 11;
final int declared_numeric_precision = 12;
final int declared_numeric_scale = 13;
final int start_with = 14;
final int next_value = 15;
//
Iterator it;
Object[] row;
NumberSequence sequence;
it = database.schemaManager.databaseObjectIterator(SchemaObject.SEQUENCE);
while (it.hasNext()) {
sequence = (NumberSequence) it.next();
if (!session.getGrantee().isAccessible(sequence)) {
continue;
}
row = t.getEmptyRowData();
NumberType type = (NumberType) sequence.getDataType();
int radix = (type.typeCode == Types.SQL_NUMERIC || type.typeCode == Types.SQL_DECIMAL) ? 10 : 2;
row[sequence_catalog] = database.getCatalogName().name;
row[sequence_schema] = sequence.getSchemaName().name;
row[sequence_name] = sequence.getName().name;
row[data_type] = sequence.getDataType().getFullNameString();
row[numeric_precision] = ValuePool.getInt((int) type.getPrecision());
row[numeric_precision_radix] = ValuePool.getInt(radix);
row[numeric_scale] = ValuePool.INTEGER_0;
row[maximum_value] = String.valueOf(sequence.getMaxValue());
row[minimum_value] = String.valueOf(sequence.getMinValue());
row[increment] = String.valueOf(sequence.getIncrement());
row[cycle_option] = sequence.isCycle() ? "YES" : "NO";
row[declared_data_type] = row[data_type];
row[declared_numeric_precision] = row[numeric_precision];
row[declared_numeric_scale] = row[declared_numeric_scale];
row[start_with] = String.valueOf(sequence.getStartValue());
row[next_value] = String.valueOf(sequence.peek());
t.insertSys(store, row);
}
return t;
}
use of org.hsqldb_voltpatches.persist.PersistentStore in project voltdb by VoltDB.
the class DatabaseInformationMain method SYSTEM_CROSSREFERENCE.
/**
* Retrieves a <code>Table</code> object describing, for each
* accessible referencing and referenced table, how the referencing
* tables import, for the purposes of referential integrity,
* the columns of the referenced tables.<p>
*
* Each row is a foreign key column description with the following
* columns: <p>
*
* <pre class="SqlCodeExample">
* PKTABLE_CAT VARCHAR referenced table catalog
* PKTABLE_SCHEM VARCHAR referenced table schema
* PKTABLE_NAME VARCHAR referenced table name
* PKCOLUMN_NAME VARCHAR referenced column name
* FKTABLE_CAT VARCHAR referencing table catalog
* FKTABLE_SCHEM VARCHAR referencing table schema
* FKTABLE_NAME VARCHAR referencing table name
* FKCOLUMN_NAME VARCHAR referencing column
* KEY_SEQ SMALLINT sequence number within foreign key
* UPDATE_RULE SMALLINT
* { Cascade | Set Null | Set Default | Restrict (No Action)}?
* DELETE_RULE SMALLINT
* { Cascade | Set Null | Set Default | Restrict (No Action)}?
* FK_NAME VARCHAR foreign key constraint name
* PK_NAME VARCHAR primary key or unique constraint name
* DEFERRABILITY SMALLINT
* { initially deferred | initially immediate | not deferrable }
* </pre> <p>
*
* @return a <code>Table</code> object describing how accessible tables
* import other accessible tables' primary key and/or unique
* constraint columns
*/
final Table SYSTEM_CROSSREFERENCE() {
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);
// not null
addColumn(t, "PKTABLE_NAME", SQL_IDENTIFIER);
// not null
addColumn(t, "PKCOLUMN_NAME", SQL_IDENTIFIER);
addColumn(t, "FKTABLE_CAT", SQL_IDENTIFIER);
addColumn(t, "FKTABLE_SCHEM", SQL_IDENTIFIER);
// not null
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);
addColumn(t, "FK_NAME", SQL_IDENTIFIER);
addColumn(t, "PK_NAME", SQL_IDENTIFIER);
// not null
addColumn(t, "DEFERRABILITY", Type.SQL_SMALLINT);
// 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.createPrimaryKey(name, new int[] { 4, 5, 6, 8, 11 }, false);
return t;
}
PersistentStore store = database.persistentStoreCollection.getStore(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;
DITableInfo pkInfo;
DITableInfo fkInfo;
// 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);
pkInfo = new DITableInfo();
fkInfo = new DITableInfo();
// 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(table)) {
continue;
}
constraints = table.getConstraints();
constraintCount = constraints.length;
for (int i = 0; i < constraintCount; i++) {
constraint = (Constraint) constraints[i];
if (constraint.getConstraintType() == Constraint.FOREIGN_KEY && isAccessibleTable(constraint.getRef())) {
fkConstraintsList.add(constraint);
}
}
}
// Do it.
for (int i = 0; i < fkConstraintsList.size(); i++) {
constraint = (Constraint) fkConstraintsList.get(i);
pkTable = constraint.getMain();
pkInfo.setTable(pkTable);
pkTableName = pkInfo.getName();
fkTable = constraint.getRef();
fkInfo.setTable(fkTable);
fkTableName = fkInfo.getName();
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 = pkInfo.getColName(mainCols[j]);
fkColumnName = fkInfo.getColName(refCols[j]);
row = t.getEmptyRowData();
row[ipk_table_cat] = pkTableCatalog;
row[ipk_table_schem] = pkTableSchema;
row[ipk_table_name] = pkTableName;
row[ipk_column_name] = pkColumnName;
row[ifk_table_cat] = fkTableCatalog;
row[ifk_table_schem] = fkTableSchema;
row[ifk_table_name] = fkTableName;
row[ifk_column_name] = fkColumnName;
row[ikey_seq] = keySequence;
row[iupdate_rule] = updateRule;
row[idelete_rule] = deleteRule;
row[ifk_name] = fkName;
row[ipk_name] = pkName;
row[ideferrability] = deferrability;
t.insertSys(store, row);
}
}
return t;
}
Aggregations