Search in sources :

Example 1 with Table

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

the class DatabaseInformationFull method ROLE_ROUTINE_GRANTS.

Table ROLE_ROUTINE_GRANTS() {
    Table t = sysTables[ROLE_ROUTINE_GRANTS];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[ROLE_ROUTINE_GRANTS]);
        // not null
        addColumn(t, "GRANTOR", SQL_IDENTIFIER);
        // not null
        addColumn(t, "GRANTEE", SQL_IDENTIFIER);
        addColumn(t, "SPECIFIC_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "SPECIFIC_SCHEMA", SQL_IDENTIFIER);
        // not null
        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, "PRIVILEGE_TYPE", CHARACTER_DATA);
        addColumn(t, "IS_GRANTABLE", YES_OR_NO);
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[ROLE_ROUTINE_GRANTS].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 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, SPECIFIC_CATALOG, SPECIFIC_SCHEMA, " + "SPECIFIC_NAME, ROUTINE_CATALOG, ROUTINE_SCHEMA, ROUTINE_NAME, " + "PRIVILEGE_TYPE, IS_GRANTABLE, 'NO' " + "FROM INFORMATION_SCHEMA.ROUTINE_PRIVILEGES " + "JOIN INFORMATION_SCHEMA.APPLICABLE_ROLES ON GRANTEE = ROLE_NAME;");
    t.insertSys(store, rs);
    sys.close();
    // Column number mappings
    final int grantor = 0;
    final int grantee = 1;
    final int table_name = 2;
    final int specific_catalog = 3;
    final int specific_schema = 4;
    final int specific_name = 5;
    final int routine_catalog = 6;
    final int routine_schema = 7;
    final int routine_name = 8;
    final int privilege_type = 9;
    final int is_grantable = 10;
    //
    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) Constraint(org.hsqldb_voltpatches.Constraint) Session(org.hsqldb_voltpatches.Session) Result(org.hsqldb_voltpatches.result.Result)

Example 2 with Table

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

the class DatabaseInformationFull method SCHEMATA.

/**
     * SCHEMATA<p>
     *
     * <b>Function</b><p>
     *
     * The SCHEMATA view has one row for each accessible schema. <p>
     *
     * <b>Definition</b><p>
     *
     * <pre class="SqlCodeExample">
     * CREATE TABLE SCHEMATA (
     *      CATALOG_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
     *      SCHEMA_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
     *      SCHEMA_OWNER INFORMATION_SCHEMA.SQL_IDENTIFIER
     *          CONSTRAINT SCHEMA_OWNER_NOT_NULL
     *              NOT NULL,
     *      DEFAULT_CHARACTER_SET_CATALOG INFORMATION_SCHEMA.SQL_IDENTIFIER
     *          CONSTRAINT DEFAULT_CHARACTER_SET_CATALOG_NOT_NULL
     *              NOT NULL,
     *      DEFAULT_CHARACTER_SET_SCHEMA INFORMATION_SCHEMA.SQL_IDENTIFIER
     *          CONSTRAINT DEFAULT_CHARACTER_SET_SCHEMA_NOT_NULL
     *              NOT NULL,
     *      DEFAULT_CHARACTER_SET_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER
     *          CONSTRAINT DEFAULT_CHARACTER_SET_NAME_NOT_NULL
     *              NOT NULL,
     *      SQL_PATH INFORMATION_SCHEMA.CHARACTER_DATA,
     *
     *      CONSTRAINT SCHEMATA_PRIMARY_KEY
     *          PRIMARY KEY ( CATALOG_NAME, SCHEMA_NAME ),
     *      CONSTRAINT SCHEMATA_FOREIGN_KEY_AUTHORIZATIONS
     *          FOREIGN KEY ( SCHEMA_OWNER )
     *              REFERENCES AUTHORIZATIONS,
     *      CONSTRAINT SCHEMATA_FOREIGN_KEY_CATALOG_NAMES
     *          FOREIGN KEY ( CATALOG_NAME )
     *              REFERENCES CATALOG_NAMES
     *      )
     * </pre>
     *
     * <b>Description</b><p>
     *
     * <ol>
     *      <li>The value of CATALOG_NAME is the name of the catalog of the
     *          schema described by this row.<p>
     *
     *      <li>The value of SCHEMA_NAME is the unqualified schema name of
     *          the schema described by this row.<p>
     *
     *      <li>The values of SCHEMA_OWNER are the authorization identifiers
     *          that own the schemata.<p>
     *
     *      <li>The values of DEFAULT_CHARACTER_SET_CATALOG,
     *          DEFAULT_CHARACTER_SET_SCHEMA, and DEFAULT_CHARACTER_SET_NAME
     *          are the catalog name, unqualified schema name, and qualified
     *          identifier, respectively, of the default character set for
     *          columns and domains in the schemata.<p>
     *
     *      <li>Case:<p>
     *          <ul>
     *              <li>If &lt;schema path specification&gt; was specified in
     *                  the &lt;schema definition&gt; that defined the schema
     *                  described by this row and the character representation
     *                  of the &lt;schema path specification&gt; can be
     *                  represented without truncation, then the value of
     *                  SQL_PATH is that character representation.<p>
     *
     *              <li>Otherwise, the value of SQL_PATH is the null value.
     *         </ul>
     * </ol>
     *
     * @return Table
     */
Table SCHEMATA() {
    Table t = sysTables[SCHEMATA];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[SCHEMATA]);
        addColumn(t, "CATALOG_NAME", SQL_IDENTIFIER);
        addColumn(t, "SCHEMA_NAME", SQL_IDENTIFIER);
        addColumn(t, "SCHEMA_OWNER", SQL_IDENTIFIER);
        addColumn(t, "DEFAULT_CHARACTER_SET_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "DEFAULT_CHARACTER_SET_SCHEMA", SQL_IDENTIFIER);
        addColumn(t, "DEFAULT_CHARACTER_SET_NAME", SQL_IDENTIFIER);
        addColumn(t, "SQL_PATH", CHARACTER_DATA);
        // order: CATALOG_NAME, SCHEMA_NAME
        // false PK, as rows may have NULL CATALOG_NAME
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[SCHEMATA].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1 }, false);
        return t;
    }
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    // Intermediate holders
    Iterator schemas;
    String schema;
    String dcsSchema = SqlInvariants.INFORMATION_SCHEMA;
    String dcsName = ValuePool.getString("UTF16");
    String sqlPath = null;
    Grantee user = session.getGrantee();
    Object[] row;
    // column number mappings
    final int schema_catalog = 0;
    final int schema_name = 1;
    final int schema_owner = 2;
    final int default_character_set_catalog = 3;
    final int default_character_set_schema = 4;
    final int default_character_set_name = 5;
    final int sql_path = 6;
    // Initialization
    schemas = database.schemaManager.fullSchemaNamesIterator();
    // Do it.
    while (schemas.hasNext()) {
        schema = (String) schemas.next();
        if (!user.hasSchemaUpdateOrGrantRights(schema)) {
            continue;
        }
        row = t.getEmptyRowData();
        row[schema_catalog] = database.getCatalogName().name;
        row[schema_name] = schema;
        row[schema_owner] = database.schemaManager.toSchemaOwner(schema).getNameString();
        row[default_character_set_catalog] = database.getCatalogName().name;
        row[default_character_set_schema] = dcsSchema;
        row[default_character_set_name] = dcsName;
        row[sql_path] = sqlPath;
        t.insertSys(store, row);
    }
    return t;
}
Also used : Grantee(org.hsqldb_voltpatches.rights.Grantee) 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) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) Constraint(org.hsqldb_voltpatches.Constraint)

Example 3 with Table

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

the class DatabaseInformationFull method ROLE_AUTHORIZATION_DESCRIPTORS.

//------------------------------------------------------------------------------
// SQL SCHEMATA BASE TABLES
/**
     * ROLE_AUTHORIZATION_DESCRIPTORS<p>
     *
     * <b>Function</b><p>
     *
     * Contains a representation of the role authorization descriptors.<p>
     * <b>Definition</b>
     *
     * <pre class="SqlCodeExample">
     * CREATE TABLE ROLE_AUTHORIZATION_DESCRIPTORS (
     *      ROLE_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
     *      GRANTEE INFORMATION_SCHEMA.SQL_IDENTIFIER,
     *      GRANTOR INFORMATION_SCHEMA.SQL_IDENTIFIER,
     *      IS_GRANTABLE INFORMATION_SCHEMA.CHARACTER_DATA
     *          CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_IS_GRANTABLE_CHECK
     *              CHECK ( IS_GRANTABLE IN
     *                  ( 'YES', 'NO' ) ),
     *          CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_PRIMARY_KEY
     *              PRIMARY KEY ( ROLE_NAME, GRANTEE ),
     *          CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_CHECK_ROLE_NAME
     *              CHECK ( ROLE_NAME IN
     *                  ( SELECT AUTHORIZATION_NAME
     *                      FROM AUTHORIZATIONS
     *                     WHERE AUTHORIZATION_TYPE = 'ROLE' ) ),
     *          CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_FOREIGN_KEY_AUTHORIZATIONS_GRANTOR
     *              FOREIGN KEY ( GRANTOR )
     *                  REFERENCES AUTHORIZATIONS,
     *          CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_FOREIGN_KEY_AUTHORIZATIONS_GRANTEE
     *              FOREIGN KEY ( GRANTEE )
     *                  REFERENCES AUTHORIZATIONS
     *      )
     * </pre>
     *
     * <b>Description</b><p>
     *
     * <ol>
     *      <li>The value of ROLE_NAME is the &lt;role name&gt; of some
     *          &lt;role granted&gt; by the &lt;grant role statement&gt; or
     *          the &lt;role name&gt; of a &lt;role definition&gt;. <p>
     *
     *      <li>The value of GRANTEE is an &lt;authorization identifier&gt;,
     *          possibly PUBLIC, or &lt;role name&gt; specified as a
     *          &lt;grantee&gt; contained in a &lt;grant role statement&gt;,
     *          or the &lt;authorization identifier&gt; of the current
     *          SQLsession when the &lt;role definition&gt; is executed. <p>
     *
     *      <li>The value of GRANTOR is the &lt;authorization identifier&gt;
     *          of the user or role who granted the role identified by
     *          ROLE_NAME to the user or role identified by the value of
     *          GRANTEE. <p>
     *
     *      <li>The values of IS_GRANTABLE have the following meanings:<p>
     *
     *      <table border cellpadding="3">
     *          <tr>
     *              <td nowrap>YES</td>
     *              <td nowrap>The described role is grantable.</td>
     *          <tr>
     *          <tr>
     *              <td nowrap>NO</td>
     *              <td nowrap>The described role is not grantable.</td>
     *          <tr>
     *      </table> <p>
     * </ol>
     *
     * @return Table
     */
Table ROLE_AUTHORIZATION_DESCRIPTORS() {
    Table t = sysTables[ROLE_AUTHORIZATION_DESCRIPTORS];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[ROLE_AUTHORIZATION_DESCRIPTORS]);
        // not null
        addColumn(t, "ROLE_NAME", SQL_IDENTIFIER);
        // not null
        addColumn(t, "GRANTEE", SQL_IDENTIFIER);
        // not null
        addColumn(t, "GRANTOR", SQL_IDENTIFIER);
        // not null
        addColumn(t, "IS_GRANTABLE", YES_OR_NO);
        // true PK
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[ROLE_AUTHORIZATION_DESCRIPTORS].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1 }, true);
        return t;
    }
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    // Intermediate holders
    String grantorName = SqlInvariants.SYSTEM_AUTHORIZATION_NAME;
    Iterator grantees;
    Grantee granteeObject;
    String granteeName;
    Iterator roles;
    String roleName;
    String isGrantable;
    Object[] row;
    // Column number mappings
    final int role_name = 0;
    final int grantee = 1;
    final int grantor = 2;
    final int is_grantable = 3;
    // Initialization
    grantees = session.getGrantee().visibleGrantees().iterator();
    //
    while (grantees.hasNext()) {
        granteeObject = (Grantee) grantees.next();
        granteeName = granteeObject.getNameString();
        roles = granteeObject.getDirectRoles().iterator();
        isGrantable = granteeObject.isAdmin() ? Tokens.T_YES : Tokens.T_NO;
        ;
        while (roles.hasNext()) {
            Grantee role = (Grantee) roles.next();
            row = t.getEmptyRowData();
            row[role_name] = role.getNameString();
            row[grantee] = granteeName;
            row[grantor] = grantorName;
            row[is_grantable] = isGrantable;
            t.insertSys(store, row);
        }
    }
    return t;
}
Also used : Grantee(org.hsqldb_voltpatches.rights.Grantee) 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) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) Constraint(org.hsqldb_voltpatches.Constraint)

Example 4 with Table

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

the class DatabaseInformationFull method CONSTRAINT_COLUMN_USAGE.

/**
     * The CONSTRAINT_COLUMN_USAGE view has one row for each column identified by
     * a table constraint or assertion.<p>
     *
     * <b>Definition:</b><p>
     *
     *      TABLE_CATALOG       VARCHAR
     *      TABLE_SCHEMA        VARCHAR
     *      TABLE_NAME          VARCHAR
     *      COLUMN_NAME         VARCHAR
     *      CONSTRAINT_CATALOG  VARCHAR
     *      CONSTRAINT_SCHEMA   VARCHAR
     *      CONSTRAINT_NAME     VARCHAR
     *
     * </pre>
     *
     * <b>Description:</b> <p>
     *
     * <ol>
     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, and
     *      COLUMN_NAME are the catalog name, unqualified schema name,
     *      qualified identifier, and column name, respectively, of a column
     *      identified by a &lt;column reference&gt; explicitly or implicitly
     *      contained in the &lt;search condition&gt; of the constraint
     *      being described.
     *
     * <li> The values of CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, and
     *      CONSTRAINT_NAME are the catalog name, unqualified schema name,
     *      and qualified identifier, respectively, of the constraint being
     *      described. <p>
     *
     * </ol>
     *
     * @return Table
     */
Table CONSTRAINT_COLUMN_USAGE() {
    Table t = sysTables[CONSTRAINT_COLUMN_USAGE];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[CONSTRAINT_COLUMN_USAGE]);
        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);
        addColumn(t, "CONSTRAINT_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "CONSTRAINT_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "CONSTRAINT_NAME", SQL_IDENTIFIER);
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[CONSTRAINT_COLUMN_USAGE].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1, 2, 3, 4, 5, 6 }, false);
        return t;
    }
    // column number mappings
    final int table_catalog = 0;
    final int table_schems = 1;
    final int table_name = 2;
    final int column_name = 3;
    final int constraint_catalog = 4;
    final int constraint_schema = 5;
    final int constraint_name = 6;
    //
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    // calculated column values
    String constraintCatalog;
    String constraintSchema;
    String constraintName;
    // Intermediate holders
    Iterator tables;
    Table table;
    Constraint[] constraints;
    int constraintCount;
    Constraint constraint;
    Iterator iterator;
    Object[] row;
    // Initialization
    tables = database.schemaManager.databaseObjectIterator(SchemaObject.TABLE);
    // Do it.
    while (tables.hasNext()) {
        table = (Table) tables.next();
        if (table.isView() || !session.getGrantee().isFullyAccessibleByRole(table)) {
            continue;
        }
        constraints = table.getConstraints();
        constraintCount = constraints.length;
        constraintCatalog = database.getCatalogName().name;
        constraintSchema = table.getSchemaName().name;
        // process constraints
        for (int i = 0; i < constraintCount; i++) {
            constraint = constraints[i];
            constraintName = constraint.getName().name;
            switch(constraint.getConstraintType()) {
                case Constraint.CHECK:
                    {
                        OrderedHashSet expressions = constraint.getCheckColumnExpressions();
                        if (expressions == null) {
                            break;
                        }
                        iterator = expressions.iterator();
                        // calculate distinct column references
                        while (iterator.hasNext()) {
                            ExpressionColumn expr = (ExpressionColumn) iterator.next();
                            HsqlName name = expr.getBaseColumnHsqlName();
                            if (name.type != SchemaObject.COLUMN) {
                                continue;
                            }
                            row = t.getEmptyRowData();
                            row[table_catalog] = database.getCatalogName().name;
                            row[table_schems] = name.schema.name;
                            row[table_name] = name.parent.name;
                            row[column_name] = name.name;
                            row[constraint_catalog] = constraintCatalog;
                            row[constraint_schema] = constraintSchema;
                            row[constraint_name] = constraintName;
                            try {
                                t.insertSys(store, row);
                            } catch (HsqlException e) {
                            }
                        }
                        break;
                    }
                case Constraint.UNIQUE:
                case Constraint.PRIMARY_KEY:
                case Constraint.FOREIGN_KEY:
                    {
                        Table target = table;
                        int[] cols = constraint.getMainColumns();
                        if (constraint.getConstraintType() == Constraint.FOREIGN_KEY) {
                            target = constraint.getMain();
                        }
                        /*
                       checkme - it seems foreign key columns are not included
                       but columns of the referenced unique constraint are included

                        if (constraint.getType() == Constraint.FOREIGN_KEY) {
                            for (int j = 0; j < cols.length; j++) {
                                row = t.getEmptyRowData();

                                Table mainTable = constraint.getMain();

                                row[table_catalog] = database.getCatalog();
                                row[table_schems] =
                                    mainTable.getSchemaName().name;
                                row[table_name] = mainTable.getName().name;
                                row[column_name] = mainTable.getColumn(
                                    cols[j]).columnName.name;
                                row[constraint_catalog] = constraintCatalog;
                                row[constraint_schema]  = constraintSchema;
                                row[constraint_name]    = constraintName;

                                try {
                                    t.insertSys(row);
                                } catch (HsqlException e) {}
                            }

                            cols = constraint.getRefColumns();
                        }
*/
                        for (int j = 0; j < cols.length; j++) {
                            row = t.getEmptyRowData();
                            row[table_catalog] = database.getCatalogName().name;
                            row[table_schems] = constraintSchema;
                            row[table_name] = target.getName().name;
                            row[column_name] = target.getColumn(cols[j]).getName().name;
                            row[constraint_catalog] = constraintCatalog;
                            row[constraint_schema] = constraintSchema;
                            row[constraint_name] = constraintName;
                            try {
                                t.insertSys(store, row);
                            } catch (HsqlException e) {
                            }
                        }
                    //
                    }
            }
        }
    }
    return t;
}
Also used : Table(org.hsqldb_voltpatches.Table) TextTable(org.hsqldb_voltpatches.TextTable) Constraint(org.hsqldb_voltpatches.Constraint) PersistentStore(org.hsqldb_voltpatches.persist.PersistentStore) Constraint(org.hsqldb_voltpatches.Constraint) ExpressionColumn(org.hsqldb_voltpatches.ExpressionColumn) Iterator(org.hsqldb_voltpatches.lib.Iterator) WrapperIterator(org.hsqldb_voltpatches.lib.WrapperIterator) OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet) HsqlName(org.hsqldb_voltpatches.HsqlNameManager.HsqlName) SchemaObject(org.hsqldb_voltpatches.SchemaObject) HsqlException(org.hsqldb_voltpatches.HsqlException)

Example 5 with Table

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

the class DatabaseInformationFull method TABLE_CONSTRAINTS.

/**
     * The TABLE_CONSTRAINTS table has one row for each table constraint
     * associated with a table.  <p>
     *
     * It effectively contains a representation of the table constraint
     * descriptors. <p>
     *
     * <b>Definition:</b> <p>
     *
     * <pre class="SqlCodeExample">
     * CREATE TABLE SYSTEM_TABLE_CONSTRAINTS (
     *      CONSTRAINT_CATALOG      VARCHAR NULL,
     *      CONSTRAINT_SCHEMA       VARCHAR NULL,
     *      CONSTRAINT_NAME         VARCHAR NOT NULL,
     *      CONSTRAINT_TYPE         VARCHAR NOT NULL,
     *      TABLE_CATALOG           VARCHAR NULL,
     *      TABLE_SCHEMA            VARCHAR NULL,
     *      TABLE_NAME              VARCHAR NOT NULL,
     *      IS_DEFERRABLE           VARCHAR NOT NULL,
     *      INITIALLY_DEFERRED      VARCHAR NOT NULL,
     *
     *      CHECK ( CONSTRAINT_TYPE IN
     *                      ( 'UNIQUE', 'PRIMARY KEY',
     *                        'FOREIGN KEY', 'CHECK' ) ),
     *
     *      CHECK ( ( IS_DEFERRABLE, INITIALLY_DEFERRED ) IN
     *              ( VALUES ( 'NO',  'NO'  ),
     *                       ( 'YES', 'NO'  ),
     *                       ( 'YES', 'YES' ) ) )
     * )
     * </pre>
     *
     * <b>Description:</b> <p>
     *
     * <ol>
     * <li> The values of CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, and
     *      CONSTRAINT_NAME are the catalog name, unqualified schema
     *      name, and qualified identifier, respectively, of the
     *      constraint being described. If the &lt;table constraint
     *      definition&gt; or &lt;add table constraint definition&gt;
     *      that defined the constraint did not specify a
     *      &lt;constraint name&gt;, then the values of CONSTRAINT_CATALOG,
     *      CONSTRAINT_SCHEMA, and CONSTRAINT_NAME are
     *      implementation-defined. <p>
     *
     * <li> The values of CONSTRAINT_TYPE have the following meanings: <p>
     *  <table border cellpadding="3">
     *  <tr>
     *      <td nowrap>FOREIGN KEY</td>
     *      <td nowrap>The constraint being described is a
     *                 foreign key constraint.</td>
     *  </tr>
     *  <tr>
     *      <td nowrap>UNIQUE</td>
     *      <td nowrap>The constraint being described is a
     *                 unique constraint.</td>
     *  </tr>
     *  <tr>
     *      <td nowrap>PRIMARY KEY</td>
     *      <td nowrap>The constraint being described is a
     *                 primary key constraint.</td>
     *  </tr>
     *  <tr>
     *      <td nowrap>CHECK</td>
     *      <td nowrap>The constraint being described is a
     *                 check constraint.</td>
     *  </tr>
     * </table> <p>
     *
     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME are
     *      the catalog name, the unqualified schema name, and the
     *      qualified identifier of the name of the table to which the
     *      table constraint being described applies. <p>
     *
     * <li> The values of IS_DEFERRABLE have the following meanings: <p>
     *
     *  <table>
     *      <tr>
     *          <td nowrap>YES</td>
     *          <td nowrap>The table constraint is deferrable.</td>
     *      </tr>
     *      <tr>
     *          <td nowrap>NO</td>
     *          <td nowrap>The table constraint is not deferrable.</td>
     *      </tr>
     *  </table> <p>
     *
     * <li> The values of INITIALLY_DEFERRED have the following meanings: <p>
     *
     *  <table>
     *      <tr>
     *          <td nowrap>YES</td>
     *          <td nowrap>The table constraint is initially deferred.</td>
     *      </tr>
     *      <tr>
     *          <td nowrap>NO</td>
     *          <td nowrap>The table constraint is initially immediate.</td>
     *      </tr>
     *  </table> <p>
     * </ol>
     *
     * @return Table
     */
Table TABLE_CONSTRAINTS() {
    Table t = sysTables[TABLE_CONSTRAINTS];
    if (t == null) {
        t = createBlankTable(sysTableHsqlNames[TABLE_CONSTRAINTS]);
        addColumn(t, "CONSTRAINT_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "CONSTRAINT_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "CONSTRAINT_NAME", SQL_IDENTIFIER);
        // not null
        addColumn(t, "CONSTRAINT_TYPE", CHARACTER_DATA);
        addColumn(t, "TABLE_CATALOG", SQL_IDENTIFIER);
        addColumn(t, "TABLE_SCHEMA", SQL_IDENTIFIER);
        // not null
        addColumn(t, "TABLE_NAME", SQL_IDENTIFIER);
        // not null
        addColumn(t, "IS_DEFERRABLE", YES_OR_NO);
        // not null
        addColumn(t, "INITIALLY_DEFERRED", YES_OR_NO);
        // false PK, as CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA,
        // TABLE_CATALOG and/or TABLE_SCHEMA may be null
        HsqlName name = HsqlNameManager.newInfoSchemaObjectName(sysTableHsqlNames[TABLE_CONSTRAINTS].name, false, SchemaObject.INDEX);
        t.createPrimaryKey(name, new int[] { 0, 1, 2, 4, 5, 6 }, false);
        return t;
    }
    PersistentStore store = database.persistentStoreCollection.getStore(t);
    // Intermediate holders
    Iterator tables;
    Table table;
    Constraint[] constraints;
    int constraintCount;
    Constraint constraint;
    String cat;
    String schem;
    Object[] row;
    // column number mappings
    final int constraint_catalog = 0;
    final int constraint_schema = 1;
    final int constraint_name = 2;
    final int constraint_type = 3;
    final int table_catalog = 4;
    final int table_schema = 5;
    final int table_name = 6;
    final int is_deferable = 7;
    final int initially_deferred = 8;
    // initialization
    tables = database.schemaManager.databaseObjectIterator(SchemaObject.TABLE);
    // else compiler complains
    table = null;
    // do it
    while (tables.hasNext()) {
        table = (Table) tables.next();
        /** @todo - requires table level INSERT or UPDATE or DELETE or REFERENCES (not SELECT) right */
        if (table.isView() || !isAccessibleTable(table)) {
            continue;
        }
        constraints = table.getConstraints();
        constraintCount = constraints.length;
        for (int i = 0; i < constraintCount; i++) {
            constraint = constraints[i];
            row = t.getEmptyRowData();
            switch(constraint.getConstraintType()) {
                case Constraint.CHECK:
                    {
                        row[constraint_type] = "CHECK";
                        break;
                    }
                case Constraint.UNIQUE:
                    {
                        row[constraint_type] = "UNIQUE";
                        break;
                    }
                case Constraint.FOREIGN_KEY:
                    {
                        row[constraint_type] = "FOREIGN KEY";
                        table = constraint.getRef();
                        break;
                    }
                case Constraint.PRIMARY_KEY:
                    {
                        row[constraint_type] = "PRIMARY KEY";
                        break;
                    }
                case Constraint.MAIN:
                default:
                    {
                        continue;
                    }
            }
            cat = database.getCatalogName().name;
            schem = table.getSchemaName().name;
            row[constraint_catalog] = cat;
            row[constraint_schema] = schem;
            row[constraint_name] = constraint.getName().name;
            row[table_catalog] = cat;
            row[table_schema] = schem;
            row[table_name] = table.getName().name;
            row[is_deferable] = Tokens.T_NO;
            row[initially_deferred] = Tokens.T_NO;
            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)

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