private void handleAddProduct(RoutingContext routingContext) { HttpServerResponse response = routingContext.response(); SQLConnection conn = routingContext.get("conn"); JsonObject product = routingContext.getBodyAsJson(); conn.updateWithParams("INSERT INTO products (name, price, weight) VALUES (?, ?, ?)", new JsonArray().add(product.getString("name")).add(product.getFloat("price")).add(product.getInteger("weight")), query -> { if (query.failed()) { sendError(500, response); } else { response.end(); } }); }
/** * Executes the given prepared statement which may be an <code>INSERT</code>, <code>UPDATE</code>, or <code>DELETE</code> * statement with the given parameters * @param sql the SQL to execute. For example <code>INSERT INTO table ...</code> * @param params these are the parameters to fill the statement. * @param resultHandler the handler which is called once the operation completes. * @return */ public io.vertx.rxjava.ext.sql.SQLConnection updateWithParams(String sql, JsonArray params, Handler<AsyncResult<UpdateResult>> resultHandler) { delegate.updateWithParams(sql, params, resultHandler); return this; }
/** * Executes the given prepared statement which may be an <code>INSERT</code>, <code>UPDATE</code>, or <code>DELETE</code> * statement with the given parameters * @param sql the SQL to execute. For example <code>INSERT INTO table ...</code> * @param params these are the parameters to fill the statement. * @param resultHandler the handler which is called once the operation completes. * @return */ public io.vertx.rxjava.ext.sql.SQLConnection updateWithParams(String sql, JsonArray params, Handler<AsyncResult<UpdateResult>> resultHandler) { delegate.updateWithParams(sql, params, resultHandler); return this; }
@Override public SQLConnection updateWithParams(String sql, JsonArray params, Handler<AsyncResult<UpdateResult>> resultHandler) { delegate.updateWithParams(sql, params, resultHandler); return this; }
protected <R> void execute(JsonArray params, String sql, R ret, Handler<AsyncResult<R>> resultHandler) { client.getConnection(connHandler(resultHandler, connection -> { connection.updateWithParams(sql, params, r -> { if (r.succeeded()) { resultHandler.handle(Future.succeededFuture(ret)); } else { resultHandler.handle(Future.failedFuture(r.cause())); } connection.close(); }); })); }
protected <K> void removeOne(K id, String sql, Handler<AsyncResult<Void>> resultHandler) { client.getConnection(connHandler(resultHandler, connection -> { JsonArray params = new JsonArray().add(id); connection.updateWithParams(sql, params, r -> { if (r.succeeded()) { resultHandler.handle(Future.succeededFuture()); } else { resultHandler.handle(Future.failedFuture(r.cause())); } connection.close(); }); })); }
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) { io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.updateWithParams(sql, params != null ? io.vertx.core.impl.ConversionHelper.toJsonArray(params) : null, 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 call(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) {
/** * Suitable for `add`, `exists` operation. * * @param params query params * @param sql sql * @param resultHandler async result handler */ protected void executeNoResult(JsonArray params, String sql, Handler<AsyncResult<Void>> resultHandler) { client.getConnection(connHandler(resultHandler, connection -> { connection.updateWithParams(sql, params, r -> { if (r.succeeded()) { resultHandler.handle(Future.succeededFuture()); } else { resultHandler.handle(Future.failedFuture(r.cause())); } connection.close(); }); })); }
/** * @see io.apiman.gateway.engine.components.jdbc.IJdbcConnection#execute(io.apiman.gateway.engine.async.IAsyncResultHandler, java.lang.String, java.lang.Object[]) */ @Override public void execute(IAsyncResultHandler<Void> handler, String sql, Object... params) { connection.updateWithParams(sql, toJsonArray(params), translateVoidHandlers(handler)); }
@Override public CompletableFuture<Integer> execute(Query query){ return getConnection().thenCompose(sqlConnection -> { CompletableFuture<Integer> cf = new VertxCompletableFuture<>(vertx); JsonArray bindValues = getBindValues(query); sqlConnection.updateWithParams(query.getSQL(), bindValues, executeAndClose(UpdateResult::getUpdated,sqlConnection,cf)); return cf; }); }
@Override public CompletableFuture<Integer> execute(Query query){ return getConnection().thenCompose(sqlConnection -> { CompletableFuture<Integer> cf = new VertxCompletableFuture<>(vertx); JsonArray bindValues = getBindValues(query); sqlConnection.updateWithParams(query.getSQL(), bindValues, executeAndClose(UpdateResult::getUpdated,sqlConnection,cf)); return cf; }); }
@Override public void addPaymentRecord(Payment payment, Handler<AsyncResult<Void>> resultHandler) { jdbc.getConnection(connHandler(resultHandler, connection -> { connection.updateWithParams(INSERT_STATEMENT, new JsonArray().add(payment.getPayId()) .add(payment.getPayAmount()) .add(payment.getPaySource()) .add(payment.getPaymentTime()), r -> { if (r.succeeded()) { resultHandler.handle(Future.succeededFuture()); } else { resultHandler.handle(Future.failedFuture(r.cause())); } connection.close(); }); })); }
@Override public CompletableFuture<Integer> execute(Function<DSLContext, ? extends Query> queryFunction) { return getConnection().thenCompose(safeExecute(sqlConnection -> { Query query = createQuery(queryFunction); log(query); CompletableFuture<Integer> cf = new VertxCompletableFuture<>(vertx); JsonArray bindValues = getBindValues(query); sqlConnection.updateWithParams(query.getSQL(), bindValues, executeAndClose(UpdateResult::getUpdated,sqlConnection,cf)); return cf; })); }
@Test public void testInvalidInsertStatement(TestContext context) { Async async = context.async(); client.getConnection(ar -> { ensureSuccess(context, ar); conn = ar.result(); conn.updateWithParams("INVALID INSERT", new JsonArray(), ar2 -> { if (ar2.failed() && ar2.cause() instanceof com.github.jasync.sql.db.exceptions.DatabaseException) { async.complete(); } else { context.fail("Should receive an exception of type DatabaseException."); } }); }); }
@Test public void testInvalidInsertStatement(TestContext context) { Async async = context.async(); client.getConnection(ar -> { ensureSuccess(context, ar); conn = ar.result(); conn.updateWithParams("INVALID INSERT", new JsonArray(), ar2 -> { if (ar2.failed() && ar2.cause() instanceof com.github.mauricio.async.db.exceptions.DatabaseException) { async.complete(); } else { context.fail("Should receive an exception of type DatabaseException."); } }); }); }
@Test public void testInvalidInsertStatement(TestContext context) { Async async = context.async(); client.getConnection(ar -> { ensureSuccess(context, ar); conn = ar.result(); conn.updateWithParams("INVALID INSERT", new JsonArray(), ar2 -> { if (ar2.failed() && ar2.cause() instanceof com.github.jasync.sql.db.exceptions.DatabaseException) { async.complete(); } else { context.fail("Should receive an exception of type DatabaseException."); } }); }); }
@Test public void testCustomInsert() { String sql = "INSERT INTO t (u, t, d, ts) VALUES (?, ?, ?, ?)"; final String uuid = UUID.randomUUID().toString(); final SQLConnection conn = connection(); conn.setAutoCommit(false, tx -> { if (tx.succeeded()) { conn.updateWithParams(sql, new JsonArray().add(uuid).add("09:00:00").add("2015-03-16").add(Instant.now()), onSuccess(resultSet -> { testComplete(); })); } }); await(); }
@Override public Future<Integer> execute(Function<DSLContext, ? extends Query> queryFunction) { return getConnection().compose(safeExecute(sqlConnection -> { Query query = createQuery(queryFunction); log(query); Future<Integer> future = Future.future(); sqlConnection.updateWithParams( query.getSQL(), getBindValues(query), this.<UpdateResult,Integer>executeAndClose(UpdateResult::getUpdated, sqlConnection, future) ); return future; })); }
@Override public Future<Integer> execute(Function<DSLContext, ? extends Query> queryFunction) { return getConnection().compose(safeExecute(sqlConnection -> { Query query = createQuery(queryFunction); log(query); Future<Integer> future = Future.future(); sqlConnection.updateWithParams( query.getSQL(), getBindValues(query), this.<UpdateResult,Integer>executeAndClose(UpdateResult::getUpdated, sqlConnection, future) ); return future; })); }
@Test public void testDeleteWithParams() { String sql = "DELETE FROM delete_table WHERE id = ?;"; JsonArray params = new JsonArray().add(2); connection().updateWithParams(sql, params, onSuccess(result -> { assertNotNull(result); assertEquals(1, result.getUpdated()); testComplete(); })); await(); }