Search in sources :

Example 61 with Db

use of org.h2.test.db.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 62 with Db

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

Example 63 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class TestFuzzOptimizations method testInSelect.

private void testInSelect() {
    Db db = new Db(conn);
    db.execute("CREATE TABLE TEST(A INT, B INT)");
    db.execute("CREATE INDEX IDX ON TEST(A)");
    db.execute("INSERT INTO TEST SELECT X/4, MOD(X, 4) " + "FROM SYSTEM_RANGE(1, 16)");
    db.execute("UPDATE TEST SET A = NULL WHERE A = 0");
    db.execute("UPDATE TEST SET B = NULL WHERE B = 0");
    Random random = new Random();
    long seed = random.nextLong();
    println("seed: " + seed);
    for (int i = 0; i < 100; i++) {
        String column = random.nextBoolean() ? "A" : "B";
        String value = new String[] { "NULL", "0", "A", "B" }[random.nextInt(4)];
        String compare = random.nextBoolean() ? "A" : "B";
        int x = random.nextInt(3);
        String sql1 = "SELECT * FROM TEST T WHERE " + column + "+0 " + "IN(SELECT " + value + " FROM TEST I WHERE I." + compare + "=?) ORDER BY 1, 2";
        String sql2 = "SELECT * FROM TEST T WHERE " + column + " " + "IN(SELECT " + value + " FROM TEST I WHERE I." + compare + "=?) ORDER BY 1, 2";
        List<Map<String, Object>> a = db.prepare(sql1).set(x).query();
        List<Map<String, Object>> b = db.prepare(sql2).set(x).query();
        assertTrue("seed: " + seed + " sql: " + sql1 + " a: " + a + " b: " + b, a.equals(b));
    }
    db.execute("DROP TABLE TEST");
}
Also used : Random(java.util.Random) Map(java.util.Map) Db(org.h2.test.db.Db)

Example 64 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class TestFuzzOptimizations method testGroupSorted.

private void testGroupSorted() {
    Db db = new Db(conn);
    db.execute("CREATE TABLE TEST(A INT, B INT, C INT)");
    Random random = new Random();
    long seed = random.nextLong();
    println("seed: " + seed);
    for (int i = 0; i < 100; i++) {
        Prepared p = db.prepare("INSERT INTO TEST VALUES(?, ?, ?)");
        p.set(new String[] { null, "0", "1", "2" }[random.nextInt(4)]);
        p.set(new String[] { null, "0", "1", "2" }[random.nextInt(4)]);
        p.set(new String[] { null, "0", "1", "2" }[random.nextInt(4)]);
        p.execute();
    }
    int len = getSize(1000, 3000);
    for (int i = 0; i < len / 10; i++) {
        db.execute("CREATE TABLE TEST_INDEXED AS SELECT * FROM TEST");
        int jLen = 1 + random.nextInt(2);
        for (int j = 0; j < jLen; j++) {
            String x = "CREATE INDEX IDX" + j + " ON TEST_INDEXED(";
            int kLen = 1 + random.nextInt(2);
            for (int k = 0; k < kLen; k++) {
                if (k > 0) {
                    x += ",";
                }
                x += new String[] { "A", "B", "C" }[random.nextInt(3)];
            }
            db.execute(x + ")");
        }
        for (int j = 0; j < 10; j++) {
            String x = "SELECT ";
            for (int k = 0; k < 3; k++) {
                if (k > 0) {
                    x += ",";
                }
                x += new String[] { "SUM(A)", "MAX(B)", "AVG(C)", "COUNT(B)" }[random.nextInt(4)];
                x += " S" + k;
            }
            x += " FROM ";
            String group = " GROUP BY ";
            int kLen = 1 + random.nextInt(2);
            for (int k = 0; k < kLen; k++) {
                if (k > 0) {
                    group += ",";
                }
                group += new String[] { "A", "B", "C" }[random.nextInt(3)];
            }
            group += " ORDER BY 1, 2, 3";
            List<Map<String, Object>> a = db.query(x + "TEST" + group);
            List<Map<String, Object>> b = db.query(x + "TEST_INDEXED" + group);
            assertEquals(a.toString(), b.toString());
            assertTrue(a.equals(b));
        }
        db.execute("DROP TABLE TEST_INDEXED");
    }
    db.execute("DROP TABLE TEST");
}
Also used : Random(java.util.Random) Prepared(org.h2.test.db.Db.Prepared) Map(java.util.Map) Db(org.h2.test.db.Db)

Example 65 with Db

use of org.h2.test.db.Db in project h2database by h2database.

the class TestDiskSpaceLeak method main.

/**
 * Run just this test.
 *
 * @param args ignored
 */
public static void main(String... args) throws Exception {
    DeleteDbFiles.execute("data", null, true);
    Class.forName("org.h2.Driver");
    Connection conn;
    long before = 0;
    for (int i = 0; i < 10; i++) {
        conn = DriverManager.getConnection("jdbc:h2:data/test");
        ResultSet rs;
        rs = conn.createStatement().executeQuery("select count(*) from information_schema.lobs");
        rs.next();
        System.out.println("lobs: " + rs.getInt(1));
        rs = conn.createStatement().executeQuery("select count(*) from information_schema.lob_map");
        rs.next();
        System.out.println("lob_map: " + rs.getInt(1));
        rs = conn.createStatement().executeQuery("select count(*) from information_schema.lob_data");
        rs.next();
        System.out.println("lob_data: " + rs.getInt(1));
        conn.close();
        Recover.execute("data", "test");
        new File("data/test.h2.sql").renameTo(new File("data/test." + i + ".sql"));
        conn = DriverManager.getConnection("jdbc:h2:data/test");
        // ((JdbcConnection) conn).setPowerOffCount(i);
        ((JdbcConnection) conn).setPowerOffCount(28);
        String last = "connect";
        try {
            conn.createStatement().execute("drop table test if exists");
            last = "drop";
            conn.createStatement().execute("create table test(id identity, b blob)");
            last = "create";
            conn.createStatement().execute("insert into test values(1, space(10000))");
            last = "insert";
            conn.createStatement().execute("delete from test");
            last = "delete";
            conn.createStatement().execute("insert into test values(1, space(10000))");
            last = "insert2";
            conn.createStatement().execute("delete from test");
            last = "delete2";
        } catch (SQLException e) {
        // ignore
        } finally {
            JdbcUtils.closeSilently(conn);
        }
        long now = new File("data/test.h2.db").length();
        long diff = now - before;
        before = now;
        System.out.println(now + " " + diff + " " + i + " " + last);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) JdbcConnection(org.h2.jdbc.JdbcConnection) File(java.io.File)

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