Skip to content

Commit 8259695

Browse files
committed
Made ClientConfigProperties as enum for easier use in JDBC
1 parent 3fa1634 commit 8259695

File tree

9 files changed

+254
-234
lines changed

9 files changed

+254
-234
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 81 additions & 80 deletions
Large diffs are not rendered by default.

client-v2/src/main/java/com/clickhouse/client/api/ClientConfigProperties.java

Lines changed: 87 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,137 +7,146 @@
77
import java.util.stream.Collectors;
88

99
/**
10-
* All known client settings at current version.
11-
*
10+
* Enumerates all client properties that are known at release.
1211
*/
13-
public class ClientConfigProperties {
12+
public enum ClientConfigProperties {
1413

15-
public static final String HTTP_HEADER_PREFIX = "http_header_";
16-
17-
public static final String SERVER_SETTING_PREFIX = "clickhouse_setting_";
18-
19-
public static String commaSeparated(Collection<?> values) {
20-
StringBuilder sb = new StringBuilder();
21-
for (Object value : values) {
22-
sb.append(value.toString().replaceAll(",", "\\\\,")).append(",");
23-
}
24-
sb.setLength(sb.length() - 1);
25-
return sb.toString();
26-
}
27-
28-
public static List<String> valuesFromCommaSeparated(String value) {
29-
if (value == null || value.isEmpty()) {
30-
return Collections.emptyList();
31-
}
32-
33-
return Arrays.stream(value.split("(?<!\\\\),")).map(s -> s.replaceAll("\\\\,", ","))
34-
.collect(Collectors.toList());
35-
}
14+
SESSION_DB_ROLES("session_db_roles"),
3615

37-
public static final String SESSION_DB_ROLES = "session_db_roles";
16+
SETTING_LOG_COMMENT(serverSetting("log_comment")),
3817

39-
public static final String SETTING_LOG_COMMENT = SERVER_SETTING_PREFIX + "log_comment";
18+
HTTP_USE_BASIC_AUTH("http_use_basic_auth"),
4019

41-
public static final String HTTP_USE_BASIC_AUTH = "http_use_basic_auth";
20+
USER("user"),
4221

43-
public static final String USER = "user";
44-
45-
public static final String PASSWORD = "password";
22+
PASSWORD("password"),
4623

4724
/**
4825
* Maximum number of active connection in internal connection pool.
4926
*/
50-
public static final String HTTP_MAX_OPEN_CONNECTIONS = "max_open_connections";
27+
HTTP_MAX_OPEN_CONNECTIONS("max_open_connections"),
5128

5229
/**
5330
* HTTP keep-alive timeout override.
5431
*/
55-
public static final String HTTP_KEEP_ALIVE_TIMEOUT = "http_keep_alive_timeout";
32+
HTTP_KEEP_ALIVE_TIMEOUT("http_keep_alive_timeout"),
5633

57-
public static final String USE_SERVER_TIMEZONE = "use_server_time_zone";
34+
USE_SERVER_TIMEZONE("use_server_time_zone"),
5835

59-
public static final String USE_TIMEZONE = "use_time_zone";
36+
USE_TIMEZONE("use_time_zone"),
6037

61-
public static final String SERVER_TIMEZONE = "server_time_zone";
38+
SERVER_TIMEZONE("server_time_zone"),
6239

63-
public static final String ASYNC_OPERATIONS = "async";
40+
ASYNC_OPERATIONS("async"),
6441

65-
public static final String CONNECTION_TTL = "connection_ttl";
42+
CONNECTION_TTL("connection_ttl"),
6643

67-
public static final String CONNECTION_TIMEOUT = "connection_timeout";
44+
CONNECTION_TIMEOUT("connection_timeout"),
6845

69-
public static final String CONNECTION_REUSE_STRATEGY = "connection_reuse_strategy";
46+
CONNECTION_REUSE_STRATEGY("connection_reuse_strategy"),
7047

71-
public static final String SOCKET_OPERATION_TIMEOUT = "socket_timeout";
48+
SOCKET_OPERATION_TIMEOUT("socket_timeout"),
7249

73-
public static final String SOCKET_RCVBUF_OPT = "socket_rcvbuf";
50+
SOCKET_RCVBUF_OPT("socket_rcvbuf"),
7451

75-
public static final String SOCKET_SNDBUF_OPT = "socket_sndbuf";
52+
SOCKET_SNDBUF_OPT("socket_sndbuf"),
7653

77-
public static final String SOCKET_REUSEADDR_OPT = "socket_reuseaddr";
54+
SOCKET_REUSEADDR_OPT("socket_reuseaddr"),
7855

79-
public static final String SOCKET_KEEPALIVE_OPT = "socket_keepalive";
56+
SOCKET_KEEPALIVE_OPT("socket_keepalive"),
8057

81-
public static final String SOCKET_TCP_NO_DELAY_OPT = "socket_tcp_nodelay";
58+
SOCKET_TCP_NO_DELAY_OPT("socket_tcp_nodelay"),
8259

83-
public static final String SOCKET_LINGER_OPT = "socket_linger";
60+
SOCKET_LINGER_OPT("socket_linger"),
8461

85-
public static final String DATABASE = "database";
62+
DATABASE("database"),
8663

87-
public static final String COMPRESS_SERVER_RESPONSE = "compress"; // actually a server setting
64+
COMPRESS_SERVER_RESPONSE("compress"), // actually a server setting, but has client effect too
8865

89-
public static final String COMPRESS_CLIENT_REQUEST = "decompress"; // actually a server setting
66+
COMPRESS_CLIENT_REQUEST("decompress"), // actually a server setting, but has client effect too
9067

91-
public static final String USE_HTTP_COMPRESSION = "client.use_http_compression";
68+
USE_HTTP_COMPRESSION("client.use_http_compression"),
9269

93-
public static final String COMPRESSION_LZ4_UNCOMPRESSED_BUF_SIZE = "compression.lz4.uncompressed_buffer_size";
70+
COMPRESSION_LZ4_UNCOMPRESSED_BUF_SIZE("compression.lz4.uncompressed_buffer_size"),
9471

95-
public static final String PROXY_TYPE = "proxy_type"; // "http"
72+
PROXY_TYPE("proxy_type"), // "http"
9673

97-
public static final String PROXY_HOST = "proxy_host";
74+
PROXY_HOST("proxy_host"),
9875

99-
public static final String PROXY_PORT = "proxy_port";
76+
PROXY_PORT("proxy_port"),
10077

101-
public static final String PROXY_USER = "proxy_user";
78+
PROXY_USER("proxy_user"),
10279

103-
public static final String PROXY_PASSWORD = "proxy_password";
80+
PROXY_PASSWORD("proxy_password"),
10481

105-
public static final String MAX_EXECUTION_TIME = "max_execution_time";
82+
MAX_EXECUTION_TIME("max_execution_time"),
10683

107-
public static final String SSL_TRUST_STORE = "trust_store";
84+
SSL_TRUST_STORE("trust_store"),
10885

109-
public static final String SSL_KEYSTORE_TYPE = "key_store_type";
86+
SSL_KEYSTORE_TYPE("key_store_type"),
11087

111-
public static final String SSL_KEY_STORE = "ssl_key_store";
88+
SSL_KEY_STORE("ssl_key_store"),
11289

113-
public static final String SSL_KEY_STORE_PASSWORD = "key_store_password";
90+
SSL_KEY_STORE_PASSWORD("key_store_password"),
11491

115-
public static final String SSL_KEY = "ssl_key";
92+
SSL_KEY("ssl_key"),
11693

117-
public static final String CA_CERTIFICATE = "sslrootcert";
94+
CA_CERTIFICATE("sslrootcert"),
11895

119-
public static final String SSL_CERTIFICATE = "sslcert";
96+
SSL_CERTIFICATE("sslcert"),
12097

121-
public static final String RETRY_ON_FAILURE = "retry";
98+
RETRY_ON_FAILURE("retry"),
12299

123-
public static final String INPUT_OUTPUT_FORMAT = "format";
100+
INPUT_OUTPUT_FORMAT("format"),
124101

125-
public static final String MAX_THREADS_PER_CLIENT = "max_threads_per_client";
102+
MAX_THREADS_PER_CLIENT("max_threads_per_client"),
126103

127-
public static final String QUERY_ID = "query_id"; // actually a server setting
104+
QUERY_ID("query_id"), // actually a server setting, but has client effect too
128105

129-
public static final String CLIENT_NETWORK_BUFFER_SIZE = "client_network_buffer_size";
106+
CLIENT_NETWORK_BUFFER_SIZE("client_network_buffer_size"),
130107

131-
// -- Experimental features --
132108

133-
/**
134-
* Server will expect a string in JSON format and parse it into a JSON object.
135-
*/
136-
public static final String INPUT_FORMAT_BINARY_READ_JSON_AS_STRING = "input_format_binary_read_json_as_string";
109+
ACCESS_TOKEN("access_token"), SSL_AUTH("ssl_authentication"),
137110

138-
/**
139-
* Server will return a JSON object as a string.
140-
*/
141-
public static final String OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING = "output_format_binary_write_json_as_string";
111+
CONNECTION_POOL_ENABLED("connection_pool_enabled"),
112+
113+
CONNECTION_REQUEST_TIMEOUT("connection_request_timeout"),
114+
115+
CLIENT_RETRY_ON_FAILURE("client_retry_on_failures");
142116

117+
private String key;
118+
119+
ClientConfigProperties(String key) {
120+
this.key = key;
121+
}
122+
123+
public String getKey() {
124+
return key;
125+
}
126+
127+
public static final String HTTP_HEADER_PREFIX = "http_header_";
128+
129+
public static final String SERVER_SETTING_PREFIX = "clickhouse_setting_";
130+
131+
public static String serverSetting(String key) {
132+
return SERVER_SETTING_PREFIX + key;
133+
}
134+
135+
public static String commaSeparated(Collection<?> values) {
136+
StringBuilder sb = new StringBuilder();
137+
for (Object value : values) {
138+
sb.append(value.toString().replaceAll(",", "\\\\,")).append(",");
139+
}
140+
sb.setLength(sb.length() - 1);
141+
return sb.toString();
142+
}
143+
144+
public static List<String> valuesFromCommaSeparated(String value) {
145+
if (value == null || value.isEmpty()) {
146+
return Collections.emptyList();
147+
}
148+
149+
return Arrays.stream(value.split("(?<!\\\\),")).map(s -> s.replaceAll("\\\\,", ","))
150+
.collect(Collectors.toList());
151+
}
143152
}

client-v2/src/main/java/com/clickhouse/client/api/data_formats/internal/AbstractBinaryFormatReader.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.clickhouse.client.api.ClientConfigProperties;
55
import com.clickhouse.client.api.data_formats.ClickHouseBinaryFormatReader;
66
import com.clickhouse.client.api.internal.MapUtils;
7+
import com.clickhouse.client.api.internal.ServerSettings;
78
import com.clickhouse.client.api.metadata.TableSchema;
89
import com.clickhouse.client.api.query.NullValueException;
910
import com.clickhouse.client.api.query.POJOSetter;
@@ -66,14 +67,14 @@ protected AbstractBinaryFormatReader(InputStream inputStream, QuerySettings quer
6667
BinaryStreamReader.ByteBufferAllocator byteBufferAllocator) {
6768
this.input = inputStream;
6869
this.settings = querySettings == null ? Collections.emptyMap() : new HashMap<>(querySettings.getAllSettings());
69-
Boolean useServerTimeZone = (Boolean) this.settings.get(ClientConfigProperties.USE_SERVER_TIMEZONE);
70+
Boolean useServerTimeZone = (Boolean) this.settings.get(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey());
7071
TimeZone timeZone = useServerTimeZone == Boolean.TRUE && querySettings != null ? querySettings.getServerTimeZone() :
71-
(TimeZone) this.settings.get(ClientConfigProperties.USE_TIMEZONE);
72+
(TimeZone) this.settings.get(ClientConfigProperties.USE_TIMEZONE.getKey());
7273
if (timeZone == null) {
7374
throw new ClientException("Time zone is not set. (useServerTimezone:" + useServerTimeZone + ")");
7475
}
7576
boolean jsonAsString = MapUtils.getFlag(this.settings,
76-
ClientConfigProperties.SERVER_SETTING_PREFIX + ClientConfigProperties.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING, false);
77+
ClientConfigProperties.serverSetting(ServerSettings.OUTPUT_FORMAT_BINARY_WRITE_JSON_AS_STRING), false);
7778
this.binaryStreamReader = new BinaryStreamReader(inputStream, timeZone, LOG, byteBufferAllocator, jsonAsString);
7879
if (schema != null) {
7980
setSchema(schema);

client-v2/src/main/java/com/clickhouse/client/api/insert/InsertSettings.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public InsertSettings httpHeaders(Map<String, String> headers) {
189189
* @return same instance of the builder
190190
*/
191191
public InsertSettings serverSetting(String name, String value) {
192-
rawSettings.put(ClientConfigProperties.SERVER_SETTING_PREFIX + name, value);
192+
rawSettings.put(ClientConfigProperties.serverSetting(name), value);
193193
return this;
194194
}
195195

@@ -200,7 +200,7 @@ public InsertSettings serverSetting(String name, String value) {
200200
* @return same instance of the builder
201201
*/
202202
public InsertSettings serverSetting(String name, Collection<String> values) {
203-
rawSettings.put(ClientConfigProperties.SERVER_SETTING_PREFIX + name, ClientConfigProperties.commaSeparated(values));
203+
rawSettings.put(ClientConfigProperties.serverSetting(name), ClientConfigProperties.commaSeparated(values));
204204
return this;
205205
}
206206

@@ -210,7 +210,7 @@ public InsertSettings serverSetting(String name, Collection<String> values) {
210210
* @param dbRoles
211211
*/
212212
public InsertSettings setDBRoles(Collection<String> dbRoles) {
213-
rawSettings.put(ClientConfigProperties.SESSION_DB_ROLES, dbRoles);
213+
rawSettings.put(ClientConfigProperties.SESSION_DB_ROLES.getKey(), dbRoles);
214214
return this;
215215
}
216216

@@ -220,7 +220,7 @@ public InsertSettings setDBRoles(Collection<String> dbRoles) {
220220
* @return list of DB roles
221221
*/
222222
public Collection<String> getDBRoles() {
223-
return (Collection<String>) rawSettings.get(ClientConfigProperties.SESSION_DB_ROLES);
223+
return (Collection<String>) rawSettings.get(ClientConfigProperties.SESSION_DB_ROLES.getKey());
224224
}
225225

226226
/**
@@ -231,7 +231,7 @@ public Collection<String> getDBRoles() {
231231
public InsertSettings logComment(String logComment) {
232232
this.logComment = logComment;
233233
if (logComment != null && !logComment.isEmpty()) {
234-
rawSettings.put(ClientConfigProperties.SETTING_LOG_COMMENT, logComment);
234+
rawSettings.put(ClientConfigProperties.SETTING_LOG_COMMENT.getKey(), logComment);
235235
}
236236
return this;
237237
}

0 commit comments

Comments
 (0)