use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestGetGeneratedKeys method testPrepareStatement_StringArray_ExecuteLargeUpdate.
/**
* Test method for
* {@link Connection#prepareStatement(String, String[])}
* .{@link PreparedStatement#executeLargeUpdate()}.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testPrepareStatement_StringArray_ExecuteLargeUpdate(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)");
JdbcPreparedStatement prep = (JdbcPreparedStatement) conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (10)", new String[0]);
prep.executeLargeUpdate();
ResultSet rs = prep.getGeneratedKeys();
assertFalse(rs.next());
rs.close();
prep = (JdbcPreparedStatement) conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (20)", new String[] { "ID", "UID" });
prep.executeLargeUpdate();
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 = (JdbcPreparedStatement) conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (30)", new String[] { "UID", "ID" });
prep.executeLargeUpdate();
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 = (JdbcPreparedStatement) conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (40)", new String[] { "UID" });
prep.executeLargeUpdate();
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");
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestGetGeneratedKeys method testStatementExecuteLargeUpdate_StringArray.
/**
* Test method for {@link Statement#executeLargeUpdate(String, String[])}.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testStatementExecuteLargeUpdate_StringArray(Connection conn) throws Exception {
JdbcStatement stat = (JdbcStatement) conn.createStatement();
stat.execute("CREATE TABLE TEST (ID BIGINT PRIMARY KEY AUTO_INCREMENT," + "UID UUID NOT NULL DEFAULT RANDOM_UUID(), VALUE INT NOT NULL)");
stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (10)", new String[0]);
ResultSet rs = stat.getGeneratedKeys();
assertFalse(rs.next());
rs.close();
stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (20)", new String[] { "ID", "UID" });
rs = stat.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();
stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (30)", new String[] { "UID", "ID" });
rs = stat.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();
stat.executeLargeUpdate("INSERT INTO TEST(VALUE) VALUES (40)", new String[] { "UID" });
rs = stat.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");
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestGetGeneratedKeys method testPrepareStatement_ExecuteLargeUpdate.
/**
* Test method for
* {@link Connection#prepareStatement(String)}
* .{@link PreparedStatement#executeLargeUpdate()}.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testPrepareStatement_ExecuteLargeUpdate(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)");
JdbcPreparedStatement prep = (JdbcPreparedStatement) conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (10)");
prep.executeLargeUpdate();
ResultSet rs = prep.getGeneratedKeys();
assertFalse(rs.next());
rs.close();
stat.execute("DROP TABLE TEST");
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestGetGeneratedKeys method testTrigger.
/**
* Test for keys generated by trigger.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testTrigger(Connection conn) throws Exception {
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE TEST(ID UUID, VALUE INT)");
stat.execute("CREATE TRIGGER TEST_INSERT BEFORE INSERT ON TEST FOR EACH ROW CALL \"" + TestGetGeneratedKeysTrigger.class.getName() + '"');
stat.executeUpdate("INSERT INTO TEST(VALUE) VALUES (10), (20)", Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stat.getGeneratedKeys();
rs.next();
UUID u1 = (UUID) rs.getObject(1);
rs.next();
UUID u2 = (UUID) rs.getObject(1);
assertFalse(rs.next());
rs = stat.executeQuery("SELECT ID FROM TEST ORDER BY VALUE");
rs.next();
assertEquals(u1, rs.getObject(1));
rs.next();
assertEquals(u2, rs.getObject(1));
stat.execute("DROP TRIGGER TEST_INSERT");
stat.execute("DROP TABLE TEST");
}
use of org.h2.command.dml.Insert in project h2database by h2database.
the class TestGetGeneratedKeys method testStatementExecute_StringArray.
/**
* Test method for {@link Statement#executeUpdate(String, String[])}.
*
* @param conn
* connection
* @throws Exception
* on exception
*/
private void testStatementExecute_StringArray(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)");
stat.execute("INSERT INTO TEST(VALUE) VALUES (10)", new String[0]);
ResultSet rs = stat.getGeneratedKeys();
assertFalse(rs.next());
rs.close();
stat.execute("INSERT INTO TEST(VALUE) VALUES (20)", new String[] { "ID", "UID" });
rs = stat.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();
stat.execute("INSERT INTO TEST(VALUE) VALUES (30)", new String[] { "UID", "ID" });
rs = stat.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();
stat.execute("INSERT INTO TEST(VALUE) VALUES (40)", new String[] { "UID" });
rs = stat.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");
}
Aggregations