package io.vertx.sqlclient.impl;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.sqlclient.PreparedStatement;
import io.vertx.sqlclient.SqlClient;
import io.vertx.sqlclient.impl.command.PrepareStatementCommand;
import io.vertx.core.*;
import io.vertx.sqlclient.impl.tracing.QueryTracer;
public abstract class SqlConnectionBase<C extends SqlClient> extends SqlClientBase<C> {
protected final ContextInternal context;
protected final Connection conn;
protected SqlConnectionBase(ContextInternal context, Connection conn, QueryTracer tracer, ClientMetrics metrics) {
super(tracer, metrics);
this.context = context;
this.conn = conn;
}
public C prepare(String sql, Handler<AsyncResult<PreparedStatement>> handler) {
Future<PreparedStatement> fut = prepare(sql);
if (handler != null) {
fut.onComplete(handler);
}
return (C)this;
}
public Future<PreparedStatement> prepare(String sql) {
Promise<io.vertx.sqlclient.impl.PreparedStatement> promise = promise();
schedule(new PrepareStatementCommand(sql, true), promise);
return promise.future().compose(
cr -> Future.succeededFuture(PreparedStatementImpl.create(conn, tracer, metrics, context, cr, autoCommit())),
err -> {
if (conn.isIndeterminatePreparedStatementError(err)) {
return Future.succeededFuture(PreparedStatementImpl.create(conn, tracer, metrics, context, sql, autoCommit()));
} else {
return Future.failedFuture(err);
}
});
}
}