Skip to content

Commit 41a4a41

Browse files
author
Paultagoras
committed
Updating tests and adding case handling
1 parent 15351da commit 41a4a41

File tree

6 files changed

+109
-23
lines changed

6 files changed

+109
-23
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class ConnectionImpl implements Connection, JdbcV2Wrapper {
2323
protected final JdbcConfiguration config;
2424

2525
private boolean closed = false;
26-
protected boolean onCluster = false;//TODO: Placeholder for cluster support
26+
protected boolean onCluster;//TODO: Placeholder for cluster support
27+
protected String cluster;
2728
private String catalog;
2829
private String schema;
2930
private QuerySettings defaultQuerySettings;
@@ -32,6 +33,8 @@ public ConnectionImpl(String url, Properties info) {
3233
log.debug("Creating connection to {}", url);
3334
this.url = url;//Raw URL
3435
this.config = new JdbcConfiguration(url, info);
36+
this.onCluster = false;
37+
this.cluster = null;
3538
String clientName = "ClickHouse JDBC Driver V2/" + Driver.driverVersion;
3639

3740
if (this.config.isDisableFrameworkDetection()) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,10 +476,14 @@ private static String encodeObject(Object x) throws SQLException {
476476
} else if (x instanceof Array) {
477477
StringBuilder listString = new StringBuilder();
478478
listString.append("[");
479+
int i = 0;
479480
for (Object item : (Object[])((Array) x).getArray()) {
480-
listString.append(encodeObject(item)).append(", ");
481+
if (i > 0) {
482+
listString.append(", ");
483+
}
484+
listString.append(encodeObject(item));
485+
i++;
481486
}
482-
listString.delete(listString.length() - 2, listString.length());
483487
listString.append("]");
484488

485489
return listString.toString();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public void cancel() throws SQLException {
224224

225225
try {
226226
connection.client.query(String.format("KILL QUERY%sWHERE query_id = '%s'",
227-
connection.onCluster ? " ON CLUSTER " : " ",
227+
connection.onCluster ? " ON CLUSTER " + connection.cluster + " " : " ",
228228
lastQueryId), connection.getDefaultQuerySettings()).get();
229229
} catch (Exception e) {
230230
throw new SQLException(e);
@@ -262,15 +262,15 @@ public boolean execute(String sql, QuerySettings settings) throws SQLException {
262262
} else if(type == StatementType.SET) {
263263
//SET ROLE
264264
List<String> tokens = JdbcUtils.tokenizeSQL(sql);
265-
if (tokens.contains("ROLE")) {
265+
if (JdbcUtils.containsIgnoresCase(tokens, "ROLE")) {
266266
List<String> roles = new ArrayList<>();
267-
int roleIndex = tokens.indexOf("ROLE");
267+
int roleIndex = JdbcUtils.indexOfIgnoresCase(tokens, "ROLE");
268268
if (roleIndex == 1) {
269269
for (int i = 2; i < tokens.size(); i++) {
270270
roles.add(tokens.get(i));
271271
}
272272

273-
if (roles.contains("NONE")) {
273+
if (JdbcUtils.containsIgnoresCase(roles, "NONE")) {
274274
connection.client.setDBRoles(Collections.emptyList());
275275
} else {
276276
connection.client.setDBRoles(roles);

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,32 @@ public static List<String> tokenizeSQL(String sql) {
7777
public static boolean isBlank(String str) {
7878
return str == null || str.isEmpty() || str.trim().isEmpty();
7979
}
80+
81+
public static boolean containsIgnoresCase(List<String> list, String str) {
82+
if (list == null || list.isEmpty() || isBlank(str)) {
83+
return false;
84+
}
85+
86+
for (String s : list) {
87+
if (s.equalsIgnoreCase(str)) {
88+
return true;
89+
}
90+
}
91+
92+
return false;
93+
}
94+
95+
public static int indexOfIgnoresCase(List<String> list, String str) {
96+
if (list == null || list.isEmpty() || isBlank(str)) {
97+
return -1;
98+
}
99+
100+
for (int i = 0; i < list.size(); i++) {
101+
if (list.get(i).equalsIgnoreCase(str)) {
102+
return i;
103+
}
104+
}
105+
106+
return -1;
107+
}
80108
}

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import static org.testng.Assert.assertTrue;
2222

2323
public class StatementTest extends JdbcIntegrationTest {
24-
@Test
24+
@Test(groups = { "integration" })
2525
public void testExecuteQuerySimpleNumbers() throws Exception {
2626
try (Connection conn = getJdbcConnection()) {
2727
try (Statement stmt = conn.createStatement()) {
@@ -41,7 +41,7 @@ public void testExecuteQuerySimpleNumbers() throws Exception {
4141
}
4242
}
4343

44-
@Test
44+
@Test(groups = { "integration" })
4545
public void testExecuteQuerySimpleFloats() throws Exception {
4646
try (Connection conn = getJdbcConnection()) {
4747
try (Statement stmt = conn.createStatement()) {
@@ -57,7 +57,7 @@ public void testExecuteQuerySimpleFloats() throws Exception {
5757
}
5858
}
5959

60-
@Test
60+
@Test(groups = { "integration" })
6161
public void testExecuteQueryBooleans() throws Exception {
6262
try (Connection conn = getJdbcConnection()) {
6363
try (Statement stmt = conn.createStatement()) {
@@ -71,7 +71,7 @@ public void testExecuteQueryBooleans() throws Exception {
7171
}
7272
}
7373

74-
@Test
74+
@Test(groups = { "integration" })
7575
public void testExecuteQueryStrings() throws Exception {
7676
try (Connection conn = getJdbcConnection()) {
7777
try (Statement stmt = conn.createStatement()) {
@@ -85,7 +85,7 @@ public void testExecuteQueryStrings() throws Exception {
8585
}
8686
}
8787

88-
@Test
88+
@Test(groups = { "integration" })
8989
public void testExecuteQueryNulls() throws Exception {
9090
try (Connection conn = getJdbcConnection()) {
9191
try (Statement stmt = conn.createStatement()) {
@@ -101,7 +101,7 @@ public void testExecuteQueryNulls() throws Exception {
101101
}
102102
}
103103

104-
@Test
104+
@Test(groups = { "integration" })
105105
public void testExecuteQueryDates() throws Exception {
106106
try (Connection conn = getJdbcConnection()) {
107107
try (Statement stmt = conn.createStatement()) {
@@ -129,7 +129,7 @@ public void testExecuteQueryDates() throws Exception {
129129
}
130130
}
131131

132-
@Test
132+
@Test(groups = { "integration" })
133133
public void testExecuteUpdateSimpleNumbers() throws Exception {
134134
try (Connection conn = getJdbcConnection()) {
135135
try (Statement stmt = conn.createStatement()) {
@@ -148,7 +148,7 @@ public void testExecuteUpdateSimpleNumbers() throws Exception {
148148
}
149149
}
150150

151-
@Test
151+
@Test(groups = { "integration" })
152152
public void testExecuteUpdateSimpleFloats() throws Exception {
153153
try (Connection conn = getJdbcConnection()) {
154154
try (Statement stmt = conn.createStatement()) {
@@ -167,7 +167,7 @@ public void testExecuteUpdateSimpleFloats() throws Exception {
167167
}
168168
}
169169

170-
@Test
170+
@Test(groups = { "integration" })
171171
public void testExecuteUpdateBooleans() throws Exception {
172172
try (Connection conn = getJdbcConnection()) {
173173
try (Statement stmt = conn.createStatement()) {
@@ -186,7 +186,7 @@ public void testExecuteUpdateBooleans() throws Exception {
186186
}
187187
}
188188

189-
@Test
189+
@Test(groups = { "integration" })
190190
public void testExecuteUpdateStrings() throws Exception {
191191
try (Connection conn = getJdbcConnection()) {
192192
try (Statement stmt = conn.createStatement()) {
@@ -205,7 +205,7 @@ public void testExecuteUpdateStrings() throws Exception {
205205
}
206206
}
207207

208-
@Test
208+
@Test(groups = { "integration" })
209209
public void testExecuteUpdateNulls() throws Exception {
210210
try (Connection conn = getJdbcConnection()) {
211211
try (Statement stmt = conn.createStatement()) {
@@ -224,7 +224,7 @@ public void testExecuteUpdateNulls() throws Exception {
224224
}
225225
}
226226

227-
@Test
227+
@Test(groups = { "integration" })
228228
public void testExecuteUpdateDates() throws Exception {
229229
try (Connection conn = getJdbcConnection()) {
230230
try (Statement stmt = conn.createStatement()) {
@@ -247,7 +247,7 @@ public void testExecuteUpdateDates() throws Exception {
247247
}
248248

249249

250-
@Test
250+
@Test(groups = { "integration" })
251251
public void testExecuteUpdateBatch() throws Exception {
252252
try (Connection conn = getJdbcConnection()) {
253253
try (Statement stmt = conn.createStatement()) {
@@ -275,7 +275,7 @@ public void testExecuteUpdateBatch() throws Exception {
275275
}
276276
}
277277

278-
@Test
278+
@Test(groups = { "integration" })
279279
public void testJdbcEscapeSyntax() throws Exception {
280280
try (Connection conn = getJdbcConnection()) {
281281
try (Statement stmt = conn.createStatement()) {
@@ -316,7 +316,7 @@ public void testJdbcEscapeSyntax() throws Exception {
316316
}
317317
}
318318

319-
@Test
319+
@Test(groups = { "integration" })
320320
public void testExecuteQueryTimeout() throws Exception {
321321
try (Connection conn = getJdbcConnection()) {
322322
try (Statement stmt = conn.createStatement()) {
@@ -331,7 +331,7 @@ public void testExecuteQueryTimeout() throws Exception {
331331
}
332332

333333

334-
@Test
334+
@Test(groups = { "integration" })
335335
private void testSettingRole() throws SQLException {
336336
List<String> roles = Arrays.asList("role1", "role2");
337337

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.clickhouse.jdbc.internal;
2+
3+
import com.clickhouse.jdbc.JdbcIntegrationTest;
4+
import org.testng.annotations.Test;
5+
6+
import java.util.List;
7+
8+
import static org.testng.Assert.assertEquals;
9+
10+
public class JdbcUtilsTest extends JdbcIntegrationTest {
11+
@Test(groups = { "integration" })
12+
public void testTokenizeSQL() {
13+
String sql1 = "SELECT * FROM table WHERE id = 1";
14+
List<String> tokens1 = JdbcUtils.tokenizeSQL(sql1);
15+
assertEquals(tokens1.size(), 8);
16+
assertEquals(tokens1.get(0), "SELECT");
17+
assertEquals(tokens1.get(1), "*");
18+
assertEquals(tokens1.get(2), "FROM");
19+
assertEquals(tokens1.get(3), "table");
20+
assertEquals(tokens1.get(4), "WHERE");
21+
assertEquals(tokens1.get(5), "id");
22+
assertEquals(tokens1.get(6), "=");
23+
assertEquals(tokens1.get(7), "1");
24+
25+
String sql2 = "SELECT * FROM table WHERE id = 1 AND name = 'John'";
26+
List<String> tokens2 = JdbcUtils.tokenizeSQL(sql2);
27+
assertEquals(tokens2.size(), 12);
28+
assertEquals(tokens2.get(0), "SELECT");
29+
assertEquals(tokens2.get(1), "*");
30+
assertEquals(tokens2.get(2), "FROM");
31+
assertEquals(tokens2.get(3), "table");
32+
assertEquals(tokens2.get(4), "WHERE");
33+
assertEquals(tokens2.get(5), "id");
34+
assertEquals(tokens2.get(6), "=");
35+
assertEquals(tokens2.get(7), "1");
36+
assertEquals(tokens2.get(8), "AND");
37+
assertEquals(tokens2.get(9), "name");
38+
assertEquals(tokens2.get(10), "=");
39+
assertEquals(tokens2.get(11), "'John'");
40+
41+
String sql3 = "SELECT * FROM table WHERE \"id = 1 AND name = 'John' OR age = 30\"";//Technically, this is not a valid SQL statement
42+
List<String> tokens3 = JdbcUtils.tokenizeSQL(sql3);
43+
assertEquals(tokens3.size(), 6);
44+
assertEquals(tokens3.get(0), "SELECT");
45+
assertEquals(tokens3.get(1), "*");
46+
assertEquals(tokens3.get(2), "FROM");
47+
assertEquals(tokens3.get(3), "table");
48+
assertEquals(tokens3.get(4), "WHERE");
49+
assertEquals(tokens3.get(5), "id = 1 AND name = 'John' OR age = 30");
50+
}
51+
}

0 commit comments

Comments
 (0)