forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgDriver.java
More file actions
80 lines (67 loc) · 3.09 KB
/
PgDriver.java
File metadata and controls
80 lines (67 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package io.vertx.pgclient.spi;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.internal.CloseFuture;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.VertxInternal;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.NetClientOptions;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.impl.PgConnectionFactory;
import io.vertx.pgclient.impl.PgConnectionImpl;
import io.vertx.pgclient.impl.PgConnectionUriParser;
import io.vertx.pgclient.impl.PgPoolOptions;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnectOptions;
import io.vertx.sqlclient.SqlConnection;
import io.vertx.sqlclient.impl.pool.PoolImpl;
import io.vertx.sqlclient.internal.SqlConnectionInternal;
import io.vertx.sqlclient.spi.DriverBase;
import io.vertx.sqlclient.spi.connection.Connection;
import io.vertx.sqlclient.spi.connection.ConnectionFactory;
import java.util.function.Supplier;
public class PgDriver extends DriverBase<PgConnectOptions> {
private static final String DISCRIMINANT = "pgclient";
public static final PgDriver INSTANCE = new PgDriver();
public PgDriver() {
super(DISCRIMINANT);
}
@Override
protected Pool newPool(VertxInternal vertx, Handler<SqlConnection> connectHandler, Supplier<Future<PgConnectOptions>> databases, PoolOptions poolOptions, NetClientOptions transportOptions, CloseFuture closeFuture) {
boolean pipelinedPool = poolOptions instanceof PgPoolOptions && ((PgPoolOptions) poolOptions).isPipelined();
ConnectionFactory<PgConnectOptions> factory = createConnectionFactory(vertx, transportOptions);
PoolImpl pool = new PoolImpl(vertx, this, pipelinedPool, poolOptions, null, null,
factory, databases, connectHandler, this::wrapConnection, closeFuture);
pool.init();
closeFuture.add(factory);
return pool;
}
@Override
public PgConnectOptions parseConnectionUri(String uri) {
JsonObject conf = PgConnectionUriParser.parse(uri, false);
return conf == null ? null : new PgConnectOptions(conf);
}
@Override
public boolean acceptsOptions(SqlConnectOptions options) {
return options instanceof PgConnectOptions || SqlConnectOptions.class.equals(options.getClass());
}
@Override
public PgConnectOptions downcast(SqlConnectOptions connectOptions) {
return connectOptions instanceof PgConnectOptions ? (PgConnectOptions) connectOptions : new PgConnectOptions(connectOptions);
}
@Override
public ConnectionFactory<PgConnectOptions> createConnectionFactory(Vertx vertx, NetClientOptions transportOptions) {
return new PgConnectionFactory((VertxInternal) vertx, transportOptions);
}
@Override
public int appendQueryPlaceholder(StringBuilder queryBuilder, int index, int current) {
queryBuilder.append('$').append(1 + index);
return index;
}
@Override
public SqlConnectionInternal wrapConnection(ContextInternal context, ConnectionFactory<PgConnectOptions> factory, Connection connection) {
return new PgConnectionImpl((PgConnectionFactory) factory, context, connection);
}
}