package io.vertx.pgclient.impl;
import io.vertx.core.impl.CloseFuture;
import io.vertx.core.impl.ContextInternal;
import io.vertx.core.spi.metrics.ClientMetrics;
import io.vertx.core.spi.metrics.VertxMetrics;
import io.vertx.pgclient.*;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.impl.Connection;
import io.vertx.sqlclient.impl.PoolBase;
import io.vertx.sqlclient.impl.SqlConnectionImpl;
import io.vertx.core.*;
import io.vertx.sqlclient.impl.tracing.QueryTracer;
public class PgPoolImpl extends PoolBase<PgPoolImpl> implements PgPool {
public static PgPoolImpl create(ContextInternal context, boolean closeVertx, PgConnectOptions connectOptions, PoolOptions poolOptions) {
QueryTracer tracer = context.tracer() == null ? null : new QueryTracer(context.tracer(), connectOptions);
VertxMetrics vertxMetrics = context.owner().metricsSPI();
ClientMetrics metrics = vertxMetrics != null ? vertxMetrics.createClientMetrics(connectOptions.getSocketAddress(), "sql", connectOptions.getMetricsName()) : null;
PgPoolImpl pool = new PgPoolImpl(context, new PgConnectionFactory(context, connectOptions), tracer, metrics, poolOptions);
CloseFuture closeFuture = pool.closeFuture();
if (closeVertx) {
closeFuture.onComplete(ar -> context.owner().close());
} else {
context.addCloseHook(closeFuture);
}
return pool;
}
private final PgConnectionFactory factory;
private PgPoolImpl(ContextInternal context, PgConnectionFactory factory, QueryTracer tracer, ClientMetrics metrics, PoolOptions poolOptions) {
super(context, factory, tracer, metrics, poolOptions);
this.factory = factory;
}
@Override
public int appendQueryPlaceholder(StringBuilder queryBuilder, int index, int current) {
queryBuilder.append('$').append(1 + index);
return index;
}
@Override
public void connect(Handler<AsyncResult<Connection>> completionHandler) {
factory.connect().onComplete(completionHandler);
}
@Override
protected SqlConnectionImpl wrap(ContextInternal context, Connection conn) {
return new PgConnectionImpl(factory, context, conn, tracer, metrics);
}
}