1 5 package org.h2.command.ddl; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Database; 10 import org.h2.engine.DbObject; 11 import org.h2.engine.Session; 12 import org.h2.engine.User; 13 import org.h2.schema.Schema; 14 import org.h2.schema.SchemaObject; 15 import org.h2.table.Table; 16 import org.h2.util.ObjectArray; 17 18 public class DropDatabase extends DefineCommand { 19 20 private boolean dropAllObjects; 21 private boolean deleteFiles; 22 23 public DropDatabase(Session session) { 24 super(session); 25 } 26 27 public int update() throws SQLException { 28 if(dropAllObjects) { 29 dropAllObjects(); 30 } 31 if(deleteFiles) { 32 session.getDatabase().setDeleteFilesOnDisconnect(true); 33 } 34 return 0; 35 } 36 37 private void dropAllObjects() throws SQLException { 38 session.getUser().checkAdmin(); 39 session.commit(); 40 Database db = session.getDatabase(); 41 ObjectArray list; 42 list = db.getAllSchemas(); 44 for(int i=0; i<list.size(); i++) { 45 Schema schema = (Schema) list.get(i); 46 if(schema.canDrop()) { 47 db.removeDatabaseObject(session, schema); 48 } 49 } 50 list = db.getAllSchemaObjects(DbObject.TABLE_OR_VIEW); 51 for(int i=0; i<list.size(); i++) { 52 Table t = (Table) list.get(i); 53 if(t.getName() != null && Table.VIEW.equals(t.getTableType())) { 54 db.removeSchemaObject(session, t); 55 } 56 } 57 for(int i=0; i<list.size(); i++) { 58 Table t = (Table) list.get(i); 59 if(t.getName() != null && Table.TABLE_LINK.equals(t.getTableType())) { 60 db.removeSchemaObject(session, t); 61 } 62 } 63 for(int i=0; i<list.size(); i++) { 64 Table t = (Table) list.get(i); 65 if(t.getName() != null && Table.TABLE.equals(t.getTableType())) { 66 db.removeSchemaObject(session, t); 67 } 68 } 69 session.findLocalTempTable(null); 70 list = db.getAllSchemaObjects(DbObject.SEQUENCE); 71 list.addAll(db.getAllSchemaObjects(DbObject.CONSTRAINT)); 73 list.addAll(db.getAllSchemaObjects(DbObject.TRIGGER)); 74 for(int i=0; i<list.size(); i++) { 75 SchemaObject obj = (SchemaObject) list.get(i); 76 db.removeSchemaObject(session, obj); 77 } 78 list = db.getAllUsers(); 79 for(int i=0; i<list.size(); i++) { 80 User user = (User) list.get(i); 81 if(user != session.getUser()) { 82 db.removeDatabaseObject(session, user); 83 } 84 } 85 list = db.getAllRoles(); 86 list.addAll(db.getAllRights()); 87 list.addAll(db.getAllFunctionAliases()); 88 for(int i=0; i<list.size(); i++) { 89 DbObject obj = (DbObject) list.get(i); 90 db.removeDatabaseObject(session, obj); 91 } 92 } 93 94 public void setDropAllObjects(boolean b) { 95 this.dropAllObjects = b; 96 } 97 98 public void setDeleteFiles(boolean b) { 99 this.deleteFiles = b; 100 } 101 102 } 103 | Popular Tags |