Search in sources :

Example 76 with Insert

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");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) ResultSet(java.sql.ResultSet) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement)

Example 77 with Insert

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");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) ResultSet(java.sql.ResultSet)

Example 78 with Insert

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");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) ResultSet(java.sql.ResultSet) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement)

Example 79 with Insert

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");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) ResultSet(java.sql.ResultSet) UUID(java.util.UUID)

Example 80 with Insert

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");
}
Also used : JdbcStatement(org.h2.jdbc.JdbcStatement) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) JdbcPreparedStatement(org.h2.jdbc.JdbcPreparedStatement) ResultSet(java.sql.ResultSet)

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