forked from eclipse-vertx/vertx-sql-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMSSQLConnectOptions.java
More file actions
253 lines (218 loc) · 7.63 KB
/
MSSQLConnectOptions.java
File metadata and controls
253 lines (218 loc) · 7.63 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
/*
* Copyright (c) 2011-2022 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.mssqlclient;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.ClientSSLOptions;
import io.vertx.mssqlclient.impl.MSSQLConnectionUriParser;
import io.vertx.sqlclient.SqlConnectOptions;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
/**
* Connect options for configuring {@link MSSQLConnection}.
*/
@DataObject
@JsonGen(publicConverter = false)
public class MSSQLConnectOptions extends SqlConnectOptions {
/**
* @return the {@code options} as MSSQL specific connect options
*/
public static MSSQLConnectOptions wrap(SqlConnectOptions options) {
if (options instanceof MSSQLConnectOptions) {
return (MSSQLConnectOptions) options;
} else {
return new MSSQLConnectOptions(options);
}
}
/**
* Provide a {@link MSSQLConnectOptions} configured from a connection URI.
*
* @param connectionUri the connection URI to configure from
* @return a {@link MSSQLConnectOptions} parsed from the connection URI
* @throws IllegalArgumentException when the {@code connectionUri} is in an invalid format
*/
public static MSSQLConnectOptions fromUri(String connectionUri) throws IllegalArgumentException {
JsonObject parsedConfiguration = MSSQLConnectionUriParser.parse(connectionUri);
return new MSSQLConnectOptions(parsedConfiguration);
}
public static final String DEFAULT_HOST = "localhost";
public static final int DEFAULT_PORT = 1433;
public static final String DEFAULT_USER = "sa";
public static final String DEFAULT_PASSWORD = "";
public static final String DEFAULT_DATABASE = "";
public static final String DEFAULT_APP_NAME = "vertx-mssql-client";
public static final String DEFAULT_CLIENT_INTERFACE_NAME = "Vert.x";
public static final Map<String, String> DEFAULT_PROPERTIES;
public static final int MIN_PACKET_SIZE = 512;
public static final int MAX_PACKET_SIZE = 32767;
public static final int DEFAULT_PACKET_SIZE = 4096;
public static final boolean DEFAULT_SSL = false;
static {
Map<String, String> defaultProperties = new HashMap<>();
defaultProperties.put("appName", DEFAULT_APP_NAME);
defaultProperties.put("clientInterfaceName", DEFAULT_CLIENT_INTERFACE_NAME);
DEFAULT_PROPERTIES = defaultProperties;
}
private boolean ssl;
private int packetSize;
public MSSQLConnectOptions() {
super();
}
public MSSQLConnectOptions(JsonObject json) {
super(json);
MSSQLConnectOptionsConverter.fromJson(json, this);
}
public MSSQLConnectOptions(SqlConnectOptions other) {
super(other);
if (other instanceof MSSQLConnectOptions) {
MSSQLConnectOptions opts = (MSSQLConnectOptions) other;
copyFields(opts);
}
}
public MSSQLConnectOptions(MSSQLConnectOptions other) {
super(other);
copyFields(other);
}
private void copyFields(MSSQLConnectOptions other) {
packetSize = other.packetSize;
ssl = other.ssl;
}
@Override
public MSSQLConnectOptions setHost(String host) {
return (MSSQLConnectOptions) super.setHost(host);
}
@Override
public MSSQLConnectOptions setPort(int port) {
return (MSSQLConnectOptions) super.setPort(port);
}
@Override
public MSSQLConnectOptions setUser(String user) {
return (MSSQLConnectOptions) super.setUser(user);
}
@Override
public MSSQLConnectOptions setPassword(String password) {
return (MSSQLConnectOptions) super.setPassword(password);
}
@Override
public MSSQLConnectOptions setDatabase(String database) {
return (MSSQLConnectOptions) super.setDatabase(database);
}
@Override
public MSSQLConnectOptions setProperties(Map<String, String> properties) {
return (MSSQLConnectOptions) super.setProperties(properties);
}
@Override
public MSSQLConnectOptions addProperty(String key, String value) {
return (MSSQLConnectOptions) super.addProperty(key, value);
}
/**
* Get the desired size (in bytes) for TDS packets.
*
* @return the desired packet size
*/
public int getPacketSize() {
return packetSize;
}
/**
* Set the desired size (in bytes) for TDS packets.
* <p>
* The client will use the value as a parameter in the LOGIN7 packet.
* The server may or may not accept it.
*
* @param packetSize the desired packet size (in bytes)
* @return a reference to this, so the API can be used fluently
* @throws IllegalArgumentException if {@code packetSize} is smaller than {@link #MIN_PACKET_SIZE} or bigger than {@link #MAX_PACKET_SIZE}
*/
public MSSQLConnectOptions setPacketSize(int packetSize) {
if (packetSize < MIN_PACKET_SIZE || packetSize > MAX_PACKET_SIZE) {
throw new IllegalArgumentException("Packet size: " + packetSize);
}
this.packetSize = packetSize;
return this;
}
/**
*
* @return is SSL/TLS enabled?
*/
public boolean isSsl() {
return ssl;
}
/**
* Set whether SSL/TLS is enabled
*
* @param ssl true if enabled
* @return a reference to this, so the API can be used fluently
*/
public MSSQLConnectOptions setSsl(boolean ssl) {
this.ssl = ssl;
return this;
}
@Override
public MSSQLConnectOptions setReconnectAttempts(int attempts) {
return (MSSQLConnectOptions) super.setReconnectAttempts(attempts);
}
@Override
public MSSQLConnectOptions setReconnectInterval(long interval) {
return (MSSQLConnectOptions) super.setReconnectInterval(interval);
}
@Override
public MSSQLConnectOptions setCachePreparedStatements(boolean cachePreparedStatements) {
return (MSSQLConnectOptions) super.setCachePreparedStatements(cachePreparedStatements);
}
@Override
public MSSQLConnectOptions setPreparedStatementCacheMaxSize(int preparedStatementCacheMaxSize) {
return (MSSQLConnectOptions) super.setPreparedStatementCacheMaxSize(preparedStatementCacheMaxSize);
}
@GenIgnore
@Override
public MSSQLConnectOptions setPreparedStatementCacheSqlFilter(Predicate<String> predicate) {
return (MSSQLConnectOptions) super.setPreparedStatementCacheSqlFilter(predicate);
}
@Override
public MSSQLConnectOptions setPreparedStatementCacheSqlLimit(int preparedStatementCacheSqlLimit) {
return (MSSQLConnectOptions) super.setPreparedStatementCacheSqlLimit(preparedStatementCacheSqlLimit);
}
@Override
public MSSQLConnectOptions setSslOptions(ClientSSLOptions sslOptions) {
return (MSSQLConnectOptions) 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));
packetSize = DEFAULT_PACKET_SIZE;
ssl = DEFAULT_SSL;
}
@Override
public JsonObject toJson() {
JsonObject json = super.toJson();
MSSQLConnectOptionsConverter.toJson(this, json);
return json;
}
@Override
public MSSQLConnectOptions merge(JsonObject other) {
JsonObject json = toJson();
json.mergeIn(other);
return new MSSQLConnectOptions(json);
}
}