Skip to content

Commit 72573f8

Browse files
authored
Merge pull request #2489 from enqueue/jdbc_url_default_ports
[jdbc-v2] Adds default port and restricts multiple endpoints
2 parents d4792b7 + cc254f1 commit 72573f8

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.clickhouse.client.api.Client;
44
import com.clickhouse.client.api.ClientConfigProperties;
5+
import com.clickhouse.client.api.http.ClickHouseHttpProto;
56
import com.clickhouse.data.ClickHouseDataType;
67
import com.clickhouse.jdbc.Driver;
78
import com.google.common.collect.ImmutableMap;
@@ -129,7 +130,14 @@ private static String createConnectionURL(String url, boolean ssl) throws SQLExc
129130
: url;
130131
try {
131132
URI tmp = URI.create(adjustedURL);
132-
return tmp.toASCIIString();
133+
String asciiString = tmp.toASCIIString();
134+
if (tmp.getPort() < 0) {
135+
String port = ssl || adjustedURL.startsWith("https")
136+
? ":" + ClickHouseHttpProto.DEFAULT_HTTPS_PORT
137+
: ":" + ClickHouseHttpProto.DEFAULT_HTTP_PORT;
138+
asciiString += port;
139+
}
140+
return asciiString;
133141
} catch (IllegalArgumentException iae) {
134142
throw new SQLException("Failed to parse URL '" + url + "'", iae);
135143
}
@@ -171,6 +179,9 @@ private Map<String, String> parseUrl(String url) throws SQLException {
171179
throw new SQLException(
172180
"Invalid authority part JDBC URL '" + url + "'");
173181
}
182+
if (uri.getAuthority().contains(",")) {
183+
throw new SQLException("Multiple endpoints not supported");
184+
}
174185
properties.put(PARSE_URL_CONN_URL_PROP, uri.getScheme() + "://"
175186
+ uri.getRawAuthority()); // will be parsed again later
176187
if (uri.getPath() != null

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.clickhouse.jdbc.internal;
22

3+
import com.clickhouse.client.api.Client;
34
import com.clickhouse.client.api.ClientConfigProperties;
5+
46
import org.testng.Assert;
57
import org.testng.annotations.DataProvider;
68
import org.testng.annotations.Test;
@@ -23,15 +25,17 @@ public class JdbcConfigurationTest {
2325
new JdbcConfigurationTestData("jdbc:clickhouse://localhost"),
2426
new JdbcConfigurationTestData("jdbc:clickhouse:http://localhost"),
2527
new JdbcConfigurationTestData("jdbc:clickhouse:https://localhost")
26-
.withExpectedConnectionURL("https://localhost"),
28+
.withExpectedConnectionURL("https://localhost:8443"),
29+
new JdbcConfigurationTestData("jdbc:clickhouse:https://localhost:8123")
30+
.withExpectedConnectionURL("https://localhost:8123"),
2731
new JdbcConfigurationTestData("jdbc:clickhouse://localhost")
2832
.withAdditionalConnectionParameters(
2933
Map.of(JdbcConfiguration.USE_SSL_PROP, "true"))
30-
.withExpectedConnectionURL("https://localhost")
34+
.withExpectedConnectionURL("https://localhost:8443")
3135
.withAdditionalExpectedClientProperties(
3236
Map.of("ssl", "true")),
3337
new JdbcConfigurationTestData("jdbc:clickhouse://[::1]")
34-
.withExpectedConnectionURL("http://[::1]"),
38+
.withExpectedConnectionURL("http://[::1]:8123"),
3539
new JdbcConfigurationTestData("jdbc:clickhouse://[::1]:8123")
3640
.withExpectedConnectionURL("http://[::1]:8123"),
3741
new JdbcConfigurationTestData("jdbc:clickhouse://localhost:8443")
@@ -111,14 +115,22 @@ public class JdbcConfigurationTest {
111115
))
112116
};
113117

118+
@SuppressWarnings("deprecation")
114119
@Test(dataProvider = "validURLTestData")
115120
public void testParseURLValid(String jdbcURL, Properties properties,
116-
String connectionUrl, Map<String, String> expectedClientProps)
121+
String connectionURL, Map<String, String> expectedClientProps)
117122
throws Exception
118123
{
119124
JdbcConfiguration configuration = new JdbcConfiguration(jdbcURL, properties);
120-
assertEquals(configuration.getConnectionUrl(), connectionUrl);
125+
assertEquals(configuration.getConnectionUrl(), connectionURL);
121126
assertEquals(configuration.clientProperties, expectedClientProps);
127+
Client.Builder bob = new Client.Builder();
128+
configuration.applyClientProperties(bob);
129+
Client client = bob.build();
130+
assertEquals(client.getEndpoints().size(), 1);
131+
assertEquals(
132+
client.getEndpoints().iterator().next(),
133+
connectionURL);
122134
}
123135

124136
@Test(dataProvider = "invalidURLs")
@@ -198,6 +210,9 @@ public Object[][] createInvalidConnectionURLs() {
198210
{ "jdbc:clickhouse://foo.bar?x&y=z" },
199211
{ "jdbc:clickhouse://foo.bar?x==&y=z" },
200212
{ "jdbc:clickhouse://localhost?☺=value1" },
213+
// multiple endpoints are invalid
214+
{ "jdbc:clickhouse://foo,bar" },
215+
{ "jdbc:clickhouse://foo,bar.com:8123" },
201216
};
202217
}
203218

@@ -210,7 +225,7 @@ private static final class JdbcConfigurationTestData {
210225
Map.of( "user", "default", "password", "");
211226

212227
private static final String DEFAULT_EXPECTED_CONNECTION_URL =
213-
"http://localhost";
228+
"http://localhost:8123";
214229

215230
private final String url;
216231
private final Properties connectionParameters;

0 commit comments

Comments
 (0)