use of org.h2.engine.User 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.User in project h2database by h2database.
the class AlterUser method update.
@Override
public int update() {
session.commit(true);
Database db = session.getDatabase();
switch(type) {
case CommandInterface.ALTER_USER_SET_PASSWORD:
if (user != session.getUser()) {
session.getUser().checkAdmin();
}
if (hash != null && salt != null) {
CreateUser.setSaltAndHash(user, session, salt, hash);
} else {
CreateUser.setPassword(user, session, password);
}
break;
case CommandInterface.ALTER_USER_RENAME:
session.getUser().checkAdmin();
if (db.findUser(newName) != null || newName.equals(user.getName())) {
throw DbException.get(ErrorCode.USER_ALREADY_EXISTS_1, newName);
}
db.renameDatabaseObject(session, user, newName);
break;
case CommandInterface.ALTER_USER_ADMIN:
session.getUser().checkAdmin();
if (!admin) {
user.checkOwnsNoSchemas();
}
user.setAdmin(admin);
break;
default:
DbException.throwInternalError("type=" + type);
}
db.updateMeta(session, user);
return 0;
}
use of org.h2.engine.User in project h2database by h2database.
the class CreateSchema method update.
@Override
public int update() {
session.getUser().checkSchemaAdmin();
session.commit(true);
Database db = session.getDatabase();
User user = db.getUser(authorization);
// during DB startup, the Right/Role records have not yet been loaded
if (!db.isStarting()) {
user.checkSchemaAdmin();
}
if (db.findSchema(schemaName) != null) {
if (ifNotExists) {
return 0;
}
throw DbException.get(ErrorCode.SCHEMA_ALREADY_EXISTS_1, schemaName);
}
int id = getObjectId();
Schema schema = new Schema(db, id, schemaName, user, false);
schema.setTableEngineParams(tableEngineParams);
db.addDatabaseObject(session, schema);
return 0;
}
use of org.h2.engine.User in project h2database by h2database.
the class Shell method runTool.
/**
* Run the shell tool with the given command line settings.
*
* @param args the command line settings
*/
@Override
public void runTool(String... args) throws SQLException {
String url = null;
String user = "";
String password = "";
String sql = null;
for (int i = 0; args != null && i < args.length; i++) {
String arg = args[i];
if (arg.equals("-url")) {
url = args[++i];
} else if (arg.equals("-user")) {
user = args[++i];
} else if (arg.equals("-password")) {
password = args[++i];
} else if (arg.equals("-driver")) {
String driver = args[++i];
JdbcUtils.loadUserClass(driver);
} else if (arg.equals("-sql")) {
sql = args[++i];
} else if (arg.equals("-properties")) {
serverPropertiesDir = args[++i];
} else if (arg.equals("-help") || arg.equals("-?")) {
showUsage();
return;
} else if (arg.equals("-list")) {
listMode = true;
} else {
showUsageAndThrowUnsupportedOption(arg);
}
}
if (url != null) {
org.h2.Driver.load();
conn = DriverManager.getConnection(url, user, password);
stat = conn.createStatement();
}
if (sql == null) {
promptLoop();
} else {
ScriptReader r = new ScriptReader(new StringReader(sql));
while (true) {
String s = r.readStatement();
if (s == null) {
break;
}
execute(s);
}
if (conn != null) {
conn.close();
}
}
}
use of org.h2.engine.User in project h2database by h2database.
the class DatabaseInfo method listSessions.
@Override
public String listSessions() {
StringBuilder buff = new StringBuilder();
for (Session session : database.getSessions(false)) {
buff.append("session id: ").append(session.getId());
buff.append(" user: ").append(session.getUser().getName()).append('\n');
buff.append("connected: ").append(new Timestamp(session.getSessionStart())).append('\n');
Command command = session.getCurrentCommand();
if (command != null) {
buff.append("statement: ").append(session.getCurrentCommand()).append('\n');
long commandStart = session.getCurrentCommandStart();
if (commandStart != 0) {
buff.append("started: ").append(new Timestamp(commandStart)).append('\n');
}
}
Table[] t = session.getLocks();
if (t.length > 0) {
for (Table table : session.getLocks()) {
if (table.isLockedExclusivelyBy(session)) {
buff.append("write lock on ");
} else {
buff.append("read lock on ");
}
buff.append(table.getSchema().getName()).append('.').append(table.getName()).append('\n');
}
}
buff.append('\n');
}
return buff.toString();
}
Aggregations