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
348 lines (299 loc) · 10.3 KB
/
PgConnectOptions.java
File metadata and controls
348 lines (299 loc) · 10.3 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
/*
* Copyright (C) 2017 Julien Viet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
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")));
}
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 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 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;
}
}
public PgConnectOptions(PgConnectOptions other) {
super(other);
pipeliningLimit = other.pipeliningLimit;
sslMode = other.sslMode;
}
@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 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;
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + pipeliningLimit;
result = 31 * result + sslMode.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);
}
}