Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.clickhouse.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

import com.clickhouse.client.ClickHouseProtocol;

import com.clickhouse.client.ClickHouseServerForTest;
import com.clickhouse.client.config.ClickHouseDefaults;
import com.clickhouse.jdbc.internal.ClickHouseJdbcUrlParser;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class ClickHouseDriverTest extends JdbcIntegrationTest {
@Test(groups = "integration")
public void testAcceptUrl() throws SQLException {
Expand All @@ -24,13 +21,21 @@ public void testAcceptUrl() throws SQLException {

@Test(groups = "integration")
public void testConnect() throws SQLException {
if (isCloud()) return; //TODO: testConnect - Revisit, see: https://github.com/ClickHouse/clickhouse-java/issues/1747
if (isCloud()) {
return; //
}

System.setProperty("clickhouse.jdbc.v1","true");
String address = getServerAddress(ClickHouseProtocol.HTTP, true);
ClickHouseDriver driver = new ClickHouseDriver();
Connection conn = driver.connect("jdbc:clickhouse://default:" + ClickHouseServerForTest.getPassword() + "@" + address, null);
conn.close();
System.setProperty("clickhouse.jdbc.v1","false");
ClickHouseDriver driver2 = new ClickHouseDriver();
Connection conn2 = driver2.connect("jdbc:clickhouse://default:" + ClickHouseServerForTest.getPassword() + "@" + address, null);
conn2.close();
}

@Test(groups = "integration")
public void testV2Driver() {
System.setProperty("clickhouse.jdbc.v1","false");
Expand Down
2 changes: 2 additions & 0 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,8 @@ public Builder useServerTimeZone(boolean useServerTimeZone) {
*/
public Builder useTimeZone(String timeZone) {
this.configuration.put(ClientConfigProperties.USE_TIMEZONE.getKey(), timeZone);
// switch using server timezone to false
this.configuration.put(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey(), String.valueOf(Boolean.FALSE));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,8 @@
package com.clickhouse.client.internal;

import com.clickhouse.client.api.ClientConfigProperties;
import com.clickhouse.client.api.data_formats.internal.ProcessParser;
import com.clickhouse.client.api.metrics.OperationMetrics;
import com.clickhouse.client.api.metrics.ServerMetrics;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
* Tests playground
*/
public class SmallTests {


@Test
public void testSummaryParser() {
OperationMetrics operationMetrics = new OperationMetrics(null);
String summary = createSummary(10, 200, 0, 0, 5, 6000);
ProcessParser.parseSummary(summary, operationMetrics);

Assert.assertEquals(operationMetrics.getMetric(ServerMetrics.NUM_ROWS_READ).getLong(), 10);
Assert.assertEquals(operationMetrics.getMetric(ServerMetrics.NUM_BYTES_READ).getLong(), 200);

}

public static String createSummary(int readRows, int readBytes, int writtenRows,
int writtenBytes, int totalRowsToRead, long elapsedNs) {
return "{\"read_rows\":\"" + readRows + "\"," +
"\"read_bytes\":\"" + readBytes + "\"," +
"\"written_rows\":\"" + writtenRows + "\"," +
"\"written_bytes\":\"" + writtenBytes + "\"," +
"\"total_rows_to_read\":\"" + totalRowsToRead + "\"," +
"\"elapsed_ns\":\"" + elapsedNs + "\"}";

}

@Test
public void testTimezoneConvertion() {
ZonedDateTime dt = ZonedDateTime.now();
System.out.println(" now: " + dt);
ZonedDateTime utcSameInstantDt = dt.withZoneSameInstant(ZoneId.of("UTC"));
System.out.println("withZoneSameInstant: " + utcSameInstantDt);
ZonedDateTime utcSameLocalDt = dt.withZoneSameLocal(ZoneId.of("UTC"));
System.out.println("withZoneSameLocal: " + utcSameLocalDt);
}

@Test
public void testGenConfigParameters() {
System.out.println("<br/> <br/> Default: `none` <br/> Enum: `none` <br/> Key: `none` "


);
for (ClientConfigProperties p : ClientConfigProperties.values()) {
String defaultValue = p.getDefaultValue() == null ? "-" : "`" + p.getDefaultValue() + "`";
System.out.println("<br/> <br/> Default: " +defaultValue + " <br/> Enum: `ClientConfigProperties." + p.name() + "` <br/> Key: `" + p.getKey() +"` "


);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.sql.Struct;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -80,7 +79,7 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
this.appName = "";
this.readOnly = false;
this.holdability = ResultSet.HOLD_CURSORS_OVER_COMMIT;
String clientName = "ClickHouse JDBC Driver V2/" + Driver.driverVersion;
String clientName = "ClickHouse JDBC Driver V2/" + Driver.getLibraryVersion();

Map<String, String> clientProperties = config.getClientProperties();
if (clientProperties.get(ClientConfigProperties.CLIENT_NAME.getKey()) != null) {
Expand Down
49 changes: 28 additions & 21 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
*/
public class Driver implements java.sql.Driver {
private static final Logger log = LoggerFactory.getLogger(Driver.class);
public static final String driverVersion;
public static final int majorVersion;
public static final int minorVersion;

private static final String driverVersion;
private static final int majorVersion;
private static final int minorVersion;
private final DataSourceImpl dataSource;

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

int tmpMajorVersion;
int tmpMinorVersion;

try {
Matcher m = Pattern.compile("(\\d+)(\\.\\d+)(\\.\\d+)").matcher(driverVersion);
if (m.find()) {
tmpMajorVersion = Integer.parseInt(m.group(1));
tmpMinorVersion = Integer.parseInt(m.group(2).substring(1));
} else {
tmpMajorVersion = 0;
tmpMinorVersion = 0;
}
} catch (Exception e) {
tmpMajorVersion = 0;
tmpMinorVersion = 0;
}
int[] versions = parseVersion(driverVersion);

majorVersion = tmpMajorVersion;
minorVersion = tmpMinorVersion;
majorVersion = versions[0];
minorVersion = versions[1];

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

@Override
public boolean jdbcCompliant() {
// Mainly because of not supported Transactions.
return false;
}

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

public static String getLibraryVersion() {
return driverVersion;
}

private static final Driver INSTANCE = new Driver();

public static int[] parseVersion(String version) {
if (version != null) {
final Pattern pattern = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)");

Matcher matcher = pattern.matcher(version);
if (matcher.find()) {
int major = Integer.parseInt(matcher.group(1));
int minor = Integer.parseInt(matcher.group(2));
int patch = Integer.parseInt(matcher.group(3));
int majorVersion = (major << 16) | minor;
return new int[]{majorVersion, patch};
}
}
return new int[] { 0, 0 };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.clickhouse.jdbc.Driver;
import com.clickhouse.jdbc.DriverProperties;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URI;
Expand All @@ -24,6 +26,7 @@
import java.util.stream.Collectors;

public class JdbcConfiguration {
public static final Logger LOG = LoggerFactory.getLogger(JdbcConfiguration.class);

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

Map<String, String> urlProperties = parseUrl(url);
String tmpConnectionUrl = urlProperties.remove(PARSE_URL_CONN_URL_PROP);
initProperties(urlProperties, info);
initProperties(urlProperties, props);

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

props.putAll(urlProperties);

if (props.containsKey(ClientConfigProperties.USE_TIMEZONE.getKey())) {
if (!props.containsKey(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey())) {
props.put(ClientConfigProperties.USE_SERVER_TIMEZONE.getKey(), String.valueOf(Boolean.FALSE));
}
}

// Process all properties
Map<String, DriverPropertyInfo> propertyInfos = new HashMap<>();

Expand Down Expand Up @@ -303,7 +313,7 @@ public void updateUserClient(String clientName, Client client) {
public static String getDefaultClientName() {
StringBuilder jdbcName = new StringBuilder();
jdbcName.append(Driver.DRIVER_CLIENT_NAME)
.append(Driver.driverVersion);
.append(Driver.getLibraryVersion());

return jdbcName.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import com.clickhouse.jdbc.Driver;
import com.clickhouse.jdbc.DriverProperties;
import com.clickhouse.jdbc.JdbcV2Wrapper;
import com.clickhouse.jdbc.ResultSetImpl;
import com.clickhouse.jdbc.internal.DetachedResultSet;
import com.clickhouse.jdbc.internal.ExceptionUtils;
import com.clickhouse.jdbc.internal.JdbcUtils;
import com.clickhouse.jdbc.internal.DetachedResultSet;
import com.clickhouse.logging.Logger;
import com.clickhouse.logging.LoggerFactory;

Expand All @@ -29,7 +28,6 @@
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;

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

@Override
public String getDriverVersion() throws SQLException {
return Driver.driverVersion;
return Driver.getLibraryVersion();
}

@Override
Expand Down
11 changes: 10 additions & 1 deletion jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -962,4 +961,14 @@ public void testClientInfoProperties() throws Exception {
assertNull(conn.getClientInfo("unknown"));
}
}

@Test(groups = {"integration"})
public void testUseUserTimeZone() throws Exception {
Properties props = new Properties();
props.put(ClientConfigProperties.USE_TIMEZONE.getKey(), "America/New_York");
try (Connection conn = getJdbcConnection(props)) {
//
}

}
}
Loading
Loading