Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@
package io.vertx.sqlclient;

import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.VertxGen;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.sqlclient.spi.DatabaseMetadata;

import java.util.List;

/**
* A connection to the database server.
*
Expand All @@ -42,11 +45,27 @@ public interface SqlConnection extends SqlClient {
@Fluent
SqlConnection prepare(String sql, Handler<AsyncResult<PreparedStatement>> handler);

/**
* Create a prepared statement using the given {@code sql} string and parameter types.
*
* @param sql the sql
* @param parameterTypes types of the prepared query parameters
* @param handler the handler notified with the prepared query asynchronously
*/
@Fluent
SqlConnection prepare(String sql, List<Class<?>> parameterTypes, Handler<AsyncResult<PreparedStatement>> handler);

/**
* Like {@link #prepare(String, Handler)} but returns a {@code Future} of the asynchronous result
*/
Future<PreparedStatement> prepare(String sql);

/**
* Like {@link #prepare(String, List, Handler)} but returns a {@code Future} of the asynchronous result
*/
@GenIgnore
Future<PreparedStatement> prepare(String sql, List<Class<?>> parameterTypes);

/**
* Set an handler called with connection errors.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import io.vertx.core.*;
import io.vertx.sqlclient.impl.tracing.QueryTracer;

import java.util.List;

/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
Expand All @@ -47,9 +49,21 @@ public C prepare(String sql, Handler<AsyncResult<PreparedStatement>> handler) {
return (C)this;
}

public C prepare(String sql, List<Class<?>> parameterTypes, Handler<AsyncResult<PreparedStatement>> handler) {
Future<PreparedStatement> fut = prepare(sql, parameterTypes);
if (handler != null) {
fut.onComplete(handler);
}
return (C)this;
}

public Future<PreparedStatement> prepare(String sql) {
return prepare(sql, (List<Class<?>>)null);
}

public Future<PreparedStatement> prepare(String sql, List<Class<?>> parameterTypes) {
Promise<io.vertx.sqlclient.impl.PreparedStatement> promise = promise();
schedule(new PrepareStatementCommand(sql, true), promise);
schedule(new PrepareStatementCommand(sql, true, parameterTypes), promise);
return promise.future().compose(
cr -> Future.succeededFuture(PreparedStatementImpl.create(conn, tracer, metrics, context, cr, autoCommit())),
err -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vertx.core.Vertx;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import io.vertx.ext.unit.impl.TestContextImpl;
import io.vertx.sqlclient.*;
import io.vertx.sqlclient.impl.RowStreamInternal;
import org.junit.After;
Expand Down Expand Up @@ -208,6 +209,24 @@ public void testPreparedUpdateWithParams(TestContext ctx) {
}));
}

@Test
public void testPreparedUpdateWithGivenTypes(TestContext ctx) {
connector.connect(ctx.asyncAssertSuccess(conn -> {
List<Class<?>> paramTypes = Arrays.asList(String.class, String.class);
conn
.prepare(statement("INSERT INTO mutable (id, val) VALUES(", ",", ")"),
paramTypes)
.compose(it -> it.query().execute(Tuple.of("value", "123456")))
.onComplete(res -> {
if (res.succeeded()) {
ctx.assertEquals(1, res.result().rowCount());
} else {
((TestContextImpl)ctx).failed(res.cause());
}
});
}));
}

@Test
public void testPreparedUpdateWithNullParams(TestContext ctx) {
connector.connect(ctx.asyncAssertSuccess(conn -> {
Expand Down