📄 schemamanager.java
字号:
if (table.isView()) { checkCascadeDropViews((View) table, cascade); } else { checkCascadeDropReferenced(table, cascade); checkCascadeDropViews(table, cascade); } // get it again as table object might be a different one table = (Table) schema.tableList.remove(dropIndex); removeExportedKeys(table); database.getUserManager().removeDbObject(table.getName()); schema.triggerNameList.removeOwner(table.tableName); schema.indexNameList.removeOwner(table.tableName); schema.constraintNameList.removeOwner(table.tableName); table.dropTriggers(); table.drop(); } void setTable(int index, Table table) { Schema schema = (Schema) schemaMap.get(table.getSchemaName()); schema.tableList.set(index, table.getName().name, table); } void renameTable(Session session, Table table, String newName, boolean isQuoted) throws HsqlException { Schema schema = (Schema) schemaMap.get(table.tableName.schema.name); int i = schema.tableList.getIndex(table.tableName.name); checkCascadeDropViews(table, false); table.rename(session, newName, isQuoted); schema.tableList.setKey(i, newName); } /** * Throws if the table is referenced in a foreign key constraint. */ private void checkCascadeDropReferenced(Table table, boolean cascade) throws HsqlException { Constraint[] constraints = table.getConstraints(); Constraint currentConstraint = null; Table refTable = null; boolean isSelfRef = false; for (int i = constraints.length - 1; i >= 0; i--) { currentConstraint = constraints[i]; if (currentConstraint.getType() != Constraint.MAIN) { continue; } refTable = currentConstraint.getRef(); isSelfRef = (refTable != null && table.equals(refTable)); if (isSelfRef) { continue; } if (cascade) { Constraint refConst = refTable.getConstraint(currentConstraint.getFkName()); TableWorks tw = new TableWorks(null, refTable); tw.dropFKConstraint(refConst); constraints = table.constraintList; i = constraints.length; } else { throw Trace.error(Trace.TABLE_REFERENCED_CONSTRAINT, Trace.Database_dropTable, new Object[] { currentConstraint.getName().name, refTable.getName().name }); } } } /** * Throws if the view is referenced in a view. */ void checkCascadeDropViews(View view, boolean cascade) throws HsqlException { View[] views = getViewsWithView(view); if (views != null) { if (cascade) { // drop from end to avoid repeat drop for (int i = views.length - 1; i >= 0; i--) { dropTable(views[i], cascade); } } else { throw Trace.error(Trace.TABLE_REFERENCED_VIEW, views[0].getName().name); } } } /** * Throws if the table is referenced in a view. */ void checkCascadeDropViews(Table table, boolean cascade) throws HsqlException { View[] views = getViewsWithTable(table, null); if (views != null) { if (cascade) { // drop from end to avoid repeat drop for (int i = views.length - 1; i >= 0; i--) { dropTable(views[i], cascade); } } else { throw Trace.error(Trace.TABLE_REFERENCED_VIEW, views[0].getName().name); } } } /** * Throws if the sequence is referenced in a view. */ void checkCascadeDropViews(NumberSequence sequence, boolean cascade) throws HsqlException { View[] views = getViewsWithSequence(sequence); if (views != null) { if (cascade) { // drop from end to avoid repeat drop for (int i = views.length - 1; i >= 0; i--) { dropTable(views[i], cascade); } } else { throw Trace.error(Trace.SEQUENCE_REFERENCED_BY_VIEW, views[0].getName().name); } } } /** * Throws if the column is referenced in a view. */ void checkColumnIsInView(Table table, String column) throws HsqlException { View[] views = getViewsWithTable(table, column); if (views != null) { throw Trace.error(Trace.COLUMN_IS_REFERENCED, views[0].getName().name); } } /** * Returns an array of views that reference another view. */ private View[] getViewsWithView(View view) { HsqlArrayList list = null; Schema schema = (Schema) schemaMap.get(view.getSchemaName()); for (int i = 0; i < schema.tableList.size(); i++) { Table t = (Table) schema.tableList.get(i); if (t.isView()) { boolean found = ((View) t).hasView(view); if (found) { if (list == null) { list = new HsqlArrayList(); } list.add(t); } } } return list == null ? null : (View[]) list.toArray(new View[list.size()]); } /** * Returns an array of views that reference the specified table or * the specified column if column parameter is not null. */ private View[] getViewsWithTable(Table table, String column) { HsqlArrayList list = null; Iterator it = allTablesIterator(); while (it.hasNext()) { Table t = (Table) it.next(); if (t.isView()) { boolean found = column == null ? ((View) t).hasTable(table) : ((View) t).hasColumn(table, column); if (found) { if (list == null) { list = new HsqlArrayList(); } list.add(t); } } } return list == null ? null : (View[]) list.toArray(new View[list.size()]); } /** * Returns an array of views that reference a sequence. */ View[] getViewsWithSequence(NumberSequence sequence) { HsqlArrayList list = null; Iterator it = allTablesIterator(); while (it.hasNext()) { Table t = (Table) it.next(); if (t.isView()) { boolean found = ((View) t).hasSequence(sequence); if (found) { if (list == null) { list = new HsqlArrayList(); } list.add(t); } } } return list == null ? null : (View[]) list.toArray(new View[list.size()]); } /** * After addition or removal of columns and indexes all views that * reference the table should be recompiled. */ void recompileViews(Table table) throws HsqlException { View[] viewlist = getViewsWithTable(table, null); if (viewlist != null) { for (int i = 0; i < viewlist.length; i++) { String schema = viewlist[i].compileTimeSchema.name; if (!schemaExists(schema)) { schema = null; } Session session = database.sessionManager.getSysSession(schema, false); viewlist[i].compile(session); } } } /** * Removes any foreign key Constraint objects (exported keys) held by any * tables referenced by the specified table. <p> * * This method is called as the last step of a successful call to * dropTable() in order to ensure that the dropped Table ceases to be * referenced when enforcing referential integrity. * * @param toDrop The table to which other tables may be holding keys. * This is a table that is in the process of being dropped. */ void removeExportedKeys(Table toDrop) { Schema schema = (Schema) schemaMap.get(toDrop.getSchemaName()); for (int i = 0; i < schema.tableList.size(); i++) { Table table = (Table) schema.tableList.get(i); for (int j = table.constraintList.length - 1; j >= 0; j--) { Table refTable = table.constraintList[j].getRef(); if (toDrop == refTable) { table.constraintList = (Constraint[]) ArrayUtil.toAdjustedArray( table.constraintList, null, j, -1); } } } } /** * Drops a trigger with the specified name in the given context. */ void dropTrigger(Session session, String name, String schemaName) throws HsqlException { Schema schema = (Schema) schemaMap.get(schemaName); boolean found = schema.triggerNameList.containsName(name); Trace.check(found, Trace.TRIGGER_NOT_FOUND, name); HsqlName tableName = (HsqlName) schema.triggerNameList.removeName(name); Table t = this.findUserTable(session, tableName.name, schemaName); t.dropTrigger(name); session.setScripting(true); } public class Schema { HsqlName name; DatabaseObjectNames triggerNameList; DatabaseObjectNames constraintNameList; DatabaseObjectNames indexNameList; SequenceManager sequenceManager; HashMappedList tableList; Schema(String name, boolean isquoted) { this.name = database.nameManager.newHsqlName(name, isquoted); triggerNameList = new DatabaseObjectNames(); indexNameList = new DatabaseObjectNames(); constraintNameList = new DatabaseObjectNames(); sequenceManager = new SequenceManager(); tableList = new HashMappedList(); } boolean isEmpty() { return sequenceManager.sequenceMap.isEmpty() && tableList.isEmpty(); } Iterator tablesIterator() { return tableList.values().iterator(); } Iterator sequencesIterator() { return sequenceManager.sequenceMap.values().iterator(); } void clearStructures() { if (tableList != null) { for (int i = 0; i < tableList.size(); i++) { Table table = (Table) tableList.get(i); table.dropTriggers(); } } triggerNameList = null; indexNameList = null; constraintNameList = null; sequenceManager = null; tableList = null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -