Search in sources :

Example 56 with Db

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

the class TableDefinition method merge.

void merge(Db db, Object obj) {
    if (primaryKeyColumnNames == null || primaryKeyColumnNames.size() == 0) {
        throw new IllegalStateException("No primary key columns defined " + "for table " + obj.getClass() + " - no update possible");
    }
    SQLStatement stat = new SQLStatement(db);
    StatementBuilder buff = new StatementBuilder("MERGE INTO ");
    buff.append(db.getDialect().getTableName(schemaName, tableName)).append(" (");
    buff.resetCount();
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append(field.columnName);
    }
    buff.append(") KEY(");
    buff.resetCount();
    for (FieldDefinition field : fields) {
        if (field.isPrimaryKey) {
            buff.appendExceptFirst(", ");
            buff.append(field.columnName);
        }
    }
    buff.append(") ");
    buff.resetCount();
    buff.append("VALUES (");
    for (FieldDefinition field : fields) {
        buff.appendExceptFirst(", ");
        buff.append('?');
        Object value = getValue(obj, field);
        stat.addParameter(value);
    }
    buff.append(')');
    stat.setSQL(buff.toString());
    StatementLogger.merge(stat.getSQL());
    stat.executeUpdate();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 57 with Db

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

the class TestAll method testAll.

private void testAll() throws Exception {
    runTests();
    if (!travis && !vmlens) {
        Profiler prof = new Profiler();
        prof.depth = 16;
        prof.interval = 1;
        prof.startCollecting();
        TestPerformance.main("-init", "-db", "1", "-size", "1000");
        prof.stopCollecting();
        System.out.println(prof.getTop(5));
        TestPerformance.main("-init", "-db", "1", "-size", "1000");
    }
}
Also used : Profiler(org.h2.util.Profiler)

Example 58 with Db

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

the class TestWeb method testTools.

private void testTools() throws Exception {
    if (config.memory || config.cipher != null) {
        return;
    }
    deleteDb(getTestName());
    Connection conn = getConnection(getTestName());
    conn.createStatement().execute("create table test(id int) as select 1");
    conn.close();
    Server server = new Server();
    server.setOut(new PrintStream(new ByteArrayOutputStream()));
    server.runTool("-web", "-webPort", "8182", "-properties", "null", "-tcp", "-tcpPort", "9101");
    try {
        String url = "http://localhost:8182";
        WebClient client;
        String result;
        client = new WebClient();
        result = client.get(url);
        client.readSessionId(result);
        result = client.get(url, "tools.jsp");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Backup&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        deleteDb(getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/backup.zip"));
        result = client.get(url, "tools.do?tool=DeleteDbFiles&args=-dir," + getBaseDir() + ",-db," + getTestName());
        String fn = getBaseDir() + "/" + getTestName();
        if (config.mvStore) {
            fn += Constants.SUFFIX_MV_FILE;
        } else {
            fn += Constants.SUFFIX_PAGE_FILE;
        }
        assertFalse(FileUtils.exists(fn));
        result = client.get(url, "tools.do?tool=Restore&args=-dir," + getBaseDir() + ",-db," + getTestName() + ",-file," + getBaseDir() + "/backup.zip");
        assertTrue(FileUtils.exists(fn));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        FileUtils.delete(getBaseDir() + "/backup.zip");
        result = client.get(url, "tools.do?tool=Recover&args=-dir," + getBaseDir() + ",-db," + getTestName());
        assertTrue(FileUtils.exists(getBaseDir() + "/" + getTestName() + ".h2.sql"));
        FileUtils.delete(getBaseDir() + "/web.h2.sql");
        result = client.get(url, "tools.do?tool=RunScript&args=-script," + getBaseDir() + "/" + getTestName() + ".h2.sql,-url," + getURL(getTestName(), true) + ",-user," + getUser() + ",-password," + getPassword());
        FileUtils.delete(getBaseDir() + "/" + getTestName() + ".h2.sql");
        assertTrue(FileUtils.exists(fn));
        deleteDb(getTestName());
    } finally {
        server.shutdown();
    }
}
Also used : PrintStream(java.io.PrintStream) Server(org.h2.tools.Server) Connection(java.sql.Connection) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 59 with Db

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

the class TestMVTableEngine method testReadOnly.

private void testReadOnly() throws Exception {
    if (config.memory) {
        return;
    }
    deleteDb(getTestName());
    String dbName = getTestName() + ";MV_STORE=TRUE";
    Connection conn;
    Statement stat;
    conn = getConnection(dbName);
    stat = conn.createStatement();
    stat.execute("create table test(id int)");
    conn.close();
    FileUtils.setReadOnly(getBaseDir() + "/" + getTestName() + Constants.SUFFIX_MV_FILE);
    conn = getConnection(dbName);
    Database db = (Database) ((JdbcConnection) conn).getSession().getDataHandler();
    assertTrue(db.getMvStore().getStore().getFileStore().isReadOnly());
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) Database(org.h2.engine.Database) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 60 with Db

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

the class TestCrashAPI method getConnection.

private Connection getConnection(int seed, boolean delete) throws SQLException {
    openCount++;
    if (delete) {
        deleteDb();
    }
    // can not use FILE_LOCK=NO, otherwise something could be written into
    // the database in the finalize method
    String add = ";MAX_QUERY_TIMEOUT=10000";
    // int testing;
    // if(openCount >= 32) {
    // int test;
    // Runtime.getRuntime().halt(0);
    // System.exit(1);
    // }
    // System.out.println("now open " + openCount);
    // add += ";TRACE_LEVEL_FILE=3";
    // config.logMode = 2;
    // }
    String dbName = "crashApi" + seed;
    String url = getURL(DIR + "/" + dbName, true) + add;
    // int test;
    // url += ";DB_CLOSE_ON_EXIT=FALSE";
    // int test;
    // url += ";TRACE_LEVEL_FILE=3";
    Connection conn = null;
    String fileName = "temp/backup/db-" + uniqueId++ + ".zip";
    Backup.execute(fileName, getBaseDir() + "/" + DIR, dbName, true);
    // close databases earlier
    System.gc();
    try {
        conn = DriverManager.getConnection(url, "sa", getPassword(""));
        // delete the backup if opening was successful
        FileUtils.delete(fileName);
    } catch (SQLException e) {
        if (e.getErrorCode() == ErrorCode.WRONG_USER_OR_PASSWORD) {
            // delete if the password changed
            FileUtils.delete(fileName);
        }
        throw e;
    }
    int len = random.getInt(50);
    int first = random.getInt(statements.size() - len);
    int end = first + len;
    Statement stat = conn.createStatement();
    stat.execute("SET LOCK_TIMEOUT 10");
    stat.execute("SET WRITE_DELAY 0");
    if (random.nextBoolean()) {
        if (random.nextBoolean()) {
            double g = random.nextGaussian();
            int size = (int) Math.abs(10000 * g * g);
            stat.execute("SET CACHE_SIZE " + size);
        } else {
            stat.execute("SET CACHE_SIZE 0");
        }
    }
    stat.execute("SCRIPT NOPASSWORDS NOSETTINGS");
    for (int i = first; i < end && i < statements.size() && !stopped; i++) {
        try {
            stat.execute("SELECT * FROM TEST WHERE ID=1");
        } catch (Throwable t) {
            printIfBad(seed, -i, -1, t);
        }
        try {
            stat.execute("SELECT * FROM TEST WHERE ID=1 OR ID=1");
        } catch (Throwable t) {
            printIfBad(seed, -i, -1, t);
        }
        String sql = statements.get(i);
        try {
            // if(openCount == 32) {
            // int test;
            // System.out.println("stop!");
            // }
            stat.execute(sql);
        } catch (Throwable t) {
            printIfBad(seed, -i, -1, t);
        }
    }
    if (random.nextBoolean()) {
        try {
            conn.commit();
        } catch (Throwable t) {
            printIfBad(seed, 0, -1, t);
        }
    }
    return conn;
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) Savepoint(java.sql.Savepoint)

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