Skip to content

Commit f64bbc1

Browse files
committed
implemented passing properties
1 parent 7be8d90 commit f64bbc1

File tree

10 files changed

+231
-124
lines changed

10 files changed

+231
-124
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,16 @@ public Builder setClientName(String clientName) {
940940
return this;
941941
}
942942

943+
/**
944+
* Sets client options from provided map. Values are copied as is
945+
* @param options - map of client options
946+
* @return same instance of the builder
947+
*/
948+
public Builder setOptions(Map<String, String> options) {
949+
this.configuration.putAll(options);
950+
return this;
951+
}
952+
943953
public Client build() {
944954
setDefaults();
945955

@@ -950,7 +960,7 @@ public Client build() {
950960
// check if username and password are empty. so can not initiate client?
951961
if (!this.configuration.containsKey("access_token") &&
952962
(!this.configuration.containsKey("user") || !this.configuration.containsKey("password")) &&
953-
!MapUtils.getFlag(this.configuration, "ssl_authentication")) {
963+
!MapUtils.getFlag(this.configuration, "ssl_authentication", false)) {
954964
throw new IllegalArgumentException("Username and password (or access token, or SSL authentication) are required");
955965
}
956966

@@ -2105,6 +2115,10 @@ public Set<String> getEndpoints() {
21052115
return Collections.unmodifiableSet(endpoints);
21062116
}
21072117

2118+
public String getUser() {
2119+
return this.configuration.get(ClientConfigProperties.USER.getKey());
2120+
}
2121+
21082122
/**
21092123
* Sets list of DB roles that should be applied to each query.
21102124
*

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public enum ClientConfigProperties {
1717

1818
HTTP_USE_BASIC_AUTH("http_use_basic_auth"),
1919

20-
USER("user"),
20+
USER("user", "default"),
2121

22-
PASSWORD("password"),
22+
PASSWORD("password", ""),
2323

2424
/**
2525
* Maximum number of active connection in internal connection pool.
2626
*/
27-
HTTP_MAX_OPEN_CONNECTIONS("max_open_connections"),
27+
HTTP_MAX_OPEN_CONNECTIONS("max_open_connections", "10"),
2828

2929
/**
3030
* HTTP keep-alive timeout override.
@@ -59,7 +59,7 @@ public enum ClientConfigProperties {
5959

6060
SOCKET_LINGER_OPT("socket_linger"),
6161

62-
DATABASE("database"),
62+
DATABASE("database", "default"),
6363

6464
COMPRESS_SERVER_RESPONSE("compress"), // actually a server setting, but has client effect too
6565

@@ -118,14 +118,37 @@ public enum ClientConfigProperties {
118118

119119
private String key;
120120

121+
private String defaultValue;
122+
123+
private List<String> choices;
124+
125+
121126
ClientConfigProperties(String key) {
127+
this(key, null, Collections.emptyList());
128+
}
129+
130+
ClientConfigProperties(String key, String defaultValue) {
131+
this(key, defaultValue, Collections.emptyList());
132+
}
133+
134+
ClientConfigProperties(String key, String defaultValue, List<String> choices) {
122135
this.key = key;
136+
this.defaultValue = defaultValue;
137+
this.choices = Collections.unmodifiableList(choices);
123138
}
124139

125140
public String getKey() {
126141
return key;
127142
}
128143

144+
public List<String> getChoices() {
145+
return choices;
146+
}
147+
148+
public String getDefaultValue() {
149+
return defaultValue;
150+
}
151+
129152
public static final String HTTP_HEADER_PREFIX = "http_header_";
130153

131154
public static final String SERVER_SETTING_PREFIX = "clickhouse_setting_";

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import com.clickhouse.client.api.query.GenericRecord;
66
import com.clickhouse.client.api.query.QuerySettings;
77
import com.clickhouse.jdbc.internal.ClientInfoProperties;
8-
import com.clickhouse.jdbc.internal.JdbcConfiguration;
98
import com.clickhouse.jdbc.internal.ExceptionUtils;
9+
import com.clickhouse.jdbc.internal.JdbcConfiguration;
1010
import org.slf4j.Logger;
1111
import org.slf4j.LoggerFactory;
1212

@@ -67,24 +67,13 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
6767
clientName += " (" + detectedFrameworks + ")";
6868
}
6969

70-
this.client = new Client.Builder()
71-
.fromUrl(this.config.getUrl())//URL without prefix
72-
.setUsername(config.getUser())
73-
.setPassword(config.getPassword())
70+
this.client = this.config.applyClientProperties(new Client.Builder())
7471
.setClientName(clientName)
7572
.build();
76-
this.schema = client.getDefaultDatabase(); // TODO: fix in properties handling?
73+
this.schema = client.getDefaultDatabase();
7774
this.defaultQuerySettings = new QuerySettings();
7875

79-
this.metadata = new com.clickhouse.jdbc.metadata.DatabaseMetaData(this, false);
80-
}
81-
82-
public String getUser() {
83-
return config.getUser();
84-
}
85-
86-
public String getURL() {
87-
return url;
76+
this.metadata = new com.clickhouse.jdbc.metadata.DatabaseMetaData(this, false, url);
8877
}
8978

9079
public QuerySettings getDefaultQuerySettings() {
@@ -489,6 +478,14 @@ public void setShardingKey(ShardingKey shardingKey) throws SQLException {
489478
Connection.super.setShardingKey(shardingKey);
490479
}
491480

481+
/**
482+
* Returns instance of the client used to execute queries by this connection.
483+
* @return - client instance
484+
*/
485+
public Client getClient() {
486+
return client;
487+
}
488+
492489
private void checkOpen() throws SQLException {
493490
if (isClosed()) {
494491
throw new SQLException("Connection is closed", ExceptionUtils.SQL_STATE_CONNECTION_EXCEPTION);

jdbc-v2/src/main/java/com/clickhouse/jdbc/Driver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public boolean acceptsURL(String url) throws SQLException {
130130

131131
@Override
132132
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
133-
return JdbcConfiguration.getDriverPropertyInfo(info).toArray(new DriverPropertyInfo[0]);
133+
return new JdbcConfiguration(url, info).getDriverPropertyInfo().toArray(new DriverPropertyInfo[0]);
134134
}
135135

136136
public static int getDriverMajorVersion() {
Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
package com.clickhouse.jdbc.internal;
22

3+
import java.util.Collections;
4+
import java.util.List;
5+
36
/**
4-
* JDBC driver specific properties. Do not include any ClickHouse client properties here.
7+
* JDBC driver specific properties. Should not include any of ClientConfigProperties.
8+
* All driver specific properties must have {@code "driver."} prefix to isolate them from everything else
59
*/
610
public enum DriverProperties {
711

8-
PLACEHOLDER("placeholder", "Placeholder for unknown properties");
12+
/**
13+
* query settings to be passed along with query operation.
14+
* {@see com.clickhouse.client.api.query.QuerySettings}
15+
*/
16+
DEFAULT_QUERY_SETTINGS("driver.query_settings", null);
917

1018
private final String key;
1119

12-
private final String description;
20+
private final String defaultValue;
21+
22+
private final List<String> choices;
1323

14-
DriverProperties(String key, String description) {
24+
DriverProperties(String key, String defaultValue) {
25+
this(key, defaultValue, Collections.emptyList());
26+
}
27+
28+
DriverProperties(String key, String defaultValue, List<String> choices) {
1529
this.key = key;
16-
this.description = description;
30+
this.defaultValue = defaultValue;
31+
this.choices = choices;
1732
}
1833

34+
public String getKey() {
35+
return key;
36+
}
37+
38+
public String getDefaultValue() {
39+
return defaultValue;
40+
}
41+
42+
public List<String> getChoices() {
43+
return choices;
44+
}
1945
}

0 commit comments

Comments
 (0)