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
39 changes: 31 additions & 8 deletions client-v2/src/main/java/com/clickhouse/client/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,24 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
*
*/
public void loadServerInfo() {
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
if (reader.next() != null) {
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
serverVersion = reader.getString("version");
// only if 2 properties are set disable retrieval from server
if (!this.configuration.containsKey(ClientConfigProperties.SERVER_TIMEZONE.getKey()) && !this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think. in the future it will be a bug when more then timezone is get with server info.
So I'd just have an explicit flag documenting that all required configuration should be set. In the builder test that all required configuration is set

try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
if (reader.next() != null) {
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
serverVersion = reader.getString("version");
}
}
} catch (Exception e) {
throw new ClientException("Failed to get server info", e);
}
} else {
LOG.info("Using server version " + this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey()) + " and timezone " + this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey()) );
if (this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
serverVersion = this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey());
}
} catch (Exception e) {
throw new ClientException("Failed to get server info", e);
}
}

Expand Down Expand Up @@ -991,6 +999,17 @@ public Builder registerClientMetrics(Object registry, String name) {
return this;
}

/**
* Sets server version that the client is interacting with.
*
* @param serverVersion - ClickHouse server version
* @return same instance of the builder
*/
public Builder setServerVersion(String serverVersion) {
this.configuration.put(ClientConfigProperties.SERVER_VERSION.getKey(), serverVersion);
return this;
}

public Client build() {
setDefaults();

Expand Down Expand Up @@ -2174,6 +2193,10 @@ public String getServerVersion() {
return this.serverVersion;
}

public String getServerTimeZone() {
return this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey());
}

public String getClientVersion() {
return clientVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public enum ClientConfigProperties {

USE_TIMEZONE("use_time_zone"),

SERVER_VERSION("server_version"),

SERVER_TIMEZONE("server_time_zone"),

ASYNC_OPERATIONS("async"),
Expand Down
21 changes: 17 additions & 4 deletions jdbc-v2/src/test/java/com/clickhouse/jdbc/ConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.util.Arrays;
import java.util.Base64;
import java.util.Properties;
import java.util.*;

import java.util.Properties;
import java.util.UUID;

import com.clickhouse.client.ClickHouseNode;
import com.clickhouse.client.ClickHouseProtocol;
Expand Down Expand Up @@ -553,5 +550,21 @@ public void testJWTWithCloud() throws Exception {
Assert.assertTrue(rs.next());
}
}
@Test(groups = { "integration" })
public void testDisableExtraCallToServer() throws Exception {
Properties properties = new Properties();
properties.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), "GMT");
properties.put(ClientConfigProperties.SERVER_VERSION.getKey(), "1.0.0");
try (Connection conn = getJdbcConnection(properties);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT 1")) {
Assert.assertTrue(rs.next());
ConnectionImpl connImpl = (ConnectionImpl) conn;

Assert.assertEquals(connImpl.getClient().getServerVersion(), "1.0.0");
Assert.assertEquals(connImpl.getClient().getServerTimeZone(), "GMT");
}

}

}
Loading