use of org.h2.engine.DbObject in project h2database by h2database.
the class DropDatabase method dropAllObjects.
private void dropAllObjects() {
session.getUser().checkAdmin();
session.commit(true);
Database db = session.getDatabase();
db.lockMeta(session);
// There can be dependencies between tables e.g. using computed columns,
// so we might need to loop over them multiple times.
boolean runLoopAgain;
do {
ArrayList<Table> tables = db.getAllTablesAndViews(false);
ArrayList<Table> toRemove = New.arrayList();
for (Table t : tables) {
if (t.getName() != null && TableType.VIEW == t.getTableType()) {
toRemove.add(t);
}
}
for (Table t : tables) {
if (t.getName() != null && TableType.TABLE_LINK == t.getTableType()) {
toRemove.add(t);
}
}
for (Table t : tables) {
if (t.getName() != null && TableType.TABLE == t.getTableType() && !t.isHidden()) {
toRemove.add(t);
}
}
for (Table t : tables) {
if (t.getName() != null && TableType.EXTERNAL_TABLE_ENGINE == t.getTableType() && !t.isHidden()) {
toRemove.add(t);
}
}
runLoopAgain = false;
for (Table t : toRemove) {
if (t.getName() == null) {
// ignore, already dead
} else if (db.getDependentTable(t, t) == null) {
db.removeSchemaObject(session, t);
} else {
runLoopAgain = true;
}
}
} while (runLoopAgain);
// TODO session-local temp tables are not removed
for (Schema schema : db.getAllSchemas()) {
if (schema.canDrop()) {
db.removeDatabaseObject(session, schema);
}
}
ArrayList<SchemaObject> list = New.arrayList();
for (SchemaObject obj : db.getAllSchemaObjects(DbObject.SEQUENCE)) {
// ones that belong to session-local temp tables.
if (!((Sequence) obj).getBelongsToTable()) {
list.add(obj);
}
}
// maybe constraints and triggers on system tables will be allowed in
// the future
list.addAll(db.getAllSchemaObjects(DbObject.CONSTRAINT));
list.addAll(db.getAllSchemaObjects(DbObject.TRIGGER));
list.addAll(db.getAllSchemaObjects(DbObject.CONSTANT));
list.addAll(db.getAllSchemaObjects(DbObject.FUNCTION_ALIAS));
for (SchemaObject obj : list) {
if (obj.isHidden()) {
continue;
}
db.removeSchemaObject(session, obj);
}
for (User user : db.getAllUsers()) {
if (user != session.getUser()) {
db.removeDatabaseObject(session, user);
}
}
for (Role role : db.getAllRoles()) {
String sql = role.getCreateSQL();
// the role PUBLIC must not be dropped
if (sql != null) {
db.removeDatabaseObject(session, role);
}
}
ArrayList<DbObject> dbObjects = New.arrayList();
dbObjects.addAll(db.getAllRights());
dbObjects.addAll(db.getAllAggregates());
dbObjects.addAll(db.getAllUserDataTypes());
for (DbObject obj : dbObjects) {
String sql = obj.getCreateSQL();
// the role PUBLIC must not be dropped
if (sql != null) {
db.removeDatabaseObject(session, obj);
}
}
}
use of org.h2.engine.DbObject in project h2database by h2database.
the class Schema method getUniqueName.
private String getUniqueName(DbObject obj, Map<String, ? extends SchemaObject> map, String prefix) {
String hash = StringUtils.toUpperEnglish(Integer.toHexString(obj.getName().hashCode()));
String name = null;
synchronized (temporaryUniqueNames) {
for (int i = 1, len = hash.length(); i < len; i++) {
name = prefix + hash.substring(0, i);
if (!map.containsKey(name) && !temporaryUniqueNames.contains(name)) {
break;
}
name = null;
}
if (name == null) {
prefix = prefix + hash + "_";
for (int i = 0; ; i++) {
name = prefix + i;
if (!map.containsKey(name) && !temporaryUniqueNames.contains(name)) {
break;
}
}
}
temporaryUniqueNames.add(name);
}
return name;
}
Aggregations