Skip to content

Commit 4279aa6

Browse files
authored
Merge pull request #2042 from ClickHouse/add-primitive-array-type
add-encoding-for-array
2 parents f671be3 + 4c77f3b commit 4279aa6

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,20 @@ private static String encodeObject(Object x) throws SQLException {
532532
sb.append(new String(buffer, 0, len));
533533
}
534534
return "'" + escapeString(sb.toString()) + "'";
535+
} else if (x instanceof Object[]) {
536+
StringBuilder arrayString = new StringBuilder();
537+
arrayString.append("[");
538+
int i = 0;
539+
for (Object item : (Object[]) x) {
540+
if (i > 0) {
541+
arrayString.append(", ");
542+
}
543+
arrayString.append(encodeObject(item));
544+
i++;
545+
}
546+
arrayString.append("]");
547+
548+
return arrayString.toString();
535549
}
536550

537551
return escapeString(x.toString());//Escape single quotes
@@ -542,6 +556,6 @@ private static String encodeObject(Object x) throws SQLException {
542556
}
543557

544558
private static String escapeString(String x) {
545-
return x.replace("'", "''");//Escape single quotes
559+
return x.replace("\\", "\\\\").replace("'", "\\'");//Escape single quotes
546560
}
547561
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.clickhouse.jdbc;
22

3+
import com.clickhouse.client.api.data_formats.internal.BinaryStreamReader;
34
import org.testng.annotations.Ignore;
45
import org.testng.annotations.Test;
56

@@ -8,10 +9,12 @@
89
import java.sql.ResultSet;
910
import java.sql.Timestamp;
1011
import java.sql.Types;
12+
import java.util.Arrays;
1113
import java.util.Calendar;
1214

1315
import static org.testng.Assert.assertEquals;
1416
import static org.testng.Assert.assertFalse;
17+
import static org.testng.Assert.assertNotNull;
1518
import static org.testng.Assert.assertNull;
1619
import static org.testng.Assert.assertTrue;
1720

@@ -222,4 +225,37 @@ public void testBigDecimal() throws Exception {
222225
}
223226
}
224227
}
228+
229+
@Test(groups = { "integration" })
230+
public void testPrimitiveArrays() throws Exception {
231+
try (Connection conn = getJdbcConnection()) {
232+
try (PreparedStatement stmt = conn.prepareStatement("SELECT ?")) {
233+
stmt.setObject(1, new String[][] {new String[]{"a"}, new String[]{"b"}, new String[]{"c"}});
234+
try (ResultSet rs = stmt.executeQuery()) {
235+
assertTrue(rs.next());
236+
assertEquals(((BinaryStreamReader.ArrayValue)((BinaryStreamReader.ArrayValue)rs.getObject(1)).get(0)).get(0), "a");
237+
assertEquals(((BinaryStreamReader.ArrayValue)((BinaryStreamReader.ArrayValue)rs.getObject(1)).get(1)).get(0), "b");
238+
assertEquals(((BinaryStreamReader.ArrayValue)((BinaryStreamReader.ArrayValue)rs.getObject(1)).get(2)).get(0), "c");
239+
assertFalse(rs.next());
240+
}
241+
}
242+
}
243+
}
244+
245+
246+
@Test(groups = { "integration" })
247+
public void testEscapeStrings() throws Exception {
248+
try (Connection conn = getJdbcConnection()) {
249+
try (PreparedStatement stmt = conn.prepareStatement("SELECT FALSE OR ? = 'test', ?")) {
250+
stmt.setString(1, "test\\' OR 1 = 1 --");
251+
stmt.setString(2, "test\\\\' OR 1 = 1 --");
252+
try (ResultSet rs = stmt.executeQuery()) {
253+
assertTrue(rs.next());
254+
assertEquals(rs.getString(1), "false");
255+
assertEquals(rs.getString(2), "test\\\\' OR 1 = 1 --");
256+
assertFalse(rs.next());
257+
}
258+
}
259+
}
260+
}
225261
}

0 commit comments

Comments
 (0)