@Override @Suspendable public void start() throws Exception { JsonObject config = new JsonObject().put("url", "jdbc:hsqldb:mem:test?shutdown=true") .put("driver_class", "org.hsqldb.jdbcDriver"); JDBCClient jdbc = JDBCClient.createShared(vertx, config); // Get a connection try (SQLConnection conn = awaitResult(jdbc::getConnection)) { // Create a table Void v = awaitResult(h -> conn.execute("CREATE TABLE test(col VARCHAR(20))", h)); // Insert some stuff for (int i = 0; i < 10; i++) { int ii = i; UpdateResult res = awaitResult(h -> conn.update("INSERT INTO test (col) VALUES ('val" + ii + "')", h)); System.out.println("Rows updated: " + res.getUpdated()); } // Select the results ResultSet res = awaitResult(h -> conn.query("SELECT * FROM test", h)); System.out.println("Selected " + res.getNumRows() + " results"); res.getResults().forEach(System.out::println); } }
/** * Executes the given SQL statement which may be an <code>INSERT</code>, <code>UPDATE</code>, or <code>DELETE</code> * statement. * @param sql the SQL to execute. For example <code>INSERT INTO table ...</code> * @param resultHandler the handler which is called once the operation completes. * @return */ public io.vertx.rxjava.ext.sql.SQLConnection update(String sql, Handler<AsyncResult<UpdateResult>> resultHandler) { delegate.update(sql, resultHandler); return this; }
/** * Executes the given SQL statement which may be an <code>INSERT</code>, <code>UPDATE</code>, or <code>DELETE</code> * statement. * @param sql the SQL to execute. For example <code>INSERT INTO table ...</code> * @param resultHandler the handler which is called once the operation completes. * @return */ public io.vertx.rxjava.ext.sql.SQLConnection update(String sql, Handler<AsyncResult<UpdateResult>> resultHandler) { delegate.update(sql, resultHandler); return this; }
public static io.vertx.ext.sql.SQLConnection update(io.vertx.ext.sql.SQLConnection j_receiver, java.lang.String sql, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.Map<String, Object>>> resultHandler) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.update(sql, resultHandler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.ext.sql.UpdateResult>>() { public void handle(io.vertx.core.AsyncResult<io.vertx.ext.sql.UpdateResult> ar) { resultHandler.handle(ar.map(event -> event != null ? io.vertx.core.impl.ConversionHelper.fromJsonObject(event.toJson()) : null)); } } : null)); return j_receiver; } public static io.vertx.ext.sql.SQLConnection updateWithParams(io.vertx.ext.sql.SQLConnection j_receiver, java.lang.String sql, java.util.List<Object> params, io.vertx.core.Handler<io.vertx.core.AsyncResult<java.util.Map<String, Object>>> resultHandler) {
@Override public SQLConnection update(String sql, Handler<AsyncResult<UpdateResult>> resultHandler) { delegate.update(sql, resultHandler); return this; }
protected void removeAll(String sql, Handler<AsyncResult<Void>> resultHandler) { client.getConnection(connHandler(resultHandler, connection -> { connection.update(sql, r -> { if (r.succeeded()) { resultHandler.handle(Future.succeededFuture()); } else { resultHandler.handle(Future.failedFuture(r.cause())); } connection.close(); }); })); }
@Override public CompletableFuture<Long> insertReturning(Query query) { return getConnection().thenCompose(sqlConnection -> { CompletableFuture<Long> cf = new VertxCompletableFuture<>(vertx); sqlConnection.update(query.getSQL(ParamType.INLINED), executeAndClose(updateResult->updateResult.getKeys().getLong(0), sqlConnection, cf)); return cf; }); }
@Override public CompletableFuture<Long> insertReturning(Query query) { return getConnection().thenCompose(sqlConnection -> { CompletableFuture<Long> cf = new VertxCompletableFuture<>(vertx); sqlConnection.update(query.getSQL(ParamType.INLINED), executeAndClose(updateResult->updateResult.getKeys().getLong(0), sqlConnection, cf)); return cf; }); }
@Test public void testUnhandledExceptionInHandlerUpdateResult(TestContext testContext) { this.<UpdateResult>testUnhandledExceptionInHandler(testContext, (sqlConnection, handler) -> { sqlConnection.update("INSERT INTO test_table (name) VALUES ('pimpo')", handler); }); }
@Test public void testUnhandledExceptionInHandlerUpdateResult(TestContext testContext) { this.<UpdateResult>testUnhandledExceptionInHandler(testContext, (sqlConnection, handler) -> { sqlConnection.update("INSERT INTO test_table (name) VALUES ('pimpo')", handler); }); }
@Test public void testUnhandledExceptionInHandlerUpdateResult(TestContext testContext) { this.<UpdateResult>testUnhandledExceptionInHandler(testContext, (sqlConnection, handler) -> { sqlConnection.update("INSERT INTO test_table (name) VALUES ('pimpo')", handler); }); }
private void setupTestTable(SQLConnection conn, Supplier<String> idNameValuesSupplier, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute(CREATE_TABLE_STATEMENT, ar3 -> conn.update("INSERT INTO test_table (id, name) VALUES " + idNameValuesSupplier.get(), ar4 -> conn.execute("COMMIT", handler))))); }
private void setupTestTable(SQLConnection conn, Supplier<String> idNameValuesSupplier, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute(CREATE_TABLE_STATEMENT, ar3 -> conn.update("INSERT INTO test_table (id, name) VALUES " + idNameValuesSupplier.get(), ar4 -> conn.execute("COMMIT", handler))))); }
private void setupSimpleTable(SQLConnection conn, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute(CREATE_TABLE_STATEMENT, ar3 -> conn.update("INSERT INTO test_table (id, name) VALUES " + Data.get(), ar4 -> conn.execute("COMMIT", handler))))); }
private void setupSimpleTable(SQLConnection conn, Handler<AsyncResult<Void>> handler) { conn.execute("BEGIN", ar -> conn.execute("DROP TABLE IF EXISTS test_table", ar2 -> conn.execute(CREATE_TABLE_STATEMENT, ar3 -> conn.update("INSERT INTO test_table (id, name) VALUES " + Data.get(), ar4 -> conn.execute("COMMIT", handler))))); }
@Test public void testNaturalInsert() { String sql = "INSERT INTO insert_table2 VALUES (1, 'doe', 'john', '2001-01-01');"; connection().update(sql, onSuccess(result -> { assertUpdate(result, 1); testComplete(); })); await(); }
@Test public void testInsert() { String sql = "INSERT INTO insert_table VALUES (null, 'doe', 'john', '2001-01-01');"; connection().update(sql, onSuccess(result -> { assertUpdate(result, 1); testComplete(); })); await(); }
@Test public void testUpdateNoMatch() { SQLConnection conn = connection(); String sql = "UPDATE update_table SET fname='jane' WHERE id = -231"; conn.update(sql, onSuccess(result -> { assertUpdate(result, 0); testComplete(); })); await(); }
@Test public void testDelete() { String sql = "DELETE FROM delete_table WHERE id = 1;"; connection().update(sql, onSuccess(result -> { assertNotNull(result); assertEquals(1, result.getUpdated()); testComplete(); })); await(); }
@Test public void testReturnIds() { connection().update("insert into customers(firstname, lastname) values('Paulo', 'Lopes')", onSuccess(updateResult -> { assertNotNull(updateResult); assertNotNull(updateResult.getKeys()); assertTrue(updateResult.getKeys().size() > 0); testComplete(); })); await(); }