Source Code Cross Referenced for DatabaseInformationMain.java in  » Database-DBMS » hsql » org » hsqldb » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » hsql » org.hsqldb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /* Copyright (c) 2001-2005, The HSQL Development Group
0002:         * All rights reserved.
0003:         *
0004:         * Redistribution and use in source and binary forms, with or without
0005:         * modification, are permitted provided that the following conditions are met:
0006:         *
0007:         * Redistributions of source code must retain the above copyright notice, this
0008:         * list of conditions and the following disclaimer.
0009:         *
0010:         * Redistributions in binary form must reproduce the above copyright notice,
0011:         * this list of conditions and the following disclaimer in the documentation
0012:         * and/or other materials provided with the distribution.
0013:         *
0014:         * Neither the name of the HSQL Development Group nor the names of its
0015:         * contributors may be used to endorse or promote products derived from this
0016:         * software without specific prior written permission.
0017:         *
0018:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019:         * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0020:         * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0021:         * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
0022:         * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0023:         * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0024:         * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
0025:         * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
0026:         * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0028:         * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029:         */
0030:
0031:        package org.hsqldb;
0032:
0033:        import org.hsqldb.HsqlNameManager.HsqlName;
0034:        import org.hsqldb.lib.HashMap;
0035:        import org.hsqldb.lib.HashSet;
0036:        import org.hsqldb.lib.HsqlArrayList;
0037:        import org.hsqldb.lib.Iterator;
0038:        import org.hsqldb.lib.WrapperIterator;
0039:        import org.hsqldb.persist.HsqlProperties;
0040:        import org.hsqldb.store.ValuePool;
0041:
0042:        // fredt@users - 1.7.2 - structural modifications to allow inheritance
0043:        // boucherb@users - 1.7.2 - 20020225
0044:        // - factored out all reusable code into DIXXX support classes
0045:        // - completed Fred's work on allowing inheritance
0046:        // boucherb@users - 1.7.2 - 20020304 - bug fixes, refinements, better java docs
0047:        // fredt@users - 1.8.0 - updated to report latest enhancements and changes
0048:
0049:        /**
0050:         * Produces a collection of tables that form the system data dictionary. <p>
0051:         *
0052:         * Implementations use a group of arrays of equal size to store various
0053:         * attributes or cached instances of system tables.<p>
0054:         *
0055:         * Two fixed static lists of reserved table names are kept in String[] and
0056:         * HsqlName[] forms. These are shared by all implementations of
0057:         * DatabaseInformtion.<p>
0058:         *
0059:         * Each implementation keeps a lookup set of names for those tables whose
0060:         * contents are never cached (nonCachedTablesSet). <p>
0061:         *
0062:         * An instance of this class uses three lists named sysTablexxxx for caching
0063:         * system tables.<p>
0064:         *
0065:         * sysTableSessionDependent indicates which tables contain data that is
0066:         * dependent on the user rights of the User associatiod with the Session.<p>
0067:         *
0068:         * sysTableSessions contains the Session with whose rights each cached table
0069:         * was built.<p>
0070:         *
0071:         * sysTables contains the cached tables.<p>
0072:         *
0073:         * At the time of instantiation, which is part of the Database.open() method
0074:         * invocation, an empty table is created and placed in sysTables with calls to
0075:         * generateTable(int) for each name in sysTableNames. Some of these
0076:         * table entries may be null if an implementation does not produce them.<p>
0077:         *
0078:         * Calls to getSystemTable(String, Session) return a cached table if various
0079:         * caching rules are met (see below), or it will delete all rows of the table
0080:         * and rebuild the contents via generateTable(int).<p>
0081:         *
0082:         * generateTable(int) calls the appropriate single method for each table.
0083:         * These methods either build and return an empty table (if sysTables
0084:         * contains null for the table slot) or populate the table with up-to-date
0085:         * rows. <p>
0086:         *
0087:         * When the setDirty() call is made externally, the internal isDirty flag
0088:         * is set. This flag is used next time a call to
0089:         * getSystemTable(String, Session) is made. <p>
0090:         *
0091:         * Rules for caching are applied as follows: <p>
0092:         *
0093:         * When a call to getSystemTable(String, Session) is made, if the isDirty flag
0094:         * is true, then the contents of all cached tables are cleared and the
0095:         * sysTableUsers slot for all tables is set to null. This also has the
0096:         * effect of clearing the isDirty and isDirtyNextIdentity flags<p>
0097:         *
0098:         * if the isDirtyNextIdentity flag is true at this point, then the contents
0099:         * of all next identity value dependent cached tables are cleared and the
0100:         * sysTableUsers slot for these tables are set to null.  Currently,
0101:         * the only member of this set is the SYSTEM_TABLES system table.
0102:         *
0103:         * If a table has non-cached contents, its contents are cleared and
0104:         * rebuilt. <p>
0105:         *
0106:         * For the rest of the tables, if the sysTableSessions slot is null or if the
0107:         * Session parameter is not the same as the Session object
0108:         * in that slot, the table contents are cleared and rebuilt. <p>
0109:         *
0110:         * (fredt@users) <p>
0111:         * @author boucherb@users
0112:         * @version 1.8.0
0113:         * @since 1.7.2
0114:         */
0115:        class DatabaseInformationMain extends DatabaseInformation {
0116:
0117:            // HsqlName objects for the system tables
0118:
0119:            /** The HsqlNames of the system tables. */
0120:            protected static final HsqlName[] sysTableHsqlNames;
0121:
0122:            /** Current user for each cached system table */
0123:            protected final int[] sysTableSessions = new int[sysTableNames.length];
0124:
0125:            /** true if the contents of a cached system table depends on the session */
0126:            protected final boolean[] sysTableSessionDependent = new boolean[sysTableNames.length];
0127:
0128:            /** cache of system tables */
0129:            protected final Table[] sysTables = new Table[sysTableNames.length];
0130:
0131:            /** Set: { names of system tables that are not to be cached } */
0132:            protected static final HashSet nonCachedTablesSet;
0133:
0134:            /**
0135:             * Map: simple <code>Column</code> name <code>String</code> object =>
0136:             * <code>HsqlName</code> object.
0137:             */
0138:            protected static final HashMap columnNameMap;
0139:
0140:            /**
0141:             * The <code>Session</code> object under consideration in the current
0142:             * executution context.
0143:             */
0144:            protected Session session;
0145:
0146:            /** The table types HSQLDB supports. */
0147:            protected static final String[] tableTypes = new String[] {
0148:                    "GLOBAL TEMPORARY", "SYSTEM TABLE", "TABLE", "VIEW" };
0149:
0150:            /** Provides naming support. */
0151:            protected DINameSpace ns;
0152:
0153:            static {
0154:                columnNameMap = new HashMap();
0155:                nonCachedTablesSet = new HashSet();
0156:                sysTableHsqlNames = new HsqlName[sysTableNames.length];
0157:
0158:                for (int i = 0; i < sysTableNames.length; i++) {
0159:                    sysTableHsqlNames[i] = HsqlNameManager
0160:                            .newHsqlSystemObjectName(sysTableNames[i]);
0161:                    sysTableHsqlNames[i].schema = SchemaManager.INFORMATION_SCHEMA_HSQLNAME;
0162:                }
0163:
0164:                // build the set of non-cached tables
0165:                nonCachedTablesSet.add("SYSTEM_CACHEINFO");
0166:                nonCachedTablesSet.add("SYSTEM_SESSIONINFO");
0167:                nonCachedTablesSet.add("SYSTEM_SESSIONS");
0168:                nonCachedTablesSet.add("SYSTEM_PROPERTIES");
0169:                nonCachedTablesSet.add("SYSTEM_SEQUENCES");
0170:            }
0171:
0172:            /**
0173:             * Constructs a table producer which provides system tables
0174:             * for the specified <code>Database</code> object. <p>
0175:             *
0176:             * <b>Note:</b> before 1.7.2 Alpha N, it was important to observe that
0177:             * by specifying an instance of this class or one of its descendents to
0178:             * handle system table production, the new set of builtin permissions
0179:             * and aliases would overwrite those of an existing database, meaning that
0180:             * metadata reporting might have been rendered less secure if the same
0181:             * database were then opened again using a lower numbered system table
0182:             * producer instance (i.e. one in a 1.7.1 or earlier distribution).
0183:             * As of 1.7.2 Alpha N, system-generated permissions and aliases are no
0184:             * longer recorded in the checkpoint script, obseleting this issue.
0185:             * Checkpointing of system-generated grants and aliases was removed
0186:             * because their existence is very close to a core requirment for correct
0187:             * operation and they are reintroduced to the system at each startup.
0188:             * In a future release, it may even be an exception condition to attempt
0189:             * to remove or alter system-generated grants and aliases,
0190:             * respectvely. <p>
0191:             *
0192:             * @param db the <code>Database</code> object for which this object
0193:             *      produces system tables
0194:             * @throws HsqlException if a database access error occurs
0195:             */
0196:            DatabaseInformationMain(Database db) throws HsqlException {
0197:
0198:                super (db);
0199:
0200:                init();
0201:            }
0202:
0203:            /**
0204:             * Adds a <code>Column</code> object with the specified name, data type,
0205:             * data size and nullability to the specified <code>Table</code>
0206:             * object. <p>
0207:             *
0208:             * @param t the table to which to add the specified column
0209:             * @param name the name of the column
0210:             * @param type the data type of the column
0211:             * @param size the precision/length of the column
0212:             * @param nullable <code>true</code> if the column is to allow null values,
0213:             *      else <code>false</code>
0214:             * @throws HsqlException if a problem occurs when adding the
0215:             *      column (e.g. duplicate name)
0216:             */
0217:            protected final void addColumn(Table t, String name, int type,
0218:                    int size, boolean nullable) throws HsqlException {
0219:
0220:                HsqlName cn;
0221:                Column c;
0222:
0223:                cn = (HsqlName) columnNameMap.get(name);
0224:
0225:                if (cn == null) {
0226:                    cn = database.nameManager.newHsqlName(name, false);
0227:
0228:                    columnNameMap.put(name, cn);
0229:                }
0230:
0231:                c = new Column(cn, nullable, type, size, 0, false, null);
0232:
0233:                t.addColumn(c);
0234:            }
0235:
0236:            /**
0237:             * Adds a <code>Column</code> object with the specified name, data type
0238:             * and nullability to the specified <code>Table</code> object. <p>
0239:             *
0240:             * @param t the table to which to add the specified column
0241:             * @param name the name of the column
0242:             * @param type the data type of the column
0243:             * @param nullable <code>true</code> if the column is to allow null values,
0244:             *      else <code>false</code>
0245:             * @throws HsqlException if a problem occurs when adding the
0246:             *      column (e.g. duplicate name)
0247:             */
0248:            protected final void addColumn(Table t, String name, int type,
0249:                    boolean nullable) throws HsqlException {
0250:                addColumn(t, name, type, 0, nullable);
0251:            }
0252:
0253:            /**
0254:             * Adds a nullable <code>Column</code> object with the specified name and
0255:             * data type to the specified <code>Table</code> object. <p>
0256:             *
0257:             * @param t the table to which to add the specified column
0258:             * @param name the name of the column
0259:             * @param type the data type of the column
0260:             * @throws HsqlException if a problem occurs when adding the
0261:             *      column (e.g. duplicate name)
0262:             */
0263:            protected final void addColumn(Table t, String name, int type)
0264:                    throws HsqlException {
0265:                addColumn(t, name, type, true);
0266:            }
0267:
0268:            /**
0269:             * Retrieves an enumeration over all of the tables in this database.
0270:             * This means all user tables, views, system tables, system views,
0271:             * including temporary and text tables. <p>
0272:             *
0273:             * @return an enumeration over all of the tables in this database
0274:             */
0275:            protected final Iterator allTables() {
0276:                return new WrapperIterator(database.schemaManager
0277:                        .allTablesIterator(), new WrapperIterator(sysTables,
0278:                        true));
0279:            }
0280:
0281:            /**
0282:             * Clears the contents of cached system tables and resets user slots
0283:             * to null. <p>
0284:             *
0285:             * @throws HsqlException if a database access error occurs
0286:             */
0287:            protected final void cacheClear() throws HsqlException {
0288:
0289:                int i = sysTables.length;
0290:
0291:                while (i-- > 0) {
0292:                    Table t = sysTables[i];
0293:
0294:                    if (t != null) {
0295:                        t.clearAllRows(session);
0296:                    }
0297:
0298:                    sysTableSessions[i] = -1;
0299:                }
0300:
0301:                isDirty = false;
0302:            }
0303:
0304:            /**
0305:             * Retrieves the system table corresponding to the specified
0306:             * tableIndex value. <p>
0307:             *
0308:             * @param tableIndex int value identifying the system table to generate
0309:             * @throws HsqlException if a database access error occurs
0310:             * @return the system table corresponding to the specified tableIndex value
0311:             */
0312:            protected Table generateTable(int tableIndex) throws HsqlException {
0313:
0314:                Table t = sysTables[tableIndex];
0315:
0316:                //        Please note that this class produces non-null tables for
0317:                //        just those absolutely essential to the JDBC 1 spec and the
0318:                //        HSQLDB core.  Also, all table producing methods except
0319:                //        SYSTEM_PROCEDURES() and SYSTEM_PROCEDURECOLUMNS() are declared final;
0320:                //        this class produces only an empty table for each, as per previous
0321:                //        DatabaseInformation implementations, whereas
0322:                //        DatabaseInformationFull produces comprehensive content for
0323:                //        them).
0324:                //
0325:                //        This break down of inheritance allows DatabaseInformation and
0326:                //        DatabaseInformationMain (this class) to be made as small as possible
0327:                //        while still meeting their mandates:
0328:                //
0329:                //        1.) DatabaseInformation prevents use of reserved system table names
0330:                //            for user tables and views, meaning that even under highly
0331:                //            constrained use cases where the notion of DatabaseMetaData can
0332:                //            be discarded (i.e. the engine operates in a distribution where
0333:                //            DatabaseInforationMain/Full and jdbcDatabaseMetaData have been
0334:                //            dropped from the JAR), it is still impossible to produce a
0335:                //            database which will be incompatible in terms of system table <=>
0336:                //            user table name clashes, if/when imported into a more
0337:                //            capable operating environment.
0338:                //
0339:                //        2.) DatabaseInformationMain builds on DatabaseInformation, providing
0340:                //            at minimum what is needed for comprehensive operation under
0341:                //            JDK 1.1/JDBC 1 and provides, at minimum, what was provided under
0342:                //            earlier implementations.
0343:                //
0344:                //        3.) descendents of DatabaseInformationMain (such as the current
0345:                //            DatabaseInformationFull) need not (and indeed: now cannot)
0346:                //            override most of the DatabaseInformationMain table producing
0347:                //            methods, as for the most part they are expected to be already
0348:                //            fully comprehensive, security aware and accessible to all users.
0349:                switch (tableIndex) {
0350:
0351:                case SYSTEM_BESTROWIDENTIFIER:
0352:                    return SYSTEM_BESTROWIDENTIFIER();
0353:
0354:                case SYSTEM_CATALOGS:
0355:                    return SYSTEM_CATALOGS();
0356:
0357:                case SYSTEM_COLUMNPRIVILEGES:
0358:                    return SYSTEM_COLUMNPRIVILEGES();
0359:
0360:                case SYSTEM_COLUMNS:
0361:                    return SYSTEM_COLUMNS();
0362:
0363:                case SYSTEM_CROSSREFERENCE:
0364:                    return SYSTEM_CROSSREFERENCE();
0365:
0366:                case SYSTEM_INDEXINFO:
0367:                    return SYSTEM_INDEXINFO();
0368:
0369:                case SYSTEM_PRIMARYKEYS:
0370:                    return SYSTEM_PRIMARYKEYS();
0371:
0372:                case SYSTEM_PROCEDURECOLUMNS:
0373:                    return SYSTEM_PROCEDURECOLUMNS();
0374:
0375:                case SYSTEM_PROCEDURES:
0376:                    return SYSTEM_PROCEDURES();
0377:
0378:                case SYSTEM_SCHEMAS:
0379:                    return SYSTEM_SCHEMAS();
0380:
0381:                case SYSTEM_TABLEPRIVILEGES:
0382:                    return SYSTEM_TABLEPRIVILEGES();
0383:
0384:                case SYSTEM_TABLES:
0385:                    return SYSTEM_TABLES();
0386:
0387:                case SYSTEM_TABLETYPES:
0388:                    return SYSTEM_TABLETYPES();
0389:
0390:                case SYSTEM_TYPEINFO:
0391:                    return SYSTEM_TYPEINFO();
0392:
0393:                case SYSTEM_USERS:
0394:                    return SYSTEM_USERS();
0395:
0396:                    // required by SYSTEM_TYPEINFRO
0397:                case SYSTEM_ALLTYPEINFO:
0398:                    return SYSTEM_ALLTYPEINFO();
0399:
0400:                case SYSTEM_CHECK_CONSTRAINTS:
0401:                    return SYSTEM_CHECK_CONSTRAINTS();
0402:
0403:                case SYSTEM_SEQUENCES:
0404:                    return SYSTEM_SEQUENCES();
0405:
0406:                default:
0407:                    return null;
0408:                }
0409:            }
0410:
0411:            /**
0412:             * One time initialisation of instance attributes
0413:             * at construction time. <p>
0414:             *
0415:             * @throws HsqlException if a database access error occurs
0416:             */
0417:            protected final void init() throws HsqlException {
0418:
0419:                ns = new DINameSpace(database);
0420:
0421:                // flag the Session-dependent cached tables
0422:                sysTableSessionDependent[SYSTEM_ALIASES] = sysTableSessionDependent[SYSTEM_CLASSPRIVILEGES] = sysTableSessionDependent[SYSTEM_BESTROWIDENTIFIER] = sysTableSessionDependent[SYSTEM_COLUMNPRIVILEGES] = sysTableSessionDependent[SYSTEM_COLUMNS] = sysTableSessionDependent[SYSTEM_CROSSREFERENCE] = sysTableSessionDependent[SYSTEM_INDEXINFO] = sysTableSessionDependent[SYSTEM_PRIMARYKEYS] = sysTableSessionDependent[SYSTEM_PROCEDURES] = sysTableSessionDependent[SYSTEM_PROCEDURECOLUMNS] = sysTableSessionDependent[SYSTEM_TABLEPRIVILEGES] = sysTableSessionDependent[SYSTEM_TABLES] = sysTableSessionDependent[SYSTEM_TRIGGERCOLUMNS] = sysTableSessionDependent[SYSTEM_TRIGGERS] = sysTableSessionDependent[SYSTEM_VIEWS] = sysTableSessionDependent[SYSTEM_TEXTTABLES] = sysTableSessionDependent[SYSTEM_CHECK_CONSTRAINTS] = sysTableSessionDependent[SYSTEM_SEQUENCES] = sysTableSessionDependent[SYSTEM_USAGE_PRIVILEGES] = sysTableSessionDependent[SYSTEM_TABLE_CONSTRAINTS] = sysTableSessionDependent[SYSTEM_CHECK_COLUMN_USAGE] = sysTableSessionDependent[SYSTEM_CHECK_ROUTINE_USAGE] = sysTableSessionDependent[SYSTEM_CHECK_TABLE_USAGE] = sysTableSessionDependent[SYSTEM_VIEW_COLUMN_USAGE] = sysTableSessionDependent[SYSTEM_VIEW_TABLE_USAGE] = sysTableSessionDependent[SYSTEM_VIEW_ROUTINE_USAGE] = sysTableSessionDependent[SYSTEM_AUTHORIZATIONS] = true;
0423:
0424:                Table t;
0425:
0426:                /*
0427:                 Session oldSession = session;
0428:
0429:                 session = database.sessionManager.getSysSession(
0430:                 database.schemaManager.INFORMATION_SCHEMA);
0431:                 */
0432:                for (int i = 0; i < sysTables.length; i++) {
0433:                    t = sysTables[i] = generateTable(i);
0434:
0435:                    if (t != null) {
0436:                        t.setDataReadOnly(true);
0437:                    }
0438:                }
0439:
0440:                GranteeManager gm = database.getGranteeManager();
0441:
0442:                for (int i = 0; i < sysTableHsqlNames.length; i++) {
0443:                    if (sysTables[i] != null) {
0444:                        gm.grant(GranteeManager.PUBLIC_ROLE_NAME,
0445:                                sysTableHsqlNames[i], UserManager.SELECT);
0446:                    }
0447:                }
0448:                /*
0449:                 session = oldSession;
0450:                 */
0451:            }
0452:
0453:            /**
0454:             * Retrieves whether any form of SQL access is allowed against the
0455:             * the specified table w.r.t the database access rights
0456:             * assigned to current Session object's User. <p>
0457:             *
0458:             * @return true if the table is accessible, else false
0459:             * @param table the table for which to check accessibility
0460:             * @throws HsqlException if a database access error occurs
0461:             */
0462:            protected final boolean isAccessibleTable(Table table)
0463:                    throws HsqlException {
0464:                return session.isAccessible(table.getName());
0465:            }
0466:
0467:            /**
0468:             * Creates a new primoidal system table with the specified name. <p>
0469:             *
0470:             * @return a new system table
0471:             * @param name of the table
0472:             * @throws HsqlException if a database access error occurs
0473:             */
0474:            protected final Table createBlankTable(HsqlName name)
0475:                    throws HsqlException {
0476:                return new Table(database, name, Table.SYSTEM_TABLE);
0477:            }
0478:
0479:            /**
0480:             * Retrieves the system <code>Table</code> object corresponding to
0481:             * the given <code>name</code> and <code>session</code> arguments. <p>
0482:             *
0483:             * @param session the Session object requesting the table
0484:             * @param name a String identifying the desired table
0485:             * @throws HsqlException if there is a problem producing the table or a
0486:             *      database access error occurs
0487:             * @return a system table corresponding to the <code>name</code> and
0488:             *      <code>session</code> arguments
0489:             */
0490:            final Table getSystemTable(Session session, String name)
0491:                    throws HsqlException {
0492:
0493:                Table t;
0494:                int tableIndex;
0495:
0496:                // must come first...many methods depend on this being set properly
0497:                this .session = session;
0498:
0499:                if (!isSystemTable(name)) {
0500:                    return null;
0501:                }
0502:
0503:                tableIndex = getSysTableID(name);
0504:                t = sysTables[tableIndex];
0505:
0506:                // fredt - any system table that is not supported will be null here
0507:                if (t == null) {
0508:                    return t;
0509:                }
0510:
0511:                // At the time of opening the database, no content is needed
0512:                // at present.  However, table structure is required at this
0513:                // point to allow processing logged View defn's against system
0514:                // tables.  Returning tables without content speeds the database
0515:                // open phase under such cases.
0516:                if (!withContent) {
0517:                    return t;
0518:                }
0519:
0520:                if (isDirty) {
0521:                    cacheClear();
0522:                }
0523:
0524:                int oldSessionId = sysTableSessions[tableIndex];
0525:                boolean tableValid = oldSessionId != -1;
0526:
0527:                // user has changed and table is user-dependent
0528:                if (session.getId() != oldSessionId
0529:                        && sysTableSessionDependent[tableIndex]) {
0530:                    tableValid = false;
0531:                }
0532:
0533:                if (nonCachedTablesSet.contains(name)) {
0534:                    tableValid = false;
0535:                }
0536:
0537:                // any valid cached table will be returned here
0538:                if (tableValid) {
0539:                    return t;
0540:                }
0541:
0542:                // fredt - clear the contents of table and set new User
0543:                t.clearAllRows(session);
0544:
0545:                sysTableSessions[tableIndex] = session.getId();
0546:
0547:                // match and if found, generate.
0548:                t = generateTable(tableIndex);
0549:
0550:                // t will be null at this point, if the implementation
0551:                // does not support the particular table
0552:                // send back what we found or generated
0553:                return t;
0554:            }
0555:
0556:            /**
0557:             * Retrieves a <code>Table</code> object describing the optimal
0558:             * set of visible columns that uniquely identifies a row
0559:             * for each accessible table defined within this database. <p>
0560:             *
0561:             * Each row describes a single column of the best row indentifier column
0562:             * set for a particular table.  Each row has the following
0563:             * columns: <p>
0564:             *
0565:             * <pre class="SqlCodeExample">
0566:             * SCOPE          SMALLINT  scope of applicability
0567:             * COLUMN_NAME    VARCHAR   simple name of the column
0568:             * DATA_TYPE      SMALLINT  SQL data type from Types
0569:             * TYPE_NAME      VARCHAR   canonical type name
0570:             * COLUMN_SIZE    INTEGER   precision
0571:             * BUFFER_LENGTH  INTEGER   transfer size in bytes, if definitely known
0572:             * DECIMAL_DIGITS SMALLINT  scale  - fixed # of decimal digits
0573:             * PSEUDO_COLUMN  SMALLINT  is this a pseudo column like an Oracle ROWID?
0574:             * TABLE_CAT      VARCHAR   table catalog
0575:             * TABLE_SCHEM    VARCHAR   simple name of table schema
0576:             * TABLE_NAME     VARCHAR   simple table name
0577:             * NULLABLE       SMALLINT  is column nullable?
0578:             * IN_KEY         BOOLEAN   column belongs to a primary or alternate key?
0579:             * </pre> <p>
0580:             *
0581:             * <b>Notes:</b><p>
0582:             *
0583:             * <code>jdbcDatabaseMetaData.getBestRowIdentifier</code> uses its
0584:             * nullable parameter to filter the rows of this table in the following
0585:             * manner: <p>
0586:             *
0587:             * If the nullable parameter is <code>false</code>, then rows are reported
0588:             * only if, in addition to satisfying the other specified filter values,
0589:             * the IN_KEY column value is TRUE. If the nullable parameter is
0590:             * <code>true</code>, then the IN_KEY column value is ignored. <p>
0591:             *
0592:             * There is not yet infrastructure in place to make some of the ranking
0593:             * descisions described below, and it is anticipated that mechanisms
0594:             * upon which cost descisions could be based will change significantly over
0595:             * the next few releases.  Hence, in the interest of simplicity and of not
0596:             * making overly complex dependency on features that will almost certainly
0597:             * change significantly in the near future, the current implementation,
0598:             * while perfectly adequate for all but the most demanding or exacting
0599:             * purposes, is actually sub-optimal in the strictest sense. <p>
0600:             *
0601:             * A description of the current implementation follows: <p>
0602:             *
0603:             * <b>DEFINTIONS:</b>  <p>
0604:             *
0605:             * <b>Alternate key</b> <p>
0606:             *
0607:             *  <UL>
0608:             *   <LI> An attribute of a table that, by virtue of its having a set of
0609:             *        columns that are both the full set of columns participating in a
0610:             *        unique constraint or index and are all not null, yeilds the same
0611:             *        selectability characteristic that would obtained by declaring a
0612:             *        primary key on those same columns.
0613:             *  </UL> <p>
0614:             *
0615:             * <b>Column set performance ranking</b> <p>
0616:             *
0617:             *  <UL>
0618:             *  <LI> The ranking of the expected average performance w.r.t a subset of
0619:             *       a table's columns used to select and/or compare rows, as taken in
0620:             *       relation to all other distinct candidate subsets under
0621:             *       consideration. This can be estimated by comparing each cadidate
0622:             *       subset in terms of total column count, relative peformance of
0623:             *       comparisons amongst the domains of the columns and differences
0624:             *       in other costs involved in the execution plans generated using
0625:             *       each subset under consideration for row selection/comparison.
0626:             *  </UL> <p>
0627:             *
0628:             *
0629:             * <b>Rules:</b> <p>
0630:             *
0631:             * Given the above definitions, the rules currently in effect for reporting
0632:             * best row identifier are as follows, in order of precedence: <p>
0633:             *
0634:             * <OL>
0635:             * <LI> if the table under consideration has a primary key contraint, then
0636:             *      the columns of the primary key are reported, with no consideration
0637:             *      given to the column set performance ranking over the set of
0638:             *      candidate keys. Each row has its IN_KEY column set to TRUE.
0639:             *
0640:             * <LI> if 1.) does not hold, then if there exits one or more alternate
0641:             *      keys, then the columns of the alternate key with the lowest column
0642:             *      count are reported, with no consideration given to the column set
0643:             *      performance ranking over the set of candidate keys. If there
0644:             *      exists a tie for lowest column count, then the columns of the
0645:             *      first such key encountered are reported.
0646:             *      Each row has its IN_KEY column set to TRUE.
0647:             *
0648:             * <LI> if both 1.) and 2.) do not hold, then, if possible, a unique
0649:             *      contraint/index is selected from the set of unique
0650:             *      contraints/indices containing at least one column having
0651:             *      a not null constraint, with no consideration given to the
0652:             *      column set performance ranking over the set of all such
0653:             *      candidate column sets. If there exists a tie for lowest non-zero
0654:             *      count of columns having a not null constraint, then the columns
0655:             *      of the first such encountered candidate set are reported. Each
0656:             *      row has its IN_KEY column set to FALSE. <p>
0657:             *
0658:             * <LI> Finally, if the set of candidate column sets in 3.) is the empty,
0659:             *      then no column set is reported for the table under consideration.
0660:             * </OL> <p>
0661:             *
0662:             * The scope reported for a best row identifier column set is determined
0663:             * thus: <p>
0664:             *
0665:             * <OL>
0666:             * <LI> if the database containing the table under consideration is in
0667:             *      read-only mode or the table under consideration is GLOBAL TEMPORARY
0668:             *      (a TEMP or TEMP TEXT table, in HSQLDB parlance), then the scope
0669:             *      is reported as
0670:             *      <code>java.sql.DatabaseMetaData.bestRowSession</code>.
0671:             *
0672:             * <LI> if 1.) does not hold, then the scope is reported as
0673:             *      <code>java.sql.DatabaseMetaData.bestRowTemporary</code>.
0674:             * </OL> <p>
0675:             *
0676:             * @return a <code>Table</code> object describing the optimal
0677:             * set of visible columns that uniquely identifies a row
0678:             * for each accessible table defined within this database
0679:             * @throws HsqlException if an error occurs while producing the table
0680:             */
0681:            final Table SYSTEM_BESTROWIDENTIFIER() throws HsqlException {
0682:
0683:                Table t = sysTables[SYSTEM_BESTROWIDENTIFIER];
0684:
0685:                if (t == null) {
0686:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_BESTROWIDENTIFIER]);
0687:
0688:                    addColumn(t, "SCOPE", Types.SMALLINT, false); // not null
0689:                    addColumn(t, "COLUMN_NAME", Types.VARCHAR, false); // not null
0690:                    addColumn(t, "DATA_TYPE", Types.SMALLINT, false); // not null
0691:                    addColumn(t, "TYPE_NAME", Types.VARCHAR, 32, false); // not null
0692:                    addColumn(t, "COLUMN_SIZE", Types.INTEGER);
0693:                    addColumn(t, "BUFFER_LENGTH", Types.INTEGER);
0694:                    addColumn(t, "DECIMAL_DIGITS", Types.SMALLINT);
0695:                    addColumn(t, "PSEUDO_COLUMN", Types.SMALLINT, false); // not null
0696:                    addColumn(t, "TABLE_CAT", Types.VARCHAR);
0697:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
0698:                    addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
0699:                    addColumn(t, "NULLABLE", Types.SMALLINT, false); // not null
0700:                    addColumn(t, "IN_KEY", Types.BOOLEAN, false); // not null
0701:
0702:                    // order: SCOPE
0703:                    // for unique:  TABLE_CAT, TABLE_SCHEM, TABLE_NAME, COLUMN_NAME
0704:                    // false PK, as TABLE_CAT and/or TABLE_SCHEM may be null
0705:                    t.createPrimaryKey(null, new int[] { 0, 8, 9, 10, 1 },
0706:                            false);
0707:
0708:                    return t;
0709:                }
0710:
0711:                // calculated column values
0712:                Integer scope; // { temp, transaction, session }
0713:                Integer pseudo;
0714:
0715:                //-------------------------------------------
0716:                // required for restriction of results via
0717:                // DatabaseMetaData filter parameters, but
0718:                // not actually required to be included in
0719:                // DatabaseMetaData.getBestRowIdentifier()
0720:                // result set
0721:                //-------------------------------------------
0722:                String tableCatalog; // table calalog
0723:                String tableSchema; // table schema
0724:                String tableName; // table name
0725:                Boolean inKey; // column participates in PK or AK?
0726:
0727:                //-------------------------------------------
0728:                // TODO:  Maybe include:
0729:                //        - backing index (constraint) name?
0730:                //        - column sequence in index (constraint)?
0731:                //-------------------------------------------
0732:                // Intermediate holders
0733:                Iterator tables;
0734:                Table table;
0735:                DITableInfo ti;
0736:                int[] cols;
0737:                Object[] row;
0738:                HsqlProperties p;
0739:
0740:                // Column number mappings
0741:                final int iscope = 0;
0742:                final int icolumn_name = 1;
0743:                final int idata_type = 2;
0744:                final int itype_name = 3;
0745:                final int icolumn_size = 4;
0746:                final int ibuffer_length = 5;
0747:                final int idecimal_digits = 6;
0748:                final int ipseudo_column = 7;
0749:                final int itable_cat = 8;
0750:                final int itable_schem = 9;
0751:                final int itable_name = 10;
0752:                final int inullable = 11;
0753:                final int iinKey = 12;
0754:
0755:                // Initialization
0756:                ti = new DITableInfo();
0757:                p = database.getProperties();
0758:                tables = p.isPropertyTrue("hsqldb.system_table_bri") ? allTables()
0759:                        : database.schemaManager.allTablesIterator();
0760:
0761:                // Do it.
0762:                while (tables.hasNext()) {
0763:                    table = (Table) tables.next();
0764:
0765:                    if (table.isView() || !isAccessibleTable(table)) {
0766:                        continue;
0767:                    }
0768:
0769:                    cols = table.getBestRowIdentifiers();
0770:
0771:                    if (cols == null) {
0772:                        continue;
0773:                    }
0774:
0775:                    ti.setTable(table);
0776:
0777:                    inKey = ValuePool.getBoolean(table
0778:                            .isBestRowIdentifiersStrict());
0779:                    tableCatalog = ns.getCatalogName(table);
0780:                    tableSchema = table.getSchemaName();
0781:                    tableName = ti.getName();
0782:                    scope = ti.getBRIScope();
0783:                    pseudo = ti.getBRIPseudo();
0784:
0785:                    for (int i = 0; i < cols.length; i++) {
0786:                        row = t.getEmptyRowData();
0787:                        row[iscope] = scope;
0788:                        row[icolumn_name] = ti.getColName(i);
0789:                        row[idata_type] = ti.getColDataType(i);
0790:                        row[itype_name] = ti.getColDataTypeName(i);
0791:                        row[icolumn_size] = ti.getColSize(i);
0792:                        row[ibuffer_length] = ti.getColBufLen(i);
0793:                        row[idecimal_digits] = ti.getColScale(i);
0794:                        row[ipseudo_column] = pseudo;
0795:                        row[itable_cat] = tableCatalog;
0796:                        row[itable_schem] = tableSchema;
0797:                        row[itable_name] = tableName;
0798:                        row[inullable] = ti.getColNullability(i);
0799:                        row[iinKey] = inKey;
0800:
0801:                        t.insertSys(row);
0802:                    }
0803:                }
0804:
0805:                t.setDataReadOnly(true);
0806:
0807:                return t;
0808:            }
0809:
0810:            /**
0811:             * Retrieves a <code>Table</code> object naming the accessible catalogs
0812:             * defined within this database. <p>
0813:             *
0814:             * Each row is a catalog name description with the following column: <p>
0815:             *
0816:             * <pre class="SqlCodeExample">
0817:             * TABLE_CAT   VARCHAR   catalog name
0818:             * </pre> <p>
0819:             *
0820:             * @return a <code>Table</code> object naming the accessible
0821:             *        catalogs defined within this database
0822:             * @throws HsqlException if an error occurs while producing the table
0823:             */
0824:            final Table SYSTEM_CATALOGS() throws HsqlException {
0825:
0826:                Table t = sysTables[SYSTEM_CATALOGS];
0827:
0828:                if (t == null) {
0829:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_CATALOGS]);
0830:
0831:                    addColumn(t, "TABLE_CAT", Types.VARCHAR, false); // not null
0832:
0833:                    // order:  TABLE_CAT
0834:                    // true PK
0835:                    t.createPrimaryKey(null, new int[] { 0 }, true);
0836:
0837:                    return t;
0838:                }
0839:
0840:                Object[] row;
0841:                Iterator catalogs;
0842:
0843:                catalogs = ns.iterateCatalogNames();
0844:
0845:                while (catalogs.hasNext()) {
0846:                    row = t.getEmptyRowData();
0847:                    row[0] = (String) catalogs.next();
0848:
0849:                    t.insertSys(row);
0850:                }
0851:
0852:                t.setDataReadOnly(true);
0853:
0854:                return t;
0855:            }
0856:
0857:            /**
0858:             * Retrieves a <code>Table</code> object describing the visible
0859:             * access rights for all visible columns of all accessible
0860:             * tables defined within this database.<p>
0861:             *
0862:             * Each row is a column privilege description with the following
0863:             * columns: <p>
0864:             *
0865:             * <pre class="SqlCodeExample">
0866:             * TABLE_CAT    VARCHAR   table catalog
0867:             * TABLE_SCHEM  VARCHAR   table schema
0868:             * TABLE_NAME   VARCHAR   table name
0869:             * COLUMN_NAME  VARCHAR   column name
0870:             * GRANTOR      VARCHAR   grantor of access
0871:             * GRANTEE      VARCHAR   grantee of access
0872:             * PRIVILEGE    VARCHAR   name of access
0873:             * IS_GRANTABLE VARCHAR   grantable?: "YES" - grant to others, else "NO"
0874:             * </pre>
0875:             *
0876:             * <b>Note:</b> As of 1.7.2, HSQLDB does not support column level
0877:             * privileges. However, it does support table-level privileges, so they
0878:             * are reflected here.  That is, the content of this table is equivalent
0879:             * to a projection of SYSTEM_TABLEPRIVILEGES and SYSTEM_COLUMNS joined by
0880:             * full table identifier. <p>
0881:             *
0882:             * @return a <code>Table</code> object describing the visible
0883:             *        access rights for all visible columns of
0884:             *        all accessible tables defined within this
0885:             *        database
0886:             * @throws HsqlException if an error occurs while producing the table
0887:             */
0888:            final Table SYSTEM_COLUMNPRIVILEGES() throws HsqlException {
0889:
0890:                Table t = sysTables[SYSTEM_COLUMNPRIVILEGES];
0891:
0892:                if (t == null) {
0893:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_COLUMNPRIVILEGES]);
0894:
0895:                    addColumn(t, "TABLE_CAT", Types.VARCHAR);
0896:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
0897:                    addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
0898:                    addColumn(t, "COLUMN_NAME", Types.VARCHAR, false); // not null
0899:                    addColumn(t, "GRANTOR", Types.VARCHAR, false); // not null
0900:                    addColumn(t, "GRANTEE", Types.VARCHAR, false); // not null
0901:                    addColumn(t, "PRIVILEGE", Types.VARCHAR, 10, false); // not null
0902:                    addColumn(t, "IS_GRANTABLE", Types.VARCHAR, 3, false); // not null
0903:
0904:                    // order: COLUMN_NAME, PRIVILEGE
0905:                    // for unique: GRANTEE, GRANTOR, TABLE_NAME, TABLE_SCHEM, TABLE_CAT
0906:                    // false PK, as TABLE_SCHEM and/or TABLE_CAT may be null
0907:                    t.createPrimaryKey(null, new int[] { 3, 6, 5, 4, 2, 1, 0 },
0908:                            false);
0909:
0910:                    return t;
0911:                }
0912:
0913:                Result rs;
0914:
0915:                rs = session
0916:                        .sqlExecuteDirectNoPreChecks("select a.TABLE_CAT, a.TABLE_SCHEM, a.TABLE_NAME, b.COLUMN_NAME, "
0917:                                + "a.GRANTOR, a.GRANTEE, a.PRIVILEGE, a.IS_GRANTABLE "
0918:                                + "from  INFORMATION_SCHEMA.SYSTEM_TABLEPRIVILEGES a, "
0919:                                + "INFORMATION_SCHEMA.SYSTEM_COLUMNS b "
0920:                                + "where a.TABLE_NAME = b.TABLE_NAME and "
0921:                                + "a.TABLE_SCHEM = b.TABLE_SCHEM;");
0922:
0923:                t.insertSys(rs);
0924:                t.setDataReadOnly(true);
0925:
0926:                return t;
0927:            }
0928:
0929:            /**
0930:             * Retrieves a <code>Table</code> object describing the
0931:             * visible columns of all accessible tables defined
0932:             * within this database.<p>
0933:             *
0934:             * Each row is a column description with the following columns: <p>
0935:             *
0936:             * <pre class="SqlCodeExample">
0937:             * TABLE_CAT         VARCHAR   table catalog
0938:             * TABLE_SCHEM       VARCHAR   table schema
0939:             * TABLE_NAME        VARCHAR   table name
0940:             * COLUMN_NAME       VARCHAR   column name
0941:             * DATA_TYPE         SMALLINT  SQL type from DITypes
0942:             * TYPE_NAME         VARCHAR   canonical type name
0943:             * COLUMN_SIZE       INTEGER   column size (length/precision)
0944:             * BUFFER_LENGTH     INTEGER   transfer size in bytes, if definitely known
0945:             * DECIMAL_DIGITS    INTEGER   # of fractional digits (scale)
0946:             * NUM_PREC_RADIX    INTEGER   Radix
0947:             * NULLABLE          INTEGER   is NULL allowed? (from DatabaseMetaData)
0948:             * REMARKS           VARCHAR   comment describing column
0949:             * COLUMN_DEF        VARCHAR   default value (possibly expression)
0950:             * SQL_DATA_TYPE     VARCHAR   type code as expected in the SQL CLI SQLDA
0951:             * SQL_DATETIME_SUB  INTEGER   the SQL CLI subtype for DATETIME types
0952:             * CHAR_OCTET_LENGTH INTEGER   for char types, max # of bytes in column
0953:             * ORDINAL_POSITION  INTEGER   1-based index of column in table
0954:             * IS_NULLABLE       VARCHAR   is column nullable? ("YES"|"NO"|""}
0955:             * SCOPE_CATLOG      VARCHAR   catalog of REF attribute scope table
0956:             * SCOPE_SCHEMA      VARCHAR   schema of REF attribute scope table
0957:             * SCOPE_TABLE       VARCHAR   name of REF attribute scope table
0958:             * SOURCE_DATA_TYPE  VARCHAR   source type of REF attribute
0959:             * TYPE_SUB          INTEGER   HSQLDB data subtype code
0960:             * </pre> <p>
0961:             *
0962:             * @return a <code>Table</code> object describing the
0963:             *        visible columns of all accessible
0964:             *        tables defined within this database.<p>
0965:             * @throws HsqlException if an error occurs while producing the table
0966:             */
0967:            final Table SYSTEM_COLUMNS() throws HsqlException {
0968:
0969:                Table t = sysTables[SYSTEM_COLUMNS];
0970:
0971:                if (t == null) {
0972:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_COLUMNS]);
0973:
0974:                    addColumn(t, "TABLE_CAT", Types.VARCHAR);
0975:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
0976:                    addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
0977:                    addColumn(t, "COLUMN_NAME", Types.VARCHAR, false); // not null
0978:                    addColumn(t, "DATA_TYPE", Types.SMALLINT, false); // not null
0979:                    addColumn(t, "TYPE_NAME", Types.VARCHAR, 32, false); // not null
0980:                    addColumn(t, "COLUMN_SIZE", Types.INTEGER);
0981:                    addColumn(t, "BUFFER_LENGTH", Types.INTEGER);
0982:                    addColumn(t, "DECIMAL_DIGITS", Types.INTEGER);
0983:                    addColumn(t, "NUM_PREC_RADIX", Types.INTEGER);
0984:                    addColumn(t, "NULLABLE", Types.INTEGER, false); // not null
0985:                    addColumn(t, "REMARKS", Types.VARCHAR);
0986:                    addColumn(t, "COLUMN_DEF", Types.VARCHAR);
0987:                    addColumn(t, "SQL_DATA_TYPE", Types.INTEGER);
0988:                    addColumn(t, "SQL_DATETIME_SUB", Types.INTEGER);
0989:                    addColumn(t, "CHAR_OCTET_LENGTH", Types.INTEGER);
0990:                    addColumn(t, "ORDINAL_POSITION", Types.INTEGER, false); // not null
0991:                    addColumn(t, "IS_NULLABLE", Types.VARCHAR, 3, false); // not null
0992:                    addColumn(t, "SCOPE_CATLOG", Types.VARCHAR);
0993:                    addColumn(t, "SCOPE_SCHEMA", Types.VARCHAR);
0994:                    addColumn(t, "SCOPE_TABLE", Types.VARCHAR);
0995:                    addColumn(t, "SOURCE_DATA_TYPE", Types.VARCHAR);
0996:                    addColumn(t, "TYPE_SUB", Types.INTEGER, false); // not null
0997:
0998:                    // order: TABLE_SCHEM, TABLE_NAME, ORDINAL_POSITION
0999:                    // added for unique: TABLE_CAT
1000:                    // false PK, as TABLE_SCHEM and/or TABLE_CAT may be null
1001:                    t.createPrimaryKey(null, new int[] { 1, 2, 16, 0 }, false);
1002:
1003:                    return t;
1004:                }
1005:
1006:                // calculated column values
1007:                String tableCatalog;
1008:                String tableSchema;
1009:                String tableName;
1010:
1011:                // intermediate holders
1012:                int columnCount;
1013:                Iterator tables;
1014:                Table table;
1015:                Object[] row;
1016:                DITableInfo ti;
1017:
1018:                // column number mappings
1019:                final int itable_cat = 0;
1020:                final int itable_schem = 1;
1021:                final int itable_name = 2;
1022:                final int icolumn_name = 3;
1023:                final int idata_type = 4;
1024:                final int itype_name = 5;
1025:                final int icolumn_size = 6;
1026:                final int ibuffer_length = 7;
1027:                final int idecimal_digits = 8;
1028:                final int inum_prec_radix = 9;
1029:                final int inullable = 10;
1030:                final int iremark = 11;
1031:                final int icolumn_def = 12;
1032:                final int isql_data_type = 13;
1033:                final int isql_datetime_sub = 14;
1034:                final int ichar_octet_length = 15;
1035:                final int iordinal_position = 16;
1036:                final int iis_nullable = 17;
1037:                final int itype_sub = 22;
1038:
1039:                // Initialization
1040:                tables = allTables();
1041:                ti = new DITableInfo();
1042:
1043:                // Do it.
1044:                while (tables.hasNext()) {
1045:                    table = (Table) tables.next();
1046:
1047:                    if (!isAccessibleTable(table)) {
1048:                        continue;
1049:                    }
1050:
1051:                    ti.setTable(table);
1052:
1053:                    tableCatalog = ns.getCatalogName(table);
1054:                    tableSchema = table.getSchemaName();
1055:                    tableName = ti.getName();
1056:                    columnCount = table.getColumnCount();
1057:
1058:                    for (int i = 0; i < columnCount; i++) {
1059:                        row = t.getEmptyRowData();
1060:                        row[itable_cat] = tableCatalog;
1061:                        row[itable_schem] = tableSchema;
1062:                        row[itable_name] = tableName;
1063:                        row[icolumn_name] = ti.getColName(i);
1064:                        row[idata_type] = ti.getColDataType(i);
1065:                        row[itype_name] = ti.getColDataTypeName(i);
1066:                        row[icolumn_size] = ti.getColSize(i);
1067:                        row[ibuffer_length] = ti.getColBufLen(i);
1068:                        row[idecimal_digits] = ti.getColScale(i);
1069:                        row[inum_prec_radix] = ti.getColPrecRadix(i);
1070:                        row[inullable] = ti.getColNullability(i);
1071:                        row[iremark] = ti.getColRemarks(i);
1072:                        row[icolumn_def] = ti.getColDefault(i);
1073:                        row[isql_data_type] = ti.getColSqlDataType(i);
1074:                        row[isql_datetime_sub] = ti.getColSqlDateTimeSub(i);
1075:                        row[ichar_octet_length] = ti.getColCharOctLen(i);
1076:                        row[iordinal_position] = ValuePool.getInt(i + 1);
1077:                        row[iis_nullable] = ti.getColIsNullable(i);
1078:                        row[itype_sub] = ti.getColDataTypeSub(i);
1079:
1080:                        t.insertSys(row);
1081:                    }
1082:                }
1083:
1084:                t.setDataReadOnly(true);
1085:
1086:                return t;
1087:            }
1088:
1089:            /**
1090:             * Retrieves a <code>Table</code> object describing, for each
1091:             * accessible referencing and referenced table, how the referencing
1092:             * tables import, for the purposes of referential integrity,
1093:             * the columns of the referenced tables.<p>
1094:             *
1095:             * Each row is a foreign key column description with the following
1096:             * columns: <p>
1097:             *
1098:             * <pre class="SqlCodeExample">
1099:             * PKTABLE_CAT   VARCHAR   referenced table catalog
1100:             * PKTABLE_SCHEM VARCHAR   referenced table schema
1101:             * PKTABLE_NAME  VARCHAR   referenced table name
1102:             * PKCOLUMN_NAME VARCHAR   referenced column name
1103:             * FKTABLE_CAT   VARCHAR   referencing table catalog
1104:             * FKTABLE_SCHEM VARCHAR   referencing table schema
1105:             * FKTABLE_NAME  VARCHAR   referencing table name
1106:             * FKCOLUMN_NAME VARCHAR   referencing column
1107:             * KEY_SEQ       SMALLINT  sequence number within foreign key
1108:             * UPDATE_RULE   SMALLINT
1109:             *    { Cascade | Set Null | Set Default | Restrict (No Action)}?
1110:             * DELETE_RULE   SMALLINT
1111:             *    { Cascade | Set Null | Set Default | Restrict (No Action)}?
1112:             * FK_NAME       VARCHAR   foreign key constraint name
1113:             * PK_NAME       VARCHAR   primary key or unique constraint name
1114:             * DEFERRABILITY SMALLINT
1115:             *    { initially deferred | initially immediate | not deferrable }
1116:             * </pre> <p>
1117:             *
1118:             * @return a <code>Table</code> object describing how accessible tables
1119:             *      import other accessible tables' primary key and/or unique
1120:             *      constraint columns
1121:             * @throws HsqlException if an error occurs while producing the table
1122:             */
1123:            final Table SYSTEM_CROSSREFERENCE() throws HsqlException {
1124:
1125:                Table t = sysTables[SYSTEM_CROSSREFERENCE];
1126:
1127:                if (t == null) {
1128:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_CROSSREFERENCE]);
1129:
1130:                    addColumn(t, "PKTABLE_CAT", Types.VARCHAR);
1131:                    addColumn(t, "PKTABLE_SCHEM", Types.VARCHAR);
1132:                    addColumn(t, "PKTABLE_NAME", Types.VARCHAR, false); // not null
1133:                    addColumn(t, "PKCOLUMN_NAME", Types.VARCHAR, false); // not null
1134:                    addColumn(t, "FKTABLE_CAT", Types.VARCHAR);
1135:                    addColumn(t, "FKTABLE_SCHEM", Types.VARCHAR);
1136:                    addColumn(t, "FKTABLE_NAME", Types.VARCHAR, false); // not null
1137:                    addColumn(t, "FKCOLUMN_NAME", Types.VARCHAR, false); // not null
1138:                    addColumn(t, "KEY_SEQ", Types.SMALLINT, false); // not null
1139:                    addColumn(t, "UPDATE_RULE", Types.SMALLINT, false); // not null
1140:                    addColumn(t, "DELETE_RULE", Types.SMALLINT, false); // not null
1141:                    addColumn(t, "FK_NAME", Types.VARCHAR);
1142:                    addColumn(t, "PK_NAME", Types.VARCHAR);
1143:                    addColumn(t, "DEFERRABILITY", Types.SMALLINT, false); // not null
1144:
1145:                    // order: FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, and KEY_SEQ
1146:                    // added for unique: FK_NAME
1147:                    // false PK, as FKTABLE_CAT, FKTABLE_SCHEM and/or FK_NAME
1148:                    // may be null
1149:                    t.createPrimaryKey(null, new int[] { 4, 5, 6, 8, 11 },
1150:                            false);
1151:
1152:                    return t;
1153:                }
1154:
1155:                // calculated column values
1156:                String pkTableCatalog;
1157:                String pkTableSchema;
1158:                String pkTableName;
1159:                String pkColumnName;
1160:                String fkTableCatalog;
1161:                String fkTableSchema;
1162:                String fkTableName;
1163:                String fkColumnName;
1164:                Integer keySequence;
1165:                Integer updateRule;
1166:                Integer deleteRule;
1167:                String fkName;
1168:                String pkName;
1169:                Integer deferrability;
1170:
1171:                // Intermediate holders
1172:                Iterator tables;
1173:                Table table;
1174:                Table fkTable;
1175:                Table pkTable;
1176:                int columnCount;
1177:                int[] mainCols;
1178:                int[] refCols;
1179:                Constraint[] constraints;
1180:                Constraint constraint;
1181:                int constraintCount;
1182:                HsqlArrayList fkConstraintsList;
1183:                Object[] row;
1184:                DITableInfo pkInfo;
1185:                DITableInfo fkInfo;
1186:
1187:                // column number mappings
1188:                final int ipk_table_cat = 0;
1189:                final int ipk_table_schem = 1;
1190:                final int ipk_table_name = 2;
1191:                final int ipk_column_name = 3;
1192:                final int ifk_table_cat = 4;
1193:                final int ifk_table_schem = 5;
1194:                final int ifk_table_name = 6;
1195:                final int ifk_column_name = 7;
1196:                final int ikey_seq = 8;
1197:                final int iupdate_rule = 9;
1198:                final int idelete_rule = 10;
1199:                final int ifk_name = 11;
1200:                final int ipk_name = 12;
1201:                final int ideferrability = 13;
1202:
1203:                tables = database.schemaManager.allTablesIterator();
1204:                pkInfo = new DITableInfo();
1205:                fkInfo = new DITableInfo();
1206:
1207:                // the only deferrability rule currently supported by hsqldb is:
1208:                deferrability = ValuePool.getInt(Constraint.NOT_DEFERRABLE);
1209:
1210:                // We must consider all the constraints in all the user tables, since
1211:                // this is where reference relationships are recorded.  However, we
1212:                // are only concerned with Constraint.FOREIGN_KEY constraints here
1213:                // because their corresponing Constraint.MAIN entries are essentially
1214:                // duplicate data recorded in the referenced rather than the
1215:                // referencing table.  Also, we skip constraints where either
1216:                // the referenced, referencing or both tables are not accessible
1217:                // relative to the session of the calling context
1218:                fkConstraintsList = new HsqlArrayList();
1219:
1220:                while (tables.hasNext()) {
1221:                    table = (Table) tables.next();
1222:
1223:                    if (!isAccessibleTable(table)) {
1224:                        continue;
1225:                    }
1226:
1227:                    constraints = table.getConstraints();
1228:                    constraintCount = constraints.length;
1229:
1230:                    for (int i = 0; i < constraintCount; i++) {
1231:                        constraint = (Constraint) constraints[i];
1232:
1233:                        if (constraint.getType() == Constraint.FOREIGN_KEY
1234:                                && isAccessibleTable(constraint.getRef())) {
1235:                            fkConstraintsList.add(constraint);
1236:                        }
1237:                    }
1238:                }
1239:
1240:                // Now that we have all of the desired constraints, we need to
1241:                // process them, generating one row in our ouput table for each
1242:                // imported/exported column pair of each constraint.
1243:                // Do it.
1244:                for (int i = 0; i < fkConstraintsList.size(); i++) {
1245:                    constraint = (Constraint) fkConstraintsList.get(i);
1246:                    pkTable = constraint.getMain();
1247:
1248:                    pkInfo.setTable(pkTable);
1249:
1250:                    pkTableName = pkInfo.getName();
1251:                    fkTable = constraint.getRef();
1252:
1253:                    fkInfo.setTable(fkTable);
1254:
1255:                    fkTableName = fkInfo.getName();
1256:                    pkTableCatalog = ns.getCatalogName(pkTable);
1257:                    pkTableSchema = pkTable.getSchemaName();
1258:                    fkTableCatalog = ns.getCatalogName(fkTable);
1259:                    fkTableSchema = fkTable.getSchemaName();
1260:                    mainCols = constraint.getMainColumns();
1261:                    refCols = constraint.getRefColumns();
1262:                    columnCount = refCols.length;
1263:                    fkName = constraint.getFkName();
1264:                    pkName = constraint.getPkName();
1265:
1266:                    //pkName = constraint.getMainIndex().getName().name;
1267:                    deleteRule = ValuePool.getInt(constraint.getDeleteAction());
1268:                    updateRule = ValuePool.getInt(constraint.getUpdateAction());
1269:
1270:                    for (int j = 0; j < columnCount; j++) {
1271:                        keySequence = ValuePool.getInt(j + 1);
1272:                        pkColumnName = pkInfo.getColName(mainCols[j]);
1273:                        fkColumnName = fkInfo.getColName(refCols[j]);
1274:                        row = t.getEmptyRowData();
1275:                        row[ipk_table_cat] = pkTableCatalog;
1276:                        row[ipk_table_schem] = pkTableSchema;
1277:                        row[ipk_table_name] = pkTableName;
1278:                        row[ipk_column_name] = pkColumnName;
1279:                        row[ifk_table_cat] = fkTableCatalog;
1280:                        row[ifk_table_schem] = fkTableSchema;
1281:                        row[ifk_table_name] = fkTableName;
1282:                        row[ifk_column_name] = fkColumnName;
1283:                        row[ikey_seq] = keySequence;
1284:                        row[iupdate_rule] = updateRule;
1285:                        row[idelete_rule] = deleteRule;
1286:                        row[ifk_name] = fkName;
1287:                        row[ipk_name] = pkName;
1288:                        row[ideferrability] = deferrability;
1289:
1290:                        t.insertSys(row);
1291:                    }
1292:                }
1293:
1294:                t.setDataReadOnly(true);
1295:
1296:                return t;
1297:            }
1298:
1299:            /**
1300:             * Retrieves a <code>Table</code> object describing the visible
1301:             * <code>Index</code> objects for each accessible table defined
1302:             * within this database.<p>
1303:             *
1304:             * Each row is an index column description with the following
1305:             * columns: <p>
1306:             *
1307:             * <pre class="SqlCodeExample">
1308:             * TABLE_CAT        VARCHAR   table's catalog
1309:             * TABLE_SCHEM      VARCHAR   simple name of table's schema
1310:             * TABLE_NAME       VARCHAR   simple name of the table using the index
1311:             * NON_UNIQUE       BOOLEAN   can index values be non-unique?
1312:             * INDEX_QUALIFIER  VARCHAR   catalog in which the index is defined
1313:             * INDEX_NAME       VARCHAR   simple name of the index
1314:             * TYPE             SMALLINT  index type: { Clustered | Hashed | Other }
1315:             * ORDINAL_POSITION SMALLINT  column sequence number within index
1316:             * COLUMN_NAME      VARCHAR   simple column name
1317:             * ASC_OR_DESC      VARCHAR   col. sort sequence: {"A" (Asc) | "D" (Desc)}
1318:             * CARDINALITY      INTEGER   # of unique values in index (not implemented)
1319:             * PAGES            INTEGER   index page use (not implemented)
1320:             * FILTER_CONDITION VARCHAR   filter condition, if any (not implemented)
1321:             * // HSQLDB-extension
1322:             * ROW_CARDINALITY  INTEGER   total # of rows in index (not implemented)
1323:             * </pre> <p>
1324:             *
1325:             * @return a <code>Table</code> object describing the visible
1326:             *        <code>Index</code> objects for each accessible
1327:             *        table defined within this database.
1328:             * @throws HsqlException if an error occurs while producing the table
1329:             */
1330:            final Table SYSTEM_INDEXINFO() throws HsqlException {
1331:
1332:                Table t = sysTables[SYSTEM_INDEXINFO];
1333:
1334:                if (t == null) {
1335:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_INDEXINFO]);
1336:
1337:                    // JDBC
1338:                    addColumn(t, "TABLE_CAT", Types.VARCHAR);
1339:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1340:                    addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // NOT NULL
1341:                    addColumn(t, "NON_UNIQUE", Types.BOOLEAN, false); // NOT NULL
1342:                    addColumn(t, "INDEX_QUALIFIER", Types.VARCHAR);
1343:                    addColumn(t, "INDEX_NAME", Types.VARCHAR);
1344:                    addColumn(t, "TYPE", Types.SMALLINT, false); // NOT NULL
1345:                    addColumn(t, "ORDINAL_POSITION", Types.SMALLINT, false); // NOT NULL
1346:                    addColumn(t, "COLUMN_NAME", Types.VARCHAR);
1347:                    addColumn(t, "ASC_OR_DESC", Types.VARCHAR, 1, true);
1348:                    addColumn(t, "CARDINALITY", Types.INTEGER);
1349:                    addColumn(t, "PAGES", Types.INTEGER);
1350:                    addColumn(t, "FILTER_CONDITION", Types.VARCHAR);
1351:
1352:                    // HSQLDB extension
1353:                    addColumn(t, "ROW_CARDINALITY", Types.INTEGER);
1354:
1355:                    // order: NON_UNIQUE, TYPE, INDEX_NAME, and ORDINAL_POSITION.
1356:                    // added for unique: INDEX_QUALIFIER, TABLE_NAME
1357:                    // false PK, as INDEX_QUALIFIER may be null
1358:                    t.createPrimaryKey(null, new int[] { 3, 6, 5, 7, 4, 2 },
1359:                            false);
1360:
1361:                    return t;
1362:                }
1363:
1364:                // calculated column values
1365:                String tableCatalog;
1366:                String tableSchema;
1367:                String tableName;
1368:                Boolean nonUnique;
1369:                String indexQualifier;
1370:                String indexName;
1371:                Integer indexType;
1372:
1373:                //Integer ordinalPosition;
1374:                //String  columnName;
1375:                //String  ascOrDesc;
1376:                Integer cardinality;
1377:                Integer pages;
1378:                String filterCondition;
1379:                Integer rowCardinality;
1380:
1381:                // Intermediate holders
1382:                Iterator tables;
1383:                Table table;
1384:                int indexCount;
1385:                int[] cols;
1386:                int col;
1387:                int colCount;
1388:                Object[] row;
1389:                DITableInfo ti;
1390:                HsqlProperties p;
1391:
1392:                // column number mappings
1393:                final int itable_cat = 0;
1394:                final int itable_schem = 1;
1395:                final int itable_name = 2;
1396:                final int inon_unique = 3;
1397:                final int iindex_qualifier = 4;
1398:                final int iindex_name = 5;
1399:                final int itype = 6;
1400:                final int iordinal_position = 7;
1401:                final int icolumn_name = 8;
1402:                final int iasc_or_desc = 9;
1403:                final int icardinality = 10;
1404:                final int ipages = 11;
1405:                final int ifilter_condition = 12;
1406:                final int irow_cardinality = 13;
1407:
1408:                // Initialization
1409:                ti = new DITableInfo();
1410:                p = database.getProperties();
1411:                tables = p.isPropertyTrue("hsqldb.system_table_indexinfo") ? allTables()
1412:                        : database.schemaManager.allTablesIterator();
1413:
1414:                // Do it.
1415:                while (tables.hasNext()) {
1416:                    table = (Table) tables.next();
1417:
1418:                    if (table.isView() || !isAccessibleTable(table)) {
1419:                        continue;
1420:                    }
1421:
1422:                    ti.setTable(table);
1423:
1424:                    tableCatalog = ns.getCatalogName(table);
1425:                    tableSchema = table.getSchemaName();
1426:                    tableName = ti.getName();
1427:
1428:                    // not supported yet
1429:                    filterCondition = null;
1430:
1431:                    // different cat for index not supported yet
1432:                    indexQualifier = tableCatalog;
1433:                    indexCount = table.getIndexCount();
1434:
1435:                    // process all of the visible indices for this table
1436:                    for (int i = 0; i < indexCount; i++) {
1437:                        colCount = ti.getIndexVisibleColumns(i);
1438:
1439:                        if (colCount < 1) {
1440:                            continue;
1441:                        }
1442:
1443:                        indexName = ti.getIndexName(i);
1444:                        nonUnique = ti.isIndexNonUnique(i);
1445:                        cardinality = ti.getIndexCardinality(i);
1446:                        pages = ValuePool.getInt(0);
1447:                        rowCardinality = ti.getIndexRowCardinality(i);
1448:                        cols = ti.getIndexColumns(i);
1449:                        indexType = ti.getIndexType(i);
1450:
1451:                        for (int k = 0; k < colCount; k++) {
1452:                            col = cols[k];
1453:                            row = t.getEmptyRowData();
1454:                            row[itable_cat] = tableCatalog;
1455:                            row[itable_schem] = tableSchema;
1456:                            row[itable_name] = tableName;
1457:                            row[inon_unique] = nonUnique;
1458:                            row[iindex_qualifier] = indexQualifier;
1459:                            row[iindex_name] = indexName;
1460:                            row[itype] = indexType;
1461:                            row[iordinal_position] = ValuePool.getInt(k + 1);
1462:                            row[icolumn_name] = ti.getColName(col);
1463:                            row[iasc_or_desc] = ti.getIndexColDirection(i, col);
1464:                            row[icardinality] = cardinality;
1465:                            row[ipages] = pages;
1466:                            row[irow_cardinality] = rowCardinality;
1467:                            row[ifilter_condition] = filterCondition;
1468:
1469:                            t.insertSys(row);
1470:                        }
1471:                    }
1472:                }
1473:
1474:                t.setDataReadOnly(true);
1475:
1476:                return t;
1477:            }
1478:
1479:            /**
1480:             * Retrieves a <code>Table</code> object describing the visible
1481:             * primary key columns of each accessible table defined within
1482:             * this database. <p>
1483:             *
1484:             * Each row is a PRIMARY KEY column description with the following
1485:             * columns: <p>
1486:             *
1487:             * <pre class="SqlCodeExample">
1488:             * TABLE_CAT   VARCHAR   table catalog
1489:             * TABLE_SCHEM VARCHAR   table schema
1490:             * TABLE_NAME  VARCHAR   table name
1491:             * COLUMN_NAME VARCHAR   column name
1492:             * KEY_SEQ     SMALLINT  sequence number within primary key
1493:             * PK_NAME     VARCHAR   primary key constraint name
1494:             * </pre> <p>
1495:             *
1496:             * @return a <code>Table</code> object describing the visible
1497:             *        primary key columns of each accessible table
1498:             *        defined within this database.
1499:             * @throws HsqlException if an error occurs while producing the table
1500:             */
1501:            final Table SYSTEM_PRIMARYKEYS() throws HsqlException {
1502:
1503:                Table t = sysTables[SYSTEM_PRIMARYKEYS];
1504:
1505:                if (t == null) {
1506:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_PRIMARYKEYS]);
1507:
1508:                    addColumn(t, "TABLE_CAT", Types.VARCHAR);
1509:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1510:                    addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
1511:                    addColumn(t, "COLUMN_NAME", Types.VARCHAR, false); // not null
1512:                    addColumn(t, "KEY_SEQ", Types.SMALLINT, false); // not null
1513:                    addColumn(t, "PK_NAME", Types.VARCHAR);
1514:
1515:                    // order: COLUMN_NAME
1516:                    // added for unique: TABLE_NAME, TABLE_SCHEM, TABLE_CAT
1517:                    // false PK, as  TABLE_SCHEM and/or TABLE_CAT may be null
1518:                    t.createPrimaryKey(null, new int[] { 3, 2, 1, 0 }, false);
1519:
1520:                    return t;
1521:                }
1522:
1523:                // calculated column values
1524:                String tableCatalog;
1525:                String tableSchema;
1526:                String tableName;
1527:
1528:                //String  columnName;
1529:                //Integer keySequence;
1530:                String primaryKeyName;
1531:
1532:                // Intermediate holders
1533:                Iterator tables;
1534:                Table table;
1535:                Object[] row;
1536:                Constraint constraint;
1537:                int[] cols;
1538:                int colCount;
1539:                HsqlProperties p;
1540:
1541:                // column number mappings
1542:                final int itable_cat = 0;
1543:                final int itable_schem = 1;
1544:                final int itable_name = 2;
1545:                final int icolumn_name = 3;
1546:                final int ikey_seq = 4;
1547:                final int ipk_name = 5;
1548:
1549:                // Initialization
1550:                p = database.getProperties();
1551:                tables = p.isPropertyTrue("hsqldb.system_table_primarykeys") ? allTables()
1552:                        : database.schemaManager.allTablesIterator();
1553:
1554:                while (tables.hasNext()) {
1555:                    table = (Table) tables.next();
1556:
1557:                    if (table.isView() || !isAccessibleTable(table)
1558:                            || !table.hasPrimaryKey()) {
1559:                        continue;
1560:                    }
1561:
1562:                    constraint = table.getPrimaryConstraint();
1563:                    tableCatalog = ns.getCatalogName(table);
1564:                    tableSchema = table.getSchemaName();
1565:                    tableName = table.getName().name;
1566:                    primaryKeyName = constraint.getName().name;
1567:                    cols = constraint.getMainColumns();
1568:                    colCount = cols.length;
1569:
1570:                    for (int j = 0; j < colCount; j++) {
1571:                        row = t.getEmptyRowData();
1572:                        row[itable_cat] = tableCatalog;
1573:                        row[itable_schem] = tableSchema;
1574:                        row[itable_name] = tableName;
1575:                        row[icolumn_name] = table.getColumn(cols[j]).columnName.name;
1576:                        row[ikey_seq] = ValuePool.getInt(j + 1);
1577:                        row[ipk_name] = primaryKeyName;
1578:
1579:                        t.insertSys(row);
1580:                    }
1581:                }
1582:
1583:                t.setDataReadOnly(true);
1584:
1585:                return t;
1586:            }
1587:
1588:            /**
1589:             * Retrieves a <code>Table</code> object describing the
1590:             * return, parameter and result columns of the accessible
1591:             * routines defined within this database.<p>
1592:             *
1593:             * Each row is a procedure column description with the following
1594:             * columns: <p>
1595:             *
1596:             * <pre class="SqlCodeExample">
1597:             * PROCEDURE_CAT   VARCHAR   routine catalog
1598:             * PROCEDURE_SCHEM VARCHAR   routine schema
1599:             * PROCEDURE_NAME  VARCHAR   routine name
1600:             * COLUMN_NAME     VARCHAR   column/parameter name
1601:             * COLUMN_TYPE     SMALLINT  kind of column/parameter
1602:             * DATA_TYPE       SMALLINT  SQL type from DITypes
1603:             * TYPE_NAME       VARCHAR   SQL type name
1604:             * PRECISION       INTEGER   precision (length) of type
1605:             * LENGTH          INTEGER   transfer size, in bytes, if definitely known
1606:             *                           (roughly equivalent to BUFFER_SIZE for table
1607:             *                           columns)
1608:             * SCALE           SMALLINT  scale
1609:             * RADIX           SMALLINT  radix
1610:             * NULLABLE        SMALLINT  can column contain NULL?
1611:             * REMARKS         VARCHAR   explanatory comment on column
1612:             * SPECIFIC_NAME   VARCHAR   typically (but not restricted to) a
1613:             *                           fully qualified Java Method name and signature
1614:             * SEQ             INTEGER   The JDBC-specified order within
1615:             *                           runs of PROCEDURE_SCHEM, PROCEDURE_NAME,
1616:             *                           SPECIFIC_NAME, which is:
1617:             *
1618:             *                           return value (0), if any, first, followed
1619:             *                           by the parameter descriptions in call order
1620:             *                           (1..n1), followed by the result column
1621:             *                           descriptions in column number order
1622:             *                           (n1 + 1..n1 + n2)
1623:             * </pre> <p>
1624:             *
1625:             * @return a <code>Table</code> object describing the
1626:             *        return, parameter and result columns
1627:             *        of the accessible routines defined
1628:             *        within this database.
1629:             * @throws HsqlException if an error occurs while producing the table
1630:             */
1631:            Table SYSTEM_PROCEDURECOLUMNS() throws HsqlException {
1632:
1633:                Table t = sysTables[SYSTEM_PROCEDURECOLUMNS];
1634:
1635:                if (t == null) {
1636:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_PROCEDURECOLUMNS]);
1637:
1638:                    // ----------------------------------------------------------------
1639:                    // required
1640:                    // ----------------------------------------------------------------
1641:                    addColumn(t, "PROCEDURE_CAT", Types.VARCHAR);
1642:                    addColumn(t, "PROCEDURE_SCHEM", Types.VARCHAR);
1643:                    addColumn(t, "PROCEDURE_NAME", Types.VARCHAR, false); // not null
1644:                    addColumn(t, "COLUMN_NAME", Types.VARCHAR, false); // not null
1645:                    addColumn(t, "COLUMN_TYPE", Types.SMALLINT, false); // not null
1646:                    addColumn(t, "DATA_TYPE", Types.SMALLINT, false); // not null
1647:                    addColumn(t, "TYPE_NAME", Types.VARCHAR, 32, false); // not null
1648:                    addColumn(t, "PRECISION", Types.INTEGER);
1649:                    addColumn(t, "LENGTH", Types.INTEGER);
1650:                    addColumn(t, "SCALE", Types.SMALLINT);
1651:                    addColumn(t, "RADIX", Types.SMALLINT);
1652:                    addColumn(t, "NULLABLE", Types.SMALLINT, false); // not null
1653:                    addColumn(t, "REMARKS", Types.VARCHAR);
1654:
1655:                    // ----------------------------------------------------------------
1656:                    // extended (and required for JDBC sort contract w.r.t. overloading)
1657:                    // ----------------------------------------------------------------
1658:                    addColumn(t, "SPECIFIC_NAME", Types.VARCHAR, false); // not null
1659:
1660:                    // ----------------------------------------------------------------
1661:                    // just required for JDBC sort contract
1662:                    // ----------------------------------------------------------------
1663:                    addColumn(t, "SEQ", Types.INTEGER, false); // not null
1664:
1665:                    // ----------------------------------------------------------------
1666:                    // order: PROCEDURE_SCHEM, PROCEDURE_NAME, SPECIFIC_NAME, SEQ
1667:                    // added for unique: PROCEDURE_CAT
1668:                    // false PK, as PROCEDURE_SCHEM and/or PROCEDURE_CAT may be null
1669:                    t.createPrimaryKey(null, new int[] { 1, 2, 13, 14, 0 },
1670:                            false);
1671:
1672:                    return t;
1673:                }
1674:
1675:                t.setDataReadOnly(true);
1676:
1677:                return t;
1678:            }
1679:
1680:            /**
1681:             * Retrieves a <code>Table</code> object describing the accessible
1682:             * routines defined within this database.
1683:             *
1684:             * Each row is a procedure description with the following
1685:             * columns: <p>
1686:             *
1687:             * <pre class="SqlCodeExample">
1688:             * PROCEDURE_CAT     VARCHAR   catalog in which routine is defined
1689:             * PROCEDURE_SCHEM   VARCHAR   schema in which routine is defined
1690:             * PROCEDURE_NAME    VARCHAR   simple routine identifier
1691:             * NUM_INPUT_PARAMS  INTEGER   number of input parameters
1692:             * NUM_OUTPUT_PARAMS INTEGER   number of output parameters
1693:             * NUM_RESULT_SETS   INTEGER   number of result sets returned
1694:             * REMARKS           VARCHAR   explanatory comment on the routine
1695:             * PROCEDURE_TYPE    SMALLINT  { Unknown | No Result | Returns Result }
1696:             * ORIGIN            VARCHAR   {ALIAS |
1697:             *                             [BUILTIN | USER DEFINED] ROUTINE |
1698:             *                             [BUILTIN | USER DEFINED] TRIGGER |
1699:             *                              ...}
1700:             * SPECIFIC_NAME     VARCHAR   typically (but not restricted to) a
1701:             *                             fully qualified Java Method name
1702:             *                             and signature
1703:             * </pre> <p>
1704:             *
1705:             * @return a <code>Table</code> object describing the accessible
1706:             *        routines defined within the this database
1707:             * @throws HsqlException if an error occurs while producing the table
1708:             */
1709:            Table SYSTEM_PROCEDURES() throws HsqlException {
1710:
1711:                Table t = sysTables[SYSTEM_PROCEDURES];
1712:
1713:                if (t == null) {
1714:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_PROCEDURES]);
1715:
1716:                    // ----------------------------------------------------------------
1717:                    // required
1718:                    // ----------------------------------------------------------------
1719:                    addColumn(t, "PROCEDURE_CAT", Types.VARCHAR);
1720:                    addColumn(t, "PROCEDURE_SCHEM", Types.VARCHAR);
1721:                    addColumn(t, "PROCEDURE_NAME", Types.VARCHAR, false); // not null
1722:                    addColumn(t, "NUM_INPUT_PARAMS", Types.INTEGER);
1723:                    addColumn(t, "NUM_OUTPUT_PARAMS", Types.INTEGER);
1724:                    addColumn(t, "NUM_RESULT_SETS", Types.INTEGER);
1725:                    addColumn(t, "REMARKS", Types.VARCHAR);
1726:
1727:                    // basically: function (returns result), procedure (no return value)
1728:                    // or unknown (say, a trigger callout routine)
1729:                    addColumn(t, "PROCEDURE_TYPE", Types.SMALLINT, false); // not null
1730:
1731:                    // ----------------------------------------------------------------
1732:                    // extended
1733:                    // ----------------------------------------------------------------
1734:                    addColumn(t, "ORIGIN", Types.VARCHAR, 32, false); // not null
1735:                    addColumn(t, "SPECIFIC_NAME", Types.VARCHAR, false); // not null
1736:
1737:                    // ----------------------------------------------------------------
1738:                    // order: PROCEDURE_SCHEM and PROCEDURE_NAME.
1739:                    // added for uniqe: SPECIFIC_NAME, PROCEDURE_CAT
1740:                    // false PK, as PROCEDURE_SCHEM and/or PROCEDURE_CAT may be null
1741:                    t.createPrimaryKey(null, new int[] { 1, 2, 9, 0 }, false);
1742:
1743:                    return t;
1744:                }
1745:
1746:                t.setDataReadOnly(true);
1747:
1748:                return t;
1749:            }
1750:
1751:            /**
1752:             * Retrieves a <code>Table</code> object describing the accessible schemas
1753:             * defined within this database. <p>
1754:             *
1755:             * Each row is a schema description with the following
1756:             * columns: <p>
1757:             *
1758:             * <pre class="SqlCodeExample">
1759:             * TABLE_SCHEM      VARCHAR   simple schema name
1760:             * TABLE_CATALOG    VARCHAR   catalog in which schema is defined
1761:             * IS_DEFAULT       BOOLEAN   is the schema the default for new sessions
1762:             * </pre> <p>
1763:             *
1764:             * @return table containing information about schemas defined
1765:             *      within this database
1766:             * @throws HsqlException if an error occurs while producing the table
1767:             */
1768:            final Table SYSTEM_SCHEMAS() throws HsqlException {
1769:
1770:                Table t = sysTables[SYSTEM_SCHEMAS];
1771:
1772:                if (t == null) {
1773:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_SCHEMAS]);
1774:
1775:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR, false); // not null
1776:                    addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
1777:                    addColumn(t, "IS_DEFAULT", Types.BOOLEAN);
1778:
1779:                    // order: TABLE_SCHEM
1780:                    // true PK, as rows never have null TABLE_SCHEM
1781:                    t.createPrimaryKey(null, new int[] { 0 }, true);
1782:
1783:                    return t;
1784:                }
1785:
1786:                Iterator schemas;
1787:                Object[] row;
1788:
1789:                // Initialization
1790:                schemas = database.schemaManager.fullSchemaNamesIterator();
1791:
1792:                String defschema = database.schemaManager
1793:                        .getDefaultSchemaName();
1794:
1795:                // Do it.
1796:                while (schemas.hasNext()) {
1797:                    row = t.getEmptyRowData();
1798:
1799:                    String schema = (String) schemas.next();
1800:
1801:                    row[0] = schema;
1802:                    row[1] = ns.getCatalogName(row[0]);
1803:                    row[2] = schema.equals(defschema) ? Boolean.TRUE
1804:                            : Boolean.FALSE;
1805:
1806:                    t.insertSys(row);
1807:                }
1808:
1809:                t.setDataReadOnly(true);
1810:
1811:                return t;
1812:            }
1813:
1814:            /**
1815:             * Retrieves a <code>Table</code> object describing the visible access
1816:             * rights for each accessible table definied within this database. <p>
1817:             *
1818:             * Each row is a table privilege description with the following columns: <p>
1819:             *
1820:             * <pre class="SqlCodeExample">
1821:             * TABLE_CAT    VARCHAR   table catalog
1822:             * TABLE_SCHEM  VARCHAR   table schema
1823:             * TABLE_NAME   VARCHAR   table name
1824:             * GRANTOR      VARCHAR   grantor of access
1825:             * GRANTEE      VARCHAR   grantee of access
1826:             * PRIVILEGE    VARCHAR   { "SELECT" | "INSERT" | "UPDATE" | "DELETE" }
1827:             * IS_GRANTABLE VARCHAR   { "YES" | "NO" |  NULL (unknown) }
1828:             * </pre>
1829:             *
1830:             * <b>Note:</b> Up to and including HSQLDB 1.7.2, the access rights granted
1831:             * on a table apply to all of the columns of that table as well. <p>
1832:             *
1833:             * @return a <code>Table</code> object describing the visible
1834:             *        access rights for each accessible table
1835:             *        defined within this database
1836:             * @throws HsqlException if an error occurs while producing the table
1837:             */
1838:            final Table SYSTEM_TABLEPRIVILEGES() throws HsqlException {
1839:
1840:                Table t = sysTables[SYSTEM_TABLEPRIVILEGES];
1841:
1842:                if (t == null) {
1843:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_TABLEPRIVILEGES]);
1844:
1845:                    addColumn(t, "TABLE_CAT", Types.VARCHAR);
1846:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1847:                    addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
1848:                    addColumn(t, "GRANTOR", Types.VARCHAR, false); // not null
1849:                    addColumn(t, "GRANTEE", Types.VARCHAR, false); // not null
1850:                    addColumn(t, "PRIVILEGE", Types.VARCHAR, 10, false); // not null
1851:                    addColumn(t, "IS_GRANTABLE", Types.VARCHAR, 3, false); // not null
1852:
1853:                    // order: TABLE_SCHEM, TABLE_NAME, and PRIVILEGE,
1854:                    // added for unique:  GRANTEE, GRANTOR, TABLE_CAT
1855:                    // false PK, as TABLE_SCHEM and/or TABLE_CAT may be null
1856:                    t.createPrimaryKey(null, new int[] { 1, 2, 5, 4, 3, 0 },
1857:                            false);
1858:
1859:                    return t;
1860:                }
1861:
1862:                // calculated column values
1863:                String tableCatalog;
1864:                String tableSchema;
1865:                String tableName;
1866:                String grantorName;
1867:                String granteeName;
1868:                String privilege;
1869:                String isGrantable;
1870:
1871:                // intermediate holders
1872:                HsqlArrayList users;
1873:                User user;
1874:                String[] tablePrivileges;
1875:                Iterator tables;
1876:                Table table;
1877:                HsqlName accessKey;
1878:                Object[] row;
1879:
1880:                // column number mappings
1881:                final int itable_cat = 0;
1882:                final int itable_schem = 1;
1883:                final int itable_name = 2;
1884:                final int igrantor = 3;
1885:                final int igrantee = 4;
1886:                final int iprivilege = 5;
1887:                final int iis_grantable = 6;
1888:
1889:                // Initialization
1890:                grantorName = GranteeManager.DBA_ADMIN_ROLE_NAME;
1891:                users = database.getUserManager().listVisibleUsers(session,
1892:                        true);
1893:                tables = allTables();
1894:
1895:                // Do it.
1896:                while (tables.hasNext()) {
1897:                    table = (Table) tables.next();
1898:                    accessKey = table.getName();
1899:
1900:                    // Only show table grants if session user is admin, has some
1901:                    // right, or the special PUBLIC user has some right.
1902:                    if (!isAccessibleTable(table)) {
1903:                        continue;
1904:                    }
1905:
1906:                    tableName = table.getName().name;
1907:                    tableCatalog = ns.getCatalogName(table);
1908:                    tableSchema = table.getSchemaName();
1909:
1910:                    for (int i = 0; i < users.size(); i++) {
1911:                        user = (User) users.get(i);
1912:                        granteeName = user.getName();
1913:
1914:                        if (user.isAdmin()) {
1915:                            tablePrivileges = UserManager
1916:                                    .getRightsArray(UserManager.ALL);
1917:                        } else {
1918:                            tablePrivileges = user
1919:                                    .listGrantedTablePrivileges(accessKey);
1920:                        }
1921:
1922:                        isGrantable = (user.isAdmin()) ? "YES" : "NO";
1923:
1924:                        for (int j = 0; j < tablePrivileges.length; j++) {
1925:                            privilege = (String) tablePrivileges[j];
1926:                            row = t.getEmptyRowData();
1927:                            row[itable_cat] = tableCatalog;
1928:                            row[itable_schem] = tableSchema;
1929:                            row[itable_name] = tableName;
1930:                            row[igrantor] = grantorName;
1931:                            row[igrantee] = granteeName;
1932:                            row[iprivilege] = privilege;
1933:                            row[iis_grantable] = isGrantable;
1934:
1935:                            t.insertSys(row);
1936:                        }
1937:                    }
1938:                }
1939:
1940:                t.setDataReadOnly(true);
1941:
1942:                return t;
1943:            }
1944:
1945:            /**
1946:             * Retrieves a <code>Table</code> object describing the accessible
1947:             * tables defined within this database. <p>
1948:             *
1949:             * Each row is a table description with the following columns: <p>
1950:             *
1951:             * <pre class="SqlCodeExample">
1952:             * TABLE_CAT                 VARCHAR   table catalog
1953:             * TABLE_SCHEM               VARCHAR   table schema
1954:             * TABLE_NAME                VARCHAR   table name
1955:             * TABLE_TYPE                VARCHAR   {"TABLE" | "VIEW" |
1956:             *                                      "SYSTEM TABLE" | "GLOBAL TEMPORARY"}
1957:             * REMARKS                   VARCHAR   comment on the table.
1958:             * TYPE_CAT                  VARCHAR   table type catalog (not implemented).
1959:             * TYPE_SCHEM                VARCHAR   table type schema (not implemented).
1960:             * TYPE_NAME                 VARCHAR   table type name (not implemented).
1961:             * SELF_REFERENCING_COL_NAME VARCHAR   designated "identifier" column of
1962:             *                                     typed table (not implemented).
1963:             * REF_GENERATION            VARCHAR   {"SYSTEM" | "USER" |
1964:             *                                      "DERIVED" | NULL } (not implemented)
1965:             * HSQLDB_TYPE               VARCHAR   HSQLDB-specific type:
1966:             *                                     {"MEMORY" | "CACHED" | "TEXT" | ...}
1967:             * READ_ONLY                 BOOLEAN   TRUE if table is read-only,
1968:             *                                     else FALSE.
1969:             * </pre> <p>
1970:             *
1971:             * @return a <code>Table</code> object describing the accessible
1972:             *      tables defined within this database
1973:             * @throws HsqlException if an error occurs while producing the table
1974:             */
1975:            final Table SYSTEM_TABLES() throws HsqlException {
1976:
1977:                Table t = sysTables[SYSTEM_TABLES];
1978:
1979:                if (t == null) {
1980:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_TABLES]);
1981:
1982:                    // -------------------------------------------------------------
1983:                    // required
1984:                    // -------------------------------------------------------------
1985:                    addColumn(t, "TABLE_CAT", Types.VARCHAR);
1986:                    addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1987:                    addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
1988:                    addColumn(t, "TABLE_TYPE", Types.VARCHAR, 16, false); // not null
1989:                    addColumn(t, "REMARKS", Types.VARCHAR);
1990:
1991:                    // -------------------------------------------------------------
1992:                    // JDBC3
1993:                    // -------------------------------------------------------------
1994:                    addColumn(t, "TYPE_CAT", Types.VARCHAR);
1995:                    addColumn(t, "TYPE_SCHEM", Types.VARCHAR);
1996:                    addColumn(t, "TYPE_NAME", Types.VARCHAR);
1997:                    addColumn(t, "SELF_REFERENCING_COL_NAME", Types.VARCHAR);
1998:                    addColumn(t, "REF_GENERATION", Types.VARCHAR);
1999:
2000:                    // -------------------------------------------------------------
2001:                    // extended
2002:                    // ------------------------------------------------------------
2003:                    addColumn(t, "HSQLDB_TYPE", Types.VARCHAR, 6, true);
2004:                    addColumn(t, "READ_ONLY", Types.BOOLEAN, false); // not null
2005:
2006:                    // ------------------------------------------------------------
2007:                    // order TABLE_TYPE, TABLE_SCHEM and TABLE_NAME
2008:                    // added for unique: TABLE_CAT
2009:                    // false PK, as TABLE_SCHEM and/or TABLE_CAT may be null
2010:                    t.createPrimaryKey(null, new int[] { 3, 1, 2, 0 }, false);
2011:
2012:                    return t;
2013:                }
2014:
2015:                // intermediate holders
2016:                Iterator tables;
2017:                Table table;
2018:                Object[] row;
2019:                HsqlName accessKey;
2020:                DITableInfo ti;
2021:
2022:                // column number mappings
2023:                // jdbc 1
2024:                final int itable_cat = 0;
2025:                final int itable_schem = 1;
2026:                final int itable_name = 2;
2027:                final int itable_type = 3;
2028:                final int iremark = 4;
2029:
2030:                // jdbc 3
2031:                final int itype_cat = 5;
2032:                final int itype_schem = 6;
2033:                final int itype_name = 7;
2034:                final int isref_cname = 8;
2035:                final int iref_gen = 9;
2036:
2037:                // hsqldb ext
2038:                final int ihsqldb_type = 10;
2039:                final int iread_only = 11;
2040:
2041:                // Initialization
2042:                tables = allTables();
2043:                ti = new DITableInfo();
2044:
2045:                // Do it.
2046:                while (tables.hasNext()) {
2047:                    table = (Table) tables.next();
2048:
2049:                    if (!isAccessibleTable(table)) {
2050:                        continue;
2051:                    }
2052:
2053:                    ti.setTable(table);
2054:
2055:                    row = t.getEmptyRowData();
2056:                    row[itable_cat] = ns.getCatalogName(table);
2057:                    row[itable_schem] = table.getSchemaName();
2058:                    row[itable_name] = ti.getName();
2059:                    row[itable_type] = ti.getStandardType();
2060:                    row[iremark] = ti.getRemark();
2061:                    row[ihsqldb_type] = ti.getHsqlType();
2062:                    row[iread_only] = ti.isReadOnly();
2063:
2064:                    t.insertSys(row);
2065:                }
2066:
2067:                t.setDataReadOnly(true);
2068:
2069:                return t;
2070:            }
2071:
2072:            /**
2073:             * Retrieves a <code>Table</code> object describing the table types
2074:             * available in this database. <p>
2075:             *
2076:             * In general, the range of values that may be commonly encounted across
2077:             * most DBMS implementations is: <p>
2078:             *
2079:             * <UL>
2080:             *   <LI><FONT color='#FF00FF'>"TABLE"</FONT>
2081:             *   <LI><FONT color='#FF00FF'>"VIEW"</FONT>
2082:             *   <LI><FONT color='#FF00FF'>"SYSTEM TABLE"</FONT>
2083:             *   <LI><FONT color='#FF00FF'>"GLOBAL TEMPORARY"</FONT>
2084:             *   <LI><FONT color='#FF00FF'>"LOCAL TEMPORARY"</FONT>
2085:             *   <LI><FONT color='#FF00FF'>"ALIAS"</FONT>
2086:             *   <LI><FONT color='#FF00FF'>"SYNONYM"</FONT>
2087:             * </UL> <p>
2088:             *
2089:             * As of HSQLDB 1.7.2, the engine supports and thus this method reports
2090:             * only a subset of the range above: <p>
2091:             *
2092:             * <UL>
2093:             *   <LI><FONT color='#FF00FF'>"TABLE"</FONT>
2094:             *    (HSQLDB MEMORY, CACHED and TEXT tables)
2095:             *   <LI><FONT color='#FF00FF'>"VIEW"</FONT>  (Views)
2096:             *   <LI><FONT color='#FF00FF'>"SYSTEM TABLE"</FONT>
2097:             *    (The tables generated by this object)
2098:             *   <LI><FONT color='#FF00FF'>"GLOBAL TEMPORARY"</FONT>
2099:             *    (HSQLDB TEMP and TEMP TEXT tables)
2100:             * </UL> <p>
2101:             *
2102:             * @return a <code>Table</code> object describing the table types
2103:             *        available in this database
2104:             * @throws HsqlException if an error occurs while producing the table
2105:             */
2106:            Table SYSTEM_TABLETYPES() throws HsqlException {
2107:
2108:                Table t = sysTables[SYSTEM_TABLETYPES];
2109:
2110:                if (t == null) {
2111:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_TABLETYPES]);
2112:
2113:                    addColumn(t, "TABLE_TYPE", Types.VARCHAR, 16, false); // not null
2114:
2115:                    // order: TABLE_TYPE
2116:                    // true PK
2117:                    t.createPrimaryKey(null, new int[] { 0 }, true);
2118:
2119:                    return t;
2120:                }
2121:
2122:                Object[] row;
2123:
2124:                for (int i = 0; i < tableTypes.length; i++) {
2125:                    row = t.getEmptyRowData();
2126:                    row[0] = tableTypes[i];
2127:
2128:                    t.insertSys(row);
2129:                }
2130:
2131:                t.setDataReadOnly(true);
2132:
2133:                return t;
2134:            }
2135:
2136:            /**
2137:             * Retrieves a <code>Table</code> object describing the
2138:             * result expected by the JDBC DatabaseMetaData interface implementation
2139:             * for system-defined SQL types supported as table columns.
2140:             *
2141:             * <pre class="SqlCodeExample">
2142:             * TYPE_NAME          VARCHAR   the canonical name for DDL statements.
2143:             * DATA_TYPE          SMALLINT  data type code from DITypes.
2144:             * PRECISION          INTEGER   max column size.
2145:             *                              number => max precision.
2146:             *                              character => max characters.
2147:             *                              datetime => max chars incl. frac. component.
2148:             * LITERAL_PREFIX     VARCHAR   char(s) prefixing literal of this type.
2149:             * LITERAL_SUFFIX     VARCHAR   char(s) terminating literal of this type.
2150:             * CREATE_PARAMS      VARCHAR   Localized syntax-order list of domain
2151:             *                              create parameter keywords.
2152:             *                              - for human consumption only
2153:             * NULLABLE           SMALLINT  {No Nulls | Nullable | Unknown}
2154:             * CASE_SENSITIVE     BOOLEAN   case-sensitive in collations/comparisons?
2155:             * SEARCHABLE         SMALLINT  {None | Char (Only WHERE .. LIKE) |
2156:             *                               Basic (Except WHERE .. LIKE) |
2157:             *                               Searchable (All forms)}
2158:             * UNSIGNED_ATTRIBUTE BOOLEAN   {TRUE  (unsigned) | FALSE (signed) |
2159:             *                               NULL (non-numeric or not applicable)}
2160:             * FIXED_PREC_SCALE   BOOLEAN   {TRUE (fixed) | FALSE (variable) |
2161:             *                               NULL (non-numeric or not applicable)}
2162:             * AUTO_INCREMENT     BOOLEAN   automatic unique value generated for
2163:             *                              inserts and updates when no value or
2164:             *                              NULL specified?
2165:             * LOCAL_TYPE_NAME    VARCHAR   localized name of data type;
2166:             *                              - NULL if not supported.
2167:             *                              - for human consuption only
2168:             * MINIMUM_SCALE      SMALLINT  minimum scale supported.
2169:             * MAXIMUM_SCALE      SMALLINT  maximum scale supported.
2170:             * SQL_DATA_TYPE      INTEGER   value expected in SQL CLI SQL_DESC_TYPE
2171:             *                              field of the SQLDA.
2172:             * SQL_DATETIME_SUB   INTEGER   SQL CLI datetime/interval subcode.
2173:             * NUM_PREC_RADIX     INTEGER   numeric base w.r.t # of digits reported in
2174:             *                              PRECISION column (typically 10).
2175:             * TYPE_SUB           INTEGER   From DITypes:
2176:             *                              {TYPE_SUB_DEFAULT | TYPE_SUB_IDENTITY |
2177:             *                               TYPE_SUB_IGNORECASE}
2178:             * </pre> <p>
2179:             *
2180:             * @return a <code>Table</code> object describing the
2181:             *      system-defined SQL types supported as table columns
2182:             * @throws HsqlException if an error occurs while producing the table
2183:             */
2184:            final Table SYSTEM_TYPEINFO() throws HsqlException {
2185:
2186:                Table t = sysTables[SYSTEM_TYPEINFO];
2187:
2188:                if (t == null) {
2189:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_TYPEINFO]);
2190:
2191:                    //-------------------------------------------
2192:                    // required by JDBC:
2193:                    // ------------------------------------------
2194:                    addColumn(t, "TYPE_NAME", Types.VARCHAR, 32, false);
2195:                    addColumn(t, "DATA_TYPE", Types.SMALLINT, false);
2196:                    addColumn(t, "PRECISION", Types.INTEGER);
2197:                    addColumn(t, "LITERAL_PREFIX", Types.VARCHAR, 2, true);
2198:                    addColumn(t, "LITERAL_SUFFIX", Types.VARCHAR, 2, true);
2199:                    addColumn(t, "CREATE_PARAMS", Types.VARCHAR);
2200:                    addColumn(t, "NULLABLE", Types.SMALLINT);
2201:                    addColumn(t, "CASE_SENSITIVE", Types.BOOLEAN);
2202:                    addColumn(t, "SEARCHABLE", Types.SMALLINT);
2203:                    addColumn(t, "UNSIGNED_ATTRIBUTE", Types.BOOLEAN);
2204:                    addColumn(t, "FIXED_PREC_SCALE", Types.BOOLEAN);
2205:                    addColumn(t, "AUTO_INCREMENT", Types.BOOLEAN);
2206:                    addColumn(t, "LOCAL_TYPE_NAME", Types.VARCHAR);
2207:                    addColumn(t, "MINIMUM_SCALE", Types.SMALLINT);
2208:                    addColumn(t, "MAXIMUM_SCALE", Types.SMALLINT);
2209:                    addColumn(t, "SQL_DATA_TYPE", Types.INTEGER);
2210:                    addColumn(t, "SQL_DATETIME_SUB", Types.INTEGER);
2211:                    addColumn(t, "NUM_PREC_RADIX", Types.INTEGER);
2212:
2213:                    //-------------------------------------------
2214:                    // for JDBC sort contract:
2215:                    //-------------------------------------------
2216:                    addColumn(t, "TYPE_SUB", Types.INTEGER);
2217:
2218:                    // order: DATA_TYPE, TYPE_SUB
2219:                    // true PK
2220:                    t.createPrimaryKey(null, new int[] { 1, 18 }, true);
2221:
2222:                    return t;
2223:                }
2224:
2225:                Result rs;
2226:
2227:                rs = session
2228:                        .sqlExecuteDirectNoPreChecks("select TYPE_NAME, DATA_TYPE, PRECISION, LITERAL_PREFIX, LITERAL_SUFFIX, CREATE_PARAMS, NULLABLE, CASE_SENSITIVE, SEARCHABLE,"
2229:                                + "UNSIGNED_ATTRIBUTE, FIXED_PREC_SCALE, AUTO_INCREMENT, LOCAL_TYPE_NAME, MINIMUM_SCALE, "
2230:                                + "MAXIMUM_SCALE, SQL_DATA_TYPE, SQL_DATETIME_SUB, NUM_PREC_RADIX, TYPE_SUB "
2231:                                + "from INFORMATION_SCHEMA.SYSTEM_ALLTYPEINFO  where AS_TAB_COL = true;");
2232:
2233:                t.insertSys(rs);
2234:                t.setDataReadOnly(true);
2235:
2236:                return t;
2237:            }
2238:
2239:            /**
2240:             * Retrieves a <code>Table</code> object describing, in an extended
2241:             * fashion, all of the system or formal specification SQL types known to
2242:             * this database, including its level of support for them (which may
2243:             * be no support at all) in various capacities. <p>
2244:             *
2245:             * <pre class="SqlCodeExample">
2246:             * TYPE_NAME          VARCHAR   the canonical name used in DDL statements.
2247:             * DATA_TYPE          SMALLINT  data type code from Types
2248:             * PRECISION          INTEGER   max column size.
2249:             *                              number => max. precision.
2250:             *                              character => max characters.
2251:             *                              datetime => max chars incl. frac. component.
2252:             * LITERAL_PREFIX     VARCHAR   char(s) prefixing literal of this type.
2253:             * LITERAL_SUFFIX     VARCHAR   char(s) terminating literal of this type.
2254:             * CREATE_PARAMS      VARCHAR   Localized syntax-order list of domain
2255:             *                              create parameter keywords.
2256:             *                              - for human consumption only
2257:             * NULLABLE           SMALLINT  { No Nulls | Nullable | Unknown }
2258:             * CASE_SENSITIVE     BOOLEAN   case-sensitive in collations/comparisons?
2259:             * SEARCHABLE         SMALLINT  { None | Char (Only WHERE .. LIKE) |
2260:             *                                Basic (Except WHERE .. LIKE) |
2261:             *                                Searchable (All forms) }
2262:             * UNSIGNED_ATTRIBUTE BOOLEAN   { TRUE  (unsigned) | FALSE (signed) |
2263:             *                                NULL (non-numeric or not applicable) }
2264:             * FIXED_PREC_SCALE   BOOLEAN   { TRUE (fixed) | FALSE (variable) |
2265:             *                                NULL (non-numeric or not applicable) }
2266:             * AUTO_INCREMENT     BOOLEAN   automatic unique value generated for
2267:             *                              inserts and updates when no value or
2268:             *                              NULL specified?
2269:             * LOCAL_TYPE_NAME    VARCHAR   Localized name of data type;
2270:             *                              - NULL => not supported (no resource avail).
2271:             *                              - for human consumption only
2272:             * MINIMUM_SCALE      SMALLINT  minimum scale supported.
2273:             * MAXIMUM_SCALE      SMALLINT  maximum scale supported.
2274:             * SQL_DATA_TYPE      INTEGER   value expected in SQL CLI SQL_DESC_TYPE
2275:             *                              field of the SQLDA.
2276:             * SQL_DATETIME_SUB   INTEGER   SQL CLI datetime/interval subcode
2277:             * NUM_PREC_RADIX     INTEGER   numeric base w.r.t # of digits reported
2278:             *                              in PRECISION column (typically 10)
2279:             * INTERVAL_PRECISION INTEGER   interval leading precision (not implemented)
2280:             * AS_TAB_COL         BOOLEAN   type supported as table column?
2281:             * AS_PROC_COL        BOOLEAN   type supported as procedure column?
2282:             * MAX_PREC_ACT       BIGINT    like PRECISION unless value would be
2283:             *                              truncated using INTEGER
2284:             * MIN_SCALE_ACT      INTEGER   like MINIMUM_SCALE unless value would be
2285:             *                              truncated using SMALLINT
2286:             * MAX_SCALE_ACT      INTEGER   like MAXIMUM_SCALE unless value would be
2287:             *                              truncated using SMALLINT
2288:             * COL_ST_CLS_NAME    VARCHAR   Java Class FQN of in-memory representation
2289:             * COL_ST_IS_SUP      BOOLEAN   is COL_ST_CLS_NAME supported under the
2290:             *                              hosting JVM and engine build option?
2291:             * STD_MAP_CLS_NAME   VARCHAR   Java class FQN of standard JDBC mapping
2292:             * STD_MAP_IS_SUP     BOOLEAN   Is STD_MAP_CLS_NAME supported under the
2293:             *                              hosting JVM?
2294:             * CST_MAP_CLS_NAME   VARCHAR   Java class FQN of HSQLDB-provided JDBC
2295:             *                              interface representation
2296:             * CST_MAP_IS_SUP     BOOLEAN   is CST_MAP_CLS_NAME supported under the
2297:             *                              hosting JVM and engine build option?
2298:             * MCOL_JDBC          INTEGER   maximum character octet length representable
2299:             *                              via JDBC interface
2300:             * MCOL_ACT           BIGINT    like MCOL_JDBC unless value would be
2301:             *                              truncated using INTEGER
2302:             * DEF_OR_FIXED_SCALE INTEGER   default or fixed scale for numeric types
2303:             * REMARKS            VARCHAR   localized comment on the data type
2304:             * TYPE_SUB           INTEGER   From Types:
2305:             *                              {TYPE_SUB_DEFAULT | TYPE_SUB_IGNORECASE}
2306:             *                              deprecated: TYPE_SUB_IDENTITY
2307:             * </pre> <p>
2308:             *
2309:             * @return a <code>Table</code> object describing all of the
2310:             *        standard SQL types known to this database
2311:             * @throws HsqlException if an error occurs while producing the table
2312:             */
2313:            final Table SYSTEM_ALLTYPEINFO() throws HsqlException {
2314:
2315:                Table t = sysTables[SYSTEM_ALLTYPEINFO];
2316:
2317:                if (t == null) {
2318:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_ALLTYPEINFO]);
2319:
2320:                    //-------------------------------------------
2321:                    // same as SYSTEM_TYPEINFO:
2322:                    // ------------------------------------------
2323:                    addColumn(t, "TYPE_NAME", Types.VARCHAR, 32, false);
2324:                    addColumn(t, "DATA_TYPE", Types.SMALLINT, false);
2325:                    addColumn(t, "PRECISION", Types.INTEGER);
2326:                    addColumn(t, "LITERAL_PREFIX", Types.VARCHAR, 2, true);
2327:                    addColumn(t, "LITERAL_SUFFIX", Types.VARCHAR, 2, true);
2328:                    addColumn(t, "CREATE_PARAMS", Types.VARCHAR);
2329:                    addColumn(t, "NULLABLE", Types.SMALLINT);
2330:                    addColumn(t, "CASE_SENSITIVE", Types.BOOLEAN);
2331:                    addColumn(t, "SEARCHABLE", Types.SMALLINT);
2332:                    addColumn(t, "UNSIGNED_ATTRIBUTE", Types.BOOLEAN);
2333:                    addColumn(t, "FIXED_PREC_SCALE", Types.BOOLEAN);
2334:                    addColumn(t, "AUTO_INCREMENT", Types.BOOLEAN);
2335:                    addColumn(t, "LOCAL_TYPE_NAME", Types.VARCHAR);
2336:                    addColumn(t, "MINIMUM_SCALE", Types.SMALLINT);
2337:                    addColumn(t, "MAXIMUM_SCALE", Types.SMALLINT);
2338:                    addColumn(t, "SQL_DATA_TYPE", Types.INTEGER);
2339:                    addColumn(t, "SQL_DATETIME_SUB", Types.INTEGER);
2340:                    addColumn(t, "NUM_PREC_RADIX", Types.INTEGER);
2341:
2342:                    //-------------------------------------------
2343:                    // SQL CLI / ODBC - not in JDBC spec
2344:                    // ------------------------------------------
2345:                    addColumn(t, "INTERVAL_PRECISION", Types.INTEGER);
2346:
2347:                    //-------------------------------------------
2348:                    // extended:
2349:                    //-------------------------------------------
2350:                    // level of support
2351:                    //-------------------------------------------
2352:                    addColumn(t, "AS_TAB_COL", Types.BOOLEAN);
2353:
2354:                    // for instance, some executable methods take Connection
2355:                    // or return non-serializable Object such as ResultSet, neither
2356:                    // of which maps to a supported table column type but which
2357:                    // we show as JAVA_OBJECT in SYSTEM_PROCEDURECOLUMNS.
2358:                    // Also, triggers take Object[] row, which we show as ARRAY
2359:                    // presently, although STRUCT would probably be better in the
2360:                    // future, as the row can actually contain mixed data types.
2361:                    addColumn(t, "AS_PROC_COL", Types.BOOLEAN);
2362:
2363:                    //-------------------------------------------
2364:                    // actual values for attributes that cannot be represented
2365:                    // within the limitations of the SQL CLI / JDBC interface
2366:                    //-------------------------------------------
2367:                    addColumn(t, "MAX_PREC_ACT", Types.BIGINT);
2368:                    addColumn(t, "MIN_SCALE_ACT", Types.INTEGER);
2369:                    addColumn(t, "MAX_SCALE_ACT", Types.INTEGER);
2370:
2371:                    //-------------------------------------------
2372:                    // how do we store this internally as a column value?
2373:                    //-------------------------------------------
2374:                    addColumn(t, "COL_ST_CLS_NAME", Types.VARCHAR);
2375:                    addColumn(t, "COL_ST_IS_SUP", Types.BOOLEAN);
2376:
2377:                    //-------------------------------------------
2378:                    // what is the standard Java mapping for the type?
2379:                    //-------------------------------------------
2380:                    addColumn(t, "STD_MAP_CLS_NAME", Types.VARCHAR);
2381:                    addColumn(t, "STD_MAP_IS_SUP", Types.BOOLEAN);
2382:
2383:                    //-------------------------------------------
2384:                    // what, if any, custom mapping do we provide?
2385:                    // (under the current build options and hosting VM)
2386:                    //-------------------------------------------
2387:                    addColumn(t, "CST_MAP_CLS_NAME", Types.VARCHAR);
2388:                    addColumn(t, "CST_MAP_IS_SUP", Types.BOOLEAN);
2389:
2390:                    //-------------------------------------------
2391:                    // what is the max representable and actual
2392:                    // character octet length, if applicable?
2393:                    //-------------------------------------------
2394:                    addColumn(t, "MCOL_JDBC", Types.INTEGER);
2395:                    addColumn(t, "MCOL_ACT", Types.BIGINT);
2396:
2397:                    //-------------------------------------------
2398:                    // what is the default or fixed scale, if applicable?
2399:                    //-------------------------------------------
2400:                    addColumn(t, "DEF_OR_FIXED_SCALE", Types.INTEGER);
2401:
2402:                    //-------------------------------------------
2403:                    // Any type-specific, localized remarks can go here
2404:                    //-------------------------------------------
2405:                    addColumn(t, "REMARKS", Types.VARCHAR);
2406:
2407:                    //-------------------------------------------
2408:                    // required for JDBC sort contract:
2409:                    //-------------------------------------------
2410:                    addColumn(t, "TYPE_SUB", Types.INTEGER);
2411:
2412:                    // order:  DATA_TYPE, TYPE_SUB
2413:                    // true primary key
2414:                    t.createPrimaryKey(null, new int[] { 1, 34 }, true);
2415:
2416:                    return t;
2417:                }
2418:
2419:                Object[] row;
2420:                int type;
2421:                DITypeInfo ti;
2422:
2423:                //-----------------------------------------
2424:                // Same as SYSTEM_TYPEINFO
2425:                //-----------------------------------------
2426:                final int itype_name = 0;
2427:                final int idata_type = 1;
2428:                final int iprecision = 2;
2429:                final int iliteral_prefix = 3;
2430:                final int iliteral_suffix = 4;
2431:                final int icreate_params = 5;
2432:                final int inullable = 6;
2433:                final int icase_sensitive = 7;
2434:                final int isearchable = 8;
2435:                final int iunsigned_attribute = 9;
2436:                final int ifixed_prec_scale = 10;
2437:                final int iauto_increment = 11;
2438:                final int ilocal_type_name = 12;
2439:                final int iminimum_scale = 13;
2440:                final int imaximum_scale = 14;
2441:                final int isql_data_type = 15;
2442:                final int isql_datetime_sub = 16;
2443:                final int inum_prec_radix = 17;
2444:
2445:                //------------------------------------------
2446:                // Extentions
2447:                //------------------------------------------
2448:                // not in JDBC, but in SQL CLI SQLDA / ODBC
2449:                //------------------------------------------
2450:                final int iinterval_precision = 18;
2451:
2452:                //------------------------------------------
2453:                // HSQLDB/Java-specific:
2454:                //------------------------------------------
2455:                final int iis_sup_as_tcol = 19;
2456:                final int iis_sup_as_pcol = 20;
2457:
2458:                //------------------------------------------
2459:                final int imax_prec_or_len_act = 21;
2460:                final int imin_scale_actual = 22;
2461:                final int imax_scale_actual = 23;
2462:
2463:                //------------------------------------------
2464:                final int ics_cls_name = 24;
2465:                final int ics_cls_is_supported = 25;
2466:
2467:                //------------------------------------------
2468:                final int ism_cls_name = 26;
2469:                final int ism_cls_is_supported = 27;
2470:
2471:                //------------------------------------------
2472:                final int icm_cls_name = 28;
2473:                final int icm_cls_is_supported = 29;
2474:
2475:                //------------------------------------------
2476:                final int imax_char_oct_len_jdbc = 30;
2477:                final int imax_char_oct_len_act = 31;
2478:
2479:                //------------------------------------------
2480:                final int idef_or_fixed_scale = 32;
2481:
2482:                //------------------------------------------
2483:                final int iremarks = 33;
2484:
2485:                //------------------------------------------
2486:                final int itype_sub = 34;
2487:
2488:                ti = new DITypeInfo();
2489:
2490:                for (int i = 0; i < Types.ALL_TYPES.length; i++) {
2491:                    ti.setTypeCode(Types.ALL_TYPES[i][0]);
2492:                    ti.setTypeSub(Types.ALL_TYPES[i][1]);
2493:
2494:                    row = t.getEmptyRowData();
2495:                    row[itype_name] = ti.getTypeName();
2496:                    row[idata_type] = ti.getDataType();
2497:                    row[iprecision] = ti.getPrecision();
2498:                    row[iliteral_prefix] = ti.getLiteralPrefix();
2499:                    row[iliteral_suffix] = ti.getLiteralSuffix();
2500:                    row[icreate_params] = ti.getCreateParams();
2501:                    row[inullable] = ti.getNullability();
2502:                    row[icase_sensitive] = ti.isCaseSensitive();
2503:                    row[isearchable] = ti.getSearchability();
2504:                    row[iunsigned_attribute] = ti.isUnsignedAttribute();
2505:                    row[ifixed_prec_scale] = ti.isFixedPrecisionScale();
2506:                    row[iauto_increment] = ti.isAutoIncrement();
2507:                    row[ilocal_type_name] = ti.getLocalName();
2508:                    row[iminimum_scale] = ti.getMinScale();
2509:                    row[imaximum_scale] = ti.getMaxScale();
2510:                    row[isql_data_type] = ti.getSqlDataType();
2511:                    row[isql_datetime_sub] = ti.getSqlDateTimeSub();
2512:                    row[inum_prec_radix] = ti.getNumPrecRadix();
2513:
2514:                    //------------------------------------------
2515:                    row[iinterval_precision] = ti.getIntervalPrecision();
2516:
2517:                    //------------------------------------------
2518:                    row[iis_sup_as_tcol] = ti.isSupportedAsTCol();
2519:                    row[iis_sup_as_pcol] = ti.isSupportedAsPCol();
2520:
2521:                    //------------------------------------------
2522:                    row[imax_prec_or_len_act] = ti.getPrecisionAct();
2523:                    row[imin_scale_actual] = ti.getMinScaleAct();
2524:                    row[imax_scale_actual] = ti.getMaxScaleAct();
2525:
2526:                    //------------------------------------------
2527:                    row[ics_cls_name] = ti.getColStClsName();
2528:                    row[ics_cls_is_supported] = ti.isColStClsSupported();
2529:
2530:                    //------------------------------------------
2531:                    row[ism_cls_name] = ti.getStdMapClsName();
2532:                    row[ism_cls_is_supported] = ti.isStdMapClsSupported();
2533:
2534:                    //------------------------------------------
2535:                    row[icm_cls_name] = ti.getCstMapClsName();
2536:
2537:                    try {
2538:                        ns.classForName((String) row[icm_cls_name]);
2539:
2540:                        row[icm_cls_is_supported] = Boolean.TRUE;
2541:                    } catch (Exception e) {
2542:                        row[icm_cls_is_supported] = Boolean.FALSE;
2543:                    }
2544:
2545:                    //------------------------------------------
2546:                    row[imax_char_oct_len_jdbc] = ti.getCharOctLen();
2547:                    row[imax_char_oct_len_act] = ti.getCharOctLenAct();
2548:
2549:                    //------------------------------------------
2550:                    row[idef_or_fixed_scale] = ti.getDefaultScale();
2551:
2552:                    //------------------------------------------
2553:                    row[iremarks] = ti.getRemarks();
2554:
2555:                    //------------------------------------------
2556:                    row[itype_sub] = ti.getDataTypeSub();
2557:
2558:                    t.insertSys(row);
2559:                }
2560:
2561:                t.setDataReadOnly(true);
2562:
2563:                return t;
2564:            }
2565:
2566:            /**
2567:             * Retrieves a <code>Table</code> object describing the
2568:             * visible <code>Users</code> defined within this database.
2569:             * @return table containing information about the users defined within
2570:             *      this database
2571:             * @throws HsqlException if an error occurs while producing the table
2572:             */
2573:            Table SYSTEM_USERS() throws HsqlException {
2574:
2575:                Table t = sysTables[SYSTEM_USERS];
2576:
2577:                if (t == null) {
2578:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_USERS]);
2579:
2580:                    addColumn(t, "USER", Types.VARCHAR, false);
2581:                    addColumn(t, "ADMIN", Types.BOOLEAN, false);
2582:
2583:                    // order: USER
2584:                    // true PK
2585:                    t.createPrimaryKey(null, new int[] { 0 }, true);
2586:
2587:                    return t;
2588:                }
2589:
2590:                // Intermediate holders
2591:                HsqlArrayList users;
2592:                User user;
2593:                int userCount;
2594:                Object[] row;
2595:
2596:                // Initialization
2597:                users = database.getUserManager().listVisibleUsers(session,
2598:                        false);
2599:
2600:                // Do it.
2601:                for (int i = 0; i < users.size(); i++) {
2602:                    row = t.getEmptyRowData();
2603:                    user = (User) users.get(i);
2604:                    row[0] = user.getName();
2605:                    row[1] = ValuePool.getBoolean(user.isAdmin());
2606:
2607:                    t.insertSys(row);
2608:                }
2609:
2610:                t.setDataReadOnly(true);
2611:
2612:                return t;
2613:            }
2614:
2615:            /**
2616:             * The SYSTEM_CHECK_CONSTRAINTS table has one row for each domain
2617:             * constraint, table check constraint, and assertion. <p>
2618:             *
2619:             * <b>Definition:</b><p>
2620:             *
2621:             * <pre class="SqlCodeExample">
2622:             * CREATE TABLE SYSTEM_CHECK_CONSTRAINTS (
2623:             *      CONSTRAINT_CATALOG  VARCHAR NULL,
2624:             *      CONSTRAINT_SCHEMA   VARCHAR NULL,
2625:             *      CONSTRAINT_NAME     VARCHAR NOT NULL,
2626:             *      CHECK_CLAUSE        VARCHAR NOT NULL,
2627:             *
2628:             *      UNIQUE ( CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME )
2629:             * )
2630:             * </pre>
2631:             *
2632:             * <b>Description:</b><p>
2633:             *
2634:             * <ol>
2635:             * <li> The values of CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA and
2636:             *      CONSTRAINT_NAME are the catalog name, unqualified schema name,
2637:             *      and qualified identifier, respectively, of the constraint being
2638:             *      described. <p>
2639:             *
2640:             * <li> Case: <p>
2641:             *
2642:             *      <table>
2643:             *          <tr>
2644:             *               <td valign="top" halign="left">a)</td>
2645:             *               <td> If the character representation of the
2646:             *                    &lt;search condition&gt; contained in the
2647:             *                    &lt;check constraint definition&gt;,
2648:             *                    &lt;domain constraint definition&gt;, or
2649:             *                    &lt;assertion definition&gt; that defined
2650:             *                    the check constraint being described can be
2651:             *                    represented without truncation, then the
2652:             *                    value of CHECK_CLAUSE is that character
2653:             *                    representation. </td>
2654:             *          </tr>
2655:             *          <tr>
2656:             *              <td align="top" halign="left">b)</td>
2657:             *              <td>Otherwise, the value of CHECK_CLAUSE is the
2658:             *                  null value.</td>
2659:             *          </tr>
2660:             *      </table>
2661:             * </ol>
2662:             */
2663:            final Table SYSTEM_CHECK_CONSTRAINTS() throws HsqlException {
2664:
2665:                Table t = sysTables[SYSTEM_CHECK_CONSTRAINTS];
2666:
2667:                if (t == null) {
2668:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_CHECK_CONSTRAINTS]);
2669:
2670:                    addColumn(t, "CONSTRAINT_CATALOG", Types.VARCHAR);
2671:                    addColumn(t, "CONSTRAINT_SCHEMA", Types.VARCHAR);
2672:                    addColumn(t, "CONSTRAINT_NAME", Types.VARCHAR, false); // not null
2673:                    addColumn(t, "CHECK_CLAUSE", Types.VARCHAR, false); // not null
2674:
2675:                    // false PK, as FKTABLE_CAT, FKTABLE_SCHEM and/or FK_NAME
2676:                    // may be null
2677:                    t.createPrimaryKey(null, new int[] { 0, 1, 2 }, false);
2678:
2679:                    return t;
2680:                }
2681:
2682:                // calculated column values
2683:                // Intermediate holders
2684:                Iterator tables;
2685:                Table table;
2686:                Constraint[] tableConstraints;
2687:                int constraintCount;
2688:                Constraint constraint;
2689:                HsqlArrayList constraintList;
2690:                Object[] row;
2691:
2692:                // column number mappings
2693:                final int icons_cat = 0;
2694:                final int icons_schem = 1;
2695:                final int icons_name = 2;
2696:                final int icons_clause = 3;
2697:
2698:                tables = database.schemaManager.allTablesIterator();
2699:                constraintList = new HsqlArrayList();
2700:
2701:                while (tables.hasNext()) {
2702:                    table = (Table) tables.next();
2703:
2704:                    if (!isAccessibleTable(table)) {
2705:                        continue;
2706:                    }
2707:
2708:                    tableConstraints = table.getConstraints();
2709:                    constraintCount = tableConstraints.length;
2710:
2711:                    for (int i = 0; i < constraintCount; i++) {
2712:                        constraint = (Constraint) tableConstraints[i];
2713:
2714:                        if (constraint.getType() == Constraint.CHECK) {
2715:                            constraintList.add(constraint);
2716:                        }
2717:                    }
2718:                }
2719:
2720:                for (int i = 0; i < constraintList.size(); i++) {
2721:                    row = t.getEmptyRowData();
2722:                    constraint = (Constraint) constraintList.get(i);
2723:                    table = constraint.getMain();
2724:                    row[icons_cat] = ns.getCatalogName(table);
2725:                    row[icons_schem] = table.getSchemaName();
2726:                    row[icons_name] = constraint.constName.name;
2727:
2728:                    try {
2729:                        row[icons_clause] = constraint.core.check.getDDL();
2730:                    } catch (Exception e) {
2731:                    }
2732:
2733:                    t.insertSys(row);
2734:                }
2735:
2736:                t.setDataReadOnly(true);
2737:
2738:                return t;
2739:            }
2740:
2741:            /**
2742:             * The SYSTEM_SEQUENCES table has one row for each external sequence
2743:             * generator. <p>
2744:             *
2745:             * <b>Definition:</b> <p>
2746:             *
2747:             * <pre class="SqlCodeExample">
2748:             * CREATE TABLE SYSTEM_SEQUENCES (
2749:             *      SEQUENCE_CATALOG     VARCHAR NULL,
2750:             *      SEQUENCE_SCHEMA      VARCHAR NULL,
2751:             *      SEQUENCE_NAME        VARCHAR NOT NULL,
2752:             *      DTD_IDENTIFIER       VARCHAR NOT NULL,
2753:             *      MAXIMUM_VALUE        VARCHAR NOT NULL,
2754:             *      MINIMUM_VALUE        VARCHAR NOT NULL,
2755:             *      INCREMENT            VARCHAR NOT NULL,
2756:             *      CYCLE_OPTION         VARCHAR NOT NULL,
2757:             *      START_WITH           VARCHAR NOT NULL,
2758:             *
2759:             *      CHECK(CYCLE_OPTION IN('YES', 'NO')),
2760:             *
2761:             *      CHECK(CAST(START_WITH AS BIGINT)
2762:             *          BETWEEN CAST(MINIMUM_VALUE AS BIGINT)
2763:             *              AND CAST(MAXIMUM_VALUE AS BIGINT)),
2764:             *
2765:             *      UNIQUE(SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME)
2766:             * )
2767:             * </pre>
2768:             *
2769:             * <b>DESCRIPTION:</b><p>
2770:             *
2771:             * <ol>
2772:             * <li> The values of SEQUENCE_CATALOG, SEQUENCE_SCHEMA, and
2773:             *      SEQUENCE_NAME are the catalog name, unqualified schema name,
2774:             *      and qualified identifier, respectively, of the sequence generator
2775:             *      being described. <p>
2776:             *
2777:             * <li> The values of SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME, and
2778:             *      DTD_IDENTIFIER are the values of OBJECT_CATALOG, OBJECT_SCHEMA,
2779:             *      OBJECT_NAME, and DTD_IDENTIFIER, respectively, of the row in
2780:             *      DATA_TYPE_DESCRIPTOR (not yet implemented) that describes the data
2781:             *      type of the sequence generator. <p>
2782:             *
2783:             * <li> The values of MAXIMUM_VALUE, MINIMUM_VALUE, and INCREMENT are the
2784:             *      character representations of maximum value, minimum value,
2785:             *      and increment, respectively, of the sequence generator being
2786:             *      described. <p>
2787:             *
2788:             * <li> The values of CYCLE_OPTION have the following meanings: <p>
2789:             *
2790:             *      <table border cellpadding="3">
2791:             *          <tr>
2792:             *              <td nowrap>YES</td>
2793:             *              <td nowrap>The cycle option of the sequence generator
2794:             *                         is CYCLE.</td>
2795:             *          <tr>
2796:             *              <td nowrap>NO</td>
2797:             *              <td nowrap>The cycle option of the sequence generator is
2798:             *                         NO CYCLE.</td>
2799:             *          </tr>
2800:             *      </table> <p>
2801:             *
2802:             * <li> The value of START_WITH is HSQLDB-specific (not in the SQL 200n
2803:             *      spec).  <p>
2804:             *
2805:             *      It is the character representation of the START WITH clause
2806:             *      value that would be required to recreate or ALTER RESET START WITH
2807:             *      the described SEQUENCE, such that its behaviour would be identical
2808:             *      to that exhibited at the instant this table is materialized in the
2809:             *      session context. <p>
2810:             *
2811:             *      In short, this is the character representation of value that
2812:             *      would be generated by NEXT VALUE FOR at the instant this table
2813:             *      is materialized in session context. <p>
2814:             *
2815:             */
2816:            final Table SYSTEM_SEQUENCES() throws HsqlException {
2817:
2818:                Table t = sysTables[SYSTEM_SEQUENCES];
2819:
2820:                if (t == null) {
2821:                    t = createBlankTable(sysTableHsqlNames[SYSTEM_SEQUENCES]);
2822:
2823:                    addColumn(t, "SEQUENCE_CATALOG", Types.VARCHAR);
2824:                    addColumn(t, "SEQUENCE_SCHEMA", Types.VARCHAR);
2825:                    addColumn(t, "SEQUENCE_NAME", Types.VARCHAR, true); // not null
2826:                    addColumn(t, "DTD_IDENTIFIER", Types.VARCHAR, true); // not null
2827:                    addColumn(t, "MAXIMUM_VALUE", Types.VARCHAR, 20, true); // not null
2828:                    addColumn(t, "MINIMUM_VALUE", Types.VARCHAR, 20, true); // not null
2829:                    addColumn(t, "INCREMENT", Types.VARCHAR, 20, true); // not null
2830:                    addColumn(t, "CYCLE_OPTION", Types.VARCHAR, 3, true); // not null
2831:
2832:                    // HSQLDB-specific
2833:                    addColumn(t, "START_WITH", Types.VARCHAR, 20, true); // not null
2834:
2835:                    // order SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME
2836:                    // false PK, as SCHEMA and/or CATALOG may be null
2837:                    t.createPrimaryKey(null, new int[] { 0, 1, 2 }, false);
2838:
2839:                    return t;
2840:                }
2841:
2842:                //
2843:                final int iseq_cat = 0;
2844:                final int iseq_schem = 1;
2845:                final int iseq_name = 2;
2846:                final int iseq_dtdid = 3;
2847:                final int iseq_max = 4;
2848:                final int iseq_min = 5;
2849:                final int iseq_incr = 6;
2850:                final int iseq_cycopt = 7;
2851:                final int iseq_start = 8;
2852:
2853:                //
2854:                final String min = ValuePool.getString("0");
2855:                final String intMax = ValuePool.getString(String
2856:                        .valueOf(Integer.MAX_VALUE));
2857:
2858:                // 19 is longer than max value pool string length (15)
2859:                final String longMax = String.valueOf(Long.MAX_VALUE);
2860:                final String cycleOption = ValuePool.getString("NO");
2861:
2862:                //
2863:                Iterator it;
2864:                Object[] row;
2865:                String sequenceName;
2866:                NumberSequence sequence;
2867:                int dataType;
2868:
2869:                it = database.schemaManager.allSequencesIterator();
2870:
2871:                while (it.hasNext()) {
2872:                    row = t.getEmptyRowData();
2873:                    sequence = (NumberSequence) it.next();
2874:                    dataType = sequence.getType();
2875:                    sequenceName = sequence.getName().name;
2876:                    row[iseq_cat] = ns.getCatalogName(sequence);
2877:                    row[iseq_schem] = sequence.getSchemaName();
2878:                    row[iseq_name] = sequenceName;
2879:                    row[iseq_dtdid] = Types.getTypeString(dataType);
2880:                    row[iseq_min] = min;
2881:                    row[iseq_incr] = String.valueOf(sequence.getIncrement());
2882:                    row[iseq_cycopt] = cycleOption;
2883:                    row[iseq_start] = String.valueOf(sequence.peek());
2884:
2885:                    switch (dataType) {
2886:
2887:                    case Types.INTEGER: {
2888:                        row[iseq_max] = intMax;
2889:
2890:                        break;
2891:                    }
2892:                    case Types.BIGINT:
2893:                    default: {
2894:                        row[iseq_max] = longMax;
2895:
2896:                        break;
2897:                    }
2898:                    }
2899:
2900:                    t.insertSys(row);
2901:                }
2902:
2903:                t.setDataReadOnly(true);
2904:
2905:                return t;
2906:            }
2907:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.