001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb;
032:
033: import java.lang.reflect.Constructor;
034:
035: import org.hsqldb.lib.IntValueHashMap;
036:
037: // fredt@users - 1.7.2 - structural modifications to allow inheritance
038: // boucherB@users 20020305 - completed inheritance work, including final access
039: // boucherB@users 20020305 - javadoc updates/corrections
040: // boucherB@users 20020305 - SYSTEM_VIEWS brought in line with SQL 200n
041: // boucherb@users 20050514 - further SQL 200n metdata support
042:
043: /**
044: * Base class for system tables. Includes a factory method which returns the
045: * most complete implementation available in the jar. This base implementation
046: * knows the names of all system tables but returns null for any system table.
047: * <p>
048: * This class has been developed from scratch to replace the previous
049: * DatabaseInformation implementations. <p>
050: *
051: * @author boucherb@users
052: * @version 1.8.0
053: * @since 1.7.2
054: */
055: class DatabaseInformation {
056:
057: // ids for system table names strictly in order of sysTableNames[]
058: protected static final int SYSTEM_BESTROWIDENTIFIER = 0;
059: protected static final int SYSTEM_CATALOGS = 1;
060: protected static final int SYSTEM_COLUMNPRIVILEGES = 2;
061: protected static final int SYSTEM_COLUMNS = 3;
062: protected static final int SYSTEM_CROSSREFERENCE = 4;
063: protected static final int SYSTEM_INDEXINFO = 5;
064: protected static final int SYSTEM_PRIMARYKEYS = 6;
065: protected static final int SYSTEM_PROCEDURECOLUMNS = 7;
066: protected static final int SYSTEM_PROCEDURES = 8;
067: protected static final int SYSTEM_SCHEMAS = 9;
068: protected static final int SYSTEM_SUPERTABLES = 10;
069: protected static final int SYSTEM_SUPERTYPES = 11;
070: protected static final int SYSTEM_TABLEPRIVILEGES = 12;
071: protected static final int SYSTEM_TABLES = 13;
072: protected static final int SYSTEM_TABLETYPES = 14;
073: protected static final int SYSTEM_TYPEINFO = 15;
074: protected static final int SYSTEM_UDTATTRIBUTES = 16;
075: protected static final int SYSTEM_UDTS = 17;
076: protected static final int SYSTEM_USERS = 18;
077: protected static final int SYSTEM_VERSIONCOLUMNS = 19;
078:
079: // HSQLDB-specific
080: protected static final int SYSTEM_ALIASES = 20;
081: protected static final int SYSTEM_BYTECODE = 21;
082: protected static final int SYSTEM_CACHEINFO = 22;
083: protected static final int SYSTEM_CLASSPRIVILEGES = 23;
084: protected static final int SYSTEM_SESSIONINFO = 24;
085: protected static final int SYSTEM_PROPERTIES = 25;
086: protected static final int SYSTEM_SESSIONS = 26;
087: protected static final int SYSTEM_TRIGGERCOLUMNS = 27;
088: protected static final int SYSTEM_TRIGGERS = 28;
089: protected static final int SYSTEM_ALLTYPEINFO = 29;
090:
091: // boucherb@users 20030305 - brought in line with SQL 200n
092: protected static final int SYSTEM_VIEWS = 30;
093:
094: // boucherb@users 20030403 - isolated and improved text table reporting
095: protected static final int SYSTEM_TEXTTABLES = 31;
096:
097: // boucherb@users 20040107 - metadata support for sequences
098: protected static final int SYSTEM_SEQUENCES = 32;
099: protected static final int SYSTEM_USAGE_PRIVILEGES = 33;
100:
101: // boucherb@users 20040107 - metadata support for constraints
102: protected static final int SYSTEM_CHECK_CONSTRAINTS = 34;
103: protected static final int SYSTEM_TABLE_CONSTRAINTS = 35;
104:
105: // boucherb@users 20040107 - metadata support for view usage breakdown- SQL 200n
106: protected static final int SYSTEM_CHECK_COLUMN_USAGE = 36;
107: protected static final int SYSTEM_CHECK_ROUTINE_USAGE = 37;
108: protected static final int SYSTEM_CHECK_TABLE_USAGE = 38;
109: protected static final int SYSTEM_VIEW_COLUMN_USAGE = 39;
110: protected static final int SYSTEM_VIEW_TABLE_USAGE = 40;
111: protected static final int SYSTEM_VIEW_ROUTINE_USAGE = 41;
112:
113: // boucherb@users 20050514 - further SQL 200n metdata support
114: protected static final int SYSTEM_AUTHORIZATIONS = 42;
115: protected static final int SYSTEM_COLLATIONS = 43;
116: protected static final int SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS = 44;
117: protected static final int SYSTEM_SCHEMATA = 45;
118:
119: /** system table names strictly in order of their ids */
120: protected static final String[] sysTableNames = {
121: "SYSTEM_BESTROWIDENTIFIER", //
122: "SYSTEM_CATALOGS", //
123: "SYSTEM_COLUMNPRIVILEGES", //
124: "SYSTEM_COLUMNS", //
125: "SYSTEM_CROSSREFERENCE", //
126: "SYSTEM_INDEXINFO", //
127: "SYSTEM_PRIMARYKEYS", //
128: "SYSTEM_PROCEDURECOLUMNS", //
129: "SYSTEM_PROCEDURES", //
130: "SYSTEM_SCHEMAS", //
131: "SYSTEM_SUPERTABLES", //
132: "SYSTEM_SUPERTYPES", //
133: "SYSTEM_TABLEPRIVILEGES", //
134: "SYSTEM_TABLES", //
135: "SYSTEM_TABLETYPES", //
136: "SYSTEM_TYPEINFO", //
137: "SYSTEM_UDTATTRIBUTES", //
138: "SYSTEM_UDTS", //
139: "SYSTEM_USERS", //
140: "SYSTEM_VERSIONCOLUMNS", //
141:
142: // HSQLDB-specific
143: "SYSTEM_ALIASES", //
144: "SYSTEM_BYTECODE", //
145: "SYSTEM_CACHEINFO", //
146: "SYSTEM_CLASSPRIVILEGES", //
147: "SYSTEM_SESSIONINFO", //
148: "SYSTEM_PROPERTIES", //
149: "SYSTEM_SESSIONS", //
150: "SYSTEM_TRIGGERCOLUMNS", //
151: "SYSTEM_TRIGGERS", //
152: "SYSTEM_ALLTYPEINFO", //
153:
154: // boucherb@users 20030305 - brought in line with SQL 200n
155: "SYSTEM_VIEWS",
156:
157: // boucherb@users 20030403 - isolated and improved text table reporting
158: "SYSTEM_TEXTTABLES",
159:
160: // boucherb@users 20040107 - metadata support for sequences - SQL 200n
161: "SYSTEM_SEQUENCES", //
162: "SYSTEM_USAGE_PRIVILEGES",
163:
164: // boucherb@users 20040107 - metadata support for constraints - SQL 200n
165: "SYSTEM_CHECK_CONSTRAINTS", //
166: "SYSTEM_TABLE_CONSTRAINTS", //
167:
168: // boucherb@users 20040107 - metadata support for usage - SQL 200n
169: "SYSTEM_CHECK_COLUMN_USAGE", //
170: "SYSTEM_CHECK_ROUTINE_USAGE", //
171: "SYSTEM_CHECK_TABLE_USAGE", //
172: "SYSTEM_VIEW_COLUMN_USAGE", //
173: "SYSTEM_VIEW_TABLE_USAGE", //
174: "SYSTEM_VIEW_ROUTINE_USAGE", //
175:
176: // boucherb@users 20050514 - further SQL 200n metadata support
177: "SYSTEM_AUTHORIZATIONS", //
178: "SYSTEM_COLLATIONS", //
179: "SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS", //
180: "SYSTEM_SCHEMATA" //
181:
182: // Future use
183: // "SYSTEM_ASSERTIONS",
184: // "SYSTEM_ATTRIBUTES",
185: // "SYSTEM_AUTHORIZATIONS", // boucherb@users 20050514 - implemented
186: // "SYSTEM_CHARACTER_ENCODING_FORMS",
187: // "SYSTEM_CHARACTER_REPERTOIRES",
188: // "SYSTEM_CHARACTER_SETS",
189: // "SYSTEM_CHECK_COLUMN_USAGE", // boucherb@users 20040107 - implemented
190: // "SYSTEM_CHECK_ROUTINE_USAGE", // boucherb@users 20040107 - implemented
191: // "SYSTEM_CHECK_CONSTRAINTS", // boucherb@users 20040107 - implemented
192: // "SYSTEM_CHECK_TABLE_USAGE", // boucherb@users 20040107 - implemented
193: // "SYSTEM_COLLATION_CHARACTER_SET_APPLICABILITY",
194: // "SYSTEM_COLLATIONS", // boucherb@users 20050514 - implemented
195: // "SYSTEM_COLUMN_COLUMN_USAGE",
196: // "SYSTEM_COLUMN_OPTIONS",
197: // "SYSTEM_COLUMN_PRIVILEGES",
198: // "SYSTEM_COLUMNS",
199: // "SYSTEM_DATA_TYPE_DESCRIPTOR",
200: // "SYSTEM_DIRECT_SUPERTABLES",
201: // "SYSTEM_DIRECT_SUPERTYPES",
202: // "SYSTEM_DOMAIN_CONSTRAINTS",
203: // "SYSTEM_DOMAINS",
204: // "SYSTEM_ELEMENT_TYPES",
205: // "SYSTEM_FIELDS",
206: // "SYSTEM_FOREIGN_DATA_WRAPPER_OPTIONS",
207: // "SYSTEM_FOREIGN_DATA_WRAPPERS",
208: // "SYSTEM_FOREIGN_SERVER_OPTIONS",
209: // "SYSTEM_FOREIGN_SERVERS",
210: // "SYSTEM_FOREIGN_TABLE_OPTIONS",
211: // "SYSTEM_FOREIGN_TABLES",
212: // "SYSTEM_JAR_JAR_USAGE",
213: // "SYSTEM_JARS",
214: // "SYSTEM_KEY_COLUMN_USAGE",
215: // "SYSTEM_METHOD_SPECIFICATION_PARAMETERS",
216: // "SYSTEM_METHOD_SPECIFICATIONS",
217: // "SYSTEM_MODULE_COLUMN_USAGE",
218: // "SYSTEM_MODULE_PRIVILEGES",
219: // "SYSTEM_MODULE_TABLE_USAGE",
220: // "SYSTEM_MODULES",
221: // "SYSTEM_PARAMETERS",
222: // "SYSTEM_REFERENCED_TYPES",
223: // "SYSTEM_REFERENTIAL_CONSTRAINTS",
224: // "SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS", // boucherb@users 20050514 - implemented
225: // "SYSTEM_ROLES",
226: // "SYSTEM_ROUTINE_COLUMN_USAGE",
227: // "SYSTEM_ROUTINE_JAR_USAGE",
228: // "SYSTEM_ROUTINE_MAPPING_OPTIONS",
229: // "SYSTEM_ROUTINE_MAPPINGS",
230: // "SYSTEM_ROUTINE_PRIVILEGES",
231: // "SYSTEM_ROUTINE_ROUTINE_USAGE",
232: // "SYSTEM_ROUTINE_SEQUENCE_USAGE",
233: // "SYSTEM_ROUTINE_TABLE_USAGE",
234: // "SYSTEM_ROUTINES",
235: // "SYSTEM_SCHEMATA", // boucherb@users 20050514 - implemented
236: // "SYSTEM_SEQUENCES", // boucherb@users 20040107 - implemented
237: // "SYSTEM_SQL_FEATURES",
238: // "SYSTEM_SQL_IMPLEMENTATION_INFO",
239: // "SYSTEM_SQL_LANGUAGES",
240: // "SYSTEM_SQL_SIZING",
241: // "SYSTEM_SQL_SIZING_PROFILES",
242: // "SYSTEM_TABLE_CONSTRAINTS", // boucherb@users 20040107 - implemented
243: // "SYSTEM_TABLE_METHOD_PRIVILEGES",
244: // "SYSTEM_TABLE_PRIVILEGES",
245: // "SYSTEM_TABLES",
246: // "SYSTEM_TRANSFORMS",
247: // "SYSTEM_TRANSLATIONS",
248: // "SYSTEM_TRIGGER_COLUMN_USAGE",
249: // "SYSTEM_TRIGGER_ROUTINE_USAGE",
250: // "SYSTEM_TRIGGER_SEQUENCE_USAGE",
251: // "SYSTEM_TRIGGER_TABLE_USAGE",
252: // "SYSTEM_TRIGGERED_UPDATE_COLUMNS",
253: // "SYSTEM_TRIGGERS",
254: // "SYSTEM_TYPE_JAR_USAGE",
255: // "SYSTEM_USAGE_PRIVILEGES", // boucherb@users 20040107 - implemented
256: // "SYSTEM_USER_DEFINED_TYPE_PRIVILEGES",
257: // "SYSTEM_USER_DEFINED_TYPES",
258: // "SYSTEM_USER_MAPPING_OPTIONS",
259: // "SYSTEM_USER_MAPPINGS",
260: // "SYSTEM_USERS",
261: // "SYSTEM_VIEW_COLUMN_USAGE", // boucherb@users 20040107 - implemented
262: // "SYSTEM_VIEW_ROUTINE_USAGE", // boucherb@users 20040107 - implemented
263: // "SYSTEM_VIEW_TABLE_USAGE", // boucherb@users 20040107 - implemented
264: // "SYSTEM_VIEWS", // boucherb@users 20030305 - implemented
265: };
266:
267: /** Map: table name => table id */
268: protected static final IntValueHashMap sysTableNamesMap;
269:
270: static {
271: sysTableNamesMap = new IntValueHashMap(47);
272:
273: for (int i = 0; i < sysTableNames.length; i++) {
274: sysTableNamesMap.put(sysTableNames[i], i);
275: }
276: }
277:
278: static int getSysTableID(String token) {
279: return sysTableNamesMap.get(token, -1);
280: }
281:
282: /** Database for which to produce tables */
283: protected final Database database;
284:
285: /**
286: * Simple object-wide flag indicating that all of this object's cached
287: * data is dirty.
288: */
289: protected boolean isDirty = true;
290:
291: /**
292: * state flag -- if true, contentful tables are to be produced, else
293: * empty (surrogate) tables are to be produced. This allows faster
294: * database startup where user views reference system tables and faster
295: * system table structural reflection for table metadata.
296: */
297: protected boolean withContent = false;
298:
299: /**
300: * Factory method retuns the fullest system table producer
301: * implementation available. This instantiates implementations beginning
302: * with the most complete, finally choosing an empty table producer
303: * implemenation (this class) if no better instance can be constructed.
304: * @param db The Database object for which to produce system tables
305: * @return the fullest system table producer
306: * implementation available
307: * @throws HsqlException never - required by constructor
308: */
309: static final DatabaseInformation newDatabaseInformation(Database db)
310: throws HsqlException {
311:
312: Class clazz = null;
313:
314: try {
315: clazz = Class.forName("org.hsqldb.DatabaseInformationFull");
316: } catch (Exception e) {
317: try {
318: clazz = Class
319: .forName("org.hsqldb.DatabaseInformationMain");
320: } catch (Exception e2) {
321: }
322: }
323:
324: try {
325: Class[] ctorParmTypes = new Class[] { Database.class };
326: Object[] ctorParms = new Object[] { db };
327: Constructor ctor = clazz
328: .getDeclaredConstructor(ctorParmTypes);
329:
330: return (DatabaseInformation) ctor.newInstance(ctorParms);
331: } catch (Exception e) {
332: }
333:
334: return new DatabaseInformation(db);
335: }
336:
337: /**
338: * Constructs a new DatabaseInformation instance which knows the names of
339: * all system tables (isSystemTable()) but simpy returns null for all
340: * getSystemTable() requests. <p>
341: *
342: * @param db The Database object for which to produce system tables
343: * @throws HsqlException never (required for descendents)
344: */
345: DatabaseInformation(Database db) throws HsqlException {
346: database = db;
347: }
348:
349: /**
350: * Tests if the specified name is that of a system table. <p>
351: *
352: * @param name the name to test
353: * @return true if the specified name is that of a system table
354: */
355: final boolean isSystemTable(String name) {
356: return sysTableNamesMap.containsKey(name);
357: }
358:
359: /**
360: * Retrieves a table with the specified name whose content may depend on
361: * the execution context indicated by the session argument as well as the
362: * current value of <code>withContent</code>. <p>
363: *
364: * @param session the context in which to produce the table
365: * @param name the name of the table to produce
366: * @throws HsqlException if a database access error occurs
367: * @return a table corresponding to the name and session arguments, or
368: * <code>null</code> if there is no such table to be produced
369: */
370: Table getSystemTable(Session session, String name)
371: throws HsqlException {
372: return null;
373: }
374:
375: /**
376: * Controls caching of all tables produced by this object. <p>
377: *
378: * Subclasses are free to ignore this, since they may choose an
379: * implementation that does not dynamically generate and/or cache
380: * table content on an as-needed basis. <p>
381: *
382: * If not ignored, this call indicates to this object that all cached
383: * table data may be dirty, requiring a complete cache clear at some
384: * point.<p>
385: *
386: * Subclasses are free to delay cache clear until next getSystemTable().
387: * However, subclasses may have to be aware of additional methods with
388: * semantics similar to getSystemTable() and act accordingly (e.g.
389: * clearing earlier than next invocation of getSystemTable()).
390: */
391: final void setDirty() {
392: isDirty = true;
393: }
394:
395: /**
396: * Switches this table producer between producing empty (surrogate)
397: * or contentful tables. <p>
398: *
399: * @param withContent if true, then produce contentful tables, else
400: * produce emtpy (surrogate) tables
401: */
402: final void setWithContent(boolean withContent) {
403: this.withContent = withContent;
404: }
405: }
|