KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > DatabaseInformationFull


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import java.lang.reflect.Method JavaDoc;
35
36 import org.hsqldb.lib.FileUtil;
37 import org.hsqldb.lib.HashMap;
38 import org.hsqldb.lib.HashSet;
39 import org.hsqldb.lib.HsqlArrayList;
40 import org.hsqldb.lib.Iterator;
41 import org.hsqldb.persist.DataFileCache;
42 import org.hsqldb.persist.HsqlDatabaseProperties;
43 import org.hsqldb.persist.HsqlProperties;
44 import org.hsqldb.persist.Log;
45 import org.hsqldb.persist.TextCache;
46 import org.hsqldb.scriptio.ScriptWriterBase;
47 import org.hsqldb.store.ValuePool;
48
49 // fredt@users - 1.7.2 - structural modifications to allow inheritance
50
// boucherb@users - 1.7.2 - 20020225
51
// - factored out all reusable code into DIXXX support classes
52
// - completed Fred's work on allowing inheritance
53
// boucherb@users - 1.7.2 - 20020304 - bug fixes, refinements, better java docs
54
// fredt@users - 1.8.0 - updated to report latest enhancements and changes
55
// boucherb@users - 1.8.0 - 20050515 - furhter SQL 2003 metadata support
56

57 /**
58  * Extends DatabaseInformationMain to provide additional system table
59  * support. <p>
60  *
61  * @author boucherb@users
62  * @version 1.8.0
63  * @since 1.7.2
64  */

65 final class DatabaseInformationFull
66 extends org.hsqldb.DatabaseInformationMain {
67
68     /** Provides SQL function/procedure reporting support. */
69     protected DIProcedureInfo pi;
70
71     /**
72      * Constructs a new DatabaseInformationFull instance. <p>
73      *
74      * @param db the database for which to produce system tables.
75      * @throws HsqlException if a database access error occurs.
76      */

77     DatabaseInformationFull(Database db) throws HsqlException {
78
79         super(db);
80
81         pi = new DIProcedureInfo(ns);
82     }
83
84     /**
85      * Retrieves the system table corresponding to the specified index. <p>
86      *
87      * @param tableIndex index identifying the system table to generate
88      * @throws HsqlException if a database access error occurs
89      * @return the system table corresponding to the specified index
90      */

91     protected Table generateTable(int tableIndex) throws HsqlException {
92
93         switch (tableIndex) {
94
95             case SYSTEM_PROCEDURECOLUMNS :
96                 return SYSTEM_PROCEDURECOLUMNS();
97
98             case SYSTEM_PROCEDURES :
99                 return SYSTEM_PROCEDURES();
100
101             case SYSTEM_SUPERTABLES :
102                 return SYSTEM_SUPERTABLES();
103
104             case SYSTEM_SUPERTYPES :
105                 return SYSTEM_SUPERTYPES();
106
107             case SYSTEM_UDTATTRIBUTES :
108                 return SYSTEM_UDTATTRIBUTES();
109
110             case SYSTEM_UDTS :
111                 return SYSTEM_UDTS();
112
113             case SYSTEM_VERSIONCOLUMNS :
114                 return SYSTEM_VERSIONCOLUMNS();
115
116             // HSQLDB-specific
117
case SYSTEM_ALIASES :
118                 return SYSTEM_ALIASES();
119
120             case SYSTEM_CACHEINFO :
121                 return SYSTEM_CACHEINFO();
122
123             case SYSTEM_CLASSPRIVILEGES :
124                 return SYSTEM_CLASSPRIVILEGES();
125
126             case SYSTEM_SESSIONINFO :
127                 return SYSTEM_SESSIONINFO();
128
129             case SYSTEM_PROPERTIES :
130                 return SYSTEM_PROPERTIES();
131
132             case SYSTEM_SESSIONS :
133                 return SYSTEM_SESSIONS();
134
135             case SYSTEM_TRIGGERCOLUMNS :
136                 return SYSTEM_TRIGGERCOLUMNS();
137
138             case SYSTEM_TRIGGERS :
139                 return SYSTEM_TRIGGERS();
140
141             case SYSTEM_VIEWS :
142                 return SYSTEM_VIEWS();
143
144             case SYSTEM_TEXTTABLES :
145                 return SYSTEM_TEXTTABLES();
146
147             case SYSTEM_USAGE_PRIVILEGES :
148                 return SYSTEM_USAGE_PRIVILEGES();
149
150             case SYSTEM_CHECK_COLUMN_USAGE :
151                 return SYSTEM_CHECK_COLUMN_USAGE();
152
153             case SYSTEM_CHECK_ROUTINE_USAGE :
154                 return SYSTEM_CHECK_ROUTINE_USAGE();
155
156             case SYSTEM_CHECK_TABLE_USAGE :
157                 return SYSTEM_CHECK_TABLE_USAGE();
158
159             case SYSTEM_TABLE_CONSTRAINTS :
160                 return SYSTEM_TABLE_CONSTRAINTS();
161
162             case SYSTEM_VIEW_TABLE_USAGE :
163                 return SYSTEM_VIEW_TABLE_USAGE();
164
165             case SYSTEM_VIEW_COLUMN_USAGE :
166                 return SYSTEM_VIEW_COLUMN_USAGE();
167
168             case SYSTEM_VIEW_ROUTINE_USAGE :
169                 return SYSTEM_VIEW_ROUTINE_USAGE();
170
171             case SYSTEM_AUTHORIZATIONS : {
172                 return SYSTEM_AUTHORIZATIONS();
173             }
174             case SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS : {
175                 return SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS();
176             }
177             case SYSTEM_SCHEMATA : {
178                 return SYSTEM_SCHEMATA();
179             }
180             case SYSTEM_COLLATIONS : {
181                 return SYSTEM_COLLATIONS();
182             }
183             default :
184                 return super.generateTable(tableIndex);
185         }
186     }
187
188     /**
189      * Retrieves a <code>Table</code> object describing the aliases defined
190      * within this database. <p>
191      *
192      * Currently two types of alias are reported: DOMAIN alaises (alternate
193      * names for column data types when issuing "CREATE TABLE" DDL) and
194      * ROUTINE aliases (alternate names that can be used when invoking
195      * routines as SQL functions or stored procedures). <p>
196      *
197      * Each row is an alias description with the following columns: <p>
198      *
199      * <pre class="SqlCodeExample">
200      * OBJECT_TYPE VARCHAR type of the aliased object
201      * OBJECT_CAT VARCHAR catalog of the aliased object
202      * OBJECT_SCHEM VARCHAR schema of the aliased object
203      * OBJECT_NAME VARCHAR simple identifier of the aliased object
204      * ALIAS_CAT VARCHAR catalog in which alias is defined
205      * ALIAS_SCHEM VARCHAR schema in which alias is defined
206      * ALIAS VARCHAR alias for the indicated object
207      * </pre> <p>
208      *
209      * <b>Note:</b> Up to and including HSQLDB 1.7.2, user-defined aliases
210      * are supported only for SQL function and stored procedure calls
211      * (indicated by the value "ROUTINE" in the OBJECT_TYPE
212      * column), and there is no syntax for dropping aliases, only for
213      * creating them. <p>
214      * @return a Table object describing the accessisble
215      * aliases in the context of the calling session
216      * @throws HsqlException if an error occurs while producing the table
217      */

218     Table SYSTEM_ALIASES() throws HsqlException {
219
220         Table t = sysTables[SYSTEM_ALIASES];
221
222         if (t == null) {
223             t = createBlankTable(sysTableHsqlNames[SYSTEM_ALIASES]);
224
225             addColumn(t, "OBJECT_TYPE", Types.VARCHAR, 32, false); // not null
226
addColumn(t, "OBJECT_CAT", Types.VARCHAR);
227             addColumn(t, "OBJECT_SCHEM", Types.VARCHAR);
228             addColumn(t, "OBJECT_NAME", Types.VARCHAR, false); // not null
229
addColumn(t, "ALIAS_CAT", Types.VARCHAR);
230             addColumn(t, "ALIAS_SCHEM", Types.VARCHAR);
231             addColumn(t, "ALIAS", Types.VARCHAR, false); // not null
232

233             // order: OBJECT_TYPE, OBJECT_NAME, ALIAS.
234
// true PK.
235
t.createPrimaryKey(null, new int[] {
236                 0, 3, 6
237             }, true);
238
239             return t;
240         }
241
242         // Holders for calculated column values
243
String JavaDoc cat;
244         String JavaDoc schem;
245         String JavaDoc alias;
246         String JavaDoc objName;
247         String JavaDoc objType;
248
249         // Intermediate holders
250
String JavaDoc className;
251         HashMap hAliases;
252         Iterator aliases;
253         Object JavaDoc[] row;
254         int pos;
255
256         // Column number mappings
257
final int ialias_object_type = 0;
258         final int ialias_object_cat = 1;
259         final int ialias_object_schem = 2;
260         final int ialias_object_name = 3;
261         final int ialias_cat = 4;
262         final int ialias_schem = 5;
263         final int ialias = 6;
264
265         // Initialization
266
hAliases = database.getAliasMap();
267         aliases = hAliases.keySet().iterator();
268         objType = "ROUTINE";
269
270         // Do it.
271
while (aliases.hasNext()) {
272             row = t.getEmptyRowData();
273             alias = (String JavaDoc) aliases.next();
274             objName = (String JavaDoc) hAliases.get(alias);
275
276             // must have class grant to see method call aliases
277
pos = objName.lastIndexOf('.');
278
279             if (pos <= 0) {
280
281                 // should never occur in practice, as this is typically a Java
282
// method name, but there's nothing preventing a user from
283
// creating an alias entry that is not in method FQN form;
284
// such entries are not illegal, only useless. Probably,
285
// we should eventually try to disallow them.
286
continue;
287             }
288
289             className = objName.substring(0, pos);
290
291             if (!session.isAccessible(className)) {
292                 continue;
293             }
294
295             cat = ns.getCatalogName(objName);
296             schem = ns.getSchemaName(className);
297             row[ialias_object_type] = objType;
298             row[ialias_object_cat] = cat;
299             row[ialias_object_schem] = schem;
300             row[ialias_object_name] = objName;
301             row[ialias_cat] = cat;
302             row[ialias_schem] = schem;
303             row[ialias] = alias;
304
305             t.insertSys(row);
306         }
307
308         // must have create/alter table rights to see domain aliases
309
if (session.isAdmin()) {
310             Iterator typeAliases = Types.typeAliases.keySet().iterator();
311
312             objType = "DOMAIN";
313
314             while (typeAliases.hasNext()) {
315                 row = t.getEmptyRowData();
316                 alias = (String JavaDoc) typeAliases.next();
317
318                 int tn = Types.typeAliases.get(alias, Integer.MIN_VALUE);
319
320                 objName = Types.getTypeString(tn);
321
322                 if (alias.equals(objName)) {
323                     continue;
324                 }
325
326                 cat = ns.getCatalogName(objName);
327                 schem = ns.getSchemaName(objName);
328                 row[ialias_object_type] = objType;
329                 row[ialias_object_cat] = cat;
330                 row[ialias_object_schem] = schem;
331                 row[ialias_object_name] = objName;
332                 row[ialias_cat] = cat;
333                 row[ialias_schem] = schem;
334                 row[ialias] = alias;
335
336                 t.insertSys(row);
337             }
338         }
339
340         t.setDataReadOnly(true);
341
342         return t;
343     }
344
345     /**
346      * Retrieves a <code>Table</code> object describing the current
347      * state of all row caching objects for the accessible
348      * tables defined within this database. <p>
349      *
350      * Currently, the row caching objects for which state is reported are: <p>
351      *
352      * <OL>
353      * <LI> the system-wide <code>Cache</code> object used by CACHED tables.
354      * <LI> any <code>TextCache</code> objects in use by [TEMP] TEXT tables.
355      * </OL> <p>
356      *
357      * Each row is a cache object state description with the following
358      * columns: <p>
359      *
360      * <pre class="SqlCodeExample">
361      * CACHE_FILE VARCHAR absolute path of cache data file
362      * MAX_CACHE_SIZE INTEGER maximum allowable cached Row objects
363      * MAX_CACHE_BYTE_SIZE INTEGER maximum allowable size of cached Row objects
364      * CACHE_LENGTH INTEGER number of data bytes currently cached
365      * CACHE_SIZE INTEGER number of rows currently cached
366      * FREE_BYTES INTEGER total bytes in available file allocation units
367      * FREE_COUNT INTEGER total # of allocation units available
368      * FREE_POS INTEGER largest file position allocated + 1
369      * </pre> <p>
370      *
371      * <b>Notes:</b> <p>
372      *
373      * <code>TextCache</code> objects do not maintain a free list because
374      * deleted rows are only marked deleted and never reused. As such, the
375      * columns FREE_BYTES, SMALLEST_FREE_ITEM, LARGEST_FREE_ITEM, and
376      * FREE_COUNT are always reported as zero for rows reporting on
377      * <code>TextCache</code> objects. <p>
378      *
379      * Currently, CACHE_SIZE, FREE_BYTES, SMALLEST_FREE_ITEM, LARGEST_FREE_ITEM,
380      * FREE_COUNT and FREE_POS are the only dynamically changing values.
381      * All others are constant for the life of a cache object. In a future
382      * release, other column values may also change over the life of a cache
383      * object, as SQL syntax may eventually be introduced to allow runtime
384      * modification of certain cache properties. <p>
385      *
386      * @return a description of the current state of all row caching
387      * objects associated with the accessible tables of the database
388      * @throws HsqlException if an error occurs while producing the table
389      */

390     Table SYSTEM_CACHEINFO() throws HsqlException {
391
392         Table t = sysTables[SYSTEM_CACHEINFO];
393
394         if (t == null) {
395             t = createBlankTable(sysTableHsqlNames[SYSTEM_CACHEINFO]);
396
397             addColumn(t, "CACHE_FILE", Types.VARCHAR, false); // not null
398
addColumn(t, "MAX_CACHE_COUNT", Types.INTEGER, false); // not null
399
addColumn(t, "MAX_CACHE_BYTES", Types.BIGINT, false); // not null
400
addColumn(t, "CACHE_SIZE", Types.INTEGER, false); // not null
401
addColumn(t, "CACHE_BYTES", Types.BIGINT, false); // not null
402
addColumn(t, "FILE_FREE_BYTES", Types.INTEGER, false); // not null
403
addColumn(t, "FILE_FREE_COUNT", Types.INTEGER, false); // not null
404
addColumn(t, "FILE_FREE_POS", Types.BIGINT, false); // not null
405
t.createPrimaryKey(null, new int[]{ 0 }, true);
406
407             return t;
408         }
409
410         DataFileCache cache;
411         Object JavaDoc[] row;
412         HashSet cacheSet;
413         Iterator caches;
414         Iterator tables;
415         Table table;
416         int iFreeBytes;
417         int iLargestFreeItem;
418         long lSmallestFreeItem;
419
420         // column number mappings
421
final int icache_file = 0;
422         final int imax_cache_sz = 1;
423         final int imax_cache_bytes = 2;
424         final int icache_size = 3;
425         final int icache_length = 4;
426         final int ifree_bytes = 5;
427         final int ifree_count = 6;
428         final int ifree_pos = 7;
429
430         // Initialization
431
cacheSet = new HashSet();
432
433         // dynamic system tables are never cached
434
tables = database.schemaManager.allTablesIterator();
435
436         while (tables.hasNext()) {
437             table = (Table) tables.next();
438
439             if (table.isFileBased() && isAccessibleTable(table)) {
440                 cache = table.getCache();
441
442                 if (cache != null) {
443                     cacheSet.add(cache);
444                 }
445             }
446         }
447
448         caches = cacheSet.iterator();
449
450         // Do it.
451
while (caches.hasNext()) {
452             cache = (DataFileCache) caches.next();
453             row = t.getEmptyRowData();
454             row[icache_file] =
455                 FileUtil.canonicalOrAbsolutePath(cache.getFileName());
456             row[imax_cache_sz] = ValuePool.getInt(cache.capacity());
457             row[imax_cache_bytes] = ValuePool.getLong(cache.bytesCapacity());
458             row[icache_size] = ValuePool.getInt(cache.getCachedObjectCount());
459             row[icache_length] =
460                 ValuePool.getLong(cache.getTotalCachedBlockSize());
461             row[ifree_bytes] =
462                 ValuePool.getInt(cache.getTotalFreeBlockSize());
463             row[ifree_count] = ValuePool.getInt(cache.getFreeBlockCount());
464             row[ifree_pos] = ValuePool.getLong(cache.getFileFreePos());
465
466             t.insertSys(row);
467         }
468
469         t.setDataReadOnly(true);
470
471         return t;
472     }
473
474     /**
475      * Retrieves a <code>Table</code> object describing the visible
476      * access rights for all accessible Java Class objects defined
477      * within this database.<p>
478      *
479      * Each row is a Class privilege description with the following
480      * columns: <p>
481      *
482      * <pre class="SqlCodeExample">
483      * CLASS_CAT VARCHAR catalog in which the class is defined
484      * CLASS_SCHEM VARCHAR schema in which the class is defined
485      * CLASS_NAME VARCHAR fully qualified name of class
486      * GRANTOR VARCHAR grantor of access
487      * GRANTEE VARCHAR grantee of access
488      * PRIVILEGE VARCHAR name of access: {"EXECUTE" | "TRIGGER"}
489      * IS_GRANTABLE VARCHAR grantable?: {"YES" | "NO" | NULL (unknown)}
490      * </pre>
491      *
492      * <b>Note:</b> Users with the administrative privilege implicily have
493      * full and unrestricted access to all Classes available to the database
494      * class loader. However, only explicitly granted rights are reported
495      * in this table. Explicit Class grants/revokes to admin users have no
496      * effect in reality, but are reported in this table anyway for
497      * completeness. <p>
498      *
499      * @return a <code>Table</code> object describing the visible
500      * access rights for all accessible Java Class
501      * objects defined within this database
502      * @throws HsqlException if an error occurs while producing the table
503      */

504     Table SYSTEM_CLASSPRIVILEGES() throws HsqlException {
505
506         Table t = sysTables[SYSTEM_CLASSPRIVILEGES];
507
508         if (t == null) {
509             t = createBlankTable(sysTableHsqlNames[SYSTEM_CLASSPRIVILEGES]);
510
511             addColumn(t, "CLASS_CAT", Types.VARCHAR);
512             addColumn(t, "CLASS_SCHEM", Types.VARCHAR);
513             addColumn(t, "CLASS_NAME", Types.VARCHAR, false); // not null
514
addColumn(t, "GRANTOR", Types.VARCHAR, false); // not null
515
addColumn(t, "GRANTEE", Types.VARCHAR, false); // not null
516
addColumn(t, "PRIVILEGE", Types.VARCHAR, 7, false); // not null
517
addColumn(t, "IS_GRANTABLE", Types.VARCHAR, 3, false); // not null
518
t.createPrimaryKey(null, new int[] {
519                 2, 4, 5
520             }, true);
521
522             return t;
523         }
524
525         // calculated column values
526
String JavaDoc clsCat;
527         String JavaDoc clsSchem;
528         String JavaDoc clsName;
529         String JavaDoc grantorName;
530         String JavaDoc granteeName;
531         String JavaDoc privilege;
532         String JavaDoc isGrantable;
533
534         // intermediate holders
535
UserManager um;
536         HsqlArrayList users;
537         HashSet classNameSet;
538         Iterator classNames;
539         User granteeUser;
540         Object JavaDoc[] row;
541
542         // column number mappings
543
final int icls_cat = 0;
544         final int icls_schem = 1;
545         final int icls_name = 2;
546         final int igrantor = 3;
547         final int igrantee = 4;
548         final int iprivilege = 5;
549         final int iis_grntbl = 6;
550
551         // Initialization
552
grantorName = GranteeManager.DBA_ADMIN_ROLE_NAME;
553         um = database.getUserManager();
554         users = um.listVisibleUsers(session, true);
555
556         // Do it.
557
for (int i = 0; i < users.size(); i++) {
558             granteeUser = (User) users.get(i);
559             granteeName = granteeUser.getName();
560             isGrantable = granteeUser.isAdmin() ? "YES"
561                                                  : "NO";
562             classNameSet = granteeUser.getGrantedClassNames(false);
563
564             if (granteeUser.isPublic()) {
565                 ns.addBuiltinToSet(classNameSet);
566             }
567
568             classNames = classNameSet.iterator();
569
570 // boucherb@users 20030305 - TODO completed.
571
// "EXECUTE" is closest to correct (from: SQL 200n ROUTINE_PRIVILEGES)
572
// There is nothing even like CLASS_PRIVILEGES table under SQL 200n spec.
573
privilege = "EXECUTE";
574
575             while (classNames.hasNext()) {
576                 clsName = (String JavaDoc) classNames.next();
577                 clsCat = ns.getCatalogName(clsName);
578                 clsSchem = ns.getSchemaName(clsName);
579                 row = t.getEmptyRowData();
580                 row[icls_cat] = clsCat;
581                 row[icls_schem] = clsSchem;
582                 row[icls_name] = clsName;
583                 row[igrantor] = grantorName;
584                 row[igrantee] = granteeName;
585                 row[iprivilege] = privilege;
586                 row[iis_grntbl] = isGrantable;
587
588                 t.insertSys(row);
589             }
590
591             classNames = ns.iterateAccessibleTriggerClassNames(granteeUser);
592
593 // boucherb@users 20030305 - TODO completed.
594
// "TRIGGER" is closest to correct. (from: SQL 200n TABLE_PRIVILEGES)
595
// There is nothing even like CLASS_PRIVILEGES table under SQL 200n spec.
596
privilege = "TRIGGER";
597
598             while (classNames.hasNext()) {
599                 clsName = (String JavaDoc) classNames.next();
600                 clsCat = ns.getCatalogName(clsName);
601                 clsSchem = ns.getSchemaName(clsName);
602                 row = t.getEmptyRowData();
603                 row[icls_cat] = clsCat;
604                 row[icls_schem] = clsSchem;
605                 row[icls_name] = clsName;
606                 row[igrantor] = grantorName;
607                 row[igrantee] = granteeName;
608                 row[iprivilege] = privilege;
609                 row[iis_grntbl] = isGrantable;
610
611                 t.insertSys(row);
612             }
613         }
614
615         t.setDataReadOnly(true);
616
617         return t;
618     }
619
620     /**
621      * Retrieves a <code>Table</code> object describing attributes
622      * for the calling session context.<p>
623      *
624      * The rows report the following {key,value} pairs:<p>
625      *
626      * <pre class="SqlCodeExample">
627      * KEY (VARCHAR) VALUE (VARCHAR)
628      * ------------------- ---------------
629      * SESSION_ID the id of the calling session
630      * AUTOCOMMIT YES: session is in autocommit mode, else NO
631      * USER the name of user connected in the calling session
632      * (was READ_ONLY)
633      * SESSION_READONLY TRUE: session is in read-only mode, else FALSE
634      * (new)
635      * DATABASE_READONLY TRUE: database is in read-only mode, else FALSE
636      * MAXROWS the MAXROWS setting in the calling session
637      * DATABASE the name of the database
638      * IDENTITY the last identity value used by calling session
639      * </pre>
640      *
641      * <b>Note:</b> This table <em>may</em> become deprecated in a future
642      * release, as the information it reports now duplicates information
643      * reported in the newer SYSTEM_SESSIONS and SYSTEM_PROPERTIES
644      * tables. <p>
645      *
646      * @return a <code>Table</code> object describing the
647      * attributes of the connection associated
648      * with the current execution context
649      * @throws HsqlException if an error occurs while producing the table
650      */

651     Table SYSTEM_SESSIONINFO() throws HsqlException {
652
653         Table t = sysTables[SYSTEM_SESSIONINFO];
654
655         if (t == null) {
656             t = createBlankTable(sysTableHsqlNames[SYSTEM_SESSIONINFO]);
657
658             addColumn(t, "KEY", Types.VARCHAR, false); // not null
659
addColumn(t, "VALUE", Types.VARCHAR, false); // not null
660
t.createPrimaryKey(null);
661
662             return t;
663         }
664
665         Object JavaDoc[] row;
666
667         row = t.getEmptyRowData();
668         row[0] = "SESSION_ID";
669         row[1] = String.valueOf(session.getId());
670
671         t.insertSys(row);
672
673         row = t.getEmptyRowData();
674         row[0] = "AUTOCOMMIT";
675         row[1] = session.isAutoCommit() ? "TRUE"
676                                         : "FALSE";
677
678         t.insertSys(row);
679
680         row = t.getEmptyRowData();
681         row[0] = "USER";
682         row[1] = session.getUsername();
683
684         t.insertSys(row);
685
686         row = t.getEmptyRowData();
687         row[0] = "SESSION_READONLY";
688         row[1] = session.isReadOnly() ? "TRUE"
689                                       : "FALSE";
690
691         t.insertSys(row);
692
693         row = t.getEmptyRowData();
694         row[0] = "DATABASE_READONLY";
695         row[1] = database.databaseReadOnly ? "TRUE"
696                                            : "FALSE";
697
698         t.insertSys(row);
699
700         // fredt - value set by SET MAXROWS in SQL, not Statement.setMaxRows()
701
row = t.getEmptyRowData();
702         row[0] = "MAXROWS";
703         row[1] = String.valueOf(session.getSQLMaxRows());
704
705         t.insertSys(row);
706
707         row = t.getEmptyRowData();
708         row[0] = "DATABASE";
709         row[1] = database.getURI();
710
711         t.insertSys(row);
712
713         row = t.getEmptyRowData();
714         row[0] = "IDENTITY";
715         row[1] = String.valueOf(session.getLastIdentity());
716
717         t.insertSys(row);
718
719         row = t.getEmptyRowData();
720         row[0] = "SCHEMA";
721         row[1] = String.valueOf(session.getSchemaName(null));
722
723         t.insertSys(row);
724         t.setDataReadOnly(true);
725
726         return t;
727     }
728
729     /**
730      * Retrieves a <code>Table</code> object describing the capabilities
731      * and operating parameter properties for the engine hosting this
732      * database, as well as their applicability in terms of scope and
733      * name space. <p>
734      *
735      * Reported properties include certain predefined <code>Database</code>
736      * properties file values as well as certain database scope
737      * attributes. <p>
738      *
739      * It is intended that all <code>Database</code> attributes and
740      * properties that can be set via the database properties file,
741      * JDBC connection properties or SQL SET/ALTER statements will
742      * eventually be reported here or, where more applicable, in an
743      * ANSI/ISO conforming feature info base table in the defintion
744      * schema. <p>
745      *
746      * Currently, the database properties reported are: <p>
747      *
748      * <OL>
749      * <LI>hsqldb.cache_file_scale - the scaling factor used to translate data and index structure file pointers
750      * <LI>hsqldb.cache_scale - base-2 exponent scaling allowable cache row count
751      * <LI>hsqldb.cache_size_scale - base-2 exponent scaling allowable cache byte count
752      * <LI>hsqldb.cache_version -
753      * <LI>hsqldb.catalogs - whether to report the database catalog (database uri)
754      * <LI>hsqldb.compatible_version -
755      * <LI>hsqldb.files_readonly - whether the database is in files_readonly mode
756      * <LI>hsqldb.gc_interval - # new records forcing gc ({0|NULL}=>never)
757      * <LI>hsqldb.max_nio_scale - scale factor for cache nio mapped buffers
758      * <LI>hsqldb.nio_data_file - whether cache uses nio mapped buffers
759      * <LI>hsqldb.original_version -
760      * <LI>sql.enforce_strict_size - column length specifications enforced strictly (raise exception on overflow)?
761      * <LI>textdb.all_quoted - default policy regarding whether to quote all character field values
762      * <LI>textdb.cache_scale - base-2 exponent scaling allowable cache row count
763      * <LI>textdb.cache_size_scale - base-2 exponent scaling allowable cache byte count
764      * <LI>textdb.encoding - default TEXT table file encoding
765      * <LI>textdb.fs - default field separator
766      * <LI>textdb.vs - default varchar field separator
767      * <LI>textdb.lvs - default long varchar field separator
768      * <LI>textdb.ignore_first - default policy regarding whether to ignore the first line
769      * <LI>textdb.quoted - default policy regarding treatement character field values that _may_ require quoting
770      * <LI>IGNORECASE - create table VARCHAR_IGNORECASE?
771      * <LI>LOGSIZSE - # bytes to which REDO log grows before auto-checkpoint
772      * <LI>REFERENTIAL_INTEGITY - currently enforcing referential integrity?
773      * <LI>SCRIPTFORMAT - 0 : TEXT, 1 : BINARY, ...
774      * <LI>WRITEDELAY - does REDO log currently use buffered write strategy?
775      * </OL> <p>
776      *
777      * @return table describing database and session operating parameters
778      * and capabilities
779      * @throws HsqlException if an error occurs while producing the table
780      */

781     Table SYSTEM_PROPERTIES() throws HsqlException {
782
783         Table t = sysTables[SYSTEM_PROPERTIES];
784
785         if (t == null) {
786             t = createBlankTable(sysTableHsqlNames[SYSTEM_PROPERTIES]);
787
788             addColumn(t, "PROPERTY_SCOPE", Types.VARCHAR, false);
789             addColumn(t, "PROPERTY_NAMESPACE", Types.VARCHAR, false);
790             addColumn(t, "PROPERTY_NAME", Types.VARCHAR, false);
791             addColumn(t, "PROPERTY_VALUE", Types.VARCHAR);
792             addColumn(t, "PROPERTY_CLASS", Types.VARCHAR, false);
793
794             // order PROPERTY_SCOPE, PROPERTY_NAMESPACE, PROPERTY_NAME
795
// true PK
796
t.createPrimaryKey(null, new int[] {
797                 0, 1, 2
798             }, true);
799
800             return t;
801         }
802
803         // calculated column values
804
String JavaDoc scope;
805         String JavaDoc nameSpace;
806
807         // intermediate holders
808
Object JavaDoc[] row;
809         HsqlDatabaseProperties props;
810
811         // column number mappings
812
final int iscope = 0;
813         final int ins = 1;
814         final int iname = 2;
815         final int ivalue = 3;
816         final int iclass = 4;
817
818         // First, we want the names and values for
819
// all JDBC capabilities constants
820
scope = "SESSION";
821         props = database.getProperties();
822         nameSpace = "database.properties";
823
824         // boolean properties
825
Iterator it = props.getUserDefinedPropertyData().iterator();
826
827         while (it.hasNext()) {
828             Object JavaDoc[] metaData = (Object JavaDoc[]) it.next();
829
830             row = t.getEmptyRowData();
831             row[iscope] = scope;
832             row[ins] = nameSpace;
833             row[iname] = metaData[HsqlDatabaseProperties.indexName];
834             row[ivalue] = props.getProperty((String JavaDoc) row[iname]);
835             row[iclass] = metaData[HsqlDatabaseProperties.indexClass];
836
837             t.insertSys(row);
838         }
839         row = t.getEmptyRowData();
840         row[iscope] = scope;
841         row[ins] = nameSpace;
842         row[iname] = "SCRIPTFORMAT";
843
844         try {
845             row[ivalue] =
846                 ScriptWriterBase
847                     .LIST_SCRIPT_FORMATS[database.logger.getScriptType()];
848         } catch (Exception JavaDoc e) {}
849
850         row[iclass] = "java.lang.String";
851
852         t.insertSys(row);
853
854         // write delay
855
row = t.getEmptyRowData();
856         row[iscope] = scope;
857         row[ins] = nameSpace;
858         row[iname] = "WRITE_DELAY";
859         row[ivalue] = "" + database.logger.getWriteDelay();
860         row[iclass] = "int";
861
862         t.insertSys(row);
863
864         // ignore case
865
row = t.getEmptyRowData();
866         row[iscope] = scope;
867         row[ins] = nameSpace;
868         row[iname] = "IGNORECASE";
869         row[ivalue] = database.isIgnoreCase() ? "true"
870                                               : "false";
871         row[iclass] = "boolean";
872
873         t.insertSys(row);
874
875         // referential integrity
876
row = t.getEmptyRowData();
877         row[iscope] = scope;
878         row[ins] = nameSpace;
879         row[iname] = "REFERENTIAL_INTEGRITY";
880         row[ivalue] = database.isReferentialIntegrity() ? "true"
881                                                         : "false";
882         row[iclass] = "boolean";
883
884         t.insertSys(row);
885         t.setDataReadOnly(true);
886
887         return t;
888     }
889
890     /**
891      * Retrieves a <code>Table</code> object describing all visible
892      * sessions. ADMIN users see *all* sessions
893      * while non-admin users see only their own session.<p>
894      *
895      * Each row is a session state description with the following columns: <p>
896      *
897      * <pre class="SqlCodeExample">
898      * SESSION_ID INTEGER session identifier
899      * CONNECTED TIMESTAMP time at which session was created
900      * USER_NAME VARCHAR db user name of current session user
901      * IS_ADMIN BOOLEAN is session user an admin user?
902      * AUTOCOMMIT BOOLEAN is session in autocommit mode?
903      * READONLY BOOLEAN is session in read-only mode?
904      * MAXROWS INTEGER session's MAXROWS setting
905      * LAST_IDENTITY INTEGER last identity value used by this session
906      * TRANSACTION_SIZE INTEGER # of undo items in current transaction
907      * SCHEMA VARCHAR current schema for session
908      * </pre> <p>
909      *
910      * @return a <code>Table</code> object describing all visible
911      * sessions
912      * @throws HsqlException if an error occurs while producing the table
913      */

914     Table SYSTEM_SESSIONS() throws HsqlException {
915
916         Table t = sysTables[SYSTEM_SESSIONS];
917
918         if (t == null) {
919             t = createBlankTable(sysTableHsqlNames[SYSTEM_SESSIONS]);
920
921             addColumn(t, "SESSION_ID", Types.INTEGER, false);
922             addColumn(t, "CONNECTED", Types.TIMESTAMP, false);
923             addColumn(t, "USER_NAME", Types.VARCHAR, false);
924             addColumn(t, "IS_ADMIN", Types.BOOLEAN, false);
925             addColumn(t, "AUTOCOMMIT", Types.BOOLEAN, false);
926             addColumn(t, "READONLY", Types.BOOLEAN, false);
927             addColumn(t, "MAXROWS", Types.INTEGER, false);
928
929             // Note: some sessions may have a NULL LAST_IDENTITY value
930
addColumn(t, "LAST_IDENTITY", Types.BIGINT);
931             addColumn(t, "TRANSACTION_SIZE", Types.INTEGER, false);
932             addColumn(t, "SCHEMA", Types.VARCHAR, false);
933
934             // order: SESSION_ID
935
// true primary key
936
t.createPrimaryKey(null, new int[]{ 0 }, true);
937
938             return t;
939         }
940
941         // intermediate holders
942
Session[] sessions;
943         Session s;
944         Object JavaDoc[] row;
945
946         // column number mappings
947
final int isid = 0;
948         final int ict = 1;
949         final int iuname = 2;
950         final int iis_admin = 3;
951         final int iautocmt = 4;
952         final int ireadonly = 5;
953         final int imaxrows = 6;
954         final int ilast_id = 7;
955         final int it_size = 8;
956         final int it_schema = 9;
957
958         // Initialisation
959
sessions = ns.listVisibleSessions(session);
960
961         // Do it.
962
for (int i = 0; i < sessions.length; i++) {
963             s = sessions[i];
964             row = t.getEmptyRowData();
965             row[isid] = ValuePool.getInt(s.getId());
966             row[ict] = HsqlDateTime.getTimestamp(s.getConnectTime());
967             row[iuname] = s.getUsername();
968             row[iis_admin] = ValuePool.getBoolean(s.isAdmin());
969             row[iautocmt] = ValuePool.getBoolean(s.isAutoCommit());
970             row[ireadonly] = ValuePool.getBoolean(s.isReadOnly());
971             row[imaxrows] = ValuePool.getInt(s.getSQLMaxRows());
972             row[ilast_id] =
973                 ValuePool.getLong(s.getLastIdentity().longValue());
974             row[it_size] = ValuePool.getInt(s.getTransactionSize());
975             row[it_schema] = s.getSchemaName(null);
976
977             t.insertSys(row);
978         }
979
980         t.setDataReadOnly(true);
981
982         return t;
983     }
984
985     /**
986      * Retrieves a <code>Table</code> object describing the accessible
987      * direct super table (if any) of each accessible table defined
988      * within this database. <p>
989      *
990      * Each row is a super table description with the following columns: <p>
991      *
992      * <pre class="SqlCodeExample">
993      * TABLE_CAT VARCHAR the table's catalog
994      * TABLE_SCHEM VARCHAR table schema
995      * TABLE_NAME VARCHAR table name
996      * SUPERTABLE_NAME VARCHAR the direct super table's name
997      * </pre> <p>
998      * @return a <code>Table</code> object describing the accessible
999      * direct supertable (if any) of each accessible
1000     * table defined within this database
1001     * @throws HsqlException if an error occurs while producing the table
1002     */

1003    Table SYSTEM_SUPERTABLES() throws HsqlException {
1004
1005        Table t = sysTables[SYSTEM_SUPERTABLES];
1006
1007        if (t == null) {
1008            t = createBlankTable(sysTableHsqlNames[SYSTEM_SUPERTABLES]);
1009
1010            addColumn(t, "TABLE_CAT", Types.VARCHAR);
1011            addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1012            addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
1013
addColumn(t, "SUPERTABLE_NAME", Types.VARCHAR, false); // not null
1014
t.createPrimaryKey(null);
1015
1016            return t;
1017        }
1018
1019        t.setDataReadOnly(true);
1020
1021        return t;
1022    }
1023
1024    /**
1025     * Retrieves a <code>Table</code> object describing the accessible
1026     * direct super type (if any) of each accessible user-defined type (UDT)
1027     * defined within this database. <p>
1028     *
1029     * Each row is a super type description with the following columns: <p>
1030     *
1031     * <pre class="SqlCodeExample">
1032     * TYPE_CAT VARCHAR the UDT's catalog
1033     * TYPE_SCHEM VARCHAR UDT's schema
1034     * TYPE_NAME VARCHAR type name of the UDT
1035     * SUPERTYPE_CAT VARCHAR the direct super type's catalog
1036     * SUPERTYPE_SCHEM VARCHAR the direct super type's schema
1037     * SUPERTYPE_NAME VARCHAR the direct super type's name
1038     * </pre> <p>
1039     * @return a <code>Table</code> object describing the accessible
1040     * direct supertype (if any) of each accessible
1041     * user-defined type (UDT) defined within this database
1042     * @throws HsqlException if an error occurs while producing the table
1043     */

1044    Table SYSTEM_SUPERTYPES() throws HsqlException {
1045
1046        Table t = sysTables[SYSTEM_SUPERTYPES];
1047
1048        if (t == null) {
1049            t = createBlankTable(sysTableHsqlNames[SYSTEM_SUPERTYPES]);
1050
1051            addColumn(t, "TYPE_CAT", Types.VARCHAR);
1052            addColumn(t, "TYPE_SCHEM", Types.VARCHAR);
1053            addColumn(t, "TYPE_NAME", Types.VARCHAR, false); // not null
1054
addColumn(t, "SUPERTYPE_CAT", Types.VARCHAR);
1055            addColumn(t, "SUPERTYPE_SCHEM", Types.VARCHAR);
1056            addColumn(t, "SUPERTYPE_NAME", Types.VARCHAR, false); // not null
1057
t.createPrimaryKey(null);
1058
1059            return t;
1060        }
1061
1062        t.setDataReadOnly(true);
1063
1064        return t;
1065    }
1066
1067    /**
1068     * Retrieves a <code>Table</code> object describing the TEXT TABLE objects
1069     * defined within this database. The table contains one row for each row
1070     * in the SYSTEM_TABLES table with a HSQLDB_TYPE of TEXT . <p>
1071     *
1072     * Each row is a description of the attributes that defines its TEXT TABLE,
1073     * with the following columns:
1074     *
1075     * <pre class="SqlCodeExample">
1076     * TABLE_CAT VARCHAR table's catalog name
1077     * TABLE_SCHEM VARCHAR table's simple schema name
1078     * TABLE_NAME VARCHAR table's simple name
1079     * DATA_SOURCE_DEFINITION VARCHAR the "spec" proption of the table's
1080     * SET TABLE ... SOURCE DDL declaration
1081     * FILE_PATH VARCHAR absolute file path.
1082     * FILE_ENCODING VARCHAR endcoding of table's text file
1083     * FIELD_SEPARATOR VARCHAR default field separator
1084     * VARCHAR_SEPARATOR VARCAHR varchar field separator
1085     * LONGVARCHAR_SEPARATOR VARCHAR longvarchar field separator
1086     * IS_IGNORE_FIRST BOOLEAN ignores first line of file?
1087     * IS_QUOTED BOOLEAN fields are quoted if necessary?
1088     * IS_ALL_QUOTED BOOLEAN all fields are quoted?
1089     * IS_DESC BOOLEAN read rows starting at end of file?
1090     * </pre> <p>
1091     *
1092     * @return a <code>Table</code> object describing the text attributes
1093     * of the accessible text tables defined within this database
1094     * @throws HsqlException if an error occurs while producing the table
1095     *
1096     */

1097    Table SYSTEM_TEXTTABLES() throws HsqlException {
1098
1099        Table t = sysTables[SYSTEM_TEXTTABLES];
1100
1101        if (t == null) {
1102            t = createBlankTable(sysTableHsqlNames[SYSTEM_TEXTTABLES]);
1103
1104            addColumn(t, "TABLE_CAT", Types.VARCHAR);
1105            addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1106            addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
1107
addColumn(t, "DATA_SOURCE_DEFINTION", Types.VARCHAR);
1108            addColumn(t, "FILE_PATH", Types.VARCHAR);
1109            addColumn(t, "FILE_ENCODING", Types.VARCHAR);
1110            addColumn(t, "FIELD_SEPARATOR", Types.VARCHAR);
1111            addColumn(t, "VARCHAR_SEPARATOR", Types.VARCHAR);
1112            addColumn(t, "LONGVARCHAR_SEPARATOR", Types.VARCHAR);
1113            addColumn(t, "IS_IGNORE_FIRST", Types.BOOLEAN);
1114            addColumn(t, "IS_ALL_QUOTED", Types.BOOLEAN);
1115            addColumn(t, "IS_QUOTED", Types.BOOLEAN);
1116            addColumn(t, "IS_DESC", Types.BOOLEAN);
1117
1118            // ------------------------------------------------------------
1119
t.createPrimaryKey();
1120
1121            return t;
1122        }
1123
1124        // intermediate holders
1125
Iterator tables;
1126        Table table;
1127        Object JavaDoc[] row;
1128
1129// DITableInfo ti;
1130
TextCache tc;
1131
1132        // column number mappings
1133
final int itable_cat = 0;
1134        final int itable_schem = 1;
1135        final int itable_name = 2;
1136        final int idsd = 3;
1137        final int ifile_path = 4;
1138        final int ifile_enc = 5;
1139        final int ifs = 6;
1140        final int ivfs = 7;
1141        final int ilvfs = 8;
1142        final int iif = 9;
1143        final int iiq = 10;
1144        final int iiaq = 11;
1145        final int iid = 12;
1146
1147        // Initialization
1148
tables = database.schemaManager.allTablesIterator();
1149
1150        // Do it.
1151
while (tables.hasNext()) {
1152            table = (Table) tables.next();
1153
1154            if (!table.isText() ||!isAccessibleTable(table)) {
1155                continue;
1156            }
1157
1158            row = t.getEmptyRowData();
1159            row[itable_cat] = ns.getCatalogName(table);
1160            row[itable_schem] = table.getSchemaName();
1161            row[itable_name] = table.getName().name;
1162
1163            if (table.getCache() instanceof TextCache) {
1164                tc = (TextCache) table.getCache();
1165                row[idsd] = table.getDataSource();
1166                row[ifile_path] =
1167                    FileUtil.canonicalOrAbsolutePath(tc.getFileName());
1168                row[ifile_enc] = tc.stringEncoding;
1169                row[ifs] = tc.fs;
1170                row[ivfs] = tc.vs;
1171                row[ilvfs] = tc.lvs;
1172                row[iif] = ValuePool.getBoolean(tc.ignoreFirst);
1173                row[iiq] = ValuePool.getBoolean(tc.isQuoted);
1174                row[iiaq] = ValuePool.getBoolean(tc.isAllQuoted);
1175                row[iid] = ValuePool.getBoolean(table.isDescDataSource());
1176            }
1177
1178            t.insertSys(row);
1179        }
1180
1181        t.setDataReadOnly(true);
1182
1183        return t;
1184    }
1185
1186    /**
1187     * Retrieves a <code>Table</code> object describing the usage
1188     * of accessible columns in accessible triggers defined within
1189     * the database. <p>
1190     *
1191     * Each column usage description has the following columns: <p>
1192     *
1193     * <pre class="SqlCodeExample">
1194     * TRIGGER_CAT VARCHAR Trigger catalog.
1195     * TRIGGER_SCHEM VARCHAR Trigger schema.
1196     * TRIGGER_NAME VARCHAR Trigger name.
1197     * TABLE_CAT VARCHAR Catalog of table on which the trigger is defined.
1198     * TABLE_SCHEM VARCHAR Schema of table on which the trigger is defined.
1199     * TABLE_NAME VARCHAR Table on which the trigger is defined.
1200     * COLUMN_NAME VARCHAR Name of the column used in the trigger.
1201     * COLUMN_LIST VARCHAR Specified in UPDATE clause?: ("Y" | "N"}
1202     * COLUMN_USAGE VARCHAR {"NEW" | "OLD" | "IN" | "OUT" | "IN OUT"}
1203     * </pre> <p>
1204     * @return a <code>Table</code> object describing of the usage
1205     * of accessible columns in accessible triggers
1206     * defined within this database
1207     * @throws HsqlException if an error occurs while producing the table
1208     */

1209    Table SYSTEM_TRIGGERCOLUMNS() throws HsqlException {
1210
1211        Table t = sysTables[SYSTEM_TRIGGERCOLUMNS];
1212
1213        if (t == null) {
1214            t = createBlankTable(sysTableHsqlNames[SYSTEM_TRIGGERCOLUMNS]);
1215
1216            addColumn(t, "TRIGGER_CAT", Types.VARCHAR);
1217            addColumn(t, "TRIGGER_SCHEM", Types.VARCHAR);
1218            addColumn(t, "TRIGGER_NAME", Types.VARCHAR);
1219            addColumn(t, "TABLE_CAT", Types.VARCHAR);
1220            addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1221            addColumn(t, "TABLE_NAME", Types.VARCHAR);
1222            addColumn(t, "COLUMN_NAME", Types.VARCHAR);
1223            addColumn(t, "COLUMN_LIST", Types.VARCHAR);
1224            addColumn(t, "COLUMN_USAGE", Types.VARCHAR);
1225
1226            // order: all columns, in order, as each column
1227
// of each table may eventually be listed under various capacities
1228
// (when a more comprehensive trugger system is put in place)
1229
// false PK, as cat and schem may be null
1230
t.createPrimaryKey(null, new int[] {
1231                0, 1, 2, 3, 4, 5, 6, 7, 8
1232            }, false);
1233
1234            return t;
1235        }
1236
1237        Result rs;
1238
1239        // - used appends to make class file constant pool smaller
1240
// - saves ~ 100 bytes jar space
1241
rs = session.sqlExecuteDirectNoPreChecks(
1242            "select a.TRIGGER_CAT,a.TRIGGER_SCHEM,a.TRIGGER_NAME, "
1243            + "a.TABLE_CAT,a.TABLE_SCHEM,a.TABLE_NAME,b.COLUMN_NAME,'Y',"
1244            + "'IN' from INFORMATION_SCHEMA.SYSTEM_TRIGGERS a, "
1245            + "INFORMATION_SCHEMA.SYSTEM_COLUMNS b where "
1246            + "a.TABLE_NAME=b.TABLE_NAME and a.TABLE_SCHEM=b.TABLE_SCHEM");
1247
1248/*
1249            (new StringBuffer(185)).append("SELECT").append(' ').append(
1250                "a.").append("TRIGGER_CAT").append(',').append("a.").append(
1251                "TRIGGER_SCHEM").append(',').append("a.").append(
1252                "TRIGGER_NAME").append(',').append("a.").append(
1253                "TABLE_CAT").append(',').append("a.").append(
1254                "TABLE_SCHEM").append(',').append("a.").append(
1255                "TABLE_NAME").append(',').append("b.").append(
1256                "COLUMN_NAME").append(',').append("'Y'").append(',').append(
1257                "'IN'").append(' ').append("from").append(' ').append(
1258                "INFORMATION_SCHEMA").append('.').append(
1259                "SYSTEM_TRIGGERS").append(" a,").append(
1260                "INFORMATION_SCHEMA").append('.').append(
1261                "SYSTEM_COLUMNS").append(" b ").append("where").append(
1262                ' ').append("a.").append("TABLE_NAME").append('=').append(
1263                "b.").append("TABLE_NAME").toString();
1264*/

1265        t.insertSys(rs);
1266        t.setDataReadOnly(true);
1267
1268        return t;
1269    }
1270
1271    /**
1272     * Retrieves a <code>Table</code> object describing the accessible
1273     * triggers defined within the database. <p>
1274     *
1275     * Each row is a trigger description with the following columns: <p>
1276     *
1277     * <pre class="SqlCodeExample">
1278     * TRIGGER_CAT VARCHAR Trigger catalog.
1279     * TRIGGER_SCHEM VARCHAR Trigger Schema.
1280     * TRIGGER_NAME VARCHAR Trigger Name.
1281     * TRIGGER_TYPE VARCHAR {("BEFORE" | "AFTER") + [" EACH ROW"] }
1282     * TRIGGERING_EVENT VARCHAR {"INSERT" | "UPDATE" | "DELETE"}
1283     * (future?: "INSTEAD OF " + ("SELECT" | ...))
1284     * TABLE_CAT VARCHAR Table's catalog.
1285     * TABLE_SCHEM VARCHAR Table's schema.
1286     * BASE_OBJECT_TYPE VARCHAR "TABLE"
1287     * (future?: "VIEW" | "SCHEMA" | "DATABASE")
1288     * TABLE_NAME VARCHAR Table on which trigger is defined
1289     * COLUMN_NAME VARCHAR NULL (future?: nested table column name)
1290     * REFERENCING_NAMES VARCHAR ROW, OLD, NEW, etc.
1291     * WHEN_CLAUSE VARCHAR Condition firing trigger (NULL => always)
1292     * STATUS VARCHAR {"ENABLED" | "DISABLED"}
1293     * DESCRIPTION VARCHAR typically, the trigger's DDL
1294     * ACTION_TYPE VARCHAR "CALL" (future?: embedded language name)
1295     * TRIGGER_BODY VARCHAR Statement(s) executed
1296     * </pre> <p>
1297     *
1298     * @return a <code>Table</code> object describing the accessible
1299     * triggers defined within this database.
1300     * @throws HsqlException if an error occurs while producing the table
1301     */

1302    Table SYSTEM_TRIGGERS() throws HsqlException {
1303
1304        Table t = sysTables[SYSTEM_TRIGGERS];
1305
1306        if (t == null) {
1307            t = createBlankTable(sysTableHsqlNames[SYSTEM_TRIGGERS]);
1308
1309            addColumn(t, "TRIGGER_CAT", Types.VARCHAR);
1310            addColumn(t, "TRIGGER_SCHEM", Types.VARCHAR);
1311            addColumn(t, "TRIGGER_NAME", Types.VARCHAR, false);
1312            addColumn(t, "TRIGGER_TYPE", Types.VARCHAR, 15, false);
1313            addColumn(t, "TRIGGERING_EVENT", Types.VARCHAR, 10, false);
1314            addColumn(t, "TABLE_CAT", Types.VARCHAR);
1315            addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1316            addColumn(t, "BASE_OBJECT_TYPE", Types.VARCHAR, 8, false);
1317            addColumn(t, "TABLE_NAME", Types.VARCHAR, false);
1318            addColumn(t, "COLUMN_NAME", Types.VARCHAR);
1319            addColumn(t, "REFERENCING_NAMES", Types.VARCHAR, false);
1320            addColumn(t, "WHEN_CLAUSE", Types.VARCHAR);
1321            addColumn(t, "STATUS", Types.VARCHAR, 8, false);
1322            addColumn(t, "DESCRIPTION", Types.VARCHAR, false);
1323            addColumn(t, "ACTION_TYPE", Types.VARCHAR, false);
1324            addColumn(t, "TRIGGER_BODY", Types.VARCHAR, false);
1325
1326            // order: TRIGGER_TYPE, TRIGGER_SCHEM, TRIGGER_NAME
1327
// added for unique: TRIGGER_CAT
1328
// false PK, as TRIGGER_SCHEM and/or TRIGGER_CAT may be null
1329
t.createPrimaryKey(null, new int[] {
1330                3, 1, 2, 0
1331            }, false);
1332
1333            return t;
1334        }
1335
1336        // calculated column values
1337
String JavaDoc triggerCatalog;
1338        String JavaDoc triggerSchema;
1339        String JavaDoc triggerName;
1340        String JavaDoc triggerType;
1341        String JavaDoc triggeringEvent;
1342        String JavaDoc tableCatalog;
1343        String JavaDoc tableSchema;
1344        String JavaDoc baseObjectType;
1345        String JavaDoc tableName;
1346        String JavaDoc columnName;
1347        String JavaDoc referencingNames;
1348        String JavaDoc whenClause;
1349        String JavaDoc status;
1350        String JavaDoc description;
1351        String JavaDoc actionType;
1352        String JavaDoc triggerBody;
1353
1354        // Intermediate holders
1355
Iterator tables;
1356        Table table;
1357        HsqlArrayList[] vTrigs;
1358        HsqlArrayList triggerList;
1359        TriggerDef def;
1360        Object JavaDoc[] row;
1361
1362        // column number mappings
1363
final int itrigger_cat = 0;
1364        final int itrigger_schem = 1;
1365        final int itrigger_name = 2;
1366        final int itrigger_type = 3;
1367        final int itriggering_event = 4;
1368        final int itable_cat = 5;
1369        final int itable_schem = 6;
1370        final int ibase_object_type = 7;
1371        final int itable_name = 8;
1372        final int icolumn_name = 9;
1373        final int ireferencing_names = 10;
1374        final int iwhen_clause = 11;
1375        final int istatus = 12;
1376        final int idescription = 13;
1377        final int iaction_type = 14;
1378        final int itrigger_body = 15;
1379
1380        // Initialization
1381
tables = database.schemaManager.allTablesIterator();
1382
1383        // these are the only values supported, currently
1384
actionType = "CALL";
1385        baseObjectType = "TABLE";
1386        columnName = null;
1387        referencingNames = "ROW";
1388        whenClause = null;
1389
1390        // Do it.
1391
while (tables.hasNext()) {
1392            table = (Table) tables.next();
1393            vTrigs = table.triggerLists;
1394
1395            // faster test first
1396
if (vTrigs == null) {
1397                continue;
1398            }
1399
1400            if (!isAccessibleTable(table)) {
1401                continue;
1402            }
1403
1404            tableCatalog = ns.getCatalogName(table);
1405            triggerCatalog = tableCatalog;
1406            tableSchema = table.getSchemaName();
1407            triggerSchema = tableSchema;
1408            tableName = table.getName().name;
1409
1410            for (int i = 0; i < vTrigs.length; i++) {
1411                triggerList = vTrigs[i];
1412
1413                if (triggerList == null) {
1414                    continue;
1415                }
1416
1417                for (int j = 0; j < triggerList.size(); j++) {
1418                    def = (TriggerDef) triggerList.get(j);
1419
1420                    if (def == null) {
1421                        continue;
1422                    }
1423
1424                    triggerName = def.name.name;
1425                    description = def.getDDL().toString();
1426                    status = def.valid ? "ENABLED"
1427                                            : "DISABLED";
1428                    triggerBody = def.triggerClassName;
1429                    triggerType = def.when;
1430
1431                    if (def.forEachRow) {
1432                        triggerType += " EACH ROW";
1433                    }
1434
1435                    triggeringEvent = def.operation;
1436                    row = t.getEmptyRowData();
1437                    row[itrigger_cat] = triggerCatalog;
1438                    row[itrigger_schem] = triggerSchema;
1439                    row[itrigger_name] = triggerName;
1440                    row[itrigger_type] = triggerType;
1441                    row[itriggering_event] = triggeringEvent;
1442                    row[itable_cat] = tableCatalog;
1443                    row[itable_schem] = tableSchema;
1444                    row[ibase_object_type] = baseObjectType;
1445                    row[itable_name] = tableName;
1446                    row[icolumn_name] = columnName;
1447                    row[ireferencing_names] = referencingNames;
1448                    row[iwhen_clause] = whenClause;
1449                    row[istatus] = status;
1450                    row[idescription] = description;
1451                    row[iaction_type] = actionType;
1452                    row[itrigger_body] = triggerBody;
1453
1454                    t.insertSys(row);
1455                }
1456            }
1457        }
1458
1459        t.setDataReadOnly(true);
1460
1461        return t;
1462    }
1463
1464    /**
1465     * Retrieves a <code>Table</code> object describing the accessible
1466     * attributes of the accessible user-defined type (UDT) objects
1467     * defined within this database. <p>
1468     *
1469     * This description does not contain inherited attributes. <p>
1470     *
1471     * Each row is a user-defined type attributes description with the
1472     * following columns:
1473     *
1474     * <pre class="SqlCodeExample">
1475     * TYPE_CAT VARCHAR type catalog
1476     * TYPE_SCHEM VARCHAR type schema
1477     * TYPE_NAME VARCHAR type name
1478     * ATTR_NAME VARCHAR attribute name
1479     * DATA_TYPE SMALLINT attribute's SQL type from DITypes
1480     * ATTR_TYPE_NAME VARCHAR UDT: fully qualified type name
1481     * REF: fully qualified type name of target type of
1482     * the reference type.
1483     * ATTR_SIZE INTEGER column size.
1484     * char or date types => maximum number of characters;
1485     * numeric or decimal types => precision.
1486     * DECIMAL_DIGITS INTEGER # of fractional digits (scale) of number type
1487     * NUM_PREC_RADIX INTEGER Radix of number type
1488     * NULLABLE INTEGER whether NULL is allowed
1489     * REMARKS VARCHAR comment describing attribute
1490     * ATTR_DEF VARCHAR default attribute value
1491     * SQL_DATA_TYPE INTEGER expected value of SQL CLI SQL_DESC_TYPE in the SQLDA
1492     * SQL_DATETIME_SUB INTEGER DATETIME/INTERVAL => datetime/interval subcode
1493     * CHAR_OCTET_LENGTH INTEGER for char types: max bytes in column
1494     * ORDINAL_POSITION INTEGER index of column in table (starting at 1)
1495     * IS_NULLABLE VARCHAR "NO" => strictly no NULL values;
1496     * "YES" => maybe NULL values;
1497     * "" => unknown.
1498     * SCOPE_CATALOG VARCHAR catalog of REF attribute scope table or NULL
1499     * SCOPE_SCHEMA VARCHAR schema of REF attribute scope table or NULL
1500     * SCOPE_TABLE VARCHAR name of REF attribute scope table or NULL
1501     * SOURCE_DATA_TYPE SMALLINT For DISTINCT or user-generated REF DATA_TYPE:
1502     * source SQL type from DITypes
1503     * For other DATA_TYPE values: NULL
1504     * </pre>
1505     *
1506     * <B>Note:</B> Currently, neither the HSQLDB engine or the JDBC driver
1507     * support UDTs, so an empty table is returned. <p>
1508     * @return a <code>Table</code> object describing the accessible
1509     * attrubutes of the accessible user-defined type
1510     * (UDT) objects defined within this database
1511     * @throws HsqlException if an error occurs while producing the table
1512     */

1513    Table SYSTEM_UDTATTRIBUTES() throws HsqlException {
1514
1515        Table t = sysTables[SYSTEM_UDTATTRIBUTES];
1516
1517        if (t == null) {
1518            t = createBlankTable(sysTableHsqlNames[SYSTEM_UDTATTRIBUTES]);
1519
1520            addColumn(t, "TYPE_CAT", Types.VARCHAR);
1521            addColumn(t, "TYPE_SCHEM", Types.VARCHAR);
1522            addColumn(t, "TYPE_NAME", Types.VARCHAR, false); // not null
1523
addColumn(t, "ATTR_NAME", Types.VARCHAR, false); // not null
1524
addColumn(t, "DATA_TYPE", Types.SMALLINT, false); // not null
1525
addColumn(t, "ATTR_TYPE_NAME", Types.VARCHAR, false); // not null
1526
addColumn(t, "ATTR_SIZE", Types.INTEGER);
1527            addColumn(t, "DECIMAL_DIGITS", Types.INTEGER);
1528            addColumn(t, "NUM_PREC_RADIX", Types.INTEGER);
1529            addColumn(t, "NULLABLE", Types.INTEGER);
1530            addColumn(t, "REMARKS", Types.VARCHAR);
1531            addColumn(t, "ATTR_DEF", Types.VARCHAR);
1532            addColumn(t, "SQL_DATA_TYPE", Types.INTEGER);
1533            addColumn(t, "SQL_DATETIME_SUB", Types.INTEGER);
1534            addColumn(t, "CHAR_OCTET_LENGTH", Types.INTEGER);
1535            addColumn(t, "ORDINAL_POSITION", Types.INTEGER, false); // not null
1536
addColumn(t, "IS_NULLABLE", Types.VARCHAR, false); // not null
1537
addColumn(t, "SCOPE_CATALOG", Types.VARCHAR);
1538            addColumn(t, "SCOPE_SCHEMA", Types.VARCHAR);
1539            addColumn(t, "SCOPE_TABLE", Types.VARCHAR);
1540            addColumn(t, "SOURCE_DATA_TYPE", Types.SMALLINT);
1541            t.createPrimaryKey(null);
1542
1543            return t;
1544        }
1545
1546        t.setDataReadOnly(true);
1547
1548        return t;
1549    }
1550
1551    /**
1552     * Retrieves a <code>Table</code> object describing the accessible
1553     * user-defined types defined in this database. <p>
1554     *
1555     * Schema-specific UDTs may have type JAVA_OBJECT, STRUCT, or DISTINCT.
1556     *
1557     * <P>Each row is a UDT descripion with the following columns:
1558     * <OL>
1559     * <LI><B>TYPE_CAT</B> <code>VARCHAR</code> => the type's catalog
1560     * <LI><B>TYPE_SCHEM</B> <code>VARCHAR</code> => type's schema
1561     * <LI><B>TYPE_NAME</B> <code>VARCHAR</code> => type name
1562     * <LI><B>CLASS_NAME</B> <code>VARCHAR</code> => Java class name
1563     * <LI><B>DATA_TYPE</B> <code>VARCHAR</code> =>
1564     * type value defined in <code>DITypes</code>;
1565     * one of <code>JAVA_OBJECT</code>, <code>STRUCT</code>, or
1566     * <code>DISTINCT</code>
1567     * <LI><B>REMARKS</B> <code>VARCHAR</code> =>
1568     * explanatory comment on the type
1569     * <LI><B>BASE_TYPE</B><code>SMALLINT</code> =>
1570     * type code of the source type of a DISTINCT type or the
1571     * type that implements the user-generated reference type of the
1572     * SELF_REFERENCING_COLUMN of a structured type as defined in
1573     * DITypes (null if DATA_TYPE is not DISTINCT or not
1574     * STRUCT with REFERENCE_GENERATION = USER_DEFINED)
1575     *
1576     * </OL> <p>
1577     *
1578     * <B>Note:</B> Currently, neither the HSQLDB engine or the JDBC driver
1579     * support UDTs, so an empty table is returned. <p>
1580     *
1581     * @return a <code>Table</code> object describing the accessible
1582     * user-defined types defined in this database
1583     * @throws HsqlException if an error occurs while producing the table
1584     */

1585    Table SYSTEM_UDTS() throws HsqlException {
1586
1587        Table t = sysTables[SYSTEM_UDTS];
1588
1589        if (t == null) {
1590            t = createBlankTable(sysTableHsqlNames[SYSTEM_UDTS]);
1591
1592            addColumn(t, "TYPE_CAT", Types.VARCHAR);
1593            addColumn(t, "TYPE_SCHEM", Types.VARCHAR);
1594            addColumn(t, "TYPE_NAME", Types.VARCHAR, false); // not null
1595
addColumn(t, "CLASS_NAME", Types.VARCHAR, false); // not null
1596
addColumn(t, "DATA_TYPE", Types.VARCHAR, false); // not null
1597
addColumn(t, "REMARKS", Types.VARCHAR);
1598            addColumn(t, "BASE_TYPE", Types.SMALLINT);
1599            t.createPrimaryKey(null);
1600
1601            return t;
1602        }
1603
1604        t.setDataReadOnly(true);
1605
1606        return t;
1607    }
1608
1609    /**
1610     * Retrieves a <code>Table</code> object describing the accessible
1611     * columns that are automatically updated when any value in a row
1612     * is updated. <p>
1613     *
1614     * Each row is a version column description with the following columns: <p>
1615     *
1616     * <OL>
1617     * <LI><B>SCOPE</B> <code>SMALLINT</code> => is not used
1618     * <LI><B>COLUMN_NAME</B> <code>VARCHAR</code> => column name
1619     * <LI><B>DATA_TYPE</B> <code>SMALLINT</code> =>
1620     * SQL data type from java.sql.Types
1621     * <LI><B>TYPE_NAME</B> <code>SMALLINT</code> =>
1622     * Data source dependent type name
1623     * <LI><B>COLUMN_SIZE</B> <code>INTEGER</code> => precision
1624     * <LI><B>BUFFER_LENGTH</B> <code>INTEGER</code> =>
1625     * length of column value in bytes
1626     * <LI><B>DECIMAL_DIGITS</B> <code>SMALLINT</code> => scale
1627     * <LI><B>PSEUDO_COLUMN</B> <code>SMALLINT</code> =>
1628     * is this a pseudo column like an Oracle <code>ROWID</code>:<BR>
1629     * (as defined in <code>java.sql.DatabaseMetadata</code>)
1630     * <UL>
1631     * <LI><code>versionColumnUnknown</code> - may or may not be
1632     * pseudo column
1633     * <LI><code>versionColumnNotPseudo</code> - is NOT a pseudo column
1634     * <LI><code>versionColumnPseudo</code> - is a pseudo column
1635     * </UL>
1636     * </OL> <p>
1637     *
1638     * <B>Note:</B> Currently, the HSQLDB engine does not support version
1639     * columns, so an empty table is returned. <p>
1640     *
1641     * @return a <code>Table</code> object describing the columns
1642     * that are automatically updated when any value
1643     * in a row is updated
1644     * @throws HsqlException if an error occurs while producing the table
1645     */

1646    Table SYSTEM_VERSIONCOLUMNS() throws HsqlException {
1647
1648        Table t = sysTables[SYSTEM_VERSIONCOLUMNS];
1649
1650        if (t == null) {
1651            t = createBlankTable(sysTableHsqlNames[SYSTEM_VERSIONCOLUMNS]);
1652
1653            // ----------------------------------------------------------------
1654
// required by DatabaseMetaData.getVersionColumns result set
1655
// ----------------------------------------------------------------
1656
addColumn(t, "SCOPE", Types.INTEGER);
1657            addColumn(t, "COLUMN_NAME", Types.VARCHAR, false); // not null
1658
addColumn(t, "DATA_TYPE", Types.SMALLINT, false); // not null
1659
addColumn(t, "TYPE_NAME", Types.VARCHAR, false); // not null
1660
addColumn(t, "COLUMN_SIZE", Types.SMALLINT);
1661            addColumn(t, "BUFFER_LENGTH", Types.INTEGER);
1662            addColumn(t, "DECIMAL_DIGITS", Types.SMALLINT);
1663            addColumn(t, "PSEUDO_COLUMN", Types.SMALLINT, false); // not null
1664

1665            // -----------------------------------------------------------------
1666
// required by DatabaseMetaData.getVersionColumns filter parameters
1667
// -----------------------------------------------------------------
1668
addColumn(t, "TABLE_CAT", Types.VARCHAR);
1669            addColumn(t, "TABLE_SCHEM", Types.VARCHAR);
1670            addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
1671

1672            // -----------------------------------------------------------------
1673
t.createPrimaryKey(null);
1674
1675            return t;
1676        }
1677
1678        t.setDataReadOnly(true);
1679
1680        return t;
1681    }
1682
1683    /**
1684     * Retrieves a <code>Table</code> object describing the VIEW objects
1685     * defined within this database. The table contains one row for each row
1686     * in the SYSTEM_TABLES table with a TABLE_TYPE of VIEW . <p>
1687     *
1688     * Each row is a description of the query expression that defines its view,
1689     * with the following columns:
1690     *
1691     * <pre class="SqlCodeExample">
1692     * TABLE_CATALOG VARCHAR name of view's defining catalog.
1693     * TABLE_SCHEMA VARCHAR unqualified name of view's defining schema.
1694     * TABLE_NAME VARCHAR the simple name of the view.
1695     * VIEW_DEFINITION VARCHAR the character representation of the
1696     * &lt;query expression&gt; contained in the
1697     * corresponding &lt;view descriptor&gt;.
1698     * CHECK_OPTION VARCHAR {"CASCADED" | "LOCAL" | "NONE"}
1699     * IS_UPDATABLE VARCHAR {"YES" | "NO"}
1700     * VALID BOOLEAN Always TRUE: VIEW_DEFINITION currently
1701     * represents a valid &lt;query expression&gt.
1702     *
1703     * </pre> <p>
1704     *
1705     * @return a tabular description of the text source of all
1706     * <code>View</code> objects accessible to
1707     * the user.
1708     * @throws HsqlException if an error occurs while producing the table
1709     */

1710    Table SYSTEM_VIEWS() throws HsqlException {
1711
1712        Table t = sysTables[SYSTEM_VIEWS];
1713
1714        if (t == null) {
1715            t = createBlankTable(sysTableHsqlNames[SYSTEM_VIEWS]);
1716
1717            addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
1718            addColumn(t, "TABLE_SCHEMA", Types.VARCHAR);
1719            addColumn(t, "TABLE_NAME", Types.VARCHAR, true); // not null
1720
addColumn(t, "VIEW_DEFINITION", Types.VARCHAR, true); // not null
1721
addColumn(t, "CHECK_OPTION", Types.VARCHAR, 8, true); // not null
1722
addColumn(t, "IS_UPDATABLE", Types.VARCHAR, 3, true); // not null
1723
addColumn(t, "VALID", Types.BOOLEAN, true); // not null
1724

1725            // order TABLE_NAME
1726
// added for unique: TABLE_SCHEMA, TABLE_CATALOG
1727
// false PK, as TABLE_SCHEMA and/or TABLE_CATALOG may be null
1728
t.createPrimaryKey(null, new int[] {
1729                1, 2, 0
1730            }, false);
1731
1732            return t;
1733        }
1734
1735        String JavaDoc defn;
1736        Iterator tables;
1737        Table table;
1738        Object JavaDoc[] row;
1739        final int icat = 0;
1740        final int ischem = 1;
1741        final int iname = 2;
1742        final int idefn = 3;
1743        final int icopt = 4;
1744        final int iiupd = 5;
1745        final int ivalid = 6;
1746
1747        tables = database.schemaManager.allTablesIterator();
1748
1749        while (tables.hasNext()) {
1750            table = (Table) tables.next();
1751
1752            if (!table.isView() ||!isAccessibleTable(table)) {
1753                continue;
1754            }
1755
1756            row = t.getEmptyRowData();
1757            defn = ((View) table).getStatement();
1758            row[icat] = ns.getCatalogName(table);
1759            row[ischem] = table.getSchemaName();
1760            row[iname] = table.getName().name;
1761            row[idefn] = defn;
1762            row[icopt] = "NONE";
1763            row[iiupd] = "NO";
1764            row[ivalid] = Boolean.TRUE;
1765
1766            t.insertSys(row);
1767        }
1768
1769        t.setDataReadOnly(true);
1770
1771        return t;
1772    }
1773
1774    /**
1775     * Retrieves a <code>Table</code> object describing the
1776     * return, parameter and result columns of the accessible
1777     * routines defined within this database.<p>
1778     *
1779     * Each row is a procedure column description with the following
1780     * columns: <p>
1781     *
1782     * <pre class="SqlCodeExample">
1783     * PROCEDURE_CAT VARCHAR routine catalog
1784     * PROCEDURE_SCHEM VARCHAR routine schema
1785     * PROCEDURE_NAME VARCHAR routine name
1786     * COLUMN_NAME VARCHAR column/parameter name
1787     * COLUMN_TYPE SMALLINT kind of column/parameter
1788     * DATA_TYPE SMALLINT SQL type from DITypes
1789     * TYPE_NAME VARCHAR SQL type name
1790     * PRECISION INTEGER precision (length) of type
1791     * LENGTH INTEGER transfer size, in bytes, if definitely known
1792     * (roughly equivalent to BUFFER_SIZE for table
1793     * columns)
1794     * SCALE SMALLINT scale
1795     * RADIX SMALLINT radix
1796     * NULLABLE SMALLINT can column contain NULL?
1797     * REMARKS VARCHAR explanatory comment on column
1798     * SPECIFIC_NAME VARCHAR typically (but not restricted to) a
1799     * fully qulified Java Method name and signature
1800     * SEQ INTEGER The JDBC-specified order within
1801     * runs of PROCEDURE_SCHEM, PROCEDURE_NAME,
1802     * SPECIFIC_NAME, which is:
1803     *
1804     * return value (0), if any, first, followed
1805     * by the parameter descriptions in call order
1806     * (1..n1), followed by the result column
1807     * descriptions in column number order
1808     * (n1 + 1..n1 + n2)
1809     * </pre> <p>
1810     *
1811     * @return a <code>Table</code> object describing the
1812     * return, parameter and result columns
1813     * of the accessible routines defined
1814     * within this database.
1815     * @throws HsqlException if an error occurs while producing the table
1816     */

1817    Table SYSTEM_PROCEDURECOLUMNS() throws HsqlException {
1818
1819        Table t = sysTables[SYSTEM_PROCEDURECOLUMNS];
1820
1821        if (t == null) {
1822            return super.SYSTEM_PROCEDURECOLUMNS();
1823        }
1824
1825        // calculated column values
1826
String JavaDoc procedureCatalog;
1827        String JavaDoc procedureSchema;
1828        String JavaDoc procedureName;
1829        String JavaDoc columnName;
1830        Integer JavaDoc columnType;
1831        Integer JavaDoc dataType;
1832        String JavaDoc dataTypeName;
1833        Integer JavaDoc precision;
1834        Integer JavaDoc length;
1835        Integer JavaDoc scale;
1836        Integer JavaDoc radix;
1837        Integer JavaDoc nullability;
1838        String JavaDoc remark;
1839        String JavaDoc specificName;
1840        int colSequence;
1841        int colCount;
1842
1843        // intermediate holders
1844
HsqlArrayList aliasList;
1845        Object JavaDoc[] info;
1846        Method JavaDoc method;
1847        Iterator methods;
1848        Object JavaDoc[] row;
1849        DITypeInfo ti;
1850
1851        // Initialization
1852
methods = ns.iterateAllAccessibleMethods(session, true); // and aliases
1853
ti = new DITypeInfo();
1854
1855        // no such thing as identity or ignorecase return/parameter
1856
// procedure columns. Future: may need to worry about this if
1857
// result columns are ever reported
1858
ti.setTypeSub(Types.TYPE_SUB_DEFAULT);
1859
1860        // Do it.
1861
while (methods.hasNext()) {
1862            info = (Object JavaDoc[]) methods.next();
1863            method = (Method JavaDoc) info[0];
1864            aliasList = (HsqlArrayList) info[1];
1865            procedureCatalog = ns.getCatalogName(method);
1866            procedureSchema = ns.getSchemaName(method);
1867
1868            pi.setMethod(method);
1869
1870            specificName = pi.getSpecificName();
1871            procedureName = pi.getFQN();
1872            colCount = pi.getColCount();
1873
1874            for (int i = 0; i < colCount; i++) {
1875                ti.setTypeCode(pi.getColTypeCode(i));
1876
1877                columnName = pi.getColName(i);
1878                columnType = pi.getColUsage(i);
1879                dataType = pi.getColDataType(i);
1880                dataTypeName = ti.getTypeName();
1881                precision = ti.getPrecision();
1882                length = pi.getColLen(i);
1883                scale = ti.getDefaultScale();
1884                radix = ti.getNumPrecRadix();
1885                nullability = pi.getColNullability(i);
1886                remark = pi.getColRemark(i);
1887                colSequence = pi.getColSequence(i);
1888
1889                addPColRows(t, aliasList, procedureCatalog, procedureSchema,
1890                            procedureName, columnName, columnType, dataType,
1891                            dataTypeName, precision, length, scale, radix,
1892                            nullability, remark, specificName, colSequence);
1893            }
1894        }
1895
1896        t.setDataReadOnly(true);
1897
1898        return t;
1899    }
1900
1901    /**
1902     * Retrieves a <code>Table</code> object describing the accessible
1903     * routines defined within this database.
1904     *
1905     * Each row is a procedure description with the following
1906     * columns: <p>
1907     *
1908     * <pre class="SqlCodeExample">
1909     * PROCEDURE_CAT VARCHAR catalog in which routine is defined
1910     * PROCEDURE_SCHEM VARCHAR schema in which routine is defined
1911     * PROCEDURE_NAME VARCHAR simple routine identifier
1912     * NUM_INPUT_PARAMS INTEGER number of input parameters
1913     * NUM_OUTPUT_PARAMS INTEGER number of output parameters
1914     * NUM_RESULT_SETS INTEGER number of result sets returned
1915     * REMARKS VARCHAR explanatory comment on the routine
1916     * PROCEDURE_TYPE SMALLINT { Unknown | No Result | Returns Result }
1917     * ORIGIN VARCHAR {ALIAS |
1918     * [BUILTIN | USER DEFINED] ROUTINE |
1919     * [BUILTIN | USER DEFINED] TRIGGER |
1920     * ...}
1921     * SPECIFIC_NAME VARCHAR typically (but not restricted to) a
1922     * a fully qualified Java Method name
1923     * and signature
1924     * </pre> <p>
1925     *
1926     * @return a <code>Table</code> object describing the accessible
1927     * routines defined within the this database
1928     * @throws HsqlException if an error occurs while producing the table
1929     */

1930    Table SYSTEM_PROCEDURES() throws HsqlException {
1931
1932        Table t = sysTables[SYSTEM_PROCEDURES];
1933
1934        if (t == null) {
1935            return super.SYSTEM_PROCEDURES();
1936        }
1937
1938        // calculated column values
1939
// ------------------------
1940
// required
1941
// ------------------------
1942
String JavaDoc catalog;
1943        String JavaDoc schema;
1944        String JavaDoc procName;
1945        Integer JavaDoc numInputParams;
1946        Integer JavaDoc numOutputParams;
1947        Integer JavaDoc numResultSets;
1948        String JavaDoc remarks;
1949        Integer JavaDoc procRType;
1950
1951        // -------------------
1952
// extended
1953
// -------------------
1954
String JavaDoc procOrigin;
1955        String JavaDoc specificName;
1956
1957        // intermediate holders
1958
String JavaDoc alias;
1959        HsqlArrayList aliasList;
1960        Iterator methods;
1961        Object JavaDoc[] methodInfo;
1962        Method JavaDoc method;
1963        String JavaDoc methodOrigin;
1964        Object JavaDoc[] row;
1965
1966        // Initialization
1967
methods = ns.iterateAllAccessibleMethods(session, true); //and aliases
1968

1969        // Do it.
1970
while (methods.hasNext()) {
1971            methodInfo = (Object JavaDoc[]) methods.next();
1972            method = (Method JavaDoc) methodInfo[0];
1973            aliasList = (HsqlArrayList) methodInfo[1];
1974            methodOrigin = (String JavaDoc) methodInfo[2];
1975
1976            pi.setMethod(method);
1977
1978            catalog = ns.getCatalogName(method);
1979            schema = ns.getSchemaName(method);
1980            procName = pi.getFQN();
1981            numInputParams = pi.getInputParmCount();
1982            numOutputParams = pi.getOutputParmCount();
1983            numResultSets = pi.getResultSetCount();
1984            remarks = pi.getRemark();
1985            procRType = pi.getResultType(methodOrigin);
1986            procOrigin = pi.getOrigin(methodOrigin);
1987            specificName = pi.getSpecificName();
1988
1989            addProcRows(t, aliasList, catalog, schema, procName,
1990                        numInputParams, numOutputParams, numResultSets,
1991                        remarks, procRType, procOrigin, specificName);
1992        }
1993
1994        t.setDataReadOnly(true);
1995
1996        return t;
1997    }
1998
1999    /**
2000     * The SYSTEM_USAGE_PRIVILEGES table has one row for each usage privilege
2001     * descriptor. <p>
2002     *
2003     * It effectively contains a representation of the usage privilege
2004     * descriptors. <p>
2005     *
2006     * <b>Definition:</b> <p>
2007     *
2008     * <pre class="SqlCodeExample">
2009     * CREATE TABLE SYSTEM_USAGE_PRIVILEGES (
2010     * GRANTOR VARCHAR NOT NULL,
2011     * GRANTEE VARCHAR NOT NULL,
2012     * OBJECT_CATALOG VARCHAR NULL,
2013     * OBJECT_SCHEMA VARCHAR NULL,
2014     * OBJECT_NAME VARCHAR NOT NULL,
2015     * OBJECT_TYPE VARCHAR NOT NULL
2016     *
2017     * CHECK ( OBJECT_TYPE IN (
2018     * 'DOMAIN',
2019     * 'CHARACTER SET',
2020     * 'COLLATION',
2021     * 'TRANSLATION',
2022     * 'SEQUENCE' ) ),
2023     *
2024     * IS_GRANTABLE VARCHAR NOT NULL
2025     *
2026     * CHECK ( IS_GRANTABLE IN ( 'YES', 'NO' ) ),
2027     *
2028     * UNIQUE( GRANTOR, GRANTEE, OBJECT_CATALOG,
2029     * OBJECT_SCHEMA, OBJECT_NAME, OBJECT_TYPE )
2030     * )
2031     * </pre>
2032     *
2033     * <b>Description:</b><p>
2034     *
2035     * <ol>
2036     * <li> The value of GRANTOR is the &lt;authorization identifier&gt; of the
2037     * user or role who granted usage privileges on the object of the type
2038     * identified by OBJECT_TYPE that is identified by OBJECT_CATALOG,
2039     * OBJECT_SCHEMA, and OBJECT_NAME, to the user or role identified by the
2040     * value of GRANTEE forthe usage privilege being described. <p>
2041     *
2042     * <li> The value of GRANTEE is the &lt;authorization identifier&gt; of some
2043     * user or role, or PUBLIC to indicate all users, to whom the usage
2044     * privilege being described is granted. <p>
2045     *
2046     * <li> The values of OBJECT_CATALOG, OBJECT_SCHEMA, and OBJECT_NAME are the
2047     * catalog name, unqualified schema name, and qualified identifier,
2048     * respectively, of the object to which the privilege applies. <p>
2049     *
2050     * <li> The values of OBJECT_TYPE have the following meanings: <p>
2051     *
2052     * <table border cellpadding="3">
2053     * <tr>
2054     * <td nowrap>DOMAIN</td>
2055     * <td nowrap>The object to which the privilege applies is
2056     * a domain.</td>
2057     * <tr>
2058     * <tr>
2059     * <td nowrap>CHARACTER SET</td>
2060     * <td nowrap>The object to which the privilege applies is a
2061     * character set.</td>
2062     * <tr>
2063     * <tr>
2064     * <td nowrap>COLLATION</td>
2065     * <td nowrap>The object to which the privilege applies is a
2066     * collation.</td>
2067     * <tr>
2068     * <tr>
2069     * <td nowrap>TRANSLATION</td>
2070     * <td nowrap>The object to which the privilege applies is a
2071     * transliteration.</td>
2072     * <tr>
2073     * <tr>
2074     * <td nowrap>SEQUENCE</td>
2075     * <td nowrap>The object to which the privilege applies is a
2076     * sequence generator.</td>
2077     * <tr>
2078     * </table> <p>
2079     *
2080     * <li> The values of IS_GRANTABLE have the following meanings: <p>
2081     *
2082     * <table border cellpadding="3">
2083     * <tr>
2084     * <td nowrap>YES</td>
2085     * <td nowrap>The privilege being described was granted
2086     * WITH GRANT OPTION and is thus grantable.</td>
2087     * <tr>
2088     * <tr>
2089     * <td nowrap>NO</td>
2090     * <td nowrap>The privilege being described was not granted
2091     * WITH GRANT OPTION and is thus not grantable.</td>
2092     * <tr>
2093     * </table> <p>
2094     * <ol>
2095     */

2096    Table SYSTEM_USAGE_PRIVILEGES() throws HsqlException {
2097
2098        Table t = sysTables[SYSTEM_USAGE_PRIVILEGES];
2099
2100        if (t == null) {
2101            t = createBlankTable(sysTableHsqlNames[SYSTEM_USAGE_PRIVILEGES]);
2102
2103            addColumn(t, "GRANTOR", Types.VARCHAR, false); // not null
2104
addColumn(t, "GRANTEE", Types.VARCHAR, false); // not null
2105
addColumn(t, "OBJECT_CATALOG", Types.VARCHAR);
2106            addColumn(t, "OBJECT_SCHEMA", Types.VARCHAR);
2107            addColumn(t, "OBJECT_NAME", Types.VARCHAR, false); // not null
2108
addColumn(t, "OBJECT_TYPE", Types.VARCHAR, 32, false); // not null
2109
addColumn(t, "IS_GRANTABLE", Types.VARCHAR, 3, false); // not null
2110

2111            // order: COLUMN_NAME, PRIVILEGE
2112
// for unique: GRANTEE, GRANTOR, TABLE_NAME, TABLE_SCHEM, TABLE_CAT
2113
// false PK, as TABLE_SCHEM and/or TABLE_CAT may be null
2114
t.createPrimaryKey(null, new int[] {
2115                0, 1, 2, 3, 4, 5
2116            }, false);
2117
2118            return t;
2119        }
2120
2121        Result rs;
2122
2123        rs = session.sqlExecuteDirectNoPreChecks(
2124            "SELECT '" + GranteeManager.SYSTEM_AUTHORIZATION_NAME
2125            + "', 'PUBLIC', SEQUENCE_CATALOG, SEQUENCE_SCHEMA, "
2126            + "SEQUENCE_NAME, 'SEQUENCE', 'FALSE' FROM INFORMATION_SCHEMA.SYSTEM_SEQUENCES");
2127
2128        t.insertSys(rs);
2129
2130        rs = session.sqlExecuteDirectNoPreChecks(
2131            "SELECT '" + GranteeManager.SYSTEM_AUTHORIZATION_NAME
2132            + "', 'PUBLIC', COLLATION_CATALOG, COLLATION_SCHEMA, "
2133            + "COLLATION_NAME, 'COLLATION', 'FALSE' FROM INFORMATION_SCHEMA.SYSTEM_COLLATIONS");
2134
2135        t.insertSys(rs);
2136        t.setDataReadOnly(true);
2137
2138        return t;
2139    }
2140
2141    /**
2142     * The CHECK_COLUMN_USAGE table has one row for each column identified by
2143     * a &lt;column reference&gt; contained in the &lt;search condition&gt;
2144     * of a check constraint, domain constraint, or assertion. <p>
2145     *
2146     * <b>Definition:</b><p>
2147     *
2148     * <pre class="SqlCodeExample">
2149     * CREATE TABLE CHECK_COLUMN_USAGE (
2150     * CONSTRAINT_CATALOG VARCHAR NULL,
2151     * CONSTRAINT_SCHEMA VARCHAR NULL,
2152     * CONSTRAINT_NAME VARCHAR NOT NULL,
2153     * TABLE_CATALOG VARCHAR NULL,
2154     * TABLE_SCHEMA VARCHAR NULL,
2155     * TABLE_NAME VARCHAR NOT NULL,
2156     * COLUMN_NAME VARCHAR NOT NULL,
2157     * UNIQUE( CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME,
2158     * TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME )
2159     * )
2160     * </pre>
2161     *
2162     * <b>Description:</b> <p>
2163     *
2164     * <ol>
2165     * <li> The values of CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, and
2166     * CONSTRAINT_NAME are the catalog name, unqualified schema name,
2167     * and qualified identifier, respectively, of the constraint being
2168     * described. <p>
2169     *
2170     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, and
2171     * COLUMN_NAME are the catalog name, unqualified schema name,
2172     * qualified identifier, and column name, respectively, of a column
2173     * identified by a &lt;column reference&gt; explicitly or implicitly
2174     * contained in the &lt;search condition&gt; of the constraint
2175     * being described.
2176     * </ol>
2177     */

2178    Table SYSTEM_CHECK_COLUMN_USAGE() throws HsqlException {
2179
2180        Table t = sysTables[SYSTEM_CHECK_COLUMN_USAGE];
2181
2182        if (t == null) {
2183            t = createBlankTable(
2184                sysTableHsqlNames[SYSTEM_CHECK_COLUMN_USAGE]);
2185
2186            addColumn(t, "CONSTRAINT_CATALOG", Types.VARCHAR);
2187            addColumn(t, "CONSTRAINT_SCHEMA", Types.VARCHAR);
2188            addColumn(t, "CONSTRAINT_NAME", Types.VARCHAR, false); // not null
2189
addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
2190            addColumn(t, "TABLE_SCHEMA", Types.VARCHAR);
2191            addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
2192
addColumn(t, "COLUMN_NAME", Types.VARCHAR, false); // not null
2193
t.createPrimaryKey(null, new int[] {
2194                0, 1, 2, 3, 4, 5, 6
2195            }, false);
2196
2197            return t;
2198        }
2199
2200        // calculated column values
2201
String JavaDoc constraintCatalog;
2202        String JavaDoc constraintSchema;
2203        String JavaDoc constraintName;
2204
2205        // Intermediate holders
2206
Iterator tables;
2207        Table table;
2208        Constraint[] constraints;
2209        int constraintCount;
2210        Constraint constraint;
2211        Expression.Collector collector;
2212        Expression expression;
2213        TableFilter tableFilter;
2214        Table columnTable;
2215        Iterator iterator;
2216        Result result;
2217        Object JavaDoc[] resultRow;
2218        Object JavaDoc[] row;
2219
2220        // column number mappings
2221
final int icons_cat = 0;
2222        final int icons_schem = 1;
2223        final int icons_name = 2;
2224        final int itab_cat = 3;
2225        final int itab_schem = 4;
2226        final int itab_name = 5;
2227        final int itab_col = 6;
2228
2229        // Initialization
2230
tables = database.schemaManager.allTablesIterator();
2231        collector = new Expression.Collector();
2232        result = new Result(ResultConstants.DATA, 4);
2233        result.metaData.colTypes[0] = result.metaData.colTypes[1] =
2234            result.metaData.colTypes[2] = result.metaData.colTypes[3] =
2235            Types.VARCHAR;
2236
2237        // Do it.
2238
while (tables.hasNext()) {
2239            table = (Table) tables.next();
2240
2241            if (!isAccessibleTable(table)) {
2242                continue;
2243            }
2244
2245            constraints = table.getConstraints();
2246            constraintCount = constraints.length;
2247            constraintCatalog = ns.getCatalogName(table);
2248            constraintSchema = table.getSchemaName();
2249
2250            // process constraints
2251
for (int i = 0; i < constraintCount; i++) {
2252                constraint = (Constraint) constraints[i];
2253
2254                if (constraint.getType() != Constraint.CHECK) {
2255                    continue;
2256                }
2257
2258                constraintName = constraint.getName().name;
2259
2260                result.setRows(null);
2261                collector.clear();
2262                collector.addAll(constraint.core.check, Expression.COLUMN);
2263
2264                iterator = collector.iterator();
2265
2266                // calculate distinct column references
2267
while (iterator.hasNext()) {
2268                    expression = (Expression) iterator.next();
2269                    tableFilter = expression.getFilter();
2270                    columnTable = tableFilter.getTable();
2271
2272                    if (columnTable.getTableType() == Table.SYSTEM_SUBQUERY
2273                            ||!isAccessibleTable(columnTable)) {
2274                        continue;
2275                    }
2276
2277                    result.add(new Object JavaDoc[] {
2278                        ns.getCatalogName(columnTable),
2279                        columnTable.getSchemaName(),
2280                        columnTable.getName().name, expression.getColumnName()
2281                    });
2282                }
2283
2284/*
2285                result.removeDuplicates(
2286                    database.sessionManager.getSysSession(
2287                        database.schemaManager.INFORMATION_SCHEMA));
2288*/

2289                result.removeDuplicates(session);
2290
2291                iterator = result.iterator();
2292
2293                while (iterator.hasNext()) {
2294                    row = t.getEmptyRowData();
2295                    resultRow = (Object JavaDoc[]) iterator.next();
2296                    row[icons_cat] = constraintCatalog;
2297                    row[icons_schem] = constraintSchema;
2298                    row[icons_name] = constraintName;
2299                    row[itab_cat] = resultRow[0];
2300                    row[itab_schem] = resultRow[1];
2301                    row[itab_name] = resultRow[2];
2302                    row[itab_col] = resultRow[3];
2303
2304                    t.insertSys(row);
2305                }
2306            }
2307        }
2308
2309        t.setDataReadOnly(true);
2310
2311        return t;
2312    }
2313
2314    /**
2315     * The CHECK_ROUTINE_USAGE base table has one row for each
2316     * SQL-invoked routine identified as the subject routine of either a
2317     * &lt;routine invocation&gt;, a &lt;method reference&gt;, a
2318     * &lt;method invocation&gt;, or a &lt;static method invocation&gt;
2319     * contained in an &lt;assertion definition&gt;, a &lt;domain
2320     * constraint&gt;, or a &lt;table constraint definition&gt;. <p>
2321     *
2322     * <b>Definition:</b> <p>
2323     *
2324     * <pre class="SqlCodeExample">
2325     * CREATE TABLE SYSTEM_CHECK_ROUTINE_USAGE (
2326     * CONSTRAINT_CATALOG VARCHAR NULL,
2327     * CONSTRAINT_SCHEMA VARCHAR NULL,
2328     * CONSTRAINT_NAME VARCHAR NOT NULL,
2329     * SPECIFIC_CATALOG VARCHAR NULL,
2330     * SPECIFIC_SCHEMA VARCHAR NULL,
2331     * SPECIFIC_NAME VARCHAR NOT NULL,
2332     * UNIQUE( CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME,
2333     * SPECIFIC_CATALOG, SPECIFIC_SCHEMA, SPECIFIC_NAME )
2334     * )
2335     * </pre>
2336     *
2337     * <b>Description:</b> <p>
2338     *
2339     * <ol>
2340     * <li> The CHECK_ROUTINE_USAGE table has one row for each
2341     * SQL-invoked routine R identified as the subject routine of either a
2342     * &lt;routine invocation&gt;, a &lt;method reference&gt;, a &lt;method
2343     * invocation&gt;, or a &lt;static method invocation&gt; contained in
2344     * an &lt;assertion definition&gt; or in the &lt;check constraint
2345     * definition&gt; contained in either a &lt;domain constraint&gt; or a
2346     * &lt;table constraint definition&gt;. <p>
2347     *
2348     * <li> The values of CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, and
2349     * CONSTRAINT_NAME are the catalog name, unqualified schema name, and
2350     * qualified identifier, respectively, of the assertion or check
2351     * constraint being described. <p>
2352     *
2353     * <li> The values of SPECIFIC_CATALOG, SPECIFIC_SCHEMA, and SPECIFIC_NAME
2354     * are the catalog name, unqualified schema name, and qualified
2355     * identifier, respectively, of the specific name of R. <p>
2356     *
2357     * </ol>
2358     */

2359    Table SYSTEM_CHECK_ROUTINE_USAGE() throws HsqlException {
2360
2361        Table t = sysTables[SYSTEM_CHECK_ROUTINE_USAGE];
2362
2363        if (t == null) {
2364            t = createBlankTable(
2365                sysTableHsqlNames[SYSTEM_CHECK_ROUTINE_USAGE]);
2366
2367            addColumn(t, "CONSTRAINT_CATALOG", Types.VARCHAR);
2368            addColumn(t, "CONSTRAINT_SCHEMA", Types.VARCHAR);
2369            addColumn(t, "CONSTRAINT_NAME", Types.VARCHAR, false); // not null
2370
addColumn(t, "SPECIFIC_CATALOG", Types.VARCHAR);
2371            addColumn(t, "SPECIFIC_SCHEMA", Types.VARCHAR);
2372            addColumn(t, "SPECIFIC_NAME", Types.VARCHAR, false); // not null
2373
t.createPrimaryKey(null, new int[] {
2374                0, 1, 2, 3, 4, 5
2375            }, false);
2376
2377            return t;
2378        }
2379
2380        // calculated column values
2381
String JavaDoc constraintCatalog;
2382        String JavaDoc constraintSchema;
2383        String JavaDoc constraintName;
2384
2385        // Intermediate holders
2386
Iterator tables;
2387        Table table;
2388        Constraint[] constraints;
2389        int constraintCount;
2390        Constraint constraint;
2391        Expression.Collector collector;
2392        Expression expression;
2393        Function function;
2394        Iterator iterator;
2395        HashSet methodSet;
2396        Method JavaDoc method;
2397        Object JavaDoc[] row;
2398
2399        // column number mappings
2400
final int icons_cat = 0;
2401        final int icons_schem = 1;
2402        final int icons_name = 2;
2403        final int ir_cat = 3;
2404        final int ir_schem = 4;
2405        final int ir_name = 5;
2406
2407        tables = database.schemaManager.allTablesIterator();
2408        collector = new Expression.Collector();
2409
2410        while (tables.hasNext()) {
2411            collector.clear();
2412
2413            table = (Table) tables.next();
2414
2415            if (!isAccessibleTable(table)) {
2416                continue;
2417            }
2418
2419            constraints = table.getConstraints();
2420            constraintCount = constraints.length;
2421            constraintCatalog = ns.getCatalogName(table);
2422            constraintSchema = table.getSchemaName();
2423
2424            for (int i = 0; i < constraintCount; i++) {
2425                constraint = (Constraint) constraints[i];
2426
2427                if (constraint.getType() != Constraint.CHECK) {
2428                    continue;
2429                }
2430
2431                constraintName = constraint.getName().name;
2432
2433                collector.addAll(constraint.core.check, Expression.FUNCTION);
2434
2435                methodSet = new HashSet();
2436                iterator = collector.iterator();
2437
2438                while (iterator.hasNext()) {
2439                    expression = (Expression) iterator.next();
2440                    function = expression.function;
2441
2442                    if (!session
2443                            .isAccessible(function.getMethod()
2444                                .getDeclaringClass().getName())) {
2445                        continue;
2446                    }
2447
2448                    methodSet.add(function.getMethod());
2449                }
2450
2451                iterator = methodSet.iterator();
2452
2453                while (iterator.hasNext()) {
2454                    method = (Method JavaDoc) iterator.next();
2455                    row = t.getEmptyRowData();
2456                    row[icons_cat] = constraintCatalog;
2457                    row[icons_schem] = constraintSchema;
2458                    row[icons_name] = constraintName;
2459                    row[ir_cat] = ns.getCatalogName(method);
2460                    row[ir_schem] = ns.getSchemaName(method);
2461                    row[ir_name] = DINameSpace.getMethodSpecificName(method);
2462
2463                    t.insertSys(row);
2464                }
2465            }
2466        }
2467
2468        t.setDataReadOnly(true);
2469
2470        return t;
2471    }
2472
2473    /**
2474     * The CHECK_TABLE_USAGE table has one row for each table identified by a
2475     * &lt;table name&gt; simply contained in a &lt;table reference&gt;
2476     * contained in the &lt;search condition&gt; of a check constraint,
2477     * domain constraint, or assertion. <p>
2478     *
2479     * <b>Definition:</b> <p>
2480     *
2481     * <pre class="SqlCodeExample">
2482     * CREATE STABLE SYSTEM_CHECK_TABLE_USAGE (
2483     * CONSTRAINT_CATALOG VARCHAR NULL,
2484     * CONSTRAINT_SCHEMA VARCHAR NULL,
2485     * CONSTRAINT_NAME VARCHAR NOT NULL,
2486     * TABLE_CATALOG VARCHAR NULL,
2487     * TABLE_SCHEMA VARCHAR NOT NULL,
2488     * TABLE_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
2489     * UNIQUE( CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, CONSTRAINT_NAME,
2490     * TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME )
2491     * )
2492     * </pre>
2493     *
2494     * <b>Description:</b> <p>
2495     *
2496     * <ol>
2497     * <li> The values of CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, and
2498     * CONSTRAINT_NAME are the catalog name, unqualified schema name,
2499     * and qualified identifier, respectively, of the constraint being
2500     * described. <p>
2501     *
2502     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME are the
2503     * catalog name, unqualified schema name, and qualified identifier,
2504     * respectively, of a table identified by a &lt;table name&gt;
2505     * simply contained in a &lt;table reference&gt; contained in the
2506     * *lt;search condition&gt; of the constraint being described.
2507     * </ol>
2508     */

2509    Table SYSTEM_CHECK_TABLE_USAGE() throws HsqlException {
2510
2511        Table t = sysTables[SYSTEM_CHECK_TABLE_USAGE];
2512
2513        if (t == null) {
2514            t = createBlankTable(sysTableHsqlNames[SYSTEM_CHECK_TABLE_USAGE]);
2515
2516            addColumn(t, "CONSTRAINT_CATALOG", Types.VARCHAR);
2517            addColumn(t, "CONSTRAINT_SCHEMA", Types.VARCHAR);
2518            addColumn(t, "CONSTRAINT_NAME", Types.VARCHAR, false); // not null
2519
addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
2520            addColumn(t, "TABLE_SCHEMA", Types.VARCHAR);
2521            addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
2522
t.createPrimaryKey(null, new int[] {
2523                0, 1, 2, 3, 4, 5
2524            }, false);
2525
2526            return t;
2527        }
2528
2529        //
2530
Result rs = session.sqlExecuteDirectNoPreChecks(
2531            "select DISTINCT CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, "
2532            + "CONSTRAINT_NAME, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME "
2533            + "from INFORMATION_SCHEMA.SYSTEM_CHECK_COLUMN_USAGE");
2534
2535        t.insertSys(rs);
2536        t.setDataReadOnly(true);
2537
2538        return t;
2539    }
2540
2541    /**
2542     * The TABLE_CONSTRAINTS table has one row for each table constraint
2543     * associated with a table. <p>
2544     *
2545     * It effectively contains a representation of the table constraint
2546     * descriptors. <p>
2547     *
2548     * <b>Definition:</b> <p>
2549     *
2550     * <pre class="SqlCodeExample">
2551     * CREATE TABLE SYSTEM_TABLE_CONSTRAINTS (
2552     * CONSTRAINT_CATALOG VARCHAR NULL,
2553     * CONSTRAINT_SCHEMA VARCHAR NULL,
2554     * CONSTRAINT_NAME VARCHAR NOT NULL,
2555     * CONSTRAINT_TYPE VARCHAR NOT NULL,
2556     * TABLE_CATALOG VARCHAR NULL,
2557     * TABLE_SCHEMA VARCHAR NULL,
2558     * TABLE_NAME VARCHAR NOT NULL,
2559     * IS_DEFERRABLE VARCHAR NOT NULL,
2560     * INITIALLY_DEFERRED VARCHAR NOT NULL,
2561     *
2562     * CHECK ( CONSTRAINT_TYPE IN
2563     * ( 'UNIQUE', 'PRIMARY KEY',
2564     * 'FOREIGN KEY', 'CHECK' ) ),
2565     *
2566     * CHECK ( ( IS_DEFERRABLE, INITIALLY_DEFERRED ) IN
2567     * ( VALUES ( 'NO', 'NO' ),
2568     * ( 'YES', 'NO' ),
2569     * ( 'YES', 'YES' ) ) )
2570     * )
2571     * </pre>
2572     *
2573     * <b>Description:</b> <p>
2574     *
2575     * <ol>
2576     * <li> The values of CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA, and
2577     * CONSTRAINT_NAME are the catalog name, unqualified schema
2578     * name, and qualified identifier, respectively, of the
2579     * constraint being described. If the &lt;table constraint
2580     * definition&gt; or &lt;add table constraint definition&gt;
2581     * that defined the constraint did not specify a
2582     * &lt;constraint name&gt;, then the values of CONSTRAINT_CATALOG,
2583     * CONSTRAINT_SCHEMA, and CONSTRAINT_NAME are
2584     * implementation-defined. <p>
2585     *
2586     * <li> The values of CONSTRAINT_TYPE have the following meanings: <p>
2587     * <table border cellpadding="3">
2588     * <tr>
2589     * <td nowrap>FOREIGN KEY</td>
2590     * <td nowrap>The constraint being described is a
2591     * foreign key constraint.</td>
2592     * </tr>
2593     * <tr>
2594     * <td nowrap>UNIQUE</td>
2595     * <td nowrap>The constraint being described is a
2596     * unique constraint.</td>
2597     * </tr>
2598     * <tr>
2599     * <td nowrap>PRIMARY KEY</td>
2600     * <td nowrap>The constraint being described is a
2601     * primary key constraint.</td>
2602     * </tr>
2603     * <tr>
2604     * <td nowrap>CHECK</td>
2605     * <td nowrap>The constraint being described is a
2606     * check constraint.</td>
2607     * </tr>
2608     * </table> <p>
2609     *
2610     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME are
2611     * the catalog name, the unqualified schema name, and the
2612     * qualified identifier of the name of the table to which the
2613     * table constraint being described applies. <p>
2614     *
2615     * <li> The values of IS_DEFERRABLE have the following meanings: <p>
2616     *
2617     * <table>
2618     * <tr>
2619     * <td nowrap>YES</td>
2620     * <td nowrap>The table constraint is deferrable.</td>
2621     * </tr>
2622     * <tr>
2623     * <td nowrap>NO</td>
2624     * <td nowrap>The table constraint is not deferrable.</td>
2625     * </tr>
2626     * </table> <p>
2627     *
2628     * <li> The values of INITIALLY_DEFERRED have the following meanings: <p>
2629     *
2630     * <table>
2631     * <tr>
2632     * <td nowrap>YES</td>
2633     * <td nowrap>The table constraint is initially deferred.</td>
2634     * </tr>
2635     * <tr>
2636     * <td nowrap>NO</td>
2637     * <td nowrap>The table constraint is initially immediate.</td>
2638     * </tr>
2639     * </table> <p>
2640     * </ol>
2641     */

2642    Table SYSTEM_TABLE_CONSTRAINTS() throws HsqlException {
2643
2644        Table t = sysTables[SYSTEM_TABLE_CONSTRAINTS];
2645
2646        if (t == null) {
2647            t = createBlankTable(sysTableHsqlNames[SYSTEM_TABLE_CONSTRAINTS]);
2648
2649            addColumn(t, "CONSTRAINT_CATALOG", Types.VARCHAR);
2650            addColumn(t, "CONSTRAINT_SCHEMA", Types.VARCHAR);
2651            addColumn(t, "CONSTRAINT_NAME", Types.VARCHAR, false); // not null
2652
addColumn(t, "CONSTRAINT_TYPE", Types.VARCHAR, 11, false); // not null
2653
addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
2654            addColumn(t, "TABLE_SCHEMA", Types.VARCHAR);
2655            addColumn(t, "TABLE_NAME", Types.VARCHAR, false); // not null
2656
addColumn(t, "IS_DEFERRABLE", Types.VARCHAR, 3, false); // not null
2657
addColumn(t, "INITIALLY_DEFERRED", Types.VARCHAR, 3, false); // not null
2658

2659            // false PK, as CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA,
2660
// TABLE_CATALOG and/or TABLE_SCHEMA may be null
2661
t.createPrimaryKey(null, new int[] {
2662                0, 1, 2, 4, 5, 6
2663            }, false);
2664
2665            return t;
2666        }
2667
2668        // Intermediate holders
2669
Iterator tables;
2670        Table table;
2671        Index index;
2672        Constraint[] constraints;
2673        int constraintCount;
2674        Constraint constraint;
2675        String JavaDoc cat;
2676        String JavaDoc schem;
2677        HashSet constraintSet;
2678        Object JavaDoc[] row;
2679
2680        // column number mappings
2681
final int icons_cat = 0;
2682        final int icons_schem = 1;
2683        final int icons_name = 2;
2684        final int icons_type = 3;
2685        final int itab_cat = 4;
2686        final int itab_schem = 5;
2687        final int itab_name = 6;
2688        final int iis_defr = 7;
2689        final int iinit_defr = 8;
2690
2691        // initialization
2692
tables = database.schemaManager.allTablesIterator();
2693        constraintSet = new HashSet();
2694        table = null; // else complier complains
2695

2696        // do it
2697
while (tables.hasNext()) {
2698            table = (Table) tables.next();
2699
2700            if (table.isView() ||!isAccessibleTable(table)) {
2701                continue;
2702            }
2703
2704            index = table.getPrimaryIndex();
2705
2706            if (table.hasPrimaryKey()) {
2707                row = t.getEmptyRowData();
2708                cat = ns.getCatalogName(table);
2709                schem = table.getSchemaName();
2710                row[icons_cat] = cat;
2711                row[icons_schem] = schem;
2712                row[icons_name] = index.getName().name;
2713                row[icons_type] = "PRIMARY KEY";
2714                row[itab_cat] = cat;
2715                row[itab_schem] = schem;
2716                row[itab_name] = table.getName().name;
2717                row[iis_defr] = "NO";
2718                row[iinit_defr] = "NO";
2719
2720                t.insertSys(row);
2721            }
2722
2723            constraints = table.getConstraints();
2724            constraintCount = constraints.length;
2725
2726            for (int i = 0; i < constraintCount; i++) {
2727                constraint = constraints[i];
2728
2729                if (constraint.getType() == Constraint.FOREIGN_KEY
2730                        &&!isAccessibleTable(constraint.getRef())) {
2731                    continue;
2732                }
2733
2734                constraintSet.add(constraint);
2735            }
2736        }
2737
2738        for (Iterator it = constraintSet.iterator(); it.hasNext(); ) {
2739            row = t.getEmptyRowData();
2740            constraint = (Constraint) it.next();
2741
2742            switch (constraint.getType()) {
2743
2744                case Constraint.CHECK : {
2745                    row[icons_type] = "CHECK";
2746                    table = constraint.getMain();
2747
2748                    break;
2749                }
2750                case Constraint.UNIQUE : {
2751                    row[icons_type] = "UNIQUE";
2752                    table = constraint.getMain();
2753
2754                    break;
2755                }
2756                case Constraint.FOREIGN_KEY : {
2757                    row[icons_type] = "FOREIGN KEY";
2758                    table = constraint.getRef();
2759
2760                    break;
2761                }
2762                case Constraint.MAIN :
2763                default : {
2764                    continue;
2765                }
2766            }
2767
2768            cat = ns.getCatalogName(table);
2769            schem = table.getSchemaName();
2770            row[icons_cat] = cat;
2771            row[icons_schem] = schem;
2772            row[icons_name] = constraint.constName.name;
2773            row[itab_cat] = cat;
2774            row[itab_schem] = schem;
2775            row[itab_name] = table.getName().name;
2776            row[iis_defr] = "NO";
2777            row[iinit_defr] = "NO";
2778
2779            t.insertSys(row);
2780        }
2781
2782        t.setDataReadOnly(true);
2783
2784        return t;
2785    }
2786
2787    /**
2788     * The SYSTEM_VIEW_TABLE_USAGE table has one row for each table identified
2789     * by a &lt;table name&gt; simply contained in a &lt;table reference&gt;
2790     * that is contained in the &lt;query expression&gt; of a view. <p>
2791     *
2792     * <b>Definition</b><p>
2793     *
2794     * <pre class="SqlCodeExample">
2795     * CREATE TABLE SYSTEM_VIEW_TABLE_USAGE (
2796     * VIEW_CATALOG VARCHAR NULL,
2797     * VIEW_SCHEMA VARCHAR NULL,
2798     * VIEW_NAME VARCHAR NULL,
2799     * TABLE_CATALOG VARCHAR NULL,
2800     * TABLE_SCHEMA VARCHAR NULL,
2801     * TABLE_NAME VARCHAR NULL,
2802     * UNIQUE( VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME,
2803     * TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME )
2804     * )
2805     * </pre>
2806     *
2807     * <b>Description:</b><p>
2808     *
2809     * <ol>
2810     * <li> The values of VIEW_CATALOG, VIEW_SCHEMA, and VIEW_NAME are the
2811     * catalog name, unqualified schema name, and qualified identifier,
2812     * respectively, of the view being described. <p>
2813     *
2814     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME are the
2815     * catalog name, unqualified schema name, and qualified identifier,
2816     * respectively, of a table identified by a &lt;table name&gt;
2817     * simply contained in a &lt;table reference&gt; that is contained in
2818     * the &lt;query expression&gt; of the view being described.
2819     * </ol>
2820     */

2821    Table SYSTEM_VIEW_TABLE_USAGE() throws HsqlException {
2822
2823        Table t = sysTables[SYSTEM_VIEW_TABLE_USAGE];
2824
2825        if (t == null) {
2826            t = createBlankTable(sysTableHsqlNames[SYSTEM_VIEW_TABLE_USAGE]);
2827
2828            addColumn(t, "VIEW_CATALOG", Types.VARCHAR);
2829            addColumn(t, "VIEW_SCHEMA", Types.VARCHAR);
2830            addColumn(t, "VIEW_NAME", Types.VARCHAR, true); // not null
2831
addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
2832            addColumn(t, "TABLE_SCHEMA", Types.VARCHAR);
2833            addColumn(t, "TABLE_NAME", Types.VARCHAR, true); // not null
2834

2835            // false PK, as VIEW_CATALOG, VIEW_SCHEMA, TABLE_CATALOG, and/or
2836
// TABLE_SCHEMA may be NULL
2837
t.createPrimaryKey(null, new int[] {
2838                0, 1, 2, 3, 4, 5
2839            }, false);
2840
2841            return t;
2842        }
2843
2844        //
2845
Result rs = session.sqlExecuteDirectNoPreChecks(
2846            "select DISTINCT VIEW_CATALOG, VIEW_SCHEMA, "
2847            + "VIEW_NAME, TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME "
2848            + "from INFORMATION_SCHEMA.SYSTEM_VIEW_COLUMN_USAGE");
2849
2850        t.insertSys(rs);
2851        t.setDataReadOnly(true);
2852
2853        return t;
2854    }
2855
2856    /**
2857     * The SYSTEM_VIEW_COLUMN_USAGE table has one row for each column of a
2858     * table that is explicitly or implicitly referenced in the
2859     * &lt;query expression&gt; of the view being described. <p>
2860     *
2861     * <b>Definition:</b> <p>
2862     *
2863     * <pre class="SqlCodeExample">
2864     * CREATE TABLE SYSTEM_VIEW_COLUMN_USAGE (
2865     * VIEW_CATALOG VARCHAR NULL,
2866     * VIEW_SCHEMA VARCHAR NULL,
2867     * VIEW_NAME VARCHAR NOT NULL,
2868     * TABLE_CATALOG VARCHAR NULL,
2869     * TABLE_SCHEMA VARCHAR NULL,
2870     * TABLE_NAME VARCHAR NOT NULL,
2871     * COLUMN_NAME VARCHAR NOT NULL,
2872     * UNIQUE ( VIEW_CATALOG, VIEW_SCHEMA, VIEW_NAME,
2873     * TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME,
2874     * COLUMN_NAME )
2875     * )
2876     * </pre>
2877     *
2878     * <b>Description:</b> <p>
2879     *
2880     * <ol>
2881     * <li> The values of VIEW_CATALOG, VIEW_SCHEMA, and VIEW_NAME are the
2882     * catalog name, unqualified schema name, and qualified identifier,
2883     * respectively, of the view being described. <p>
2884     *
2885     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, and
2886     * COLUMN_NAME are the catalog name, unqualified schema name,
2887     * qualified identifier, and column name, respectively, of a column
2888     * of a table that is explicitly or implicitly referenced in the
2889     * &lt;query expression&gt; of the view being described.
2890     * </ol>
2891     */

2892    Table SYSTEM_VIEW_COLUMN_USAGE() throws HsqlException {
2893
2894        Table t = sysTables[SYSTEM_VIEW_COLUMN_USAGE];
2895
2896        if (t == null) {
2897            t = createBlankTable(sysTableHsqlNames[SYSTEM_VIEW_COLUMN_USAGE]);
2898
2899            addColumn(t, "VIEW_CATALOG", Types.VARCHAR);
2900            addColumn(t, "VIEW_SCHEMA", Types.VARCHAR);
2901            addColumn(t, "VIEW_NAME", Types.VARCHAR, true); // not null
2902
addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
2903            addColumn(t, "TABLE_SCHEMA", Types.VARCHAR);
2904            addColumn(t, "TABLE_NAME", Types.VARCHAR, true); // not null
2905
addColumn(t, "COLUMN_NAME", Types.VARCHAR, true); // not null
2906

2907            // false PK, as VIEW_CATALOG, VIEW_SCHEMA, TABLE_CATALOG, and/or
2908
// TABLE_SCHEMA may be NULL
2909
t.createPrimaryKey(null, new int[] {
2910                0, 1, 2, 3, 4, 5, 6
2911            }, false);
2912
2913            return t;
2914        }
2915
2916        // Calculated column values
2917
String JavaDoc viewCatalog;
2918        String JavaDoc viewSchema;
2919        String JavaDoc viewName;
2920
2921        // Intermediate holders
2922
Iterator tables;
2923        View view;
2924        Table table;
2925        Object JavaDoc[] row;
2926        SubQuery[] subqueries;
2927        Select select;
2928        Expression expression;
2929        TableFilter tableFilter;
2930        Table columnTable;
2931        Result result;
2932        Object JavaDoc[] resultRow;
2933        Iterator iterator;
2934        Expression.Collector collector;
2935
2936        // Column number mappings
2937
final int iv_cat = 0;
2938        final int iv_schem = 1;
2939        final int iv_name = 2;
2940        final int it_cat = 3;
2941        final int it_schem = 4;
2942        final int it_name = 5;
2943        final int it_cname = 6;
2944
2945        // Initialization
2946
tables = database.schemaManager.allTablesIterator();
2947        collector = new Expression.Collector();
2948        result = new Result(ResultConstants.DATA, 4);
2949        result.metaData.colTypes[0] = result.metaData.colTypes[1] =
2950            result.metaData.colTypes[2] = result.metaData.colTypes[3] =
2951            Types.VARCHAR;
2952
2953        // Do it.
2954
while (tables.hasNext()) {
2955            collector.clear();
2956            result.setRows(null);
2957
2958            table = (Table) tables.next();
2959
2960            if (table.isView() && isAccessibleTable(table)) {
2961
2962                // fall through
2963
} else {
2964                continue;
2965            }
2966
2967            viewCatalog = ns.getCatalogName(table);
2968            viewSchema = table.getSchemaName();
2969            viewName = table.getName().name;
2970            view = (View) table;
2971            subqueries = view.viewSubqueries;
2972
2973            collector.addAll(view.viewSelect, Expression.COLUMN);
2974
2975            for (int i = 0; i < subqueries.length; i++) {
2976                collector.addAll(subqueries[i].select, Expression.COLUMN);
2977            }
2978
2979            iterator = collector.iterator();
2980
2981            while (iterator.hasNext()) {
2982                expression = (Expression) iterator.next();
2983                tableFilter = expression.getFilter();
2984                columnTable = tableFilter.getTable();
2985
2986                if (columnTable.getTableType() == Table.SYSTEM_SUBQUERY
2987                        ||!isAccessibleTable(columnTable)) {
2988                    continue;
2989                }
2990
2991                result.add(new Object JavaDoc[] {
2992                    ns.getCatalogName(columnTable),
2993                    columnTable.getSchemaName(), columnTable.getName().name,
2994                    expression.getColumnName()
2995                });
2996            }
2997
2998/*
2999            result.removeDuplicates(
3000                database.sessionManager.getSysSession(
3001                    database.schemaManager.INFORMATION_SCHEMA));
3002*/

3003            result.removeDuplicates(session);
3004
3005            iterator = result.iterator();
3006
3007            while (iterator.hasNext()) {
3008                row = t.getEmptyRowData();
3009                resultRow = (Object JavaDoc[]) iterator.next();
3010                row[iv_cat] = viewCatalog;
3011                row[iv_schem] = viewSchema;
3012                row[iv_name] = viewName;
3013                row[it_cat] = resultRow[0];
3014                row[it_schem] = resultRow[1];
3015                row[it_name] = resultRow[2];
3016                row[it_cname] = resultRow[3];
3017
3018                t.insertSys(row);
3019            }
3020        }
3021
3022        t.setDataReadOnly(true);
3023
3024        return t;
3025    }
3026
3027    /**
3028     * The SYSTEM_VIEW_ROUTINE_USAGE table has one row for each SQL-invoked
3029     * routine identified as the subject routine of either a &lt;routine
3030     * invocation&gt;, a &lt;method reference&gt;, a &lt;method invocation&gt;,
3031     * or a &lt;static method invocation&gt; contained in a &lt;view
3032     * definition&gt;. <p>
3033     *
3034     * <b>Definition</b><p>
3035     *
3036     * <pre class="SqlCodeExample">
3037     * CREATE TABLE VIEW_ROUTINE_USAGE (
3038     * TABLE_CATALOG VARCHAR NULL,
3039     * TABLE_SCHEMA VARCHAR NULL,
3040     * TABLE_NAME VARCHAR NOT NULL,
3041     * SPECIFIC_CATALOG VARCHAR NULL,
3042     * SPECIFIC_SCHEMA VARCHAR NULL,
3043     * SPECIFIC_NAME VARCHAR NOT NULL,
3044     * UNIQUE( TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME,
3045     * SPECIFIC_CATALOG, SPECIFIC_SCHEMA,
3046     * SPECIFIC_NAME )
3047     * )
3048     * </pre>
3049     *
3050     * <b>Description</b><p>
3051     *
3052     * <ol>
3053     * <li> The values of TABLE_CATALOG, TABLE_SCHEMA, and TABLE_NAME are the
3054     * catalog name, unqualified schema name, and qualified identifier,
3055     * respectively, of the viewed table being described. <p>
3056     *
3057     * <li> The values of SPECIFIC_CATALOG, SPECIFIC_SCHEMA, and SPECIFIC_NAME are
3058     * the catalog name, unqualified schema name, and qualified identifier,
3059     * respectively, of the specific name of R. <p>
3060     * </ol>
3061     */

3062    Table SYSTEM_VIEW_ROUTINE_USAGE() throws HsqlException {
3063
3064        Table t = sysTables[SYSTEM_VIEW_ROUTINE_USAGE];
3065
3066        if (t == null) {
3067            t = createBlankTable(
3068                sysTableHsqlNames[SYSTEM_VIEW_ROUTINE_USAGE]);
3069
3070            addColumn(t, "TABLE_CATALOG", Types.VARCHAR);
3071            addColumn(t, "TABLE_SCHEMA", Types.VARCHAR);
3072            addColumn(t, "TABLE_NAME", Types.VARCHAR, true); // not null
3073
addColumn(t, "SPECIFIC_CATALOG", Types.VARCHAR);
3074            addColumn(t, "SPECIFIC_SCHEMA", Types.VARCHAR);
3075            addColumn(t, "SPECIFIC_NAME", Types.VARCHAR, true); // not null
3076

3077            // false PK, as VIEW_CATALOG, VIEW_SCHEMA, TABLE_CATALOG, and/or
3078
// TABLE_SCHEMA may be NULL
3079
t.createPrimaryKey(null, new int[] {
3080                0, 1, 2, 3, 4, 5
3081            }, false);
3082
3083            return t;
3084        }
3085
3086        // Calculated column values
3087
String JavaDoc viewCat;
3088        String JavaDoc viewSchem;
3089        String JavaDoc viewName;
3090
3091        // Intermediate holders
3092
Iterator tables;
3093        View view;
3094        Table table;
3095        Object JavaDoc[] row;
3096        SubQuery[] subqueries;
3097        Select select;
3098        Expression expression;
3099        Function function;
3100        Expression.Collector collector;
3101        Method JavaDoc method;
3102        HashSet methodSet;
3103        Iterator iterator;
3104
3105        // Column number mappings
3106
final int iv_cat = 0;
3107        final int iv_schem = 1;
3108        final int iv_name = 2;
3109        final int ir_cat = 3;
3110        final int ir_schem = 4;
3111        final int ir_name = 5;
3112        final int ir_sig = 6;
3113
3114        // Initialization
3115
tables = database.schemaManager.allTablesIterator();
3116        collector = new Expression.Collector();
3117
3118        // Do it.
3119
while (tables.hasNext()) {
3120            collector.clear();
3121
3122            table = (Table) tables.next();
3123
3124            if (table.isView() && isAccessibleTable(table)) {
3125
3126                // fall through
3127
} else {
3128                continue;
3129            }
3130
3131            viewCat = ns.getCatalogName(table);
3132            viewSchem = table.getSchemaName();
3133            viewName = table.getName().name;
3134            view = (View) table;
3135            subqueries = view.viewSubqueries;
3136
3137            collector.addAll(view.viewSelect, Expression.FUNCTION);
3138
3139            for (int i = 0; i < subqueries.length; i++) {
3140                collector.addAll(subqueries[i].select, Expression.FUNCTION);
3141            }
3142
3143            methodSet = new HashSet();
3144            iterator = collector.iterator();
3145
3146            while (iterator.hasNext()) {
3147                expression = (Expression) iterator.next();
3148                function = expression.function;
3149
3150                if (session.isAccessible(
3151                        function.getMethod().getDeclaringClass().getName())) {
3152                    methodSet.add(function.getMethod());
3153                }
3154            }
3155
3156            iterator = methodSet.iterator();
3157
3158            while (iterator.hasNext()) {
3159                method = (Method JavaDoc) iterator.next();
3160                row = t.getEmptyRowData();
3161                row[iv_cat] = viewCat;
3162                row[iv_schem] = viewSchem;
3163                row[iv_name] = viewName;
3164                row[ir_cat] = ns.getCatalogName(method);
3165                row[ir_schem] = ns.getSchemaName(method);
3166                row[ir_name] = DINameSpace.getMethodSpecificName(method);
3167
3168                t.insertSys(row);
3169            }
3170        }
3171
3172        t.setDataReadOnly(true);
3173
3174        return t;
3175    }
3176
3177    /**
3178     * Inserts a set of procedure column description rows into the
3179     * <code>Table</code> specified by the <code>t</code> argument. <p>
3180     *
3181     * @param t the table in which the rows are to be inserted
3182     * @param l the list of procedure name aliases to which the
3183     * specified column values apply
3184     * @param cat the procedure's catalog name
3185     * @param schem the procedure's schema name
3186     * @param pName the procedure's simple base (non-alias) name
3187     * @param cName the procedure column name
3188     * @param cType the column type (return, parameter, result)
3189     * @param dType the column's data type code
3190     * @param tName the column's canonical data type name
3191     * @param prec the column's precision
3192     * @param len the column's buffer length
3193     * @param scale the column's scale (decimal digits)
3194     * @param radix the column's numeric precision radix
3195     * @param nullability the column's java.sql.DatbaseMetaData
3196     * nullabiliy code
3197     * @param remark a human-readable remark regarding the column
3198     * @param specificName the specific name of the procedure
3199     * (typically but not limited to
3200     * a fully qualified Java Method name and signature)
3201     * @param seq helper value to back JDBC contract sort order
3202     * @throws HsqlException if there is problem inserting the specified rows
3203     * in the table
3204     *
3205     */

3206    protected void addPColRows(Table t, HsqlArrayList l, String JavaDoc cat,
3207                               String JavaDoc schem, String JavaDoc pName, String JavaDoc cName,
3208                               Integer JavaDoc cType, Integer JavaDoc dType, String JavaDoc tName,
3209                               Integer JavaDoc prec, Integer JavaDoc len, Integer JavaDoc scale,
3210                               Integer JavaDoc radix, Integer JavaDoc nullability,
3211                               String JavaDoc remark, String JavaDoc specificName,
3212                               int seq) throws HsqlException {
3213
3214        // column number mappings
3215
final int icat = 0;
3216        final int ischem = 1;
3217        final int iname = 2;
3218        final int icol_name = 3;
3219        final int icol_type = 4;
3220        final int idata_type = 5;
3221        final int itype_name = 6;
3222        final int iprec = 7;
3223        final int ilength = 8;
3224        final int iscale = 9;
3225        final int iradix = 10;
3226        final int inullable = 11;
3227        final int iremark = 12;
3228        final int isn = 13;
3229        final int iseq = 14;
3230        Object JavaDoc[] row = t.getEmptyRowData();
3231        Integer JavaDoc sequence = ValuePool.getInt(seq);
3232
3233        row[icat] = cat;
3234        row[ischem] = schem;
3235        row[iname] = pName;
3236        row[icol_name] = cName;
3237        row[icol_type] = cType;
3238        row[idata_type] = dType;
3239        row[itype_name] = tName;
3240        row[iprec] = prec;
3241        row[ilength] = len;
3242        row[iscale] = scale;
3243        row[iradix] = radix;
3244        row[inullable] = nullability;
3245        row[iremark] = remark;
3246        row[isn] = specificName;
3247        row[iseq] = sequence;
3248
3249        t.insertSys(row);
3250
3251        if (l != null) {
3252            int size = l.size();
3253
3254            for (int i = 0; i < size; i++) {
3255                row = t.getEmptyRowData();
3256                pName = (String JavaDoc) l.get(i);
3257                row[icat] = cat;
3258                row[ischem] = schem;
3259                row[iname] = pName;
3260                row[icol_name] = cName;
3261                row[icol_type] = cType;
3262                row[idata_type] = dType;
3263                row[itype_name] = tName;
3264                row[iprec] = prec;
3265                row[ilength] = len;
3266                row[iscale] = scale;
3267                row[iradix] = radix;
3268                row[inullable] = nullability;
3269                row[iremark] = remark;
3270                row[isn] = specificName;
3271                row[iseq] = sequence;
3272
3273                t.insertSys(row);
3274            }
3275        }
3276    }
3277
3278    /**
3279     * Inserts a set of procedure description rows into the <code>Table</code>
3280     * object specified by the <code>t</code> argument. <p>
3281     *
3282     * @param t the table into which the specified rows will eventually
3283     * be inserted
3284     * @param l the list of procedure name aliases to which the specified column
3285     * values apply
3286     * @param cat the procedure catalog name
3287     * @param schem the procedure schema name
3288     * @param pName the base (non-alias) procedure name
3289     * @param ip the procedure input parameter count
3290     * @param op the procedure output parameter count
3291     * @param rs the procedure result column count
3292     * @param remark a human-readable remark regarding the procedure
3293     * @param pType the procedure type code, indicating whether it is a
3294     * function, procedure, or uncatagorized (i.e. returns
3295     * a value, does not return a value, or it is unknown
3296     * if it returns a value)
3297     * @param origin origin of the procedure, e.g.
3298     * (["BUILTIN" | "USER DEFINED"] "ROUTINE" | "TRIGGER") | "ALIAS", etc.
3299     * @param specificName the specific name of the procedure
3300     * (typically but not limited to a
3301     * fully qualified Java Method name and signature)
3302     * @throws HsqlException if there is problem inserting the specified rows
3303     * in the table
3304     *
3305     */

3306    protected void addProcRows(Table t, HsqlArrayList l, String JavaDoc cat,
3307                               String JavaDoc schem, String JavaDoc pName, Integer JavaDoc ip,
3308                               Integer JavaDoc op, Integer JavaDoc rs, String JavaDoc remark,
3309                               Integer JavaDoc pType, String JavaDoc origin,
3310                               String JavaDoc specificName) throws HsqlException {
3311
3312        // column number mappings
3313
final int icat = 0;
3314        final int ischem = 1;
3315        final int ipname = 2;
3316        final int iinput_parms = 3;
3317        final int ioutput_parms = 4;
3318        final int iresult_sets = 5;
3319        final int iremark = 6;
3320        final int iptype = 7;
3321        final int iporigin = 8;
3322        final int isn = 9;
3323        Object JavaDoc[] row = t.getEmptyRowData();
3324
3325        row[icat] = cat;
3326        row[ischem] = schem;
3327        row[ipname] = pName;
3328        row[iinput_parms] = ip;
3329        row[ioutput_parms] = op;
3330        row[iresult_sets] = rs;
3331        row[iremark] = remark;
3332        row[iptype] = pType;
3333        row[iporigin] = origin;
3334        row[isn] = specificName;
3335
3336        t.insertSys(row);
3337
3338        if (l != null) {
3339            int size = l.size();
3340
3341            for (int i = 0; i < size; i++) {
3342                row = t.getEmptyRowData();
3343                pName = (String JavaDoc) l.get(i);
3344                row[icat] = cat;
3345                row[ischem] = schem;
3346                row[ipname] = pName;
3347                row[iinput_parms] = ip;
3348                row[ioutput_parms] = op;
3349                row[iresult_sets] = rs;
3350                row[iremark] = remark;
3351                row[iptype] = pType;
3352                row[iporigin] = "ALIAS";
3353                row[isn] = specificName;
3354
3355                t.insertSys(row);
3356            }
3357        }
3358    }
3359
3360//------------------------------------------------------------------------------
3361
// boucherb@users 20050515 further SQL2003 metadata support
3362

3363    /**
3364     * SYSTEM_AUTHORIZATIONS<p>
3365     *
3366     * <b>Function</b><p>
3367     *
3368     * The AUTHORIZATIONS table has one row for each &lt;role name&gt; and
3369     * one row for each &lt;authorization identifier &gt; referenced in the
3370     * Information Schema. These are the &lt;role name&gt;s and
3371     * &lt;authorization identifier&gt;s that may grant privileges as well as
3372     * those that may create a schema, or currently own a schema created
3373     * through a &lt;schema definition&gt;. <p>
3374     *
3375     * <b>Definition</b><p>
3376     *
3377     * <pre class="SqlCodeExample">
3378     * CREATE TABLE AUTHORIZATIONS (
3379     * AUTHORIZATION_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
3380     * AUTHORIZATION_TYPE INFORMATION_SCHEMA.CHARACTER_DATA
3381     * CONSTRAINT AUTHORIZATIONS_AUTHORIZATION_TYPE_NOT_NULL
3382     * NOT NULL
3383     * CONSTRAINT AUTHORIZATIONS_AUTHORIZATION_TYPE_CHECK
3384     * CHECK ( AUTHORIZATION_TYPE IN ( 'USER', 'ROLE' ) ),
3385     * CONSTRAINT AUTHORIZATIONS_PRIMARY_KEY
3386     * PRIMARY KEY (AUTHORIZATION_NAME)
3387     * )
3388     * </pre>
3389     *
3390     * <b>Description</b><p>
3391     *
3392     * <ol>
3393     * <li> The values of AUTHORIZATION_TYPE have the following meanings:<p>
3394     *
3395     * <table border cellpadding="3">
3396     * <tr>
3397     * <td nowrap>USER</td>
3398     * <td nowrap>The value of AUTHORIZATION_NAME is a known
3399     * &lt;user identifier&gt;.</td>
3400     * <tr>
3401     * <tr>
3402     * <td nowrap>NO</td>
3403     * <td nowrap>The value of AUTHORIZATION_NAME is a &lt;role
3404     * name&gt; defined by a &lt;role definition&gt;.</td>
3405     * <tr>
3406     * </table> <p>
3407     * </ol>
3408     */

3409    Table SYSTEM_AUTHORIZATIONS() throws HsqlException {
3410
3411        Table t = sysTables[SYSTEM_AUTHORIZATIONS];
3412
3413        if (t == null) {
3414            t = createBlankTable(sysTableHsqlNames[SYSTEM_AUTHORIZATIONS]);
3415
3416            addColumn(t, "AUTHORIZATION_NAME", Types.VARCHAR, true); // not null
3417
addColumn(t, "AUTHORIZATION_TYPE", Types.VARCHAR, true); // not null
3418

3419            // true PK
3420
t.createPrimaryKey(null, new int[]{ 0 }, true);
3421
3422            return t;
3423        }
3424
3425        // Intermediate holders
3426
HsqlArrayList users;
3427        Iterator roles;
3428        User user;
3429        int userCount;
3430        Object JavaDoc[] row;
3431
3432        // Initialization
3433
users = database.getUserManager().listVisibleUsers(session, false);
3434        userCount = users.size();
3435
3436        // Do it.
3437
for (int i = 0; i < users.size(); i++) {
3438            row = t.getEmptyRowData();
3439            user = (User) users.get(i);
3440            row[0] = user.getName();
3441            row[1] = "USER";
3442
3443            t.insertSys(row);
3444        }
3445
3446        roles = database.getGranteeManager().getRoleNames().iterator();
3447
3448        while (roles.hasNext()) {
3449            row = t.getEmptyRowData();
3450            row[0] = roles.next().toString();
3451            row[1] = "ROLE";
3452
3453            t.insertSys(row);
3454        }
3455
3456        t.setDataReadOnly(true);
3457
3458        return t;
3459    }
3460
3461    /**
3462     * SYSTEM_COLLATIONS<p>
3463     *
3464     * <b>Function<b><p>
3465     *
3466     * The COLLATIONS table has one row for each character collation
3467     * descriptor. <p>
3468     *
3469     * <b>Definition</b>
3470     *
3471     * <pre class="SqlCodeExample">
3472     * CREATE TABLE COLLATIONS (
3473     * COLLATION_CATALOG INFORMATION_SCHEMA.SQL_IDENTIFIER,
3474     * COLLATION_SCHEMA INFORMATION_SCHEMA.SQL_IDENTIFIER,
3475     * COLLATION_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
3476     * PAD_ATTRIBUTE INFORMATION_SCHEMA.CHARACTER_DATA
3477     * CONSTRAINT COLLATIONS_PAD_ATTRIBUTE_CHECK
3478     * CHECK ( PAD_ATTRIBUTE IN
3479     * ( 'NO PAD', 'PAD SPACE' ) ),
3480     * COLLATION_TYPE INFORMATION_SCHEMA.SQL_IDENTIFIER,
3481     * COLLATION_DEFINITION INFORMATION_SCHEMA.CHARACTER_DATA,
3482     * COLLATION_DICTIONARY INFORMATION_SCHEMA.CHARACTER_DATA,
3483     * CHARACTER_REPERTOIRE_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER
3484     * CONSTRAINT CHARACTER_REPERTOIRE_NAME_NOT_NULL
3485     * NOT NULL,
3486     * CONSTRAINT COLLATIONS_PRIMARY_KEY
3487     * PRIMARY KEY ( COLLATION_CATALOG, COLLATION_SCHEMA, COLLATION_NAME ),
3488     * CONSTRAINT COLLATIONS_FOREIGN_KEY_SCHEMATA
3489     * FOREIGN KEY ( COLLATION_CATALOG, COLLATION_SCHEMA )
3490     * REFERENCES SCHEMATA
3491     * )
3492     * </pre>
3493     *
3494     * <b>Description</b><p>
3495     *
3496     * <ol>
3497     * <li>The values of COLLATION_CATALOG, COLLATION_SCHEMA, and
3498     * COLLATION_NAME are the catalog name, unqualified schema name,
3499     * and qualified identifier, respectively, of the collation being
3500     * described.<p>
3501     *
3502     * <li>The values of COLLATION_TYPE, COLLATION_DICTIONARY, and
3503     * COLLATION_DEFINITION are the null value (deprectated). <p>
3504     *
3505     * <li>The values of PAD_ATTRIBUTE have the following meanings:<p>
3506     *
3507     * <table border cellpadding="3">
3508     * <tr>
3509     * <td nowrap>NO PAD</td>
3510     * <td nowrap>The collation being described has the NO PAD
3511     * characteristic.</td>
3512     * <tr>
3513     * <tr>
3514     * <td nowrap>PAD</td>
3515     * <td nowrap>The collation being described has the PAD SPACE
3516     * characteristic.</td>
3517     * <tr>
3518     * </table> <p>
3519     *
3520     * <li>The value of CHARACTER_REPERTOIRE_NAME is the name of the
3521     * character repertoire to which the collation being described
3522     * is applicable.
3523     * </ol>
3524     */

3525    Table SYSTEM_COLLATIONS() throws HsqlException {
3526
3527        Table t = sysTables[SYSTEM_COLLATIONS];
3528
3529        if (t == null) {
3530            t = createBlankTable(sysTableHsqlNames[SYSTEM_COLLATIONS]);
3531
3532            addColumn(t, "COLLATION_CATALOG", Types.VARCHAR);
3533            addColumn(t, "COLLATION_SCHEMA", Types.VARCHAR, true);
3534            addColumn(t, "COLLATION_NAME", Types.VARCHAR, true);
3535            addColumn(t, "PAD_ATTRIBUTE", Types.VARCHAR, 9, true);
3536            addColumn(t, "COLLATION_TYPE", Types.VARCHAR, true);
3537            addColumn(t, "COLLATION_DEFINITION", Types.VARCHAR);
3538            addColumn(t, "COLLATION_DICTIONARY", Types.VARCHAR);
3539            addColumn(t, "CHARACTER_REPERTOIRE_NAME", Types.VARCHAR, true);
3540
3541            // false PK, as rows may have NULL COLLATION_CATALOG
3542
t.createPrimaryKey(null, new int[] {
3543                0, 1, 2
3544            }, false);
3545
3546            return t;
3547        }
3548
3549        Iterator collations;
3550        String JavaDoc collation;
3551        String JavaDoc collationSchema = SchemaManager.PUBLIC_SCHEMA;
3552        String JavaDoc padAttribute = "NO PAD";
3553        String JavaDoc characterRepertoireName = "UNICODE";
3554        Object JavaDoc[] row;
3555        final int icolcat = 0;
3556        final int icolschem = 1;
3557        final int icolname = 2;
3558        final int ipadattr = 3;
3559        final int icoltype = 4;
3560        final int icoldef = 5;
3561        final int icoldict = 6;
3562        final int icharrep = 7;
3563
3564        collations = Collation.nameToJavaName.keySet().iterator();
3565
3566        while (collations.hasNext()) {
3567            row = t.getEmptyRowData();
3568            collation = (String JavaDoc) collations.next();
3569            row[icolcat] = ns.getCatalogName(collation);
3570            row[icolschem] = collationSchema;
3571            row[icolname] = collation;
3572            row[ipadattr] = padAttribute;
3573            row[icharrep] = characterRepertoireName;
3574
3575            t.insertSys(row);
3576        }
3577
3578        t.setDataReadOnly(true);
3579
3580        return t;
3581    }
3582
3583    /**
3584     * SYSTEM_ENABLED_ROLES<p>
3585     *
3586     * <b>Function</b><p>
3587     *
3588     * Identify the enabled roles for the current SQL-session.<p>
3589     *
3590     * Definition<p>
3591     *
3592     * <pre class="SqlCodeExample">
3593     * CREATE RECURSIVE VIEW ENABLED_ROLES ( ROLE_NAME ) AS
3594     * VALUES ( CURRENT_ROLE )
3595     * UNION
3596     * SELECT RAD.ROLE_NAME
3597     * FROM DEFINITION_SCHEMA.ROLE_AUTHORIZATION_DESCRIPTORS RAD
3598     * JOIN ENABLED_ROLES R
3599     * ON RAD.GRANTEE = R.ROLE_NAME;
3600     *
3601     * GRANT SELECT ON TABLE ENABLED_ROLES
3602     * TO PUBLIC WITH GRANT OPTION;
3603     * </pre>
3604     */

3605
3606    /**
3607     * SYSTEM_APPLICABLE_ROLES<p>
3608     *
3609     * <b>Function</b><p>
3610     *
3611     * Identifies the applicable roles for the current user.<p>
3612     *
3613     * <b>Definition</b><p>
3614     *
3615     * <pre class="SqlCodeExample">
3616     * CREATE RECURSIVE VIEW APPLICABLE_ROLES ( GRANTEE, ROLE_NAME, IS_GRANTABLE ) AS
3617     * ( ( SELECT GRANTEE, ROLE_NAME, IS_GRANTABLE
3618     * FROM DEFINITION_SCHEMA.ROLE_AUTHORIZATION_DESCRIPTORS
3619     * WHERE ( GRANTEE IN ( CURRENT_USER, 'PUBLIC' )
3620     * OR GRANTEE IN ( SELECT ROLE_NAME
3621     * FROM ENABLED_ROLES ) ) )
3622     * UNION
3623     * ( SELECT RAD.GRANTEE, RAD.ROLE_NAME, RAD.IS_GRANTABLE
3624     * FROM DEFINITION_SCHEMA.ROLE_AUTHORIZATION_DESCRIPTORS RAD
3625     * JOIN APPLICABLE_ROLES R
3626     * ON RAD.GRANTEE = R.ROLE_NAME ) );
3627     *
3628     * GRANT SELECT ON TABLE APPLICABLE_ROLES
3629     * TO PUBLIC WITH GRANT OPTION;
3630     * </pre>
3631     */

3632
3633    /**
3634     * SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS<p>
3635     *
3636     * <b>Function</b><p>
3637     *
3638     * Contains a representation of the role authorization descriptors.<p>
3639     *
3640     * <b>Definition</b>
3641     *
3642     * <pre class="SqlCodeExample">
3643     * CREATE TABLE ROLE_AUTHORIZATION_DESCRIPTORS (
3644     * ROLE_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
3645     * GRANTEE INFORMATION_SCHEMA.SQL_IDENTIFIER,
3646     * GRANTOR INFORMATION_SCHEMA.SQL_IDENTIFIER,
3647     * IS_GRANTABLE INFORMATION_SCHEMA.CHARACTER_DATA
3648     * CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_IS_GRANTABLE_CHECK
3649     * CHECK ( IS_GRANTABLE IN
3650     * ( 'YES', 'NO' ) ),
3651     * CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_PRIMARY_KEY
3652     * PRIMARY KEY ( ROLE_NAME, GRANTEE ),
3653     * CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_CHECK_ROLE_NAME
3654     * CHECK ( ROLE_NAME IN
3655     * ( SELECT AUTHORIZATION_NAME
3656     * FROM AUTHORIZATIONS
3657     * WHERE AUTHORIZATION_TYPE = 'ROLE' ) ),
3658     * CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_FOREIGN_KEY_AUTHORIZATIONS_GRANTOR
3659     * FOREIGN KEY ( GRANTOR )
3660     * REFERENCES AUTHORIZATIONS,
3661     * CONSTRAINT ROLE_AUTHORIZATION_DESCRIPTORS_FOREIGN_KEY_AUTHORIZATIONS_GRANTEE
3662     * FOREIGN KEY ( GRANTEE )
3663     * REFERENCES AUTHORIZATIONS
3664     * )
3665     * </pre>
3666     *
3667     * <b>Description</b><p>
3668     *
3669     * <ol>
3670     * <li>The value of ROLE_NAME is the &lt;role name&gt; of some
3671     * &lt;role granted&gt; by the &lt;grant role statement&gt; or
3672     * the &lt;role name&gt; of a &lt;role definition&gt;. <p>
3673     *
3674     * <li>The value of GRANTEE is an &lt;authorization identifier&gt;,
3675     * possibly PUBLIC, or &lt;role name&gt; specified as a
3676     * &lt;grantee&gt; contained in a &lt;grant role statement&gt;,
3677     * or the &lt;authorization identifier&gt; of the current
3678     * SQLsession when the &lt;role definition&gt; is executed. <p>
3679     *
3680     * <li>The value of GRANTOR is the &lt;authorization identifier&gt;
3681     * of the user or role who granted the role identified by
3682     * ROLE_NAME to the user or role identified by the value of
3683     * GRANTEE. <p>
3684     *
3685     * <li>The values of IS_GRANTABLE have the following meanings:<p>
3686     *
3687     * <table border cellpadding="3">
3688     * <tr>
3689     * <td nowrap>YES</td>
3690     * <td nowrap>The described role is grantable.</td>
3691     * <tr>
3692     * <tr>
3693     * <td nowrap>NO</td>
3694     * <td nowrap>The described role is not grantable.</td>
3695     * <tr>
3696     * </table> <p>
3697     * </ol>
3698     */

3699    Table SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS() throws HsqlException {
3700
3701        Table t = sysTables[SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS];
3702
3703        if (t == null) {
3704            t = createBlankTable(
3705                sysTableHsqlNames[SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS]);
3706
3707            addColumn(t, "ROLE_NAME", Types.VARCHAR, true); // not null
3708
addColumn(t, "GRANTEE", Types.VARCHAR, true); // not null
3709
addColumn(t, "GRANTOR", Types.VARCHAR, true); // not null
3710
addColumn(t, "IS_GRANTABLE", Types.VARCHAR, true); // not null
3711

3712            // true PK
3713
t.createPrimaryKey(null, new int[] {
3714                0, 1
3715            }, true);
3716
3717            return t;
3718        }
3719
3720        // Intermediate holders
3721
String JavaDoc grantorName = GranteeManager.SYSTEM_AUTHORIZATION_NAME;
3722        Iterator grantees;
3723        Grantee grantee;
3724        String JavaDoc granteeName;
3725        Iterator roles;
3726        String JavaDoc roleName;
3727        String JavaDoc isGrantable;
3728        Object JavaDoc[] row;
3729        final int irole = 0;
3730        final int igrantee = 1;
3731        final int igrantor = 2;
3732        final int igrantable = 3;
3733
3734        // Initialization
3735
grantees = database.getGranteeManager().getGrantees().iterator();
3736
3737        // Do it.
3738
while (grantees.hasNext()) {
3739            grantee = (Grantee) grantees.next();
3740            granteeName = grantee.getName();
3741            roles = grantee.getDirectRoles().iterator();
3742
3743            while (roles.hasNext()) {
3744                row = t.getEmptyRowData();
3745                roleName = (String JavaDoc) roles.next();
3746                isGrantable =
3747                    grantee.hasRole(GranteeManager.DBA_ADMIN_ROLE_NAME)
3748                    ? "YES"
3749                    : "NO";
3750                row[irole] = roleName;
3751                row[igrantee] = granteeName;
3752                row[igrantor] = grantorName;
3753                row[igrantable] = isGrantable;
3754
3755                t.insertSys(row);
3756            }
3757        }
3758
3759        t.setDataReadOnly(true);
3760
3761        return t;
3762    }
3763
3764    /**
3765     * SYSTEM_SCHEMATA<p>
3766     *
3767     * <b>Function</b><p>
3768     *
3769     * The SCHEMATA table has one row for each schema. <p>
3770     *
3771     * <b>Definition</b><p>
3772     *
3773     * <pre class="SqlCodeExample">
3774     * CREATE TABLE SCHEMATA (
3775     * CATALOG_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
3776     * SCHEMA_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER,
3777     * SCHEMA_OWNER INFORMATION_SCHEMA.SQL_IDENTIFIER
3778     * CONSTRAINT SCHEMA_OWNER_NOT_NULL
3779     * NOT NULL,
3780     * DEFAULT_CHARACTER_SET_CATALOG INFORMATION_SCHEMA.SQL_IDENTIFIER
3781     * CONSTRAINT DEFAULT_CHARACTER_SET_CATALOG_NOT_NULL
3782     * NOT NULL,
3783     * DEFAULT_CHARACTER_SET_SCHEMA INFORMATION_SCHEMA.SQL_IDENTIFIER
3784     * CONSTRAINT DEFAULT_CHARACTER_SET_SCHEMA_NOT_NULL
3785     * NOT NULL,
3786     * DEFAULT_CHARACTER_SET_NAME INFORMATION_SCHEMA.SQL_IDENTIFIER
3787     * CONSTRAINT DEFAULT_CHARACTER_SET_NAME_NOT_NULL
3788     * NOT NULL,
3789     * SQL_PATH INFORMATION_SCHEMA.CHARACTER_DATA,
3790     *
3791     * CONSTRAINT SCHEMATA_PRIMARY_KEY
3792     * PRIMARY KEY ( CATALOG_NAME, SCHEMA_NAME ),
3793     * CONSTRAINT SCHEMATA_FOREIGN_KEY_AUTHORIZATIONS
3794     * FOREIGN KEY ( SCHEMA_OWNER )
3795     * REFERENCES AUTHORIZATIONS,
3796     * CONSTRAINT SCHEMATA_FOREIGN_KEY_CATALOG_NAMES
3797     * FOREIGN KEY ( CATALOG_NAME )
3798     * REFERENCES CATALOG_NAMES
3799     * )
3800     * </pre>
3801     *
3802     * <b>Description</b><p>
3803     *
3804     * <ol>
3805     * <li>The value of CATALOG_NAME is the name of the catalog of the
3806     * schema described by this row.<p>
3807     *
3808     * <li>The value of SCHEMA_NAME is the unqualified schema name of
3809     * the schema described by this row.<p>
3810     *
3811     * <li>The values of SCHEMA_OWNER are the authorization identifiers
3812     * that own the schemata.<p>
3813     *
3814     * <li>The values of DEFAULT_CHARACTER_SET_CATALOG,
3815     * DEFAULT_CHARACTER_SET_SCHEMA, and DEFAULT_CHARACTER_SET_NAME
3816     * are the catalog name, unqualified schema name, and qualified
3817     * identifier, respectively, of the default character set for
3818     * columns and domains in the schemata.<p>
3819     *
3820     * <li>Case:<p>
3821     * <ul>
3822     * <li>If &lt;schema path specification&gt; was specified in
3823     * the &lt;schema definition&gt; that defined the schema
3824     * described by this row and the character representation
3825     * of the &lt;schema path specification&gt; can be
3826     * represented without truncation, then the value of
3827     * SQL_PATH is that character representation.<p>
3828     *
3829     * <li>Otherwise, the value of SQL_PATH is the null value.
3830     * </ul>
3831     * </ol>
3832     */

3833    Table SYSTEM_SCHEMATA() throws HsqlException {
3834
3835        Table t = sysTables[SYSTEM_SCHEMATA];
3836
3837        if (t == null) {
3838            t = createBlankTable(sysTableHsqlNames[SYSTEM_SCHEMATA]);
3839
3840            addColumn(t, "CATALOG_NAME", Types.VARCHAR);
3841            addColumn(t, "SCHEMA_NAME", Types.VARCHAR, true);
3842            addColumn(t, "SCHEMA_OWNER", Types.VARCHAR, true);
3843            addColumn(t, "DEFAULT_CHARACTER_SET_CATALOG", Types.VARCHAR);
3844            addColumn(t, "DEFAULT_CHARACTER_SET_SCHEMA", Types.VARCHAR, true);
3845            addColumn(t, "DEFAULT_CHARACTER_SET_NAME", Types.VARCHAR);
3846            addColumn(t, "SQL_PATH", Types.VARCHAR);
3847
3848            // order: CATALOG_NAME, SCHEMA_NAME
3849
// false PK, as rows may have NULL CATALOG_NAME
3850
t.createPrimaryKey(null, new int[] {
3851                0, 1
3852            }, false);
3853
3854            return t;
3855        }
3856
3857        Iterator schemas;
3858        String JavaDoc schema;
3859        String JavaDoc schemaOwner = GranteeManager.DBA_ADMIN_ROLE_NAME;
3860        String JavaDoc dcsSchema = SchemaManager.INFORMATION_SCHEMA;
3861        String JavaDoc dcsName = ValuePool.getString("UTF16");
3862        String JavaDoc sqlPath = null;
3863        Object JavaDoc[] row;
3864        final int ischema_catalog = 0;
3865        final int ischema_name = 1;
3866        final int ischema_owner = 2;
3867        final int idef_charset_cat = 3;
3868        final int idef_charset_schem = 4;
3869        final int idef_charset_name = 5;
3870        final int isql_path = 6;
3871
3872        // Initialization
3873
schemas = database.schemaManager.fullSchemaNamesIterator();
3874
3875        // Do it.
3876
while (schemas.hasNext()) {
3877            row = t.getEmptyRowData();
3878            schema = (String JavaDoc) schemas.next();
3879            row[ischema_catalog] = ns.getCatalogName(schema);
3880            row[ischema_name] = schema;
3881            row[ischema_owner] = schemaOwner;
3882            row[idef_charset_cat] = ns.getCatalogName(dcsSchema);
3883            row[idef_charset_schem] = dcsSchema;
3884            row[idef_charset_name] = dcsName;
3885            row[isql_path] = sqlPath;
3886
3887            t.insertSys(row);
3888        }
3889
3890        t.setDataReadOnly(true);
3891
3892        return t;
3893    }
3894}
3895
Popular Tags