Skip to content

Commit fb75c2f

Browse files
authored
Merge pull request #2588 from ClickHouse/jdbc_driver_2410
JDBC Driver class changes
2 parents e5fe79c + b9d8419 commit fb75c2f

File tree

9 files changed

+141
-112
lines changed

9 files changed

+141
-112
lines changed

clickhouse-jdbc/src/test/java/com/clickhouse/jdbc/ClickHouseDriverTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package com.clickhouse.jdbc;
22

3-
import java.sql.Connection;
4-
import java.sql.SQLException;
5-
63
import com.clickhouse.client.ClickHouseProtocol;
7-
84
import com.clickhouse.client.ClickHouseServerForTest;
9-
import com.clickhouse.client.config.ClickHouseDefaults;
10-
import com.clickhouse.jdbc.internal.ClickHouseJdbcUrlParser;
115
import org.testng.Assert;
126
import org.testng.annotations.Test;
137

8+
import java.sql.Connection;
9+
import java.sql.SQLException;
10+
1411
public class ClickHouseDriverTest extends JdbcIntegrationTest {
1512
@Test(groups = "integration")
1613
public void testAcceptUrl() throws SQLException {
@@ -24,13 +21,21 @@ public void testAcceptUrl() throws SQLException {
2421

2522
@Test(groups = "integration")
2623
public void testConnect() throws SQLException {
27-
if (isCloud()) return; //TODO: testConnect - Revisit, see: https://github.com/ClickHouse/clickhouse-java/issues/1747
24+
if (isCloud()) {
25+
return; //
26+
}
27+
2828
System.setProperty("clickhouse.jdbc.v1","true");
2929
String address = getServerAddress(ClickHouseProtocol.HTTP, true);
3030
ClickHouseDriver driver = new ClickHouseDriver();
3131
Connection conn = driver.connect("jdbc:clickhouse://default:" + ClickHouseServerForTest.getPassword() + "@" + address, null);
3232
conn.close();
33+
System.setProperty("clickhouse.jdbc.v1","false");
34+
ClickHouseDriver driver2 = new ClickHouseDriver();
35+
Connection conn2 = driver2.connect("jdbc:clickhouse://default:" + ClickHouseServerForTest.getPassword() + "@" + address, null);
36+
conn2.close();
3337
}
38+
3439
@Test(groups = "integration")
3540
public void testV2Driver() {
3641
System.setProperty("clickhouse.jdbc.v1","false");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ public Builder useServerTimeZone(boolean useServerTimeZone) {
766766
*/
767767
public Builder useTimeZone(String timeZone) {
768768
this.configuration.put(ClientConfigProperties.USE_TIMEZONE.getKey(), timeZone);
769+
// switch using server timezone to false
770+
this.configuration.put(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey(), String.valueOf(Boolean.FALSE));
769771
return this;
770772
}
771773

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,8 @@
11
package com.clickhouse.client.internal;
22

3-
import com.clickhouse.client.api.ClientConfigProperties;
4-
import com.clickhouse.client.api.data_formats.internal.ProcessParser;
5-
import com.clickhouse.client.api.metrics.OperationMetrics;
6-
import com.clickhouse.client.api.metrics.ServerMetrics;
7-
import org.testng.Assert;
8-
import org.testng.annotations.Test;
9-
10-
import java.math.BigDecimal;
11-
import java.math.BigInteger;
12-
import java.time.ZoneId;
13-
import java.time.ZonedDateTime;
14-
3+
/**
4+
* Tests playground
5+
*/
156
public class SmallTests {
167

17-
18-
@Test
19-
public void testSummaryParser() {
20-
OperationMetrics operationMetrics = new OperationMetrics(null);
21-
String summary = createSummary(10, 200, 0, 0, 5, 6000);
22-
ProcessParser.parseSummary(summary, operationMetrics);
23-
24-
Assert.assertEquals(operationMetrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 10);
25-
Assert.assertEquals(operationMetrics.getMetric(ServerMetrics.NUM_BYTES_READ).getLong(), 200);
26-
27-
}
28-
29-
public static String createSummary(int readRows, int readBytes, int writtenRows,
30-
int writtenBytes, int totalRowsToRead, long elapsedNs) {
31-
return "{\"read_rows\":\"" + readRows + "\"," +
32-
"\"read_bytes\":\"" + readBytes + "\"," +
33-
"\"written_rows\":\"" + writtenRows + "\"," +
34-
"\"written_bytes\":\"" + writtenBytes + "\"," +
35-
"\"total_rows_to_read\":\"" + totalRowsToRead + "\"," +
36-
"\"elapsed_ns\":\"" + elapsedNs + "\"}";
37-
38-
}
39-
40-
@Test
41-
public void testTimezoneConvertion() {
42-
ZonedDateTime dt = ZonedDateTime.now();
43-
System.out.println(" now: " + dt);
44-
ZonedDateTime utcSameInstantDt = dt.withZoneSameInstant(ZoneId.of("UTC"));
45-
System.out.println("withZoneSameInstant: " + utcSameInstantDt);
46-
ZonedDateTime utcSameLocalDt = dt.withZoneSameLocal(ZoneId.of("UTC"));
47-
System.out.println("withZoneSameLocal: " + utcSameLocalDt);
48-
}
49-
50-
@Test
51-
public void testGenConfigParameters() {
52-
System.out.println("<br/> <br/> Default: `none` <br/> Enum: `none` <br/> Key: `none` "
53-
54-
55-
);
56-
for (ClientConfigProperties p : ClientConfigProperties.values()) {
57-
String defaultValue = p.getDefaultValue() == null ? "-" : "`" + p.getDefaultValue() + "`";
58-
System.out.println("<br/> <br/> Default: " +defaultValue + " <br/> Enum: `ClientConfigProperties." + p.name() + "` <br/> Key: `" + p.getKey() +"` "
59-
60-
61-
);
62-
}
63-
}
648
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.sql.Struct;
3737
import java.time.Duration;
3838
import java.time.temporal.ChronoUnit;
39-
import java.util.Arrays;
4039
import java.util.Calendar;
4140
import java.util.Collections;
4241
import java.util.HashSet;
@@ -80,7 +79,7 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
8079
this.appName = "";
8180
this.readOnly = false;
8281
this.holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
83-
String clientName = "ClickHouse JDBC Driver V2/" + Driver.driverVersion;
82+
String clientName = "ClickHouse JDBC Driver V2/" + Driver.getLibraryVersion();
8483

8584
Map<String, String> clientProperties = config.getClientProperties();
8685
if (clientProperties.get(ClientConfigProperties.CLIENT_NAME.getKey()) != null) {

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
*/
2727
public class Driver implements java.sql.Driver {
2828
private static final Logger log = LoggerFactory.getLogger(Driver.class);
29-
public static final String driverVersion;
30-
public static final int majorVersion;
31-
public static final int minorVersion;
29+
30+
private static final String driverVersion;
31+
private static final int majorVersion;
32+
private static final int minorVersion;
3233
private final DataSourceImpl dataSource;
3334

3435
public static String frameworksDetected = null;
@@ -66,25 +67,10 @@ public static String getFrameworksDetected() {
6667
driverVersion = ClickHouseClientOption.readVersionFromResource("jdbc-v2-version.properties");
6768
log.debug("ClickHouse JDBC driver version: {}", driverVersion);
6869

69-
int tmpMajorVersion;
70-
int tmpMinorVersion;
71-
72-
try {
73-
Matcher m = Pattern.compile("(\\d+)(\\.\\d+)(\\.\\d+)").matcher(driverVersion);
74-
if (m.find()) {
75-
tmpMajorVersion = Integer.parseInt(m.group(1));
76-
tmpMinorVersion = Integer.parseInt(m.group(2).substring(1));
77-
} else {
78-
tmpMajorVersion = 0;
79-
tmpMinorVersion = 0;
80-
}
81-
} catch (Exception e) {
82-
tmpMajorVersion = 0;
83-
tmpMinorVersion = 0;
84-
}
70+
int[] versions = parseVersion(driverVersion);
8571

86-
majorVersion = tmpMajorVersion;
87-
minorVersion = tmpMinorVersion;
72+
majorVersion = versions[0];
73+
minorVersion = versions[1];
8874

8975
//Load the driver
9076
//load(); //Commented out to avoid loading the driver multiple times, because we're referenced in V1
@@ -153,6 +139,7 @@ public int getMinorVersion() {
153139

154140
@Override
155141
public boolean jdbcCompliant() {
142+
// Mainly because of not supported Transactions.
156143
return false;
157144
}
158145

@@ -165,5 +152,25 @@ public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedE
165152
throw new SQLFeatureNotSupportedException("Method not supported", ExceptionUtils.SQL_STATE_FEATURE_NOT_SUPPORTED);
166153
}
167154

155+
public static String getLibraryVersion() {
156+
return driverVersion;
157+
}
158+
168159
private static final Driver INSTANCE = new Driver();
160+
161+
public static int[] parseVersion(String version) {
162+
if (version != null) {
163+
final Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");
164+
165+
Matcher matcher = pattern.matcher(version);
166+
if (matcher.find()) {
167+
int major = Integer.parseInt(matcher.group(1));
168+
int minor = Integer.parseInt(matcher.group(2));
169+
int patch = Integer.parseInt(matcher.group(3));
170+
int majorVersion = (major << 16) | minor;
171+
return new int[]{majorVersion, patch};
172+
}
173+
}
174+
return new int[] { 0, 0 };
175+
}
169176
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.clickhouse.jdbc.Driver;
88
import com.clickhouse.jdbc.DriverProperties;
99
import com.google.common.collect.ImmutableMap;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
1012

1113
import java.io.UnsupportedEncodingException;
1214
import java.net.URI;
@@ -24,6 +26,7 @@
2426
import java.util.stream.Collectors;
2527

2628
public class JdbcConfiguration {
29+
public static final Logger LOG = LoggerFactory.getLogger(JdbcConfiguration.class);
2730

2831
private static final String PREFIX_CLICKHOUSE = "jdbc:clickhouse:";
2932
private static final String PREFIX_CLICKHOUSE_SHORT = "jdbc:ch:";
@@ -61,19 +64,20 @@ public boolean isIgnoreUnsupportedRequests() {
6164
* @param info - Driver and Client properties.
6265
*/
6366
public JdbcConfiguration(String url, Properties info) throws SQLException {
64-
this.disableFrameworkDetection = info != null && Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false"));
67+
final Properties props = info == null ? new Properties() : info;
68+
this.disableFrameworkDetection = Boolean.parseBoolean(props.getProperty("disable_frameworks_detection", "false"));
6569
this.clientProperties = new HashMap<>();
6670
this.driverProperties = new HashMap<>();
6771

6872
Map<String, String> urlProperties = parseUrl(url);
6973
String tmpConnectionUrl = urlProperties.remove(PARSE_URL_CONN_URL_PROP);
70-
initProperties(urlProperties, info);
74+
initProperties(urlProperties, props);
7175

7276
// after initializing all properties - set final connection URL
73-
boolean useSSLInfo = Boolean.parseBoolean(info.getProperty(DriverProperties.SECURE_CONNECTION.getKey(), "false"));
77+
boolean useSSLInfo = Boolean.parseBoolean(props.getProperty(DriverProperties.SECURE_CONNECTION.getKey(), "false"));
7478
boolean useSSLUrlProperties = Boolean.parseBoolean(urlProperties.getOrDefault(DriverProperties.SECURE_CONNECTION.getKey(), "false"));
7579
boolean useSSL = useSSLInfo || useSSLUrlProperties;
76-
String bearerToken = info.getProperty(ClientConfigProperties.BEARERTOKEN_AUTH.getKey(), null);
80+
String bearerToken = props.getProperty(ClientConfigProperties.BEARERTOKEN_AUTH.getKey(), null);
7781
if (bearerToken != null) {
7882
clientProperties.put(ClientConfigProperties.BEARERTOKEN_AUTH.getKey(), bearerToken);
7983
}
@@ -233,6 +237,12 @@ private void initProperties(Map<String, String> urlProperties, Properties provid
233237

234238
props.putAll(urlProperties);
235239

240+
if (props.containsKey(ClientConfigProperties.USE_TIMEZONE.getKey())) {
241+
if (!props.containsKey(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey())) {
242+
props.put(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey(), String.valueOf(Boolean.FALSE));
243+
}
244+
}
245+
236246
// Process all properties
237247
Map<String, DriverPropertyInfo> propertyInfos = new HashMap<>();
238248

@@ -303,7 +313,7 @@ public void updateUserClient(String clientName, Client client) {
303313
public static String getDefaultClientName() {
304314
StringBuilder jdbcName = new StringBuilder();
305315
jdbcName.append(Driver.DRIVER_CLIENT_NAME)
306-
.append(Driver.driverVersion);
316+
.append(Driver.getLibraryVersion());
307317

308318
return jdbcName.toString();
309319
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/metadata/DatabaseMetaDataImpl.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
import com.clickhouse.jdbc.Driver;
99
import com.clickhouse.jdbc.DriverProperties;
1010
import com.clickhouse.jdbc.JdbcV2Wrapper;
11-
import com.clickhouse.jdbc.ResultSetImpl;
11+
import com.clickhouse.jdbc.internal.DetachedResultSet;
1212
import com.clickhouse.jdbc.internal.ExceptionUtils;
1313
import com.clickhouse.jdbc.internal.JdbcUtils;
14-
import com.clickhouse.jdbc.internal.DetachedResultSet;
1514
import com.clickhouse.logging.Logger;
1615
import com.clickhouse.logging.LoggerFactory;
1716

@@ -29,7 +28,6 @@
2928
import java.util.List;
3029
import java.util.Map;
3130
import java.util.function.Consumer;
32-
import java.util.function.Function;
3331

3432
public class DatabaseMetaDataImpl implements java.sql.DatabaseMetaData, JdbcV2Wrapper {
3533
private static final Logger log = LoggerFactory.getLogger(DatabaseMetaDataImpl.class);
@@ -132,7 +130,7 @@ public String getDriverName() throws SQLException {
132130

133131
@Override
134132
public String getDriverVersion() throws SQLException {
135-
return Driver.driverVersion;
133+
return Driver.getLibraryVersion();
136134
}
137135

138136
@Override

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.util.Arrays;
4040
import java.util.Base64;
4141
import java.util.Collections;
42-
import java.util.List;
4342
import java.util.Properties;
4443
import java.util.UUID;
4544
import java.util.concurrent.ExecutorService;
@@ -962,4 +961,14 @@ public void testClientInfoProperties() throws Exception {
962961
assertNull(conn.getClientInfo("unknown"));
963962
}
964963
}
964+
965+
@Test(groups = {"integration"})
966+
public void testUseUserTimeZone() throws Exception {
967+
Properties props = new Properties();
968+
props.put(ClientConfigProperties.USE_TIMEZONE.getKey(), "America/New_York");
969+
try (Connection conn = getJdbcConnection(props)) {
970+
//
971+
}
972+
973+
}
965974
}

0 commit comments

Comments
 (0)