Search in sources :

Example 71 with User

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);
        }
    }
}
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 72 with User

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

Example 73 with User

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

Example 74 with User

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();
        }
    }
}
Also used : StringReader(java.io.StringReader) ScriptReader(org.h2.util.ScriptReader)

Example 75 with User

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();
}
Also used : Table(org.h2.table.Table) Command(org.h2.command.Command) Timestamp(java.sql.Timestamp) Session(org.h2.engine.Session)

Aggregations

Connection (java.sql.Connection)36 SQLException (java.sql.SQLException)21 PreparedStatement (java.sql.PreparedStatement)17 Statement (java.sql.Statement)17 ResultSet (java.sql.ResultSet)16 Server (org.h2.tools.Server)15 DbException (org.h2.message.DbException)14 Column (org.h2.table.Column)12 ValueString (org.h2.value.ValueString)12 Properties (java.util.Properties)10 Database (org.h2.engine.Database)10 Schema (org.h2.schema.Schema)8 IOException (java.io.IOException)7 User (org.h2.engine.User)7 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)7 SimpleResultSet (org.h2.tools.SimpleResultSet)7 Value (org.h2.value.Value)7 PrintStream (java.io.PrintStream)6 Timestamp (java.sql.Timestamp)6 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)6