Search in sources :

Example 36 with Delete

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

the class TestFunctions method testTransactionId.

private void testTransactionId() throws SQLException {
    if (config.memory) {
        return;
    }
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int)");
    ResultSet rs;
    rs = stat.executeQuery("call transaction_id()");
    rs.next();
    assertTrue(rs.getString(1) == null && rs.wasNull());
    stat.execute("insert into test values(1)");
    rs = stat.executeQuery("call transaction_id()");
    rs.next();
    assertTrue(rs.getString(1) == null && rs.wasNull());
    conn.setAutoCommit(false);
    stat.execute("delete from test");
    rs = stat.executeQuery("call transaction_id()");
    rs.next();
    assertNotNull(rs.getString(1));
    stat.execute("drop table test");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet)

Example 37 with Delete

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

the class TestIndex method testHashIndex.

private void testHashIndex() throws SQLException {
    reconnect();
    stat.execute("create table testA(id int primary key, name varchar)");
    stat.execute("create table testB(id int primary key hash, name varchar)");
    int len = getSize(300, 3000);
    stat.execute("insert into testA select x, 'Hello' from " + "system_range(1, " + len + ")");
    stat.execute("insert into testB select x, 'Hello' from " + "system_range(1, " + len + ")");
    Random rand = new Random(1);
    for (int i = 0; i < len; i++) {
        int x = rand.nextInt(len);
        String sql = "";
        switch(rand.nextInt(3)) {
            case 0:
                sql = "delete from testA where id = " + x;
                break;
            case 1:
                sql = "update testA set name = " + rand.nextInt(100) + " where id = " + x;
                break;
            case 2:
                sql = "select name from testA where id = " + x;
                break;
            default:
        }
        boolean result = stat.execute(sql);
        if (result) {
            ResultSet rs = stat.getResultSet();
            String s1 = rs.next() ? rs.getString(1) : null;
            rs = stat.executeQuery(sql.replace('A', 'B'));
            String s2 = rs.next() ? rs.getString(1) : null;
            assertEquals(s1, s2);
        } else {
            int count1 = stat.getUpdateCount();
            int count2 = stat.executeUpdate(sql.replace('A', 'B'));
            assertEquals(count1, count2);
        }
    }
    stat.execute("drop table testA, testB");
    conn.close();
}
Also used : Random(java.util.Random) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 38 with Delete

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

the class TestIndex method testIndexTypes.

private void testIndexTypes() throws SQLException {
    Connection conn = getConnection("index");
    stat = conn.createStatement();
    for (String type : new String[] { "unique", "hash", "unique hash" }) {
        stat.execute("create table test(id int)");
        stat.execute("create " + type + " index idx_name on test(id)");
        stat.execute("insert into test select x from system_range(1, 1000)");
        ResultSet rs = stat.executeQuery("select * from test where id=100");
        assertTrue(rs.next());
        assertFalse(rs.next());
        stat.execute("delete from test where id=100");
        rs = stat.executeQuery("select * from test where id=100");
        assertFalse(rs.next());
        rs = stat.executeQuery("select min(id), max(id) from test");
        assertTrue(rs.next());
        assertEquals(1, rs.getInt(1));
        assertEquals(1000, rs.getInt(2));
        stat.execute("drop table test");
    }
    conn.close();
    deleteDb("index");
}
Also used : Connection(java.sql.Connection) SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet)

Example 39 with Delete

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

the class TestLob method testRemoveAfterDeleteAndClose.

private void testRemoveAfterDeleteAndClose() throws Exception {
    if (config.memory || config.cipher != null) {
        return;
    }
    deleteDb("lob");
    Connection conn = getConnection("lob");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, data clob)");
    for (int i = 0; i < 10; i++) {
        stat.execute("insert into test values(1, space(100000))");
        if (i > 5) {
            ResultSet rs = stat.executeQuery("select * from test");
            rs.next();
            Clob c = rs.getClob(2);
            stat.execute("delete from test where id = 1");
            c.getSubString(1, 10);
        } else {
            stat.execute("delete from test where id = 1");
        }
    }
    // some clobs are removed only here (those that were queries for)
    conn.close();
    Recover.execute(getBaseDir(), "lob");
    long size = FileUtils.size(getBaseDir() + "/lob.h2.sql");
    assertTrue("size: " + size, size > 1000 && size < 10000);
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Clob(java.sql.Clob) Savepoint(java.sql.Savepoint)

Example 40 with Delete

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

the class TestLob method testDeadlock.

/**
 * Test for issue 315: Java Level Deadlock on Database & Session Objects
 */
private void testDeadlock() throws Exception {
    deleteDb("lob");
    Connection conn = getConnection("lob");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int primary key, name clob)");
    stat.execute("insert into test select x, space(10000) from system_range(1, 3)");
    final Connection conn2 = getConnection("lob");
    Task task = new Task() {

        @Override
        public void call() throws Exception {
            Statement stat = conn2.createStatement();
            stat.setFetchSize(1);
            for (int i = 0; !stop; i++) {
                ResultSet rs = stat.executeQuery("select * from test where id > -" + i);
                while (rs.next()) {
                // ignore
                }
            }
        }
    };
    task.execute();
    stat.execute("create table test2(id int primary key, name clob)");
    for (int i = 0; i < 100; i++) {
        stat.execute("delete from test2");
        stat.execute("insert into test2 values(1, space(10000 + " + i + "))");
    }
    task.get();
    conn.close();
    conn2.close();
}
Also used : Task(org.h2.util.Task) 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)

Aggregations

Connection (java.sql.Connection)40 PreparedStatement (java.sql.PreparedStatement)39 Statement (java.sql.Statement)38 ResultSet (java.sql.ResultSet)36 JdbcConnection (org.h2.jdbc.JdbcConnection)25 SQLException (java.sql.SQLException)17 SimpleResultSet (org.h2.tools.SimpleResultSet)14 Savepoint (java.sql.Savepoint)13 StatementBuilder (org.h2.util.StatementBuilder)9 DbException (org.h2.message.DbException)8 Column (org.h2.table.Column)8 ValueString (org.h2.value.ValueString)7 Random (java.util.Random)6 Expression (org.h2.expression.Expression)5 ValueExpression (org.h2.expression.ValueExpression)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)4