📄 schemamanager.java
字号:
/* Copyright (c) 2001-2005, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hsqldb;import org.hsqldb.HsqlNameManager.HsqlName;import org.hsqldb.lib.ArrayUtil;import org.hsqldb.lib.HsqlArrayList;import org.hsqldb.lib.HashMappedList;import org.hsqldb.lib.Iterator;import org.hsqldb.lib.WrapperIterator;import org.hsqldb.persist.Logger;/** * Manages all SCHEMA related database objects * * @author fredt@users * @version 1.8.0 * @since 1.8.0 */public class SchemaManager { static final String SYSTEM_SCHEMA = "SYSTEM_SCHEMA"; static final String DEFINITION_SCHEMA = "DEFINITION_SCHEMA"; static final String INFORMATION_SCHEMA = "INFORMATION_SCHEMA"; static final String PUBLIC_SCHEMA = "PUBLIC"; static HsqlName INFORMATION_SCHEMA_HSQLNAME = HsqlNameManager.newHsqlSystemObjectName(INFORMATION_SCHEMA); static HsqlName SYSTEM_SCHEMA_HSQLNAME = HsqlNameManager.newHsqlSystemObjectName(SYSTEM_SCHEMA); Database database; HsqlName defaultSchemaHsqlName; HashMappedList schemaMap = new HashMappedList(); SchemaManager(Database database) { this.database = database; Schema schema = new Schema(PUBLIC_SCHEMA, false); defaultSchemaHsqlName = schema.name; schemaMap.put(PUBLIC_SCHEMA, schema); } void createSchema(String name, boolean isQuoted) throws HsqlException { if (DEFINITION_SCHEMA.equals(name) || INFORMATION_SCHEMA.equals(name) || SYSTEM_SCHEMA.equals(name)) { throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS); } Schema schema = new Schema(name, isQuoted); schemaMap.add(name, schema); } void dropSchema(String name, boolean cascade) throws HsqlException { Schema schema = (Schema) schemaMap.get(name); if (schema == null) { throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS); } if (!cascade &&!schema.isEmpty()) { throw Trace.error(Trace.DEPENDENT_DATABASE_OBJECT_EXISTS); } Iterator tableIterator = schema.tablesIterator(); while (tableIterator.hasNext()) { Table table = ((Table) tableIterator.next()); database.getUserManager().removeDbObject(table.getName()); table.drop(); } Iterator sequenceIterator = schema.sequencesIterator(); while (tableIterator.hasNext()) { NumberSequence sequence = ((NumberSequence) sequenceIterator.next()); database.getUserManager().removeDbObject(sequence.getName()); } schema.clearStructures(); schemaMap.remove(name); if (defaultSchemaHsqlName.name.equals(name)) { if (schemaMap.isEmpty()) { schema = new Schema(PUBLIC_SCHEMA, false); } else { schema = (Schema) schemaMap.get(0); } defaultSchemaHsqlName = schema.name; schemaMap.put(defaultSchemaHsqlName.name, schema); } } void renameSchema(String name, String newName, boolean isQuoted) throws HsqlException { Schema schema = (Schema) schemaMap.get(name); Schema exists = (Schema) schemaMap.get(newName); if (schema == null || exists != null || INFORMATION_SCHEMA.equals(newName)) { throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS, schema == null ? name : newName); } schema.name.rename(newName, isQuoted); int index = schemaMap.getIndex(name); schemaMap.set(index, newName, schema); } void clearStructures() { Iterator it = schemaMap.values().iterator(); while (it.hasNext()) { Schema schema = (Schema) it.next(); schema.clearStructures(); } } public Iterator userSchemaNameIterator() { return schemaMap.keySet().iterator(); } HsqlName toSchemaHsqlName(String name) { Schema schema = (Schema) schemaMap.get(name); return schema == null ? null : schema.name; } HsqlName getDefaultSchemaHsqlName() { return defaultSchemaHsqlName; } public String getDefaultSchemaName() { return defaultSchemaHsqlName.name; } boolean schemaExists(String name) { if (INFORMATION_SCHEMA.equals(name)) { return true; } return schemaMap.containsKey(name); } /** * If schemaName is null, return the current schema name, else return * the HsqlName object for the schema. If schemaName does not exist, * throw. */ HsqlName getSchemaHsqlName(String name) throws HsqlException { if (name == null) { return defaultSchemaHsqlName; } if (name.equals(INFORMATION_SCHEMA)) { return INFORMATION_SCHEMA_HSQLNAME; } Schema schema = ((Schema) schemaMap.get(name)); if (schema == null) { throw Trace.error(Trace.INVALID_SCHEMA_NAME_NO_SUBCLASS, name); } return schema.name; } /** * Same as above, but return string */ String getSchemaName(String name) throws HsqlException { return getSchemaHsqlName(name).name; } /** * Iterator includes INFORMATION_SCHEMA */ Iterator fullSchemaNamesIterator() { return new WrapperIterator(new WrapperIterator(INFORMATION_SCHEMA), schemaMap.keySet().iterator()); } /** * is a schema read-only */ public boolean isSystemSchema(HsqlName schema) { return (INFORMATION_SCHEMA_HSQLNAME.equals(schema) || SYSTEM_SCHEMA_HSQLNAME.equals(schema)) ? true : false; } public Iterator tablesIterator(String schema) { Schema temp = (Schema) schemaMap.get(schema); return temp.tablesIterator(); } public Iterator allTablesIterator() { Iterator schemas = userSchemaNameIterator(); Iterator tables = new WrapperIterator(); while (schemas.hasNext()) { String name = (String) schemas.next(); Iterator t = tablesIterator(name); tables = new WrapperIterator(tables, t); } return tables; } Iterator sequenceIterator(String schema) { Schema temp = (Schema) schemaMap.get(schema); return temp.sequencesIterator(); } public Iterator allSequencesIterator() { Iterator it = schemaMap.values().iterator(); Iterator sequences = new WrapperIterator(); while (it.hasNext()) { Schema temp = (Schema) it.next(); sequences = new WrapperIterator(sequences, temp.sequencesIterator()); } return sequences; } /** * Returns an HsqlArrayList containing references to all non-system * tables and views. This includes all tables and views registered with * this Database. */ public HsqlArrayList getAllTables() { Iterator schemas = userSchemaNameIterator(); HsqlArrayList alltables = new HsqlArrayList(); while (schemas.hasNext()) { String name = (String) schemas.next(); HashMappedList current = getTables(name); alltables.addAll(current.values()); } return alltables; } public HashMappedList getTables(String schema) { Schema temp = (Schema) schemaMap.get(schema); return temp.tableList; } /** * @throws HsqlException if exists. */ void checkUserViewNotExists(Session session, String viewName, String schema) throws HsqlException { boolean exists = database.schemaManager.findUserTable(session, viewName, schema) != null; if (exists) { throw Trace.error(Trace.VIEW_ALREADY_EXISTS, viewName); } } /** * @throws HsqlException if exists */ void checkUserTableNotExists(Session session, String tableName, String schema) throws HsqlException { boolean exists = findUserTable(session, tableName, schema) != null; if (exists) { throw Trace.error(Trace.TABLE_ALREADY_EXISTS, tableName); } } /** * Retruns the specified user-defined table or view visible within the * context of the specified Session, or any system table of the given * name. It excludes any temp tables created in other Sessions. * Throws if the table does not exist in the context. */ public Table getTable(Session session, String name, String schema) throws HsqlException { Table t = findUserTable(session, name, schema); if (t == null) { if (!"INFORMATION_SCHEMA".equals(schema)) { throw Trace.error(Trace.TABLE_NOT_FOUND); } if (database.dbInfo != null) { t = database.dbInfo.getSystemTable(session, name); } } if (t == null) { throw Trace.error(Trace.TABLE_NOT_FOUND, name); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -