Search in sources :

Example 36 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class Parser method readSequence.

private Sequence readSequence() {
    // same algorithm as readTableOrView
    String sequenceName = readIdentifierWithSchema(null);
    if (schemaName != null) {
        return getSchema().getSequence(sequenceName);
    }
    Sequence sequence = findSequence(session.getCurrentSchemaName(), sequenceName);
    if (sequence != null) {
        return sequence;
    }
    throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName);
}
Also used : ValueString(org.h2.value.ValueString) DropSequence(org.h2.command.ddl.DropSequence) CreateSequence(org.h2.command.ddl.CreateSequence) Sequence(org.h2.schema.Sequence) AlterSequence(org.h2.command.dml.AlterSequence)

Example 37 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class Parser method parseDrop.

private Prepared parseDrop() {
    if (readIf("TABLE")) {
        boolean ifExists = readIfExists(false);
        String tableName = readIdentifierWithSchema();
        DropTable command = new DropTable(session, getSchema());
        command.setTableName(tableName);
        while (readIf(",")) {
            tableName = readIdentifierWithSchema();
            DropTable next = new DropTable(session, getSchema());
            next.setTableName(tableName);
            command.addNextDropTable(next);
        }
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
            readIf("CONSTRAINTS");
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        } else if (readIf("IGNORE")) {
            command.setDropAction(ConstraintActionType.SET_DEFAULT);
        }
        return command;
    } else if (readIf("INDEX")) {
        boolean ifExists = readIfExists(false);
        String indexName = readIdentifierWithSchema();
        DropIndex command = new DropIndex(session, getSchema());
        command.setIndexName(indexName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        // Support for MySQL: DROP INDEX index_name ON tbl_name
        if (readIf("ON")) {
            readIdentifierWithSchema();
        }
        return command;
    } else if (readIf("USER")) {
        boolean ifExists = readIfExists(false);
        DropUser command = new DropUser(session);
        command.setUserName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        readIf("CASCADE");
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SEQUENCE")) {
        boolean ifExists = readIfExists(false);
        String sequenceName = readIdentifierWithSchema();
        DropSequence command = new DropSequence(session, getSchema());
        command.setSequenceName(sequenceName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("CONSTANT")) {
        boolean ifExists = readIfExists(false);
        String constantName = readIdentifierWithSchema();
        DropConstant command = new DropConstant(session, getSchema());
        command.setConstantName(constantName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("TRIGGER")) {
        boolean ifExists = readIfExists(false);
        String triggerName = readIdentifierWithSchema();
        DropTrigger command = new DropTrigger(session, getSchema());
        command.setTriggerName(triggerName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("VIEW")) {
        boolean ifExists = readIfExists(false);
        String viewName = readIdentifierWithSchema();
        DropView command = new DropView(session, getSchema());
        command.setViewName(viewName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        ConstraintActionType dropAction = parseCascadeOrRestrict();
        if (dropAction != null) {
            command.setDropAction(dropAction);
        }
        return command;
    } else if (readIf("ROLE")) {
        boolean ifExists = readIfExists(false);
        DropRole command = new DropRole(session);
        command.setRoleName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("ALIAS")) {
        boolean ifExists = readIfExists(false);
        String aliasName = readIdentifierWithSchema();
        DropFunctionAlias command = new DropFunctionAlias(session, getSchema());
        command.setAliasName(aliasName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SCHEMA")) {
        boolean ifExists = readIfExists(false);
        DropSchema command = new DropSchema(session);
        command.setSchemaName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        }
        return command;
    } else if (readIf("ALL")) {
        read("OBJECTS");
        DropDatabase command = new DropDatabase(session);
        command.setDropAllObjects(true);
        if (readIf("DELETE")) {
            read("FILES");
            command.setDeleteFiles(true);
        }
        return command;
    } else if (readIf("DOMAIN")) {
        return parseDropUserDataType();
    } else if (readIf("TYPE")) {
        return parseDropUserDataType();
    } else if (readIf("DATATYPE")) {
        return parseDropUserDataType();
    } else if (readIf("AGGREGATE")) {
        return parseDropAggregate();
    } else if (readIf("SYNONYM")) {
        boolean ifExists = readIfExists(false);
        String synonymName = readIdentifierWithSchema();
        DropSynonym command = new DropSynonym(session, getSchema());
        command.setSynonymName(synonymName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    }
    throw getSyntaxError();
}
Also used : DropConstant(org.h2.command.ddl.DropConstant) DropTrigger(org.h2.command.ddl.DropTrigger) DropView(org.h2.command.ddl.DropView) DropUser(org.h2.command.ddl.DropUser) DropSynonym(org.h2.command.ddl.DropSynonym) DropSequence(org.h2.command.ddl.DropSequence) ValueString(org.h2.value.ValueString) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) DropRole(org.h2.command.ddl.DropRole) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) DropDatabase(org.h2.command.ddl.DropDatabase) ConstraintActionType(org.h2.constraint.ConstraintActionType) DropIndex(org.h2.command.ddl.DropIndex)

Example 38 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class AlterTableAlterColumn method removeSequence.

private void removeSequence(Table table, Sequence sequence) {
    if (sequence != null) {
        table.removeSequence(sequence);
        sequence.setBelongsToTable(false);
        Database db = session.getDatabase();
        db.removeSchemaObject(session, sequence);
    }
}
Also used : Database(org.h2.engine.Database)

Example 39 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class DropSequence method update.

@Override
public int update() {
    session.getUser().checkAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    Sequence sequence = getSchema().findSequence(sequenceName);
    if (sequence == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.SEQUENCE_NOT_FOUND_1, sequenceName);
        }
    } else {
        if (sequence.getBelongsToTable()) {
            throw DbException.get(ErrorCode.SEQUENCE_BELONGS_TO_A_TABLE_1, sequenceName);
        }
        db.removeSchemaObject(session, sequence);
    }
    return 0;
}
Also used : Database(org.h2.engine.Database) Sequence(org.h2.schema.Sequence)

Example 40 with Sequence

use of org.h2.schema.Sequence 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)

Aggregations

Sequence (org.h2.schema.Sequence)19 ValueString (org.h2.value.ValueString)14 PreparedStatement (java.sql.PreparedStatement)10 Column (org.h2.table.Column)10 Database (org.h2.engine.Database)9 Expression (org.h2.expression.Expression)8 DbException (org.h2.message.DbException)8 Table (org.h2.table.Table)8 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)7 Constraint (org.h2.constraint.Constraint)7 Index (org.h2.index.Index)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 SchemaObject (org.h2.schema.SchemaObject)6 ArrayList (java.util.ArrayList)5 DropSequence (org.h2.command.ddl.DropSequence)5 DbObject (org.h2.engine.DbObject)5 Schema (org.h2.schema.Schema)5 TriggerObject (org.h2.schema.TriggerObject)5