forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPgConnectOptions.java
More file actions
396 lines (343 loc) · 12.2 KB
/
Copy pathPgConnectOptions.java
File metadata and controls
396 lines (343 loc) · 12.2 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (c) 2011-2026 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.pgclient;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.annotations.Unstable;
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.ClientSSLOptions;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.pgclient.impl.PgConnectionUriParser;
import io.vertx.sqlclient.SqlConnectOptions;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import static java.lang.Integer.parseInt;
import static java.lang.System.getenv;
/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
* @author Billy Yuan <billy112487983@gmail.com>
*/
@DataObject
@JsonGen(publicConverter = false)
public class PgConnectOptions extends SqlConnectOptions {
/**
* @return the {@code options} as PostgreSQL specific connect options
*/
public static PgConnectOptions wrap(SqlConnectOptions options) {
if (options instanceof PgConnectOptions) {
return (PgConnectOptions) options;
} else {
return new PgConnectOptions(options);
}
}
/**
* Provide a {@link PgConnectOptions} configured from a connection URI.
*
* @param connectionUri the connection URI to configure from
* @return a {@link PgConnectOptions} parsed from the connection URI
* @throws IllegalArgumentException when the {@code connectionUri} is in an invalid format
*/
public static PgConnectOptions fromUri(String connectionUri) throws IllegalArgumentException {
JsonObject parsedConfiguration = PgConnectionUriParser.parse(connectionUri);
return new PgConnectOptions(parsedConfiguration);
}
/**
* Provide a {@link PgConnectOptions} configured with environment variables, if the environment variable
* is not set, then a default value will take precedence over this.
*/
public static PgConnectOptions fromEnv() {
PgConnectOptions pgConnectOptions = new PgConnectOptions();
if (getenv("PGHOSTADDR") == null) {
if (getenv("PGHOST") != null) {
pgConnectOptions.setHost(getenv("PGHOST"));
}
} else {
pgConnectOptions.setHost(getenv("PGHOSTADDR"));
}
if (getenv("PGPORT") != null) {
try {
pgConnectOptions.setPort(parseInt(getenv("PGPORT")));
} catch (NumberFormatException e) {
// port will be set to default
}
}
if (getenv("PGDATABASE") != null) {
pgConnectOptions.setDatabase(getenv("PGDATABASE"));
}
if (getenv("PGUSER") != null) {
pgConnectOptions.setUser(getenv("PGUSER"));
}
if (getenv("PGPASSWORD") != null) {
pgConnectOptions.setPassword(getenv("PGPASSWORD"));
}
if (getenv("PGSSLMODE") != null) {
pgConnectOptions.setSslMode(SslMode.of(getenv("PGSSLMODE")));
}
if (getenv("PGSSLNEGOTIATION") != null) {
pgConnectOptions.setSslNegotiation(SslNegotiation.of(getenv("PGSSLNEGOTIATION")));
}
if (getenv("PGCHANNELBINDING") != null) {
pgConnectOptions.setChannelBinding(ChannelBinding.of(getenv("PGCHANNELBINDING")));
}
return pgConnectOptions;
}
public static final String DEFAULT_HOST = "localhost";
public static int DEFAULT_PORT = 5432;
public static final String DEFAULT_DATABASE = "db";
public static final String DEFAULT_USER = "user";
public static final String DEFAULT_PASSWORD = "pass";
public static final int DEFAULT_PIPELINING_LIMIT = 256;
public static final SslMode DEFAULT_SSLMODE = SslMode.DISABLE;
public static final SslNegotiation DEFAULT_SSL_NEGOTIATION = SslNegotiation.POSTGRES;
public static final ChannelBinding DEFAULT_CHANNEL_BINDING = ChannelBinding.PREFER;
public static final boolean DEFAULT_USE_LAYER_7_PROXY = false;
public static final Map<String, String> DEFAULT_PROPERTIES;
static {
Map<String, String> defaultProperties = new HashMap<>();
defaultProperties.put("application_name", "vertx-pg-client");
defaultProperties.put("client_encoding", "utf8");
defaultProperties.put("DateStyle", "ISO");
defaultProperties.put("extra_float_digits", "2");
DEFAULT_PROPERTIES = Collections.unmodifiableMap(defaultProperties);
}
private int pipeliningLimit = DEFAULT_PIPELINING_LIMIT;
private SslMode sslMode = DEFAULT_SSLMODE;
private SslNegotiation sslNegotiation = DEFAULT_SSL_NEGOTIATION;
private ChannelBinding channelBinding = DEFAULT_CHANNEL_BINDING;
private boolean useLayer7Proxy = DEFAULT_USE_LAYER_7_PROXY;
public PgConnectOptions() {
super();
}
public PgConnectOptions(JsonObject json) {
super(json);
PgConnectOptionsConverter.fromJson(json, this);
}
public PgConnectOptions(SqlConnectOptions other) {
super(other);
if (other instanceof PgConnectOptions) {
PgConnectOptions opts = (PgConnectOptions) other;
pipeliningLimit = opts.pipeliningLimit;
sslMode = opts.sslMode;
sslNegotiation = opts.sslNegotiation;
channelBinding = opts.channelBinding;
}
}
public PgConnectOptions(PgConnectOptions other) {
super(other);
pipeliningLimit = other.pipeliningLimit;
sslMode = other.sslMode;
sslNegotiation = other.sslNegotiation;
channelBinding = other.channelBinding;
}
@Override
public PgConnectOptions setHost(String host) {
return (PgConnectOptions) super.setHost(host);
}
@Override
public PgConnectOptions setPort(int port) {
return (PgConnectOptions) super.setPort(port);
}
@Override
public PgConnectOptions setUser(String user) {
return (PgConnectOptions) super.setUser(user);
}
@Override
public PgConnectOptions setPassword(String password) {
return (PgConnectOptions) super.setPassword(password);
}
@Override
public PgConnectOptions setDatabase(String database) {
return (PgConnectOptions) super.setDatabase(database);
}
public int getPipeliningLimit() {
return pipeliningLimit;
}
public PgConnectOptions setPipeliningLimit(int pipeliningLimit) {
if (pipeliningLimit < 1) {
throw new IllegalArgumentException();
}
this.pipeliningLimit = pipeliningLimit;
return this;
}
@Override
public PgConnectOptions setCachePreparedStatements(boolean cachePreparedStatements) {
return (PgConnectOptions) super.setCachePreparedStatements(cachePreparedStatements);
}
@Override
public PgConnectOptions setPreparedStatementCacheMaxSize(int preparedStatementCacheMaxSize) {
return (PgConnectOptions) super.setPreparedStatementCacheMaxSize(preparedStatementCacheMaxSize);
}
@GenIgnore
@Override
public PgConnectOptions setPreparedStatementCacheSqlFilter(Predicate<String> predicate) {
return (PgConnectOptions) super.setPreparedStatementCacheSqlFilter(predicate);
}
@Override
public PgConnectOptions setPreparedStatementCacheSqlLimit(int preparedStatementCacheSqlLimit) {
return (PgConnectOptions) super.setPreparedStatementCacheSqlLimit(preparedStatementCacheSqlLimit);
}
@Override
public PgConnectOptions setProperties(Map<String, String> properties) {
return (PgConnectOptions) super.setProperties(properties);
}
@GenIgnore
@Override
public PgConnectOptions addProperty(String key, String value) {
return (PgConnectOptions) super.addProperty(key, value);
}
/**
* @return the value of current sslmode
*/
public SslMode getSslMode() {
return sslMode;
}
/**
* Set {@link SslMode} for the client, this option can be used to provide different levels of secure protection.
*
* @param sslmode the value of sslmode
* @return a reference to this, so the API can be used fluently
*/
public PgConnectOptions setSslMode(SslMode sslmode) {
this.sslMode = sslmode;
return this;
}
/**
* @return the value of current SSL negotiation mode
*/
public SslNegotiation getSslNegotiation() {
return sslNegotiation;
}
/**
* Set {@link SslNegotiation} for the client, this option controls how SSL/TLS is negotiated with the server.
*
* @param sslNegotiation the SSL negotiation mode
* @return a reference to this, so the API can be used fluently
*/
public PgConnectOptions setSslNegotiation(SslNegotiation sslNegotiation) {
this.sslNegotiation = sslNegotiation;
return this;
}
/**
* @return the value of current Channel Binding mode
*/
public ChannelBinding getChannelBinding() {
return channelBinding;
}
/**
* Set {@link ChannelBinding} for the client, this option controls the client's use of channel binding.
*
* @param channelBinding the channel binding mode
* @return a reference to this, so the API can be used fluently
*/
public PgConnectOptions setChannelBinding(ChannelBinding channelBinding) {
this.channelBinding = channelBinding;
return this;
}
/**
* @return whether the client interacts with a layer 7 proxy instead of a server
*/
@Unstable
public boolean getUseLayer7Proxy() {
return useLayer7Proxy;
}
/**
* Set the client to use a layer 7 (application) proxy compatible protocol, set to {@code true} when the client
* interacts with a layer 7 proxy like PgBouncer instead of a server. Prepared statement caching must be disabled.
*
* @param useLayer7Proxy whether to use a layer 7 proxy instead of a server
* @return a reference to this, so the API can be used fluently
*/
@Unstable
public PgConnectOptions setUseLayer7Proxy(boolean useLayer7Proxy) {
this.useLayer7Proxy = useLayer7Proxy;
return this;
}
@Override
public PgConnectOptions setReconnectAttempts(int attempts) {
return (PgConnectOptions)super.setReconnectAttempts(attempts);
}
@Override
public PgConnectOptions setReconnectInterval(long interval) {
return (PgConnectOptions)super.setReconnectInterval(interval);
}
@Override
public PgConnectOptions setTracingPolicy(TracingPolicy tracingPolicy) {
return (PgConnectOptions) super.setTracingPolicy(tracingPolicy);
}
@Override
public PgConnectOptions setSslOptions(ClientSSLOptions sslOptions) {
return (PgConnectOptions) super.setSslOptions(sslOptions);
}
/**
* Initialize with the default options.
*/
@Override
protected void init() {
super.init();
this.setHost(DEFAULT_HOST);
this.setPort(DEFAULT_PORT);
this.setUser(DEFAULT_USER);
this.setPassword(DEFAULT_PASSWORD);
this.setDatabase(DEFAULT_DATABASE);
this.setMetricsName(DEFAULT_METRICS_NAME);
this.setProperties(new HashMap<>(DEFAULT_PROPERTIES));
}
@Override
public JsonObject toJson() {
JsonObject json = super.toJson();
PgConnectOptionsConverter.toJson(this, json);
return json;
}
@Override
@GenIgnore
public SocketAddress getSocketAddress() {
if (!isUsingDomainSocket()) {
return super.getSocketAddress();
} else {
return SocketAddress.domainSocketAddress(getHost() + "/.s.PGSQL." + getPort());
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PgConnectOptions)) return false;
if (!super.equals(o)) return false;
PgConnectOptions that = (PgConnectOptions) o;
if (pipeliningLimit != that.pipeliningLimit) return false;
if (sslMode != that.sslMode) return false;
if (sslNegotiation != that.sslNegotiation) return false;
if (channelBinding != that.channelBinding) return false;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + pipeliningLimit;
result = 31 * result + sslMode.hashCode();
result = 31 * result + sslNegotiation.hashCode();
result = 31 * result + channelBinding.hashCode();
return result;
}
@Override
public boolean isUsingDomainSocket() {
return this.getHost().startsWith("/");
}
@Override
public PgConnectOptions merge(JsonObject other) {
JsonObject json = toJson();
json.mergeIn(other);
return new PgConnectOptions(json);
}
}