Debian Codegraph @

src/main/org/h2/command/ddl/DropDatabase.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://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 java.util.Collection;
10
11 import org.h2.command.CommandInterface;
12 import org.h2.engine.Database;
13 import org.h2.engine.DbObject;
14 import org.h2.engine.Right;
15 import org.h2.engine.RightOwner;
16 import org.h2.engine.Role;
17 import org.h2.engine.SessionLocal;
18 import org.h2.engine.User;
19 import org.h2.schema.Schema;
20 import org.h2.schema.SchemaObject;
21 import org.h2.schema.Sequence;
22 import org.h2.table.Table;
23 import org.h2.table.TableType;
24 import org.h2.value.ValueNull;
25
26 /**
27 * This class represents the statement
28 * DROP ALL OBJECTS
29 */
30 public class DropDatabase extends DefineCommand {
31
32 private boolean dropAllObjects;
33 private boolean deleteFiles;
34
35 public DropDatabase(SessionLocal session) {
36 super(session);
37 }
38
39 @Override
40 public long update() {
41 if (dropAllObjects) {
42 dropAllObjects();
43 }
44 if (deleteFiles) {
45 getDatabase().setDeleteFilesOnDisconnect(true);
46 }
47 return 0;
48 }
49
50 private void dropAllObjects() {
51 User user = session.getUser();
52 user.checkAdmin();
53 Database db = getDatabase();
54 db.lockMeta(session);
55
56 // There can be dependencies between tables e.g. using computed columns,
57 // so we might need to loop over them multiple times.
58 boolean runLoopAgain;
59 do {
60 ArrayList<Table> tables = db.getAllTablesAndViews();
61 ArrayList<Table> toRemove = new ArrayList<>(tables.size());
62 for (Table t : tables) {
63 if (t.getName() != null &&
64 TableType.VIEW == t.getTableType()) {
65 toRemove.add(t);
66 }
67 }
68 for (Table t : tables) {
69 if (t.getName() != null &&
70 TableType.TABLE_LINK == t.getTableType()) {
71 toRemove.add(t);
72 }
73 }
74 for (Table t : tables) {
75 if (t.getName() != null &&
76 TableType.TABLE == t.getTableType() &&
77 !t.isHidden()) {
78 toRemove.add(t);
79 }
80 }
81 for (Table t : tables) {
82 if (t.getName() != null &&
83 TableType.EXTERNAL_TABLE_ENGINE == t.getTableType() &&
84 !t.isHidden()) {
85 toRemove.add(t);
86 }
87 }
88 runLoopAgain = false;
89 for (Table t : toRemove) {
90 if (t.getName() == null) {
91 // ignore, already dead
92 } else if (db.getDependentTable(t, t) == null) {
93 db.removeSchemaObject(session, t);
94 } else {
95 runLoopAgain = true;
96 }
97 }
98 } while (runLoopAgain);
99
100 // TODO session-local temp tables are not removed
101 Collection<Schema> schemas = db.getAllSchemasNoMeta();
102 for (Schema schema : schemas) {
103 if (schema.canDrop()) {
104 db.removeDatabaseObject(session, schema);
105 }
106 }
107 ArrayList<SchemaObject> list = new ArrayList<>();
108 for (Schema schema : schemas) {
109 for (Sequence sequence : schema.getAllSequences()) {
110 // ignore these. the ones we want to drop will get dropped when we
111 // drop their associated tables, and we will ignore the problematic
112 // ones that belong to session-local temp tables.
113 if (!sequence.getBelongsToTable()) {
114 list.add(sequence);
115 }
116 }
117 }
118 // maybe constraints and triggers on system tables will be allowed in
119 // the future
120 addAll(schemas, DbObject.CONSTRAINT, list);
121 addAll(schemas, DbObject.TRIGGER, list);
122 addAll(schemas, DbObject.CONSTANT, list);
123 // Function aliases and aggregates are stored together
124 addAll(schemas, DbObject.FUNCTION_ALIAS, list);
125 addAll(schemas, DbObject.DOMAIN, list);
126 for (SchemaObject obj : list) {
127 if (!obj.getSchema().isValid() || obj.isHidden()) {
128 continue;
129 }
130 db.removeSchemaObject(session, obj);
131 }
132 Role publicRole = db.getPublicRole();
133 for (RightOwner rightOwner : db.getAllUsersAndRoles()) {
134 if (rightOwner != user && rightOwner != publicRole) {
135 db.removeDatabaseObject(session, rightOwner);
136 }
137 }
138 for (Right right : db.getAllRights()) {
139 db.removeDatabaseObject(session, right);
140 }
141 for (SessionLocal s : db.getSessions(false)) {
142 s.setLastIdentity(ValueNull.INSTANCE);
143 }
144 }
145
146 private static void addAll(Collection<Schema> schemas, int type, ArrayList<SchemaObject> list) {
147 for (Schema schema : schemas) {
148 schema.getAll(type, list);
149 }
150 }
151
152 public void setDropAllObjects(boolean b) {
153 this.dropAllObjects = b;
154 }
155
156 public void setDeleteFiles(boolean b) {
157 this.deleteFiles = b;
158 }
159
160 @Override
161 public int getType() {
162 return CommandInterface.DROP_ALL_OBJECTS;
163 }
164
165 }