Skip to content

Commit ab1917b

Browse files
authored
Solve issue 2329 - StringIndexOutOfBoundsException on empty collection in encodeObject method. (#2330)
1 parent 01f47f3 commit ab1917b

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,9 @@ private static String encodeObject(Object x) throws SQLException {
590590
for (Object item : (Collection<?>) x) {
591591
listString.append(encodeObject(item)).append(", ");
592592
}
593-
listString.delete(listString.length() - 2, listString.length());
593+
if (listString.length() > 1) {
594+
listString.delete(listString.length() - 2, listString.length());
595+
}
594596
listString.append("]");
595597

596598
return listString.toString();

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

+29-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
import java.sql.ResultSetMetaData;
1414
import java.sql.Statement;
1515
import java.sql.Types;
16-
import java.util.Arrays;
17-
import java.util.GregorianCalendar;
18-
import java.util.TimeZone;
19-
import java.util.UUID;
16+
import java.util.*;
2017

2118
import static org.testng.Assert.assertEquals;
2219
import static org.testng.Assert.assertFalse;
@@ -583,7 +580,7 @@ void testClearParameters() throws Exception {
583580
}
584581

585582
@Test(groups = {"integration"})
586-
void testWriteCollection() throws Exception {
583+
void testWriteUUID() throws Exception {
587584
String sql = "insert into `test_issue_2327` (`id`, `uuid`) values (?, ?)";
588585
try (Connection conn = getJdbcConnection();
589586
PreparedStatementImpl ps = (PreparedStatementImpl) conn.prepareStatement(sql)) {
@@ -604,4 +601,31 @@ void testWriteCollection() throws Exception {
604601
}
605602

606603
}
604+
605+
@Test(groups = {"integration"})
606+
void testWriteCollection() throws Exception {
607+
String sql = "insert into `test_issue_2329` (`id`, `name`, `age`, `arr`) values (?, ?, ?, ?)";
608+
try (Connection conn = getJdbcConnection();
609+
PreparedStatementImpl ps = (PreparedStatementImpl) conn.prepareStatement(sql)) {
610+
611+
try (Statement stmt = conn.createStatement()) {
612+
stmt.execute("CREATE TABLE IF NOT EXISTS `test_issue_2329` (`id` Nullable(String), `name` Nullable(String), `age` Int32, `arr` Array(String)) ENGINE Memory;");
613+
}
614+
615+
Assert.assertEquals(ps.getParametersCount(), 4);
616+
Collection<String> arr = new ArrayList<String>();
617+
ps.setString(1, "testId01");
618+
ps.setString(2, "testName");
619+
ps.setInt(3, 18);
620+
ps.setObject(4, arr);
621+
ps.execute();
622+
623+
try (Statement stmt = conn.createStatement()) {
624+
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM `test_issue_2329`");
625+
Assert.assertTrue(rs.next());
626+
Assert.assertEquals(rs.getInt(1), 1);
627+
}
628+
}
629+
630+
}
607631
}

0 commit comments

Comments
 (0)