Search in sources :

Example 96 with Insert

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

the class TestGetGeneratedKeys method testPrepareStatement_StringArray_Execute.

/**
 * Test method for
 * {@link Connection#prepareStatement(String, String[])}
 * .{@link PreparedStatement#execute()}.
 *
 * @param conn
 *            connection
 * @throws Exception
 *             on exception
 */
private void testPrepareStatement_StringArray_Execute(Connection conn) throws Exception {
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT," + "UID UUID NOT NULL DEFAULT RANDOM_UUID(), VALUE INT NOT NULL)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (10)", new String[0]);
    prep.executeUpdate();
    ResultSet rs = prep.getGeneratedKeys();
    assertFalse(rs.next());
    rs.close();
    prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (20)", new String[] { "ID", "UID" });
    prep.execute();
    rs = prep.getGeneratedKeys();
    assertEquals(2, rs.getMetaData().getColumnCount());
    assertEquals("ID", rs.getMetaData().getColumnName(1));
    assertEquals("UID", rs.getMetaData().getColumnName(2));
    assertTrue(rs.next());
    assertEquals(2L, rs.getLong(1));
    assertEquals(UUID.class, rs.getObject(2).getClass());
    assertFalse(rs.next());
    rs.close();
    prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (30)", new String[] { "UID", "ID" });
    prep.execute();
    rs = prep.getGeneratedKeys();
    assertEquals(2, rs.getMetaData().getColumnCount());
    assertEquals("UID", rs.getMetaData().getColumnName(1));
    assertEquals("ID", rs.getMetaData().getColumnName(2));
    assertTrue(rs.next());
    assertEquals(UUID.class, rs.getObject(1).getClass());
    assertEquals(3L, rs.getLong(2));
    assertFalse(rs.next());
    rs.close();
    prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (40)", new String[] { "UID" });
    prep.execute();
    rs = prep.getGeneratedKeys();
    assertEquals(1, rs.getMetaData().getColumnCount());
    assertEquals("UID", rs.getMetaData().getColumnName(1));
    assertTrue(rs.next());
    assertEquals(UUID.class, rs.getObject(1).getClass());
    assertFalse(rs.next());
    rs.close();
    stat.execute("DROP TABLE TEST");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement)

Example 97 with Insert

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

the class TestJavaObjectSerializer method testStaticGlobalSerializer.

private void testStaticGlobalSerializer() throws Exception {
    JdbcUtils.serializer = new JavaObjectSerializer() {

        @Override
        public byte[] serialize(Object obj) throws Exception {
            assertEquals(100500, ((Integer) obj).intValue());
            return new byte[] { 1, 2, 3 };
        }

        @Override
        public Object deserialize(byte[] bytes) throws Exception {
            assertEquals(new byte[] { 1, 2, 3 }, bytes);
            return 100500;
        }
    };
    try {
        deleteDb("javaSerializer");
        Connection conn = getConnection("javaSerializer");
        Statement stat = conn.createStatement();
        stat.execute("create table t(id identity, val other)");
        PreparedStatement ins = conn.prepareStatement("insert into t(val) values(?)");
        ins.setObject(1, 100500, Types.JAVA_OBJECT);
        assertEquals(1, ins.executeUpdate());
        Statement s = conn.createStatement();
        ResultSet rs = s.executeQuery("select val from t");
        assertTrue(rs.next());
        assertEquals(100500, ((Integer) rs.getObject(1)).intValue());
        assertEquals(new byte[] { 1, 2, 3 }, rs.getBytes(1));
        conn.close();
        deleteDb("javaSerializer");
    } finally {
        JdbcUtils.serializer = null;
    }
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JavaObjectSerializer(org.h2.api.JavaObjectSerializer)

Example 98 with Insert

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

the class TestXA method testRollbackAfterPrepare.

private void testRollbackAfterPrepare() throws Exception {
    if (config.memory) {
        return;
    }
    Xid xid = new Xid() {

        @Override
        public int getFormatId() {
            return 3145;
        }

        @Override
        public byte[] getGlobalTransactionId() {
            return new byte[] { 1, 2, 3, 4, 5, 6, 6, 7, 8 };
        }

        @Override
        public byte[] getBranchQualifier() {
            return new byte[] { 34, 43, 33, 3, 3, 3, 33, 33, 3 };
        }
    };
    deleteDb("xa");
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(getURL("xa", true));
    ds.setPassword(getPassword());
    Connection dm = ds.getConnection();
    Statement stat = dm.createStatement();
    stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, VAL INT)");
    stat.execute("INSERT INTO TEST(ID,VAL) VALUES (1,1)");
    dm.close();
    XAConnection c = ds.getXAConnection();
    XAResource xa = c.getXAResource();
    Connection connection = c.getConnection();
    xa.start(xid, XAResource.TMJOIN);
    PreparedStatement ps = connection.prepareStatement("UPDATE TEST SET VAL=? WHERE ID=?");
    ps.setInt(1, new Random().nextInt());
    ps.setInt(2, 1);
    ps.close();
    xa.prepare(xid);
    xa.rollback(xid);
    connection.close();
    c.close();
    deleteDb("xa");
}
Also used : Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) Random(java.util.Random) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PreparedStatement(java.sql.PreparedStatement) XAConnection(javax.sql.XAConnection)

Example 99 with Insert

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

the class TestXA method testRollbackWithoutPrepare.

private void testRollbackWithoutPrepare() throws Exception {
    if (config.memory) {
        return;
    }
    Xid xid = new Xid() {

        @Override
        public int getFormatId() {
            return 3145;
        }

        @Override
        public byte[] getGlobalTransactionId() {
            return new byte[] { 1, 2, 3, 4, 5, 6, 6, 7, 8 };
        }

        @Override
        public byte[] getBranchQualifier() {
            return new byte[] { 34, 43, 33, 3, 3, 3, 33, 33, 3 };
        }
    };
    deleteDb("xa");
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL(getURL("xa", true));
    ds.setPassword(getPassword());
    Connection dm = ds.getConnection();
    Statement stat = dm.createStatement();
    stat.execute("CREATE TABLE IF NOT EXISTS TEST(ID INT PRIMARY KEY, VAL INT)");
    stat.execute("INSERT INTO TEST(ID,VAL) VALUES (1,1)");
    dm.close();
    XAConnection c = ds.getXAConnection();
    XAResource xa = c.getXAResource();
    Connection connection = c.getConnection();
    xa.start(xid, XAResource.TMJOIN);
    PreparedStatement ps = connection.prepareStatement("UPDATE TEST SET VAL=? WHERE ID=?");
    ps.setInt(1, new Random().nextInt());
    ps.setInt(2, 1);
    ps.close();
    xa.rollback(xid);
    connection.close();
    c.close();
    deleteDb("xa");
}
Also used : Xid(javax.transaction.xa.Xid) XAResource(javax.transaction.xa.XAResource) Random(java.util.Random) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcDataSource(org.h2.jdbcx.JdbcDataSource) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PreparedStatement(java.sql.PreparedStatement) XAConnection(javax.sql.XAConnection)

Example 100 with Insert

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

the class TestMvccMultiThreaded method testConcurrentSelectForUpdate.

private void testConcurrentSelectForUpdate() throws Exception {
    deleteDb(getTestName());
    Connection conn = getConnection(getTestName() + ";MULTI_THREADED=TRUE");
    Statement stat = conn.createStatement();
    stat.execute("create table test(id int not null primary key, updated int not null)");
    stat.execute("insert into test(id, updated) values(1, 100)");
    ArrayList<Task> tasks = new ArrayList<>();
    int count = 3;
    for (int i = 0; i < count; i++) {
        Task task = new Task() {

            @Override
            public void call() throws Exception {
                Connection conn = getConnection(getTestName());
                Statement stat = conn.createStatement();
                try {
                    while (!stop) {
                        try {
                            stat.execute("select * from test where id=1 for update");
                        } catch (SQLException e) {
                            int errorCode = e.getErrorCode();
                            assertTrue(e.getMessage(), errorCode == ErrorCode.DEADLOCK_1 || errorCode == ErrorCode.LOCK_TIMEOUT_1);
                        }
                    }
                } finally {
                    conn.close();
                }
            }
        }.execute();
        tasks.add(task);
    }
    for (int i = 0; i < 10; i++) {
        Thread.sleep(100);
        ResultSet rs = stat.executeQuery("select * from test");
        assertTrue(rs.next());
    }
    for (Task t : tasks) {
        t.get();
    }
    conn.close();
    deleteDb(getTestName());
}
Also used : Task(org.h2.util.Task) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

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