Search in sources :

Example 31 with Merge

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

the class TestGetGeneratedKeys method testBatchAndMergeInto.

/**
 * Test for batch updates and MERGE INTO operator.
 *
 * @param conn
 *            connection
 * @throws Exception
 *             on exception
 */
private void testBatchAndMergeInto(Connection conn) throws Exception {
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID BIGINT AUTO_INCREMENT, UID UUID DEFAULT RANDOM_UUID(), VALUE INT)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO TEST(VALUE) VALUES (?), (?)", Statement.RETURN_GENERATED_KEYS);
    prep.setInt(1, 1);
    prep.setInt(2, 2);
    prep.addBatch();
    prep.setInt(1, 3);
    prep.setInt(1, 4);
    prep.addBatch();
    prep.executeBatch();
    ResultSet rs = prep.getGeneratedKeys();
    ResultSetMetaData meta = rs.getMetaData();
    assertEquals("BIGINT", meta.getColumnTypeName(1));
    assertEquals("UUID", meta.getColumnTypeName(2));
    rs.next();
    assertEquals(1L, rs.getLong(1));
    UUID u1 = (UUID) rs.getObject(2);
    assertNotNull(u1);
    rs.next();
    assertEquals(2L, rs.getLong(1));
    UUID u2 = (UUID) rs.getObject(2);
    assertNotNull(u2);
    rs.next();
    assertEquals(3L, rs.getLong(1));
    UUID u3 = (UUID) rs.getObject(2);
    assertNotNull(u3);
    rs.next();
    assertEquals(4L, rs.getLong(1));
    UUID u4 = (UUID) rs.getObject(2);
    assertNotNull(u4);
    assertFalse(rs.next());
    assertFalse(u1.equals(u2));
    assertFalse(u2.equals(u3));
    assertFalse(u3.equals(u4));
    prep = conn.prepareStatement("MERGE INTO TEST(ID, VALUE) KEY(ID) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
    prep.setInt(1, 2);
    prep.setInt(2, 10);
    prep.execute();
    rs = prep.getGeneratedKeys();
    assertFalse(rs.next());
    prep.setInt(1, 5);
    prep.executeUpdate();
    rs = prep.getGeneratedKeys();
    rs.next();
    assertEquals(UUID.class, rs.getObject(1).getClass());
    assertFalse(rs.next());
    stat.execute("DROP TABLE TEST");
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) 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) UUID(java.util.UUID)

Example 32 with Merge

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

the class MixedMode method main.

/**
 * This method is called when executing this sample application from the
 * command line.
 *
 * @param args the command line parameters
 */
public static void main(String... args) throws Exception {
    // start the server, allows to access the database remotely
    Server server = Server.createTcpServer("-tcpPort", "9081");
    server.start();
    System.out.println("You can access the database remotely now, using the URL:");
    System.out.println("jdbc:h2:tcp://localhost:9081/~/test (user: sa, password: sa)");
    // now use the database in your application in embedded mode
    Class.forName("org.h2.Driver");
    Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
    // some simple 'business usage'
    Statement stat = conn.createStatement();
    stat.execute("DROP TABLE TIMER IF EXISTS");
    stat.execute("CREATE TABLE TIMER(ID INT PRIMARY KEY, TIME VARCHAR)");
    System.out.println("Execute this a few times: " + "SELECT TIME FROM TIMER");
    System.out.println("To stop this application " + "(and the server), run: DROP TABLE TIMER");
    try {
        while (true) {
            // runs forever, except if you drop the table remotely
            stat.execute("MERGE INTO TIMER VALUES(1, NOW())");
            Thread.sleep(1000);
        }
    } catch (SQLException e) {
        System.out.println("Error: " + e.toString());
    }
    conn.close();
    // stop the server
    server.stop();
}
Also used : Server(org.h2.tools.Server) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection)

Aggregations

Column (org.h2.table.Column)10 Expression (org.h2.expression.Expression)8 PreparedStatement (java.sql.PreparedStatement)7 Statement (java.sql.Statement)6 ArrayList (java.util.ArrayList)6 Connection (java.sql.Connection)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 ValueExpression (org.h2.expression.ValueExpression)4 ValueString (org.h2.value.ValueString)4 BinaryObject (org.apache.ignite.binary.BinaryObject)3 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)3 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)3 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)3 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)3 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 ResultInterface (org.h2.result.ResultInterface)3 IndexColumn (org.h2.table.IndexColumn)3