use of org.h2.test.db.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;
}
use of org.h2.test.db.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);
}
use of org.h2.test.db.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);
}
}
use of org.h2.test.db.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;
}
use of org.h2.test.db.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();
}
}
Aggregations