001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.command.ddl;
007:
008: import java.sql.SQLException;
009: import org.h2.engine.Database;
010: import org.h2.engine.DbObject;
011: import org.h2.engine.Role;
012: import org.h2.engine.Session;
013: import org.h2.engine.User;
014: import org.h2.schema.Schema;
015: import org.h2.schema.SchemaObject;
016: import org.h2.table.Table;
017: import org.h2.util.ObjectArray;
018:
019: /**
020: * This class represents the statement
021: * DROP ALL OBJECTS
022: */
023: public class DropDatabase extends DefineCommand {
024:
025: private boolean dropAllObjects;
026: private boolean deleteFiles;
027:
028: public DropDatabase(Session session) {
029: super (session);
030: }
031:
032: public int update() throws SQLException {
033: if (dropAllObjects) {
034: dropAllObjects();
035: }
036: if (deleteFiles) {
037: session.getDatabase().setDeleteFilesOnDisconnect(true);
038: }
039: return 0;
040: }
041:
042: private void dropAllObjects() throws SQLException {
043: session.getUser().checkAdmin();
044: session.commit(true);
045: Database db = session.getDatabase();
046: ObjectArray list;
047: // TODO local temp tables are not removed
048: list = db.getAllSchemas();
049: for (int i = 0; i < list.size(); i++) {
050: Schema schema = (Schema) list.get(i);
051: if (schema.canDrop()) {
052: db.removeDatabaseObject(session, schema);
053: }
054: }
055: list = db.getAllSchemaObjects(DbObject.TABLE_OR_VIEW);
056: for (int i = 0; i < list.size(); i++) {
057: Table t = (Table) list.get(i);
058: if (t.getName() != null
059: && Table.VIEW.equals(t.getTableType())) {
060: db.removeSchemaObject(session, t);
061: }
062: }
063: for (int i = 0; i < list.size(); i++) {
064: Table t = (Table) list.get(i);
065: if (t.getName() != null
066: && Table.TABLE_LINK.equals(t.getTableType())) {
067: db.removeSchemaObject(session, t);
068: }
069: }
070: for (int i = 0; i < list.size(); i++) {
071: Table t = (Table) list.get(i);
072: if (t.getName() != null
073: && Table.TABLE.equals(t.getTableType())) {
074: db.removeSchemaObject(session, t);
075: }
076: }
077: session.findLocalTempTable(null);
078: list = db.getAllSchemaObjects(DbObject.SEQUENCE);
079: // maybe constraints and triggers on system tables will be allowed in
080: // the future
081: list.addAll(db.getAllSchemaObjects(DbObject.CONSTRAINT));
082: list.addAll(db.getAllSchemaObjects(DbObject.TRIGGER));
083: list.addAll(db.getAllSchemaObjects(DbObject.CONSTANT));
084: for (int i = 0; i < list.size(); i++) {
085: SchemaObject obj = (SchemaObject) list.get(i);
086: db.removeSchemaObject(session, obj);
087: }
088: list = db.getAllUsers();
089: for (int i = 0; i < list.size(); i++) {
090: User user = (User) list.get(i);
091: if (user != session.getUser()) {
092: db.removeDatabaseObject(session, user);
093: }
094: }
095: list = db.getAllRoles();
096: for (int i = 0; i < list.size(); i++) {
097: Role role = (Role) list.get(i);
098: String sql = role.getCreateSQL();
099: // the role PUBLIC must not be dropped
100: if (sql != null) {
101: db.removeDatabaseObject(session, role);
102: }
103: }
104: list = db.getAllRights();
105: list.addAll(db.getAllFunctionAliases());
106: list.addAll(db.getAllAggregates());
107: list.addAll(db.getAllUserDataTypes());
108: for (int i = 0; i < list.size(); i++) {
109: DbObject obj = (DbObject) list.get(i);
110: String sql = obj.getCreateSQL();
111: // the role PUBLIC must not be dropped
112: if (sql != null) {
113: db.removeDatabaseObject(session, obj);
114: }
115: }
116: }
117:
118: public void setDropAllObjects(boolean b) {
119: this .dropAllObjects = b;
120: }
121:
122: public void setDeleteFiles(boolean b) {
123: this.deleteFiles = b;
124: }
125:
126: }
|