KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > DatabaseInformation


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.Constructor JavaDoc;
35
36 import org.hsqldb.lib.IntValueHashMap;
37
38 // fredt@users - 1.7.2 - structural modifications to allow inheritance
39
// boucherB@users 20020305 - completed inheritance work, including final access
40
// boucherB@users 20020305 - javadoc updates/corrections
41
// boucherB@users 20020305 - SYSTEM_VIEWS brought in line with SQL 200n
42
// boucherb@users 20050514 - further SQL 200n metdata support
43

44 /**
45  * Base class for system tables. Includes a factory method which returns the
46  * most complete implementation available in the jar. This base implementation
47  * knows the names of all system tables but returns null for any system table.
48  * <p>
49  * This class has been developed from scratch to replace the previous
50  * DatabaseInformation implementations. <p>
51  *
52  * @author boucherb@users
53  * @version 1.8.0
54  * @since 1.7.2
55  */

56 class DatabaseInformation {
57
58     // ids for system table names strictly in order of sysTableNames[]
59
protected static final int SYSTEM_BESTROWIDENTIFIER = 0;
60     protected static final int SYSTEM_CATALOGS = 1;
61     protected static final int SYSTEM_COLUMNPRIVILEGES = 2;
62     protected static final int SYSTEM_COLUMNS = 3;
63     protected static final int SYSTEM_CROSSREFERENCE = 4;
64     protected static final int SYSTEM_INDEXINFO = 5;
65     protected static final int SYSTEM_PRIMARYKEYS = 6;
66     protected static final int SYSTEM_PROCEDURECOLUMNS = 7;
67     protected static final int SYSTEM_PROCEDURES = 8;
68     protected static final int SYSTEM_SCHEMAS = 9;
69     protected static final int SYSTEM_SUPERTABLES = 10;
70     protected static final int SYSTEM_SUPERTYPES = 11;
71     protected static final int SYSTEM_TABLEPRIVILEGES = 12;
72     protected static final int SYSTEM_TABLES = 13;
73     protected static final int SYSTEM_TABLETYPES = 14;
74     protected static final int SYSTEM_TYPEINFO = 15;
75     protected static final int SYSTEM_UDTATTRIBUTES = 16;
76     protected static final int SYSTEM_UDTS = 17;
77     protected static final int SYSTEM_USERS = 18;
78     protected static final int SYSTEM_VERSIONCOLUMNS = 19;
79
80     // HSQLDB-specific
81
protected static final int SYSTEM_ALIASES = 20;
82     protected static final int SYSTEM_BYTECODE = 21;
83     protected static final int SYSTEM_CACHEINFO = 22;
84     protected static final int SYSTEM_CLASSPRIVILEGES = 23;
85     protected static final int SYSTEM_SESSIONINFO = 24;
86     protected static final int SYSTEM_PROPERTIES = 25;
87     protected static final int SYSTEM_SESSIONS = 26;
88     protected static final int SYSTEM_TRIGGERCOLUMNS = 27;
89     protected static final int SYSTEM_TRIGGERS = 28;
90     protected static final int SYSTEM_ALLTYPEINFO = 29;
91
92 // boucherb@users 20030305 - brought in line with SQL 200n
93
protected static final int SYSTEM_VIEWS = 30;
94
95 // boucherb@users 20030403 - isolated and improved text table reporting
96
protected static final int SYSTEM_TEXTTABLES = 31;
97
98 // boucherb@users 20040107 - metadata support for sequences
99
protected static final int SYSTEM_SEQUENCES = 32;
100     protected static final int SYSTEM_USAGE_PRIVILEGES = 33;
101
102 // boucherb@users 20040107 - metadata support for constraints
103
protected static final int SYSTEM_CHECK_CONSTRAINTS = 34;
104     protected static final int SYSTEM_TABLE_CONSTRAINTS = 35;
105
106 // boucherb@users 20040107 - metadata support for view usage breakdown- SQL 200n
107
protected static final int SYSTEM_CHECK_COLUMN_USAGE = 36;
108     protected static final int SYSTEM_CHECK_ROUTINE_USAGE = 37;
109     protected static final int SYSTEM_CHECK_TABLE_USAGE = 38;
110     protected static final int SYSTEM_VIEW_COLUMN_USAGE = 39;
111     protected static final int SYSTEM_VIEW_TABLE_USAGE = 40;
112     protected static final int SYSTEM_VIEW_ROUTINE_USAGE = 41;
113
114 // boucherb@users 20050514 - further SQL 200n metdata support
115
protected static final int SYSTEM_AUTHORIZATIONS = 42;
116     protected static final int SYSTEM_COLLATIONS = 43;
117     protected static final int SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS = 44;
118     protected static final int SYSTEM_SCHEMATA = 45;
119
120     /** system table names strictly in order of their ids */
121     protected static final String JavaDoc[] sysTableNames = {
122         "SYSTEM_BESTROWIDENTIFIER", //
123
"SYSTEM_CATALOGS", //
124
"SYSTEM_COLUMNPRIVILEGES", //
125
"SYSTEM_COLUMNS", //
126
"SYSTEM_CROSSREFERENCE", //
127
"SYSTEM_INDEXINFO", //
128
"SYSTEM_PRIMARYKEYS", //
129
"SYSTEM_PROCEDURECOLUMNS", //
130
"SYSTEM_PROCEDURES", //
131
"SYSTEM_SCHEMAS", //
132
"SYSTEM_SUPERTABLES", //
133
"SYSTEM_SUPERTYPES", //
134
"SYSTEM_TABLEPRIVILEGES", //
135
"SYSTEM_TABLES", //
136
"SYSTEM_TABLETYPES", //
137
"SYSTEM_TYPEINFO", //
138
"SYSTEM_UDTATTRIBUTES", //
139
"SYSTEM_UDTS", //
140
"SYSTEM_USERS", //
141
"SYSTEM_VERSIONCOLUMNS", //
142

143         // HSQLDB-specific
144
"SYSTEM_ALIASES", //
145
"SYSTEM_BYTECODE", //
146
"SYSTEM_CACHEINFO", //
147
"SYSTEM_CLASSPRIVILEGES", //
148
"SYSTEM_SESSIONINFO", //
149
"SYSTEM_PROPERTIES", //
150
"SYSTEM_SESSIONS", //
151
"SYSTEM_TRIGGERCOLUMNS", //
152
"SYSTEM_TRIGGERS", //
153
"SYSTEM_ALLTYPEINFO", //
154

155         // boucherb@users 20030305 - brought in line with SQL 200n
156
"SYSTEM_VIEWS",
157
158         // boucherb@users 20030403 - isolated and improved text table reporting
159
"SYSTEM_TEXTTABLES",
160
161         // boucherb@users 20040107 - metadata support for sequences - SQL 200n
162
"SYSTEM_SEQUENCES", //
163
"SYSTEM_USAGE_PRIVILEGES",
164
165         // boucherb@users 20040107 - metadata support for constraints - SQL 200n
166
"SYSTEM_CHECK_CONSTRAINTS", //
167
"SYSTEM_TABLE_CONSTRAINTS", //
168

169         // boucherb@users 20040107 - metadata support for usage - SQL 200n
170
"SYSTEM_CHECK_COLUMN_USAGE", //
171
"SYSTEM_CHECK_ROUTINE_USAGE", //
172
"SYSTEM_CHECK_TABLE_USAGE", //
173
"SYSTEM_VIEW_COLUMN_USAGE", //
174
"SYSTEM_VIEW_TABLE_USAGE", //
175
"SYSTEM_VIEW_ROUTINE_USAGE", //
176

177         // boucherb@users 20050514 - further SQL 200n metadata support
178
"SYSTEM_AUTHORIZATIONS", //
179
"SYSTEM_COLLATIONS", //
180
"SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS", //
181
"SYSTEM_SCHEMATA" //
182

183         // Future use
184
// "SYSTEM_ASSERTIONS",
185
// "SYSTEM_ATTRIBUTES",
186
// "SYSTEM_AUTHORIZATIONS", // boucherb@users 20050514 - implemented
187
// "SYSTEM_CHARACTER_ENCODING_FORMS",
188
// "SYSTEM_CHARACTER_REPERTOIRES",
189
// "SYSTEM_CHARACTER_SETS",
190
// "SYSTEM_CHECK_COLUMN_USAGE", // boucherb@users 20040107 - implemented
191
// "SYSTEM_CHECK_ROUTINE_USAGE", // boucherb@users 20040107 - implemented
192
// "SYSTEM_CHECK_CONSTRAINTS", // boucherb@users 20040107 - implemented
193
// "SYSTEM_CHECK_TABLE_USAGE", // boucherb@users 20040107 - implemented
194
// "SYSTEM_COLLATION_CHARACTER_SET_APPLICABILITY",
195
// "SYSTEM_COLLATIONS", // boucherb@users 20050514 - implemented
196
// "SYSTEM_COLUMN_COLUMN_USAGE",
197
// "SYSTEM_COLUMN_OPTIONS",
198
// "SYSTEM_COLUMN_PRIVILEGES",
199
// "SYSTEM_COLUMNS",
200
// "SYSTEM_DATA_TYPE_DESCRIPTOR",
201
// "SYSTEM_DIRECT_SUPERTABLES",
202
// "SYSTEM_DIRECT_SUPERTYPES",
203
// "SYSTEM_DOMAIN_CONSTRAINTS",
204
// "SYSTEM_DOMAINS",
205
// "SYSTEM_ELEMENT_TYPES",
206
// "SYSTEM_FIELDS",
207
// "SYSTEM_FOREIGN_DATA_WRAPPER_OPTIONS",
208
// "SYSTEM_FOREIGN_DATA_WRAPPERS",
209
// "SYSTEM_FOREIGN_SERVER_OPTIONS",
210
// "SYSTEM_FOREIGN_SERVERS",
211
// "SYSTEM_FOREIGN_TABLE_OPTIONS",
212
// "SYSTEM_FOREIGN_TABLES",
213
// "SYSTEM_JAR_JAR_USAGE",
214
// "SYSTEM_JARS",
215
// "SYSTEM_KEY_COLUMN_USAGE",
216
// "SYSTEM_METHOD_SPECIFICATION_PARAMETERS",
217
// "SYSTEM_METHOD_SPECIFICATIONS",
218
// "SYSTEM_MODULE_COLUMN_USAGE",
219
// "SYSTEM_MODULE_PRIVILEGES",
220
// "SYSTEM_MODULE_TABLE_USAGE",
221
// "SYSTEM_MODULES",
222
// "SYSTEM_PARAMETERS",
223
// "SYSTEM_REFERENCED_TYPES",
224
// "SYSTEM_REFERENTIAL_CONSTRAINTS",
225
// "SYSTEM_ROLE_AUTHORIZATION_DESCRIPTORS", // boucherb@users 20050514 - implemented
226
// "SYSTEM_ROLES",
227
// "SYSTEM_ROUTINE_COLUMN_USAGE",
228
// "SYSTEM_ROUTINE_JAR_USAGE",
229
// "SYSTEM_ROUTINE_MAPPING_OPTIONS",
230
// "SYSTEM_ROUTINE_MAPPINGS",
231
// "SYSTEM_ROUTINE_PRIVILEGES",
232
// "SYSTEM_ROUTINE_ROUTINE_USAGE",
233
// "SYSTEM_ROUTINE_SEQUENCE_USAGE",
234
// "SYSTEM_ROUTINE_TABLE_USAGE",
235
// "SYSTEM_ROUTINES",
236
// "SYSTEM_SCHEMATA", // boucherb@users 20050514 - implemented
237
// "SYSTEM_SEQUENCES", // boucherb@users 20040107 - implemented
238
// "SYSTEM_SQL_FEATURES",
239
// "SYSTEM_SQL_IMPLEMENTATION_INFO",
240
// "SYSTEM_SQL_LANGUAGES",
241
// "SYSTEM_SQL_SIZING",
242
// "SYSTEM_SQL_SIZING_PROFILES",
243
// "SYSTEM_TABLE_CONSTRAINTS", // boucherb@users 20040107 - implemented
244
// "SYSTEM_TABLE_METHOD_PRIVILEGES",
245
// "SYSTEM_TABLE_PRIVILEGES",
246
// "SYSTEM_TABLES",
247
// "SYSTEM_TRANSFORMS",
248
// "SYSTEM_TRANSLATIONS",
249
// "SYSTEM_TRIGGER_COLUMN_USAGE",
250
// "SYSTEM_TRIGGER_ROUTINE_USAGE",
251
// "SYSTEM_TRIGGER_SEQUENCE_USAGE",
252
// "SYSTEM_TRIGGER_TABLE_USAGE",
253
// "SYSTEM_TRIGGERED_UPDATE_COLUMNS",
254
// "SYSTEM_TRIGGERS",
255
// "SYSTEM_TYPE_JAR_USAGE",
256
// "SYSTEM_USAGE_PRIVILEGES", // boucherb@users 20040107 - implemented
257
// "SYSTEM_USER_DEFINED_TYPE_PRIVILEGES",
258
// "SYSTEM_USER_DEFINED_TYPES",
259
// "SYSTEM_USER_MAPPING_OPTIONS",
260
// "SYSTEM_USER_MAPPINGS",
261
// "SYSTEM_USERS",
262
// "SYSTEM_VIEW_COLUMN_USAGE", // boucherb@users 20040107 - implemented
263
// "SYSTEM_VIEW_ROUTINE_USAGE", // boucherb@users 20040107 - implemented
264
// "SYSTEM_VIEW_TABLE_USAGE", // boucherb@users 20040107 - implemented
265
// "SYSTEM_VIEWS", // boucherb@users 20030305 - implemented
266
};
267
268     /** Map: table name => table id */
269     protected static final IntValueHashMap sysTableNamesMap;
270
271     static {
272         sysTableNamesMap = new IntValueHashMap(47);
273
274         for (int i = 0; i < sysTableNames.length; i++) {
275             sysTableNamesMap.put(sysTableNames[i], i);
276         }
277     }
278
279     static int getSysTableID(String JavaDoc token) {
280         return sysTableNamesMap.get(token, -1);
281     }
282
283     /** Database for which to produce tables */
284     protected final Database database;
285
286     /**
287      * Simple object-wide flag indicating that all of this object's cached
288      * data is dirty.
289      */

290     protected boolean isDirty = true;
291
292     /**
293      * state flag -- if true, contentful tables are to be produced, else
294      * empty (surrogate) tables are to be produced. This allows faster
295      * database startup where user views reference system tables and faster
296      * system table structural reflection for table metadata.
297      */

298     protected boolean withContent = false;
299
300     /**
301      * Factory method retuns the fullest system table producer
302      * implementation available. This instantiates implementations beginning
303      * with the most complete, finally choosing an empty table producer
304      * implemenation (this class) if no better instance can be constructed.
305      * @param db The Database object for which to produce system tables
306      * @return the fullest system table producer
307      * implementation available
308      * @throws HsqlException never - required by constructor
309      */

310     static final DatabaseInformation newDatabaseInformation(Database db)
311     throws HsqlException {
312
313         Class JavaDoc clazz = null;
314
315         try {
316             clazz = Class.forName("org.hsqldb.DatabaseInformationFull");
317         } catch (Exception JavaDoc e) {
318             try {
319                 clazz = Class.forName("org.hsqldb.DatabaseInformationMain");
320             } catch (Exception JavaDoc e2) {}
321         }
322
323         try {
324             Class JavaDoc[] ctorParmTypes = new Class JavaDoc[]{ Database.class };
325             Object JavaDoc[] ctorParms = new Object JavaDoc[]{ db };
326             Constructor JavaDoc ctor = clazz.getDeclaredConstructor(ctorParmTypes);
327
328             return (DatabaseInformation) ctor.newInstance(ctorParms);
329         } catch (Exception JavaDoc e) {}
330
331         return new DatabaseInformation(db);
332     }
333
334     /**
335      * Constructs a new DatabaseInformation instance which knows the names of
336      * all system tables (isSystemTable()) but simpy returns null for all
337      * getSystemTable() requests. <p>
338      *
339      * @param db The Database object for which to produce system tables
340      * @throws HsqlException never (required for descendents)
341      */

342     DatabaseInformation(Database db) throws HsqlException {
343         database = db;
344     }
345
346     /**
347      * Tests if the specified name is that of a system table. <p>
348      *
349      * @param name the name to test
350      * @return true if the specified name is that of a system table
351      */

352     final boolean isSystemTable(String JavaDoc name) {
353         return sysTableNamesMap.containsKey(name);
354     }
355
356     /**
357      * Retrieves a table with the specified name whose content may depend on
358      * the execution context indicated by the session argument as well as the
359      * current value of <code>withContent</code>. <p>
360      *
361      * @param session the context in which to produce the table
362      * @param name the name of the table to produce
363      * @throws HsqlException if a database access error occurs
364      * @return a table corresponding to the name and session arguments, or
365      * <code>null</code> if there is no such table to be produced
366      */

367     Table getSystemTable(Session session, String JavaDoc name) throws HsqlException {
368         return null;
369     }
370
371     /**
372      * Controls caching of all tables produced by this object. <p>
373      *
374      * Subclasses are free to ignore this, since they may choose an
375      * implementation that does not dynamically generate and/or cache
376      * table content on an as-needed basis. <p>
377      *
378      * If not ignored, this call indicates to this object that all cached
379      * table data may be dirty, requiring a complete cache clear at some
380      * point.<p>
381      *
382      * Subclasses are free to delay cache clear until next getSystemTable().
383      * However, subclasses may have to be aware of additional methods with
384      * semantics similar to getSystemTable() and act accordingly (e.g.
385      * clearing earlier than next invocation of getSystemTable()).
386      */

387     final void setDirty() {
388         isDirty = true;
389     }
390
391     /**
392      * Switches this table producer between producing empty (surrogate)
393      * or contentful tables. <p>
394      *
395      * @param withContent if true, then produce contentful tables, else
396      * produce emtpy (surrogate) tables
397      */

398     final void setWithContent(boolean withContent) {
399         this.withContent = withContent;
400     }
401 }
402
Popular Tags