Search in sources :

Example 26 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class Explain method query.

@Override
public ResultInterface query(int maxrows) {
    Column column = new Column("PLAN", Value.STRING);
    Database db = session.getDatabase();
    ExpressionColumn expr = new ExpressionColumn(db, column);
    Expression[] expressions = { expr };
    result = new LocalResult(session, expressions, 1);
    if (maxrows >= 0) {
        String plan;
        if (executeCommand) {
            PageStore store = null;
            Store mvStore = null;
            if (db.isPersistent()) {
                store = db.getPageStore();
                if (store != null) {
                    store.statisticsStart();
                }
                mvStore = db.getMvStore();
                if (mvStore != null) {
                    mvStore.statisticsStart();
                }
            }
            if (command.isQuery()) {
                command.query(maxrows);
            } else {
                command.update();
            }
            plan = command.getPlanSQL();
            Map<String, Integer> statistics = null;
            if (store != null) {
                statistics = store.statisticsEnd();
            } else if (mvStore != null) {
                statistics = mvStore.statisticsEnd();
            }
            if (statistics != null) {
                int total = 0;
                for (Entry<String, Integer> e : statistics.entrySet()) {
                    total += e.getValue();
                }
                if (total > 0) {
                    statistics = new TreeMap<>(statistics);
                    StringBuilder buff = new StringBuilder();
                    if (statistics.size() > 1) {
                        buff.append("total: ").append(total).append('\n');
                    }
                    for (Entry<String, Integer> e : statistics.entrySet()) {
                        int value = e.getValue();
                        int percent = (int) (100L * value / total);
                        buff.append(e.getKey()).append(": ").append(value);
                        if (statistics.size() > 1) {
                            buff.append(" (").append(percent).append("%)");
                        }
                        buff.append('\n');
                    }
                    plan += "\n/*\n" + buff.toString() + "*/";
                }
            }
        } else {
            plan = command.getPlanSQL();
        }
        add(plan);
    }
    result.done();
    return result;
}
Also used : Store(org.h2.mvstore.db.MVTableEngine.Store) PageStore(org.h2.store.PageStore) PageStore(org.h2.store.PageStore) ValueString(org.h2.value.ValueString) ExpressionColumn(org.h2.expression.ExpressionColumn) LocalResult(org.h2.result.LocalResult) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) Expression(org.h2.expression.Expression) Database(org.h2.engine.Database)

Example 27 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class GrantRevoke method revokeRole.

private void revokeRole(Role grantedRole) {
    Right right = grantee.getRightForRole(grantedRole);
    if (right == null) {
        return;
    }
    Database db = session.getDatabase();
    db.removeDatabaseObject(session, right);
}
Also used : Right(org.h2.engine.Right) Database(org.h2.engine.Database)

Example 28 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class GrantRevoke method grantRight.

private void grantRight(DbObject object) {
    Database db = session.getDatabase();
    Right right = grantee.getRightForObject(object);
    if (right == null) {
        int id = getObjectId();
        right = new Right(db, id, grantee, rightMask, object);
        grantee.grantRight(object, right);
        db.addDatabaseObject(session, right);
    } else {
        right.setRightMask(right.getRightMask() | rightMask);
        db.updateMeta(session, right);
    }
}
Also used : Database(org.h2.engine.Database) Right(org.h2.engine.Right)

Example 29 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class DropRole method update.

@Override
public int update() {
    session.getUser().checkAdmin();
    session.commit(true);
    Database db = session.getDatabase();
    if (roleName.equals(Constants.PUBLIC_ROLE_NAME)) {
        throw DbException.get(ErrorCode.ROLE_CAN_NOT_BE_DROPPED_1, roleName);
    }
    Role role = db.findRole(roleName);
    if (role == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.ROLE_NOT_FOUND_1, roleName);
        }
    } else {
        db.removeDatabaseObject(session, role);
    }
    return 0;
}
Also used : Role(org.h2.engine.Role) Database(org.h2.engine.Database)

Example 30 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class DropTable method executeDrop.

private void executeDrop() {
    // need to get the table again, because it may be dropped already
    // meanwhile (dependent object, or same object)
    table = getSchema().findTableOrView(session, tableName);
    if (table != null) {
        table.setModified();
        Database db = session.getDatabase();
        db.lockMeta(session);
        db.removeSchemaObject(session, table);
    }
    if (next != null) {
        next.executeDrop();
    }
}
Also used : Database(org.h2.engine.Database)

Aggregations

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6