Search in sources :

Example 21 with DbObject

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);
        }
    }
}
Also used : Role(org.h2.engine.Role) SchemaObject(org.h2.schema.SchemaObject) Table(org.h2.table.Table) User(org.h2.engine.User) DbObject(org.h2.engine.DbObject) Schema(org.h2.schema.Schema) Database(org.h2.engine.Database)

Example 22 with DbObject

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;
}
Also used : Constraint(org.h2.constraint.Constraint)

Aggregations

DbObject (org.h2.engine.DbObject)12 Constraint (org.h2.constraint.Constraint)11 Table (org.h2.table.Table)9 Database (org.h2.engine.Database)8 Right (org.h2.engine.Right)5 Index (org.h2.index.Index)5 Sequence (org.h2.schema.Sequence)5 Schema (org.h2.schema.Schema)4 SchemaObject (org.h2.schema.SchemaObject)4 Column (org.h2.table.Column)4 TableView (org.h2.table.TableView)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Role (org.h2.engine.Role)3 User (org.h2.engine.User)3 DbException (org.h2.message.DbException)3 TriggerObject (org.h2.schema.TriggerObject)3 IOException (java.io.IOException)2 ConstraintReferential (org.h2.constraint.ConstraintReferential)2 Comment (org.h2.engine.Comment)2