Skip to content

Commit c4b8d21

Browse files
committed
Merge branch 'main' into jdbc_fix_prepared_statement
2 parents e32e239 + 5ae9b83 commit c4b8d21

File tree

7 files changed

+109
-35
lines changed

7 files changed

+109
-35
lines changed

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,24 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
192192
*
193193
*/
194194
public void loadServerInfo() {
195-
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
196-
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
197-
if (reader.next() != null) {
198-
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
199-
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
200-
serverVersion = reader.getString("version");
195+
// only if 2 properties are set disable retrieval from server
196+
if (!this.configuration.containsKey(ClientConfigProperties.SERVER_TIMEZONE.getKey()) && !this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
197+
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
198+
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
199+
if (reader.next() != null) {
200+
this.configuration.put(ClientConfigProperties.USER.getKey(), reader.getString("user"));
201+
this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone"));
202+
serverVersion = reader.getString("version");
203+
}
201204
}
205+
} catch (Exception e) {
206+
throw new ClientException("Failed to get server info", e);
207+
}
208+
} else {
209+
LOG.info("Using server version " + this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey()) + " and timezone " + this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey()) );
210+
if (this.configuration.containsKey(ClientConfigProperties.SERVER_VERSION.getKey())) {
211+
serverVersion = this.configuration.get(ClientConfigProperties.SERVER_VERSION.getKey());
202212
}
203-
} catch (Exception e) {
204-
throw new ClientException("Failed to get server info", e);
205213
}
206214
}
207215

@@ -991,6 +999,17 @@ public Builder registerClientMetrics(Object registry, String name) {
991999
return this;
9921000
}
9931001

1002+
/**
1003+
* Sets server version that the client is interacting with.
1004+
*
1005+
* @param serverVersion - ClickHouse server version
1006+
* @return same instance of the builder
1007+
*/
1008+
public Builder setServerVersion(String serverVersion) {
1009+
this.configuration.put(ClientConfigProperties.SERVER_VERSION.getKey(), serverVersion);
1010+
return this;
1011+
}
1012+
9941013
public Client build() {
9951014
setDefaults();
9961015

@@ -2174,6 +2193,10 @@ public String getServerVersion() {
21742193
return this.serverVersion;
21752194
}
21762195

2196+
public String getServerTimeZone() {
2197+
return this.configuration.get(ClientConfigProperties.SERVER_TIMEZONE.getKey());
2198+
}
2199+
21772200
public String getClientVersion() {
21782201
return clientVersion;
21792202
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public enum ClientConfigProperties {
3636

3737
USE_TIMEZONE("use_time_zone"),
3838

39+
SERVER_VERSION("server_version"),
40+
3941
SERVER_TIMEZONE("server_time_zone"),
4042

4143
ASYNC_OPERATIONS("async"),

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,7 @@
4444
import java.time.format.DateTimeFormatter;
4545
import java.time.format.DateTimeFormatterBuilder;
4646
import java.time.temporal.ChronoField;
47-
import java.util.ArrayList;
48-
import java.util.Arrays;
49-
import java.util.Calendar;
50-
import java.util.Collection;
51-
import java.util.Collections;
52-
import java.util.List;
53-
import java.util.Map;
47+
import java.util.*;
5448

5549
public class PreparedStatementImpl extends StatementImpl implements PreparedStatement, JdbcV2Wrapper {
5650
private static final Logger LOG = LoggerFactory.getLogger(PreparedStatementImpl.class);
@@ -717,7 +711,9 @@ private static String encodeObject(Object x) throws SQLException {
717711
for (Object item : (Collection<?>) x) {
718712
listString.append(encodeObject(item)).append(", ");
719713
}
720-
listString.delete(listString.length() - 2, listString.length());
714+
if (listString.length() > 1) {
715+
listString.delete(listString.length() - 2, listString.length());
716+
}
721717
listString.append("]");
722718

723719
return listString.toString();
@@ -780,6 +776,8 @@ private static String encodeObject(Object x) throws SQLException {
780776
}
781777
tupleString.append(")");
782778
return tupleString.toString();
779+
} else if (x instanceof UUID) {
780+
return "'" + escapeString(((UUID) x).toString()) + "'";
783781
}
784782

785783
return escapeString(x.toString());//Escape single quotes

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,62 +1087,52 @@ public boolean supportsResultSetConcurrency(int type, int concurrency) throws SQ
10871087

10881088
@Override
10891089
public boolean ownUpdatesAreVisible(int type) throws SQLException {
1090-
// TODO: should be true
10911090
return false;
10921091
}
10931092

10941093
@Override
10951094
public boolean ownDeletesAreVisible(int type) throws SQLException {
1096-
// TODO: should be true
10971095
return false;
10981096
}
10991097

11001098
@Override
11011099
public boolean ownInsertsAreVisible(int type) throws SQLException {
1102-
// TODO: should be true
11031100
return false;
11041101
}
11051102

11061103
@Override
11071104
public boolean othersUpdatesAreVisible(int type) throws SQLException {
1108-
// TODO: should be checked
11091105
return false;
11101106
}
11111107

11121108
@Override
11131109
public boolean othersDeletesAreVisible(int type) throws SQLException {
1114-
// TODO: should be checked
11151110
return false;
11161111
}
11171112

11181113
@Override
11191114
public boolean othersInsertsAreVisible(int type) throws SQLException {
1120-
// TODO: should be checked
11211115
return false;
11221116
}
11231117

11241118
@Override
11251119
public boolean updatesAreDetected(int type) throws SQLException {
1126-
// TODO: should be checked
11271120
return false;
11281121
}
11291122

11301123
@Override
11311124
public boolean deletesAreDetected(int type) throws SQLException {
1132-
// TODO: should be checked
11331125
return false;
11341126
}
11351127

11361128
@Override
11371129
public boolean insertsAreDetected(int type) throws SQLException {
1138-
// TODO: should be checked
11391130
return false;
11401131
}
11411132

11421133
@Override
11431134
public boolean supportsBatchUpdates() throws SQLException {
1144-
// TODO: should be checked
1145-
return false;
1135+
return true;
11461136
}
11471137

11481138
@Override

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
import java.nio.charset.StandardCharsets;
44
import java.sql.*;
5-
import java.util.Arrays;
6-
import java.util.Base64;
7-
import java.util.Properties;
5+
import java.util.*;
86

97
import java.util.Properties;
10-
import java.util.UUID;
118

129
import com.clickhouse.client.ClickHouseNode;
1310
import com.clickhouse.client.ClickHouseProtocol;
@@ -553,5 +550,21 @@ public void testJWTWithCloud() throws Exception {
553550
Assert.assertTrue(rs.next());
554551
}
555552
}
553+
@Test(groups = { "integration" })
554+
public void testDisableExtraCallToServer() throws Exception {
555+
Properties properties = new Properties();
556+
properties.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), "GMT");
557+
properties.put(ClientConfigProperties.SERVER_VERSION.getKey(), "1.0.0");
558+
try (Connection conn = getJdbcConnection(properties);
559+
Statement stmt = conn.createStatement();
560+
ResultSet rs = stmt.executeQuery("SELECT 1")) {
561+
Assert.assertTrue(rs.next());
562+
ConnectionImpl connImpl = (ConnectionImpl) conn;
563+
564+
Assert.assertEquals(connImpl.getClient().getServerVersion(), "1.0.0");
565+
Assert.assertEquals(connImpl.getClient().getServerTimeZone(), "GMT");
566+
}
567+
568+
}
556569

557570
}

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

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import java.sql.SQLException;
1616
import java.sql.Statement;
1717
import java.sql.Types;
18-
import java.util.Arrays;
19-
import java.util.GregorianCalendar;
20-
import java.util.TimeZone;
18+
import java.util.*;
2119

2220
import static org.testng.Assert.assertEquals;
2321
import static org.testng.Assert.assertFalse;
@@ -583,6 +581,56 @@ void testClearParameters() throws Exception {
583581
}
584582
}
585583

584+
@Test(groups = {"integration"})
585+
void testWriteUUID() throws Exception {
586+
String sql = "insert into `test_issue_2327` (`id`, `uuid`) values (?, ?)";
587+
try (Connection conn = getJdbcConnection();
588+
PreparedStatementImpl ps = (PreparedStatementImpl) conn.prepareStatement(sql)) {
589+
590+
try (Statement stmt = conn.createStatement()) {
591+
stmt.execute("CREATE TABLE IF NOT EXISTS `test_issue_2327` (`id` Nullable(String), `uuid` UUID) ENGINE Memory;");
592+
}
593+
UUID uuid = UUID.randomUUID();
594+
ps.setString(1, "testId01");
595+
ps.setObject(2, uuid);
596+
ps.execute();
597+
598+
try (Statement stmt = conn.createStatement()) {
599+
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM `test_issue_2327`");
600+
Assert.assertTrue(rs.next());
601+
Assert.assertEquals(rs.getInt(1), 1);
602+
}
603+
}
604+
605+
}
606+
607+
@Test(groups = {"integration"})
608+
void testWriteCollection() throws Exception {
609+
String sql = "insert into `test_issue_2329` (`id`, `name`, `age`, `arr`) values (?, ?, ?, ?)";
610+
try (Connection conn = getJdbcConnection();
611+
PreparedStatementImpl ps = (PreparedStatementImpl) conn.prepareStatement(sql)) {
612+
613+
try (Statement stmt = conn.createStatement()) {
614+
stmt.execute("CREATE TABLE IF NOT EXISTS `test_issue_2329` (`id` Nullable(String), `name` Nullable(String), `age` Int32, `arr` Array(String)) ENGINE Memory;");
615+
}
616+
617+
Assert.assertEquals(ps.getParametersCount(), 4);
618+
Collection<String> arr = new ArrayList<String>();
619+
ps.setString(1, "testId01");
620+
ps.setString(2, "testName");
621+
ps.setInt(3, 18);
622+
ps.setObject(4, arr);
623+
ps.execute();
624+
625+
try (Statement stmt = conn.createStatement()) {
626+
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM `test_issue_2329`");
627+
Assert.assertTrue(rs.next());
628+
Assert.assertEquals(rs.getInt(1), 1);
629+
}
630+
}
631+
632+
}
633+
586634
@Test
587635
void testMethodsNotAllowedToBeCalled() throws Exception {
588636
/* Story About Broken API

performance/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<properties>
1616
<apache.httpclient.version>5.3.1</apache.httpclient.version>
1717
<slf4j.version>2.0.17</slf4j.version>
18-
<ch.jdbc.revision>0.8.3-SNAPSHOT</ch.jdbc.revision>
18+
<ch.jdbc.revision>0.8.4-SNAPSHOT</ch.jdbc.revision>
1919
<jmh.version>1.37</jmh.version>
2020
<testcontainers.version>1.20.6</testcontainers.version>
2121

0 commit comments

Comments
 (0)