1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.command.ddl; |
7 | |
8 | import java.util.ArrayList; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.engine.Database; |
11 | import org.h2.engine.DbObject; |
12 | import org.h2.engine.Role; |
13 | import org.h2.engine.Session; |
14 | import org.h2.engine.User; |
15 | import org.h2.schema.Schema; |
16 | import org.h2.schema.SchemaObject; |
17 | import org.h2.table.Table; |
18 | import org.h2.util.New; |
19 | |
20 | /** |
21 | * This class represents the statement |
22 | * DROP ALL OBJECTS |
23 | */ |
24 | public class DropDatabase extends DefineCommand { |
25 | |
26 | private boolean dropAllObjects; |
27 | private boolean deleteFiles; |
28 | |
29 | public DropDatabase(Session session) { |
30 | super(session); |
31 | } |
32 | |
33 | @Override |
34 | public int update() { |
35 | if (dropAllObjects) { |
36 | dropAllObjects(); |
37 | } |
38 | if (deleteFiles) { |
39 | session.getDatabase().setDeleteFilesOnDisconnect(true); |
40 | } |
41 | return 0; |
42 | } |
43 | |
44 | private void dropAllObjects() { |
45 | session.getUser().checkAdmin(); |
46 | session.commit(true); |
47 | Database db = session.getDatabase(); |
48 | db.lockMeta(session); |
49 | |
50 | // There can be dependencies between tables e.g. using computed columns, |
51 | // so we might need to loop over them multiple times. |
52 | boolean runLoopAgain; |
53 | do { |
54 | ArrayList<Table> tables = db.getAllTablesAndViews(false); |
55 | ArrayList<Table> toRemove = New.arrayList(); |
56 | for (Table t : tables) { |
57 | if (t.getName() != null && |
58 | Table.VIEW.equals(t.getTableType())) { |
59 | toRemove.add(t); |
60 | } |
61 | } |
62 | for (Table t : tables) { |
63 | if (t.getName() != null && |
64 | Table.TABLE_LINK.equals(t.getTableType())) { |
65 | toRemove.add(t); |
66 | } |
67 | } |
68 | for (Table t : tables) { |
69 | if (t.getName() != null && |
70 | Table.TABLE.equals(t.getTableType()) && |
71 | !t.isHidden()) { |
72 | toRemove.add(t); |
73 | } |
74 | } |
75 | for (Table t : tables) { |
76 | if (t.getName() != null && |
77 | Table.EXTERNAL_TABLE_ENGINE.equals(t.getTableType()) && |
78 | !t.isHidden()) { |
79 | toRemove.add(t); |
80 | } |
81 | } |
82 | runLoopAgain = false; |
83 | for (Table t : toRemove) { |
84 | if (t.getName() == null) { |
85 | // ignore |
86 | } else if (db.getDependentTable(t, t) == null) { |
87 | db.removeSchemaObject(session, t); |
88 | } else { |
89 | runLoopAgain = true; |
90 | } |
91 | } |
92 | } while (runLoopAgain); |
93 | |
94 | // TODO local temp tables are not removed |
95 | for (Schema schema : db.getAllSchemas()) { |
96 | if (schema.canDrop()) { |
97 | db.removeDatabaseObject(session, schema); |
98 | } |
99 | } |
100 | session.findLocalTempTable(null); |
101 | ArrayList<SchemaObject> list = New.arrayList(); |
102 | list.addAll(db.getAllSchemaObjects(DbObject.SEQUENCE)); |
103 | // maybe constraints and triggers on system tables will be allowed in |
104 | // the future |
105 | list.addAll(db.getAllSchemaObjects(DbObject.CONSTRAINT)); |
106 | list.addAll(db.getAllSchemaObjects(DbObject.TRIGGER)); |
107 | list.addAll(db.getAllSchemaObjects(DbObject.CONSTANT)); |
108 | list.addAll(db.getAllSchemaObjects(DbObject.FUNCTION_ALIAS)); |
109 | for (SchemaObject obj : list) { |
110 | if (obj.isHidden()) { |
111 | continue; |
112 | } |
113 | db.removeSchemaObject(session, obj); |
114 | } |
115 | for (User user : db.getAllUsers()) { |
116 | if (user != session.getUser()) { |
117 | db.removeDatabaseObject(session, user); |
118 | } |
119 | } |
120 | for (Role role : db.getAllRoles()) { |
121 | String sql = role.getCreateSQL(); |
122 | // the role PUBLIC must not be dropped |
123 | if (sql != null) { |
124 | db.removeDatabaseObject(session, role); |
125 | } |
126 | } |
127 | ArrayList<DbObject> dbObjects = New.arrayList(); |
128 | dbObjects.addAll(db.getAllRights()); |
129 | dbObjects.addAll(db.getAllAggregates()); |
130 | dbObjects.addAll(db.getAllUserDataTypes()); |
131 | for (DbObject obj : dbObjects) { |
132 | String sql = obj.getCreateSQL(); |
133 | // the role PUBLIC must not be dropped |
134 | if (sql != null) { |
135 | db.removeDatabaseObject(session, obj); |
136 | } |
137 | } |
138 | } |
139 | |
140 | public void setDropAllObjects(boolean b) { |
141 | this.dropAllObjects = b; |
142 | } |
143 | |
144 | public void setDeleteFiles(boolean b) { |
145 | this.deleteFiles = b; |
146 | } |
147 | |
148 | @Override |
149 | public int getType() { |
150 | return CommandInterface.DROP_ALL_OBJECTS; |
151 | } |
152 | |
153 | } |