Skip to content

Commit 6ecc277

Browse files
authored
Merge pull request #2305 from CCweixiao/dev-2299
[jdbc-v2] Fixes in the PreparedStatementImpl class, the initial length of the parameters is incorrectly changed in the clearParameters method
2 parents e62a180 + a1d1383 commit 6ecc277

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public PreparedStatementImpl(ConnectionImpl connection, String sql) throws SQLEx
9090
this.parameterMetaData = new ParameterMetaDataImpl(this.parameters.length);
9191
}
9292

93-
private String compileSql(String [] segments) {
93+
private String compileSql(String []segments) {
9494
StringBuilder sb = new StringBuilder();
9595
for (int i = 0; i < segments.length; i++) {
9696
sb.append(segments[i]);
@@ -217,7 +217,7 @@ public void setBinaryStream(int parameterIndex, InputStream x, int length) throw
217217
public void clearParameters() throws SQLException {
218218
checkClosed();
219219
if (originalSql.contains("?")) {
220-
this.parameters = new Object[sqlSegments.length];
220+
this.parameters = new Object[sqlSegments.length - 1];
221221
} else {
222222
this.parameters = new Object[0];
223223
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.sql.Connection;
1111
import java.sql.PreparedStatement;
1212
import java.sql.ResultSet;
13-
import java.sql.ResultSetMetaData;
1413
import java.sql.Statement;
1514
import java.sql.Types;
1615
import java.util.Arrays;
@@ -553,4 +552,31 @@ void testStatementSplit() throws Exception {
553552

554553
}
555554
}
555+
556+
@Test(groups = {"integration"})
557+
void testClearParameters() throws Exception {
558+
String sql = "insert into `test_issue_2299` (`id`, `name`, `age`) values (?, ?, ?)";
559+
try (Connection conn = getJdbcConnection();
560+
PreparedStatementImpl ps = (PreparedStatementImpl) conn.prepareStatement(sql)) {
561+
562+
try (Statement stmt = conn.createStatement()) {
563+
stmt.execute("CREATE TABLE IF NOT EXISTS `test_issue_2299` (`id` Nullable(String), `name` Nullable(String), `age` Int32) ENGINE Memory;");
564+
}
565+
566+
Assert.assertEquals(ps.parameters.length, 3);
567+
568+
ps.setString(1, "testId");
569+
ps.setString(2, "testName");
570+
ps.setInt(3, 18);
571+
ps.execute();
572+
573+
ps.clearParameters();
574+
Assert.assertEquals(ps.parameters.length, 3);
575+
576+
ps.setString(1, "testId2");
577+
ps.setString(2, "testName2");
578+
ps.setInt(3, 19);
579+
ps.execute();
580+
}
581+
}
556582
}

0 commit comments

Comments
 (0)