Search in sources :

Example 46 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class TestMVTableEngine method testTwoPhaseCommit.

private void testTwoPhaseCommit() throws Exception {
    if (config.memory) {
        return;
    }
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name varchar)");
    stat.execute("set write_delay 0");
    conn.setAutoCommit(false);
    stat.execute("insert into test values(1, 'Hello')");
    stat.execute("prepare commit test_tx");
    stat.execute("shutdown immediately");
    JdbcUtils.closeSilently(conn);
    conn = getConnection(url);
    stat = conn.createStatement();
    ResultSet rs;
    rs = stat.executeQuery("select * from information_schema.in_doubt");
    assertTrue(rs.next());
    stat.execute("commit transaction test_tx");
    rs = stat.executeQuery("select * from test");
    assertTrue(rs.next());
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 47 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class TestMVTableEngine method testSecondaryIndex.

private void testSecondaryIndex() throws SQLException {
    Connection conn;
    Statement stat;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(id int)");
    int size = 8 * 1024;
    stat.execute("insert into test select mod(x * 111, " + size + ") " + "from system_range(1, " + size + ")");
    stat.execute("create index on test(id)");
    ResultSet rs = stat.executeQuery("select count(*) from test inner join " + "system_range(1, " + size + ") where " + "id = mod(x * 111, " + size + ")");
    rs.next();
    assertEquals(size, rs.getInt(1));
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint)

Example 48 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class TestMVTableEngine method testMinMaxWithNull.

private void testMinMaxWithNull() throws Exception {
    Connection conn;
    Connection conn2;
    Statement stat;
    Statement stat2;
    deleteDb(getTestName());
    String url = getTestName() + ";MV_STORE=TRUE;MVCC=TRUE";
    url = getURL(url, true);
    conn = getConnection(url);
    stat = conn.createStatement();
    stat.execute("create table test(data int)");
    stat.execute("create index on test(data)");
    stat.execute("insert into test values(null), (2)");
    conn2 = getConnection(url);
    stat2 = conn2.createStatement();
    conn.setAutoCommit(false);
    conn2.setAutoCommit(false);
    stat.execute("insert into test values(1)");
    ResultSet rs;
    rs = stat.executeQuery("select min(data) from test");
    rs.next();
    assertEquals(1, rs.getInt(1));
    rs = stat2.executeQuery("select min(data) from test");
    rs.next();
    // not yet committed
    assertEquals(2, rs.getInt(1));
    conn2.close();
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 49 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class TestMVTableEngine method testLobCopy.

private void testLobCopy() throws Exception {
    deleteDb(getTestName());
    Connection conn = getConnection(getTestName());
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, data clob)");
    stat = conn.createStatement();
    stat.execute("insert into test(id, data) values(2, space(300))");
    stat.execute("insert into test(id, data) values(1, space(300))");
    stat.execute("alter table test add column x int");
    if (!config.memory) {
        conn.close();
        conn = getConnection(getTestName());
    }
    stat = conn.createStatement();
    ResultSet rs = stat.executeQuery("select data from test");
    while (rs.next()) {
        rs.getString(1);
    }
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet)

Example 50 with Insert

use of org.h2.command.dml.Insert in project h2database by h2database.

the class TestMVTableEngine method testLocking.

private void testLocking() throws Exception {
    deleteDb(getTestName());
    String dbName = getTestName() + ";MV_STORE=TRUE;MVCC=FALSE";
    Connection conn = getConnection(dbName);
    Statement stat = conn.createStatement();
    stat.execute("set lock_timeout 1000");
    stat.execute("create table a(id int primary key, name varchar)");
    stat.execute("create table b(id int primary key, name varchar)");
    Connection conn1 = getConnection(dbName);
    final Statement stat1 = conn1.createStatement();
    stat1.execute("set lock_timeout 1000");
    conn.setAutoCommit(false);
    conn1.setAutoCommit(false);
    stat.execute("insert into a values(1, 'Hello')");
    stat1.execute("insert into b values(1, 'Hello')");
    Task t = new Task() {

        @Override
        public void call() throws Exception {
            stat1.execute("insert into a values(2, 'World')");
        }
    };
    t.execute();
    try {
        stat.execute("insert into b values(2, 'World')");
        throw t.getException();
    } catch (SQLException e) {
        assertEquals(e.toString(), ErrorCode.DEADLOCK_1, e.getErrorCode());
    }
    conn1.close();
    conn.close();
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Aggregations

Statement (java.sql.Statement)215 ResultSet (java.sql.ResultSet)205 PreparedStatement (java.sql.PreparedStatement)202 Connection (java.sql.Connection)201 JdbcConnection (org.h2.jdbc.JdbcConnection)99 SimpleResultSet (org.h2.tools.SimpleResultSet)64 SQLException (java.sql.SQLException)56 JdbcStatement (org.h2.jdbc.JdbcStatement)46 JdbcPreparedStatement (org.h2.jdbc.JdbcPreparedStatement)35 Savepoint (java.sql.Savepoint)32 Random (java.util.Random)28 Value (org.h2.value.Value)28 DbException (org.h2.message.DbException)27 Column (org.h2.table.Column)18 Task (org.h2.util.Task)17 ValueString (org.h2.value.ValueString)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 StringReader (java.io.StringReader)12 ArrayList (java.util.ArrayList)12 InputStream (java.io.InputStream)11