Search in sources :

Example 66 with Table

use of org.hsqldb_voltpatches.Table in project voltdb by VoltDB.

the class DatabaseInformationFull method KEY_COLUMN_USAGE.

/**
     * Retrieves a <code>Table</code> object describing the
     * primary key and unique constraint columns of each accessible table
     * defined within this database. <p>
     *
     * Each row is a PRIMARY KEY or UNIQUE column description with the following
     * columns: <p>
     *
     * <pre class="SqlCodeExample">
     * CONSTRAINT_CATALOG              VARCHAR NULL,
     * CONSTRAINT_SCHEMA               VARCHAR NULL,
     * CONSTRAINT_NAME                 VARCHAR NOT NULL,
     * TABLE_CATALOG                   VARCHAR   table catalog
     * TABLE_SCHEMA                    VARCHAR   table schema
     * TABLE_NAME                      VARCHAR   table name
     * COLUMN_NAME                     VARCHAR   column name
     * ORDINAL_POSITION                INT
     * POSITION_IN_UNIQUE_CONSTRAINT   INT
     * </pre> <p>
     *
     * @return a <code>Table</code> object describing the visible
     *        primary key and unique columns of each accessible table
     *        defined within this database.
     */
Table KEY_COLUMN_USAGE() {
    Table t = sysTables[KEY_COLUMN_USAGE];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[KEY_COLUMN_USAGE]);
        addColumn(t, "CONSTRAINT_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "CONSTRAINT_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "CONSTRAINT_NAME", SQL_IDENTIFIER);
        addColumn(t, "TABLE_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "TABLE_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "TABLE_NAME", SQL_IDENTIFIER);
        // not null
        addColumn(t, "COLUMN_NAME", SQL_IDENTIFIER);
        // not null
        addColumn(t, "ORDINAL_POSITION", CARDINAL_NUMBER);
        // not null
        addColumn(t, "POSITION_IN_UNIQUE_CONSTRAINT", CARDINAL_NUMBER);
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[KEY_COLUMN_USAGE].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 2, 1, 0, 6, 7 }, false);
        return t;
    }
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    // Intermediate holders
    Iterator tables;
    Object[] row;
    // column number mappings
    final int constraint_catalog = 0;
    final int constraint_schema = 1;
    final int constraint_name = 2;
    final int table_catalog = 3;
    final int table_schema = 4;
    final int table_name = 5;
    final int column_name = 6;
    final int ordinal_position = 7;
    final int position_in_unique_constraint = 8;
    // Initialization
    tables = database.schemaManager.databaseObjectIterator(SchemaObject.TABLE);
    while (tables.hasNext()) {
        Table table = (Table) tables.next();
        String tableCatalog = database.getCatalogName().name;
        String tableSchema = table.getSchemaName().name;
        String tableName = table.getName().name;
        /** @todo - requires access to the actual columns */
        if (table.isView() || !isAccessibleTable(table)) {
            continue;
        }
        Constraint[] constraints = table.getConstraints();
        for (int i = 0; i < constraints.length; i++) {
            Constraint constraint = constraints[i];
            if (constraint.getConstraintType() == Constraint.PRIMARY_KEY || constraint.getConstraintType() == Constraint.UNIQUE || constraint.getConstraintType() == Constraint.FOREIGN_KEY) {
                String constraintName = constraint.getName().name;
                int[] cols = constraint.getMainColumns();
                int[] uniqueColMap = null;
                if (constraint.getConstraintType() == Constraint.FOREIGN_KEY) {
                    Table uniqueConstTable = constraint.getMain();
                    Constraint uniqueConstraint = uniqueConstTable.getConstraint(constraint.getUniqueName().name);
                    int[] uniqueConstIndexes = uniqueConstraint.getMainColumns();
                    uniqueColMap = new int[cols.length];
                    for (int j = 0; j < cols.length; j++) {
                        uniqueColMap[j] = ArrayUtil.find(uniqueConstIndexes, cols[j]);
                    }
                    cols = constraint.getRefColumns();
                }
                for (int j = 0; j < cols.length; j++) {
                    row = t.getEmptyRowData();
                    row[constraint_catalog] = tableCatalog;
                    row[constraint_schema] = tableSchema;
                    row[constraint_name] = constraintName;
                    row[table_catalog] = tableCatalog;
                    row[table_schema] = tableSchema;
                    row[table_name] = tableName;
                    row[column_name] = table.getColumn(cols[j]).getName().name;
                    row[ordinal_position] = ValuePool.getInt(j + 1);
                    if (constraint.getConstraintType() == Constraint.FOREIGN_KEY) {
                        row[position_in_unique_constraint] = ValuePool.getInt(uniqueColMap[j] + 1);
                    }
                    t.insertSys(store, row);
                }
            }
        }
    }
    return t;
}
Also used : Table(org.hsqldb_voltpatches.Table) TextTable(org.hsqldb_voltpatches.TextTable) Constraint(org.hsqldb_voltpatches.Constraint) Iterator(org.hsqldb_voltpatches.lib.Iterator) WrapperIterator(org.hsqldb_voltpatches.lib.WrapperIterator) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) Constraint(org.hsqldb_voltpatches.Constraint)

Example 67 with Table

use of org.hsqldb_voltpatches.Table in project voltdb by VoltDB.

the class DatabaseInformationFull method DATA_TYPE_PRIVILEGES.

Table DATA_TYPE_PRIVILEGES() {
    Table t = sysTables[DATA_TYPE_PRIVILEGES];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[DATA_TYPE_PRIVILEGES]);
        addColumn(t, "OBJECT_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "OBJECT_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "OBJECT_NAME", SQL_IDENTIFIER);
        addColumn(t, "OBJECT_TYPE", SQL_IDENTIFIER);
        addColumn(t, "DTD_IDENTIFIER", SQL_IDENTIFIER);
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[DATA_TYPE_PRIVILEGES].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1, 2, 3, 4 }, false);
        return t;
    }
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    //
    Session sys = database.sessionManager.newSysSession(SqlInvariants.INFORMATION_SCHEMA_HSQLNAME, session.getUser());
    String sql = (String) statementMap.get("/*data_type_privileges*/");
    Result rs = sys.executeDirectStatement(sql);
    t.insertSys(store, rs);
    sys.close();
    return t;
}
Also used : Table(org.hsqldb_voltpatches.Table) TextTable(org.hsqldb_voltpatches.TextTable) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) Session(org.hsqldb_voltpatches.Session) Result(org.hsqldb_voltpatches.result.Result)

Example 68 with Table

use of org.hsqldb_voltpatches.Table in project voltdb by VoltDB.

the class DatabaseInformationFull method ROLE_TABLE_GRANTS.

Table ROLE_TABLE_GRANTS() {
    Table t = sysTables[ROLE_TABLE_GRANTS];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[ROLE_TABLE_GRANTS]);
        // not null
        addColumn(t, "GRANTOR", SQL_IDENTIFIER);
        // not null
        addColumn(t, "GRANTEE", SQL_IDENTIFIER);
        addColumn(t, "TABLE_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "TABLE_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "TABLE_NAME", SQL_IDENTIFIER);
        // not null
        addColumn(t, "PRIVILEGE_TYPE", CHARACTER_DATA);
        // not null
        addColumn(t, "IS_GRANTABLE", YES_OR_NO);
        addColumn(t, "WITH_HIERARCHY", YES_OR_NO);
        // order:  TABLE_SCHEM, TABLE_NAME, and PRIVILEGE,
        // added for unique:  GRANTEE, GRANTOR,
        // false PK, as TABLE_SCHEM and/or TABLE_CAT may be null
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[ROLE_TABLE_GRANTS].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 3, 4, 5, 0, 1 }, false);
        return t;
    }
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    Session sys = database.sessionManager.newSysSession(SqlInvariants.INFORMATION_SCHEMA_HSQLNAME, session.getUser());
    Result rs = sys.executeDirectStatement("SELECT GRANTOR, GRANTEE, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, PRIVILEGE_TYPE, IS_GRANTABLE, 'NO' " + "FROM INFORMATION_SCHEMA.TABLE_PRIVILEGES " + "JOIN INFORMATION_SCHEMA.APPLICABLE_ROLES ON GRANTEE = ROLE_NAME;");
    t.insertSys(store, rs);
    sys.close();
    return t;
}
Also used : Table(org.hsqldb_voltpatches.Table) TextTable(org.hsqldb_voltpatches.TextTable) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) Session(org.hsqldb_voltpatches.Session) Result(org.hsqldb_voltpatches.result.Result)

Example 69 with Table

use of org.hsqldb_voltpatches.Table in project voltdb by VoltDB.

the class DatabaseInformationFull method ROUTINE_ROUTINE_USAGE.

/**
     * needs to provide list of specific referenced routines
     */
Table ROUTINE_ROUTINE_USAGE() {
    Table t = sysTables[ROUTINE_ROUTINE_USAGE];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[ROUTINE_ROUTINE_USAGE]);
        addColumn(t, "SPECIFIC_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "SPECIFIC_SCHEMA", SQL_IDENTIFIER);
        addColumn(t, "SPECIFIC_NAME", SQL_IDENTIFIER);
        addColumn(t, "ROUTINE_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "ROUTINE_SCHEMA", SQL_IDENTIFIER);
        addColumn(t, "ROUTINE_NAME", SQL_IDENTIFIER);
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[ROUTINE_ROUTINE_USAGE].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1, 2, 3, 4, 5 }, false);
        return t;
    }
    // column number mappings
    final int specific_catalog = 0;
    final int specific_schema = 1;
    final int specific_name = 2;
    final int routine_catalog = 3;
    final int routine_schema = 4;
    final int routine_name = 5;
    //
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    Iterator it;
    Object[] row;
    it = database.schemaManager.databaseObjectIterator(SchemaObject.ROUTINE);
    while (it.hasNext()) {
        RoutineSchema routine = (RoutineSchema) it.next();
        if (!session.getGrantee().isAccessible(routine)) {
            continue;
        }
        Routine[] specifics = routine.getSpecificRoutines();
        for (int m = 0; m < specifics.length; m++) {
            OrderedHashSet set = specifics[m].getReferences();
            for (int i = 0; i < set.size(); i++) {
                HsqlName refName = (HsqlName) set.get(i);
                if (refName.type != SchemaObject.FUNCTION && refName.type != SchemaObject.PROCEDURE) {
                    continue;
                }
                if (!session.getGrantee().isAccessible(refName)) {
                    continue;
                }
                row = t.getEmptyRowData();
                row[specific_catalog] = database.getCatalogName().name;
                row[specific_schema] = specifics[m].getSchemaName().name;
                row[specific_name] = specifics[m].getName().name;
                row[routine_catalog] = database.getCatalogName().name;
                row[routine_schema] = refName.schema.name;
                row[routine_name] = refName.name;
                try {
                    t.insertSys(store, row);
                } catch (HsqlException e) {
                }
            }
        }
    }
    return t;
}
Also used : Table(org.hsqldb_voltpatches.Table) TextTable(org.hsqldb_voltpatches.TextTable) Iterator(org.hsqldb_voltpatches.lib.Iterator) WrapperIterator(org.hsqldb_voltpatches.lib.WrapperIterator) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) Routine(org.hsqldb_voltpatches.Routine) Constraint(org.hsqldb_voltpatches.Constraint) HsqlException(org.hsqldb_voltpatches.HsqlException) RoutineSchema(org.hsqldb_voltpatches.RoutineSchema)

Example 70 with Table

use of org.hsqldb_voltpatches.Table in project voltdb by VoltDB.

the class DatabaseInformationFull method ROUTINE_TABLE_USAGE.

Table ROUTINE_TABLE_USAGE() {
    Table t = sysTables[ROUTINE_TABLE_USAGE];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[ROUTINE_TABLE_USAGE]);
        addColumn(t, "SPECIFIC_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "SPECIFIC_SCHEMA", SQL_IDENTIFIER);
        addColumn(t, "SPECIFIC_NAME", SQL_IDENTIFIER);
        addColumn(t, "ROUTINE_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "ROUTINE_SCHEMA", SQL_IDENTIFIER);
        addColumn(t, "ROUTINE_NAME", SQL_IDENTIFIER);
        addColumn(t, "TABLE_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "TABLE_SCHEMA", SQL_IDENTIFIER);
        addColumn(t, "TABLE_NAME", SQL_IDENTIFIER);
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[ROUTINE_TABLE_USAGE].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 3, 4, 5, 0, 1, 2, 6, 7, 8 }, false);
        return t;
    }
    // column number mappings
    final int specific_catalog = 0;
    final int specific_schema = 1;
    final int specific_name = 2;
    final int routine_catalog = 3;
    final int routine_schema = 4;
    final int routine_name = 5;
    final int table_catalog = 6;
    final int table_schema = 7;
    final int table_name = 8;
    //
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    Iterator it;
    Object[] row;
    it = database.schemaManager.databaseObjectIterator(SchemaObject.ROUTINE);
    while (it.hasNext()) {
        RoutineSchema routine = (RoutineSchema) it.next();
        if (!session.getGrantee().isAccessible(routine)) {
            continue;
        }
        Routine[] specifics = routine.getSpecificRoutines();
        for (int m = 0; m < specifics.length; m++) {
            OrderedHashSet set = specifics[m].getReferences();
            for (int i = 0; i < set.size(); i++) {
                HsqlName refName = (HsqlName) set.get(i);
                if (refName.type != SchemaObject.TABLE && refName.type != SchemaObject.VIEW) {
                    continue;
                }
                if (!session.getGrantee().isAccessible(refName)) {
                    continue;
                }
                row = t.getEmptyRowData();
                row[specific_catalog] = database.getCatalogName().name;
                row[specific_schema] = specifics[m].getSchemaName().name;
                row[specific_name] = specifics[m].getName().name;
                row[routine_catalog] = database.getCatalogName().name;
                row[routine_schema] = routine.getSchemaName().name;
                row[routine_name] = routine.getName().name;
                row[table_catalog] = database.getCatalogName().name;
                row[table_schema] = refName.schema.name;
                row[table_name] = refName.name;
                try {
                    t.insertSys(store, row);
                } catch (HsqlException e) {
                }
            }
        }
    }
    return t;
}
Also used : Table(org.hsqldb_voltpatches.Table) TextTable(org.hsqldb_voltpatches.TextTable) Iterator(org.hsqldb_voltpatches.lib.Iterator) WrapperIterator(org.hsqldb_voltpatches.lib.WrapperIterator) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) Routine(org.hsqldb_voltpatches.Routine) Constraint(org.hsqldb_voltpatches.Constraint) HsqlException(org.hsqldb_voltpatches.HsqlException) RoutineSchema(org.hsqldb_voltpatches.RoutineSchema)

Aggregations

Table (org.hsqldb_voltpatches.Table)94 HsqlName (org.hsqldb_voltpatches.HsqlNameManager.HsqlName)86 PersistentStore (org.hsqldb_voltpatches.persist.PersistentStore)74 TextTable (org.hsqldb_voltpatches.TextTable)65 Constraint (org.hsqldb_voltpatches.Constraint)61 SchemaObject (org.hsqldb_voltpatches.SchemaObject)60 Iterator (org.hsqldb_voltpatches.lib.Iterator)55 WrapperIterator (org.hsqldb_voltpatches.lib.WrapperIterator)54 HsqlException (org.hsqldb_voltpatches.HsqlException)20 OrderedHashSet (org.hsqldb_voltpatches.lib.OrderedHashSet)19 Session (org.hsqldb_voltpatches.Session)17 Result (org.hsqldb_voltpatches.result.Result)14 NumberType (org.hsqldb_voltpatches.types.NumberType)10 Type (org.hsqldb_voltpatches.types.Type)9 Grantee (org.hsqldb_voltpatches.rights.Grantee)8 Routine (org.hsqldb_voltpatches.Routine)7 RoutineSchema (org.hsqldb_voltpatches.RoutineSchema)7 HsqlArrayList (org.hsqldb_voltpatches.lib.HsqlArrayList)7 TriggerDef (org.hsqldb_voltpatches.TriggerDef)6 CharacterType (org.hsqldb_voltpatches.types.CharacterType)6