Skip to content

Commit a0dd199

Browse files
committed
Solve issue 2329 - StringIndexOutOfBoundsException on empty collection in encodeObject method.
1 parent a245c1f commit a0dd199

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ private static String encodeObject(Object x) throws SQLException {
596596
for (Object item : (Collection<?>) x) {
597597
listString.append(encodeObject(item)).append(", ");
598598
}
599-
listString.delete(listString.length() - 2, listString.length());
599+
if (listString.length() > 1) {
600+
listString.delete(listString.length() - 2, listString.length());
601+
}
600602
listString.append("]");
601603

602604
return listString.toString();

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +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;
16+
import java.util.*;
1917

2018
import static org.testng.Assert.assertEquals;
2119
import static org.testng.Assert.assertFalse;
@@ -580,4 +578,32 @@ void testClearParameters() throws Exception {
580578
ps.execute();
581579
}
582580
}
581+
582+
@Test(groups = {"integration"})
583+
void testWriteCollection() throws Exception {
584+
String sql = "insert into `test_issue_2329` (`id`, `name`, `age`, `arr`) values (?, ?, ?, ?)";
585+
try (Connection conn = getJdbcConnection();
586+
PreparedStatementImpl ps = (PreparedStatementImpl) conn.prepareStatement(sql)) {
587+
588+
try (Statement stmt = conn.createStatement()) {
589+
stmt.execute("CREATE TABLE IF NOT EXISTS `test_issue_2329` (`id` Nullable(String), `name` Nullable(String), `age` Int32, `arr` Array(String)) ENGINE Memory;");
590+
}
591+
592+
Assert.assertEquals(ps.getParametersCount(), 4);
593+
Collection<String> arr = new ArrayList<String>();
594+
ps.setString(1, "testId01");
595+
ps.setString(2, "testName");
596+
ps.setInt(3, 18);
597+
ps.setObject(4, arr);
598+
ps.execute();
599+
600+
try (Statement stmt = conn.createStatement()) {
601+
ResultSet rs = stmt.executeQuery("SELECT count(*) FROM `test_issue_2329`");
602+
Assert.assertTrue(rs.next());
603+
Assert.assertEquals(rs.getInt(1), 1);
604+
}
605+
}
606+
607+
}
608+
583609
}

0 commit comments

Comments
 (0)