Skip to content

Commit decd223

Browse files
committed
fixed selecting database thru URL path
1 parent f602b63 commit decd223

File tree

5 files changed

+80
-12
lines changed

5 files changed

+80
-12
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ public void close() {
209209
if (oldClient != null) {
210210
oldClient.close();
211211
}
212+
213+
if (httpClientHelper != null) {
214+
httpClientHelper.close();
215+
}
212216
}
213217

214218
public static class Builder {
@@ -290,7 +294,7 @@ public Builder addEndpoint(String endpoint) {
290294
throw new IllegalArgumentException("Only HTTP and HTTPS protocols are supported");
291295
}
292296
} catch (java.net.MalformedURLException e) {
293-
throw new IllegalArgumentException("Endpoint should be a valid URL string", e);
297+
throw new IllegalArgumentException("Endpoint should be a valid URL string, but was " + endpoint, e);
294298
}
295299
return this;
296300
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.hc.core5.http.impl.io.DefaultHttpResponseParserFactory;
4343
import org.apache.hc.core5.http.io.SocketConfig;
4444
import org.apache.hc.core5.http.io.entity.EntityTemplate;
45+
import org.apache.hc.core5.io.CloseMode;
4546
import org.apache.hc.core5.io.IOCallback;
4647
import org.apache.hc.core5.net.URIBuilder;
4748
import org.apache.hc.core5.pool.PoolConcurrencyPolicy;
@@ -639,4 +640,8 @@ public static Map<String, String> parseUrlParameters(URL url) {
639640

640641
return params;
641642
}
643+
644+
public void close() {
645+
httpClient.close(CloseMode.IMMEDIATE);
646+
}
642647
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.net.MalformedURLException;
1212
import java.net.URI;
13+
import java.net.URISyntaxException;
1314
import java.net.URL;
1415
import java.sql.DriverPropertyInfo;
1516
import java.sql.SQLException;
@@ -50,7 +51,7 @@ public JdbcConfiguration(String url, Properties info) throws SQLException {
5051
this.disableFrameworkDetection = Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false"));
5152
this.clientProperties = new HashMap<>();
5253
this.driverProperties = new HashMap<>();
53-
initProperties(url, info);
54+
initProperties(stripUrlPrefix(url), info);
5455

5556
boolean useSSL = Boolean.parseBoolean(info.getProperty("ssl", "false"));
5657
this.connectionUrl = createConnectionURL(url, useSSL);
@@ -67,8 +68,9 @@ public String getConnectionUrl() {
6768

6869
/**
6970
* Returns normalized URL that can be passed as parameter to Client#addEndpoint().
70-
* Returned url has only schema and authority and doesn't have query parameters.
71-
* Note: Some BI tools do not let pass
71+
* Returned url has only schema and authority and doesn't have query parameters or path.
72+
* JDBC URL should have only a single path parameter to specify database name.
73+
* Note: Some BI tools do not let pass JDBC URL, so ssl is passed as property.
7274
* @param url - JDBC url
7375
* @param ssl - if SSL protocol should be used when protocol is not specified
7476
* @return URL without JDBC prefix
@@ -81,7 +83,7 @@ static String createConnectionURL(String url, boolean ssl) throws SQLException {
8183

8284
try {
8385
URI tmp = URI.create(url);
84-
return tmp.getScheme() + "://" + tmp.getAuthority() + tmp.getPath();
86+
return tmp.getScheme() + "://" + tmp.getAuthority();
8587
} catch (Exception e) {
8688
throw new SQLException("Failed to parse url", e);
8789
}
@@ -100,6 +102,24 @@ private static String stripUrlPrefix(String url) {
100102
List<DriverPropertyInfo> listOfProperties;
101103

102104
private void initProperties(String url, Properties providedProperties) {
105+
106+
// Parse url for database name and override
107+
try {
108+
URI tmp = new URI(url);
109+
String path = tmp.getPath();
110+
if (path != null) {
111+
String[] pathElements = path.split("([\\/]+)+", 3);
112+
if (pathElements.length > 2) {
113+
throw new IllegalArgumentException("There can be only one URL path element indicating a database name");
114+
} else if (pathElements.length == 2) {
115+
providedProperties.put(ClientConfigProperties.DATABASE.getKey(), pathElements[1]);
116+
}
117+
}
118+
} catch (URISyntaxException e) {
119+
throw new IllegalArgumentException("Invalid JDBC URL is specified");
120+
}
121+
122+
// Process properties
103123
Map<String, String> props = new HashMap<>();
104124
for (Map.Entry<Object, Object> entry : providedProperties.entrySet()) {
105125
if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
@@ -128,7 +148,8 @@ private void initProperties(String url, Properties providedProperties) {
128148
}
129149
}
130150

131-
// Fill list of driver properties information, add not specified properties, copy know driver properties from client properties
151+
// Fill list of driver properties information, add not specified properties,
152+
// copy know driver properties from client properties
132153
for (DriverProperties driverProp : DriverProperties.values()) {
133154
DriverPropertyInfo propertyInfo = propertyInfos.get(driverProp.getKey());
134155
if (propertyInfo == null) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,42 @@ public void testSecureConnection() throws Exception {
343343
Assert.assertEquals(count, 10);
344344
}
345345
}
346+
347+
@Test
348+
public void testSelectingDatabase() throws Exception {
349+
ClickHouseNode server = getServer(ClickHouseProtocol.HTTP);
350+
Properties properties = new Properties();
351+
properties.put(ClientConfigProperties.USER.getKey(), "default");
352+
properties.put(ClientConfigProperties.PASSWORD.getKey(), "");
353+
354+
String jdbcUrl = "jdbc:clickhouse://" + server.getHost() + ":" + server.getPort();
355+
try (Connection conn = new ConnectionImpl(jdbcUrl, properties);
356+
Statement stmt = conn.createStatement();
357+
ResultSet rs = stmt.executeQuery("SELECT database()")) {
358+
Assert.assertTrue(rs.next());
359+
Assert.assertEquals(rs.getString(1), "default");
360+
}
361+
362+
properties.put(ClientConfigProperties.DATABASE.getKey(), "system");
363+
jdbcUrl = "jdbc:clickhouse://"+server.getHost() + ":" + server.getPort() + "/default";
364+
365+
try (Connection conn = new ConnectionImpl(jdbcUrl, properties);
366+
Statement stmt = conn.createStatement();
367+
ResultSet rs = stmt.executeQuery("SELECT database()")) {
368+
369+
Assert.assertTrue(rs.next());
370+
Assert.assertEquals(rs.getString(1), "default");
371+
}
372+
373+
properties.put(ClientConfigProperties.DATABASE.getKey(), "default1");
374+
jdbcUrl = "jdbc:clickhouse://"+server.getHost() + ":" + server.getPort() + "/system";
375+
376+
try (Connection conn = new ConnectionImpl(jdbcUrl, properties);
377+
Statement stmt = conn.createStatement();
378+
ResultSet rs = stmt.executeQuery("SELECT database()")) {
379+
380+
Assert.assertTrue(rs.next());
381+
Assert.assertEquals(rs.getString(1), "system");
382+
}
383+
}
346384
}

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public static Object[][] testConnectionUrlDataProvider() {
2828
useSSL.put(JdbcConfiguration.USE_SSL_PROP, "true");
2929

3030
return new Object[][] {
31-
{"jdbc:clickhouse://localhost:8123/", "http://localhost:8123/", defaultProps},
32-
{"jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2", "http://localhost:8443/clickhouse", defaultProps},
33-
{"jdbc:clickhouse:https://localhost:8123/clickhouse?param1=value1&param2=value2", "https://localhost:8123/clickhouse", defaultProps},
34-
{"jdbc:clickhouse://localhost:8443/", "https://localhost:8443/", useSSL},
35-
{"jdbc:clickhouse://localhost:8443/default?param1=value1&custom_header1=val1,val2,val3", "https://localhost:8443/default", useSSL},
31+
{"jdbc:clickhouse://localhost:8123/", "http://localhost:8123", defaultProps},
32+
{"jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2", "http://localhost:8443", defaultProps},
33+
{"jdbc:clickhouse:https://localhost:8123/clickhouse?param1=value1&param2=value2", "https://localhost:8123", defaultProps},
34+
{"jdbc:clickhouse://localhost:8443/", "https://localhost:8443", useSSL},
35+
{"jdbc:clickhouse://localhost:8443/default?param1=value1&custom_header1=val1,val2,val3", "https://localhost:8443", useSSL},
3636
};
3737
}
3838

@@ -45,7 +45,7 @@ public void testConfigurationProperties() throws Exception {
4545
ClientConfigProperties.commaSeparated(Arrays.asList("http_headers=header1=3,header2=4")));
4646
String url = "jdbc:clickhouse://localhost:8123/clickhouse?client_name=test_application&database=default1";
4747
JdbcConfiguration configuration = new JdbcConfiguration(url, properties);
48-
Assert.assertEquals(configuration.getConnectionUrl(), "http://localhost:8123/clickhouse");
48+
Assert.assertEquals(configuration.getConnectionUrl(), "http://localhost:8123");
4949
Map<String, DriverPropertyInfo> infos = configuration.getDriverPropertyInfo().stream().collect(Collectors.toMap(d -> d.name, d -> d));
5050

5151
DriverPropertyInfo p = infos.get(ClientConfigProperties.DATABASE.getKey());

0 commit comments

Comments
 (0)