Skip to content

Commit c1be459

Browse files
committed
Added handling unknown properties in configuration for JDBC. Added filtering out JDBC properties from client ones
1 parent 53543e8 commit c1be459

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ public static Map<String, Object> parseConfigMap(Map<String, String> configMap)
343343
}
344344

345345
if (!tmpMap.isEmpty()) {
346+
tmpMap.remove(ClientConfigProperties.NO_THROW_ON_UNKNOWN_CONFIG);
346347
String msg = "Unknown and unmapped config properties: " + tmpMap.keySet();
347348
if (configMap.containsKey(NO_THROW_ON_UNKNOWN_CONFIG)) {
348349
LOG.warn(msg);

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

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

3+
import com.clickhouse.client.api.ClientConfigProperties;
34
import com.clickhouse.client.api.internal.ServerSettings;
45

56
import java.util.Arrays;
@@ -98,4 +99,12 @@ public String getDefaultValue() {
9899
public List<String> getChoices() {
99100
return choices;
100101
}
102+
103+
public static String serverSetting(String key) {
104+
return ClientConfigProperties.serverSetting(key);
105+
}
106+
107+
public static String httpHeader(String key) {
108+
return ClientConfigProperties.httpHeader(key);
109+
}
101110
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.clickhouse.jdbc.Driver;
88
import com.clickhouse.jdbc.DriverProperties;
99
import com.google.common.collect.ImmutableMap;
10+
import com.google.common.collect.ImmutableSet;
1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213

@@ -22,6 +23,7 @@
2223
import java.util.List;
2324
import java.util.Map;
2425
import java.util.Properties;
26+
import java.util.Set;
2527
import java.util.regex.Pattern;
2628
import java.util.stream.Collectors;
2729

@@ -57,6 +59,17 @@ public boolean isIgnoreUnsupportedRequests() {
5759
return isIgnoreUnsupportedRequests;
5860
}
5961

62+
private static final Set<String> DRIVER_PROP_KEYS;
63+
static {
64+
65+
ImmutableSet.Builder<String> driverPropertiesMapBuidler = ImmutableSet.builder();
66+
for (DriverProperties prop : DriverProperties.values()) {
67+
driverPropertiesMapBuidler.add(prop.getKey());
68+
}
69+
70+
DRIVER_PROP_KEYS = driverPropertiesMapBuidler.build();
71+
}
72+
6073
/**
6174
* Parses URL to get property and target host.
6275
* Properties that are passed in the {@code info} parameter will override that are set in the {@code url}.
@@ -251,7 +264,10 @@ private void initProperties(Map<String, String> urlProperties, Properties provid
251264
DriverPropertyInfo propertyInfo = new DriverPropertyInfo(prop.getKey(), prop.getValue());
252265
propertyInfo.description = "(User Defined)";
253266
propertyInfos.put(prop.getKey(), propertyInfo);
254-
clientProperties.put(prop.getKey(), prop.getValue());
267+
if (!DRIVER_PROP_KEYS.contains(prop.getKey())) {
268+
// filter out driver properties
269+
clientProperties.put(prop.getKey(), prop.getValue());
270+
}
255271
}
256272

257273
// Fill list of client properties information, add not specified properties (doesn't affect client properties)

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

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
package com.clickhouse.jdbc;
22

33
import com.clickhouse.client.api.ClientConfigProperties;
4+
import com.clickhouse.client.api.ClientMisconfigurationException;
5+
import com.clickhouse.client.api.ServerException;
46
import org.testng.Assert;
57
import org.testng.annotations.DataProvider;
68
import org.testng.annotations.Test;
79

10+
import java.sql.Connection;
811
import java.sql.DriverManager;
912
import java.sql.DriverPropertyInfo;
1013
import java.sql.SQLException;
14+
import java.sql.Statement;
1115
import java.util.HashMap;
1216
import java.util.Map;
1317
import java.util.Properties;
1418

19+
import static org.testng.Assert.assertEquals;
1520
import static org.testng.AssertJUnit.assertTrue;
1621

1722

@@ -62,16 +67,16 @@ public void testGetPropertyInfo(String url, Properties props, Map<String, String
6267
for (DriverPropertyInfo property : properties) {
6368
Object expectedValue = checkPropertiesCopy.remove(property.name);
6469
if (expectedValue != null) {
65-
Assert.assertEquals(property.value, expectedValue);
70+
assertEquals(property.value, expectedValue);
6671
} else {
6772
for (DriverProperties driverProp : DriverProperties.values()) {
6873
if (driverProp.getKey().equalsIgnoreCase(property.name)) {
69-
Assert.assertEquals(property.value, driverProp.getDefaultValue());
74+
assertEquals(property.value, driverProp.getDefaultValue());
7075
}
7176
}
7277
for (ClientConfigProperties clientProp : ClientConfigProperties.values()) {
7378
if (clientProp.getKey().equalsIgnoreCase(property.name)) {
74-
Assert.assertEquals(property.value, clientProp.getDefaultValue());
79+
assertEquals(property.value, clientProp.getDefaultValue());
7580
}
7681
}
7782
}
@@ -107,7 +112,7 @@ public void testGetDriverVersion() {
107112
@Test(groups = {"integration"}, dataProvider = "testParsingDriverVersionDP")
108113
public void testParsingDriverVersion(String version, int expectedMajor, int expectedMinor) {
109114
int[] versions = Driver.parseVersion(version);
110-
Assert.assertEquals(versions, new int[] { expectedMajor, expectedMinor });
115+
assertEquals(versions, new int[] { expectedMajor, expectedMinor });
111116
}
112117

113118
@DataProvider(name = "testParsingDriverVersionDP")
@@ -139,7 +144,55 @@ public void testGetParentLogger() {
139144
driver.getParentLogger();
140145
Assert.fail("Should not reach here");
141146
} catch (SQLException e) {
142-
Assert.assertEquals(e.getMessage(), "Method not supported");
147+
assertEquals(e.getMessage(), "Method not supported");
143148
}
144149
}
150+
151+
@Test(groups = { "integration" })
152+
public void testUnknownSettings() throws Exception {
153+
Driver driver = new Driver();
154+
try {
155+
driver.connect(getEndpointString() + "?unknown_setting=1", new Properties());
156+
Assert.fail("Exception expected");
157+
} catch (SQLException e) {
158+
Assert.assertTrue(e.getCause() instanceof ClientMisconfigurationException);
159+
Assert.assertTrue(e.getCause().getMessage().contains("unknown_setting"));
160+
}
161+
162+
try {
163+
Properties properties = new Properties();
164+
properties.put("unknown_setting1", "1");
165+
driver.connect(getEndpointString(), properties).close();
166+
Assert.fail("Exception expected");
167+
} catch (SQLException e) {
168+
Assert.assertTrue(e.getCause() instanceof ClientMisconfigurationException);
169+
Assert.assertTrue(e.getCause().getMessage().contains("unknown_setting1"));
170+
}
171+
172+
{
173+
Properties properties = new Properties();
174+
properties.put(DriverProperties.serverSetting("unknown_setting2"), "1");
175+
try (Connection connection = getJdbcConnection(properties)) {
176+
try {
177+
connection.createStatement().execute("SELECT 1");
178+
Assert.fail("Exception expected");
179+
} catch (SQLException e) {
180+
Assert.assertTrue(e.getCause() instanceof ServerException);
181+
assertEquals(((ServerException) e.getCause()).getCode(), ServerException.UNKNOWN_SETTING);
182+
}
183+
}
184+
}
185+
186+
{
187+
Properties properties = new Properties();
188+
properties.put(DriverProperties.SECURE_CONNECTION.getKey(), "true");
189+
properties.put(DriverProperties.SCHEMA_TERM.getKey(), "catalog");
190+
try (Connection connection = getJdbcConnection(properties);
191+
Statement stmt = connection.createStatement()) {
192+
//
193+
}
194+
}
195+
196+
driver.connect(getEndpointString() + "?unknown_setting=1&" + ClientConfigProperties.NO_THROW_ON_UNKNOWN_CONFIG + "=1", new Properties()).close();
197+
}
145198
}

0 commit comments

Comments
 (0)