File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\DropDatabase.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import java.util.ArrayList;
10 import org.h2.command.CommandInterface;
11 import org.h2.engine.Database;
12 import org.h2.engine.DbObject;
13 import org.h2.engine.Role;
14 import org.h2.engine.Session;
15 import org.h2.engine.User;
16 import org.h2.schema.Schema;
17 import org.h2.schema.SchemaObject;
18 import org.h2.table.Table;
19 import org.h2.util.New;
20
21 /**
22 * This class represents the statement
23 * DROP ALL OBJECTS
24 */
25 public class DropDatabase extends DefineCommand {
26
27 private boolean dropAllObjects;
28 private boolean deleteFiles;
29
30 public DropDatabase(Session session) {
31 super(session);
32 }
33
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 // TODO local temp tables are not removed
50 for (Schema schema : db.getAllSchemas()) {
51 if (schema.canDrop()) {
52 db.removeDatabaseObject(session, schema);
53 }
54 }
55 ArrayList<Table> tables = db.getAllTablesAndViews(false);
56 for (Table t : tables) {
57 if (t.getName() != null && Table.VIEW.equals(t.getTableType())) {
58 db.removeSchemaObject(session, t);
59 }
60 }
61 for (Table t : tables) {
62 if (t.getName() != null && Table.TABLE_LINK.equals(t.getTableType())) {
63 db.removeSchemaObject(session, t);
64 }
65 }
66 for (Table t : tables) {
67 if (t.getName() != null && Table.TABLE.equals(t.getTableType()) && !t.isHidden()) {
68 db.removeSchemaObject(session, t);
69 }
70 }
71 session.findLocalTempTable(null);
72 ArrayList<SchemaObject> list = New.arrayList();
73 list.addAll(db.getAllSchemaObjects(DbObject.SEQUENCE));
74 // maybe constraints and triggers on system tables will be allowed in
75 // the future
76 list.addAll(db.getAllSchemaObjects(DbObject.CONSTRAINT));
77 list.addAll(db.getAllSchemaObjects(DbObject.TRIGGER));
78 list.addAll(db.getAllSchemaObjects(DbObject.CONSTANT));
79 list.addAll(db.getAllSchemaObjects(DbObject.FUNCTION_ALIAS));
80 for (SchemaObject obj : list) {
81 if (obj.isHidden()) {
82 continue;
83 }
84 db.removeSchemaObject(session, obj);
85 }
86 for (User user : db.getAllUsers()) {
87 if (user != session.getUser()) {
88 db.removeDatabaseObject(session, user);
89 }
90 }
91 for (Role role : db.getAllRoles()) {
92 String sql = role.getCreateSQL();
93 // the role PUBLIC must not be dropped
94 if (sql != null) {
95 db.removeDatabaseObject(session, role);
96 }
97 }
98 ArrayList<DbObject> dbObjects = New.arrayList();
99 dbObjects.addAll(db.getAllRights());
100 dbObjects.addAll(db.getAllAggregates());
101 dbObjects.addAll(db.getAllUserDataTypes());
102 for (DbObject obj : dbObjects) {
103 String sql = obj.getCreateSQL();
104 // the role PUBLIC must not be dropped
105 if (sql != null) {
106 db.removeDatabaseObject(session, obj);
107 }
108 }
109 }
110
111 public void setDropAllObjects(boolean b) {
112 this.dropAllObjects = b;
113 }
114
115 public void setDeleteFiles(boolean b) {
116 this.deleteFiles = b;
117 }
118
119 public int getType() {
120 return CommandInterface.DROP_ALL_OBJECTS;
121 }
122
123 }
124