📄 schemamanager.java
字号:
return t; } /** * Retruns the specified user-defined table or view visible within the * context of the specified Session. It excludes system tables and * any temp tables created in different Sessions. * Throws if the table does not exist in the context. */ public Table getUserTable(Session session, String name, String schema) throws HsqlException { Table t = findUserTable(session, name, schema); if (t == null) { throw Trace.error(Trace.TABLE_NOT_FOUND, name); } return t; } /** * Retruns the specified user-defined table or view visible within the * context of the specified schema. It excludes system tables. * Returns null if the table does not exist in the context. */ Table findUserTable(Session session, String name, String schemaName) { Schema schema = (Schema) schemaMap.get(schemaName); if (schema == null) { return null; } for (int i = 0, tsize = schema.tableList.size(); i < tsize; i++) { Table t = (Table) schema.tableList.get(i); if (t.equals(session, name)) { return t; } } return null; } /** * Registers the specified table or view with this Database. */ void linkTable(Table t) { Schema schema = (Schema) schemaMap.get(t.getSchemaName()); schema.tableList.add(t.getName().name, t); } NumberSequence getSequence(String name, String schemaName) throws HsqlException { NumberSequence sequence = findSequence(name, schemaName); if (sequence == null) { throw Trace.error(Trace.SEQUENCE_NOT_FOUND, name); } return sequence; } public NumberSequence findSequence(String name, String schemaName) throws HsqlException { Schema schema = (Schema) schemaMap.get(schemaName); NumberSequence sequence = schema.sequenceManager.getSequence(name); return sequence; } /** * Returns the table in that has an index with the given name and schema. */ Table findUserTableForIndex(Session session, String name, String schemaName) { Schema schema = (Schema) schemaMap.get(schemaName); HsqlName tablename = schema.indexNameList.getOwner(name); if (tablename == null) { return null; } return findUserTable(session, tablename.name, schemaName); } /** * Returns index of a table or view in the HsqlArrayList that * contains the table objects for this Database. * * @param table the Table object * @return the index of the specified table or view, or -1 if not found */ int getTableIndex(Table table) { Schema schema = (Schema) schemaMap.get(table.getSchemaName()); for (int i = 0, tsize = schema.tableList.size(); i < tsize; i++) { Table t = (Table) schema.tableList.get(i); if (t == table) { return i; } } return -1; } /** * Drops the index with the specified name. */ void dropIndex(Session session, String indexname, String schema, boolean ifExists) throws HsqlException { Table t = findUserTableForIndex(session, indexname, schema); if (t == null) { if (ifExists) { return; } else { throw Trace.error(Trace.INDEX_NOT_FOUND, indexname); } } t.checkDropIndex(indexname, null, false); session.commit(); session.setScripting(true); TableWorks tw = new TableWorks(session, t); tw.dropIndex(indexname); } //------------ name management /** * Checks if a Trigger with given name either exists or does not, based on * the value of the argument, yes. */ void checkTriggerExists(String name, String schemaName, boolean yes) throws HsqlException { Schema schema = (Schema) schemaMap.get(schemaName); boolean exists = schema.triggerNameList.containsName(name); if (exists != yes) { int code = yes ? Trace.TRIGGER_NOT_FOUND : Trace.TRIGGER_ALREADY_EXISTS; throw Trace.error(code, name); } } void registerTriggerName(String name, HsqlName tableName) throws HsqlException { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.triggerNameList.addName(name, tableName, Trace.TRIGGER_ALREADY_EXISTS); } void checkIndexExists(String name, String schemaName, boolean yes) throws HsqlException { Schema schema = (Schema) schemaMap.get(schemaName); boolean exists = schema.indexNameList.containsName(name); if (exists != yes) { int code = yes ? Trace.INDEX_NOT_FOUND : Trace.INDEX_ALREADY_EXISTS; throw Trace.error(code, name); } } void registerIndexName(String name, HsqlName tableName) throws HsqlException { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.indexNameList.addName(name, tableName, Trace.INDEX_ALREADY_EXISTS); } void removeIndexName(String name, HsqlName tableName) throws HsqlException { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.indexNameList.removeName(name); } void removeIndexNames(HsqlName tableName) { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.indexNameList.removeOwner(tableName); } void renameIndex(String oldName, String newName, HsqlName tableName) throws HsqlException { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.indexNameList.rename(oldName, newName, Trace.INDEX_ALREADY_EXISTS); } void checkConstraintExists(String name, String schemaName, boolean yes) throws HsqlException { Schema schema = (Schema) schemaMap.get(schemaName); boolean exists = schema.constraintNameList.containsName(name); if (exists != yes) { int code = yes ? Trace.CONSTRAINT_NOT_FOUND : Trace.CONSTRAINT_ALREADY_EXISTS; throw Trace.error(code, name); } } void registerConstraintName(String name, HsqlName tableName) throws HsqlException { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.constraintNameList.addName(name, tableName, Trace.CONSTRAINT_ALREADY_EXISTS); } void removeConstraintName(String name, HsqlName tableName) throws HsqlException { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.constraintNameList.removeName(name); } void removeConstraintNames(HsqlName tableName) { Schema schema = (Schema) schemaMap.get(tableName.schema.name); schema.constraintNameList.removeOwner(tableName); } // sequence NumberSequence createSequence(HsqlName hsqlname, long start, long increment, int type) throws HsqlException { Schema schema = (Schema) schemaMap.get(hsqlname.schema.name); return schema.sequenceManager.createSequence(hsqlname, start, increment, type); } void dropSequence(NumberSequence sequence) throws HsqlException { Schema schema = (Schema) schemaMap.get(sequence.getSchemaName()); schema.sequenceManager.dropSequence(sequence.getName().name); } void logSequences(Session session, Logger logger) throws HsqlException { for (int i = 0, size = schemaMap.size(); i < size; i++) { Schema schema = (Schema) schemaMap.get(i); schema.sequenceManager.logSequences(session, logger); } } /** * Clear copies of a temporary table from all sessions apart from one. */ void clearTempTables(Session exclude, Table table) { Session[] sessions = database.sessionManager.getAllSessions(); Index[] indexes = table.getIndexes(); for (int i = 0; i < sessions.length; i++) { if (sessions[i] != exclude) { for (int j = 0; j < indexes.length; j++) { sessions[i].dropIndex(indexes[j].getName(), false); } } } } /** * Drops the specified user-defined view or table from this Database * object. <p> * * The process of dropping a table or view includes: * <OL> * <LI> checking that the specified Session's currently connected User * has the right to perform this operation and refusing to proceed if * not by throwing. * <LI> checking for referential constraints that conflict with this * operation and refusing to proceed if they exist by throwing.</LI> * * <LI> removing the specified Table from this Database object. * <LI> removing any exported foreign keys Constraint objects held by * any tables referenced by the table to be dropped. This is especially * important so that the dropped Table ceases to be referenced, * eventually allowing its full garbage collection. * <LI> * </OL> * <p> * * @param name of the table or view to drop * @param ifExists if true and if the Table to drop does not exist, fail * silently, else throw * @param isView true if the name argument refers to a View * @param session the connected context in which to perform this * operation * @throws HsqlException if any of the checks listed above fail */ void dropTable(Session session, String name, String schemaName, boolean ifExists, boolean isView, boolean cascade) throws HsqlException { Table table = null; int dropIndex = -1; Schema schema = (Schema) schemaMap.get(schemaName); for (int i = 0; i < schema.tableList.size(); i++) { table = (Table) schema.tableList.get(i); if (table.equals(session, name) && isView == table.isView()) { dropIndex = i; break; } else { table = null; } } if (dropIndex == -1) { if (ifExists) { return; } else { throw Trace.error(isView ? Trace.VIEW_NOT_FOUND : Trace.TABLE_NOT_FOUND, name); } } session.checkAdmin(); session.checkDDLWrite(); dropTable(table, cascade); session.setScripting(!table.isTemp()); session.commit(); } void dropTable(Table table, boolean cascade) throws HsqlException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -