Skip to content

Commit b902094

Browse files
author
jielongping
committed
Resolved code conflicts.
1 parent 92b46fc commit b902094

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public PreparedStatementImpl(ConnectionImpl connection, String sql) throws SQLEx
8484
this.defaultCalendar = connection.defaultCalendar;
8585
}
8686

87-
private String compileSql(String [] segments) {
87+
String compileSql(String []segments) {
8888
StringBuilder sb = new StringBuilder();
8989
for (int i = 0; i < segments.length; i++) {
9090
sb.append(segments[i]);

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

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.clickhouse.jdbc;
22

33
import org.apache.commons.lang3.RandomStringUtils;
4+
import org.testng.Assert;
45
import org.testng.annotations.Ignore;
56
import org.testng.annotations.Test;
67

78
import java.sql.Array;
89
import java.sql.Connection;
9-
import java.sql.JDBCType;
1010
import java.sql.PreparedStatement;
1111
import java.sql.ResultSet;
12-
import java.sql.ResultSetMetaData;
12+
import java.sql.SQLException;
1313
import java.sql.Statement;
1414
import java.sql.Types;
1515
import java.util.Arrays;
@@ -491,4 +491,68 @@ void testStatementSplit() throws Exception {
491491
}
492492
}
493493
}
494+
495+
@Test(groups = {"integration"})
496+
void testClearParameters() throws Exception {
497+
String sql = "insert into `test_issue_2299` (`id`, `name`, `age`) values (?, ?, ?)";
498+
try (Connection conn = getJdbcConnection();
499+
PreparedStatementImpl ps = (PreparedStatementImpl) conn.prepareStatement(sql);
500+
TestPreparedStatementImpl ps2 = new TestPreparedStatementImpl((ConnectionImpl) conn, sql)) {
501+
502+
try (Statement stmt = conn.createStatement()) {
503+
stmt.execute("CREATE TABLE IF NOT EXISTS `test_issue_2299` (`id` Nullable(String), `name` Nullable(String), `age` Int32) ENGINE Memory;");
504+
}
505+
506+
Assert.assertEquals(ps.parameters.length, 3);
507+
ps.clearParameters();
508+
Assert.assertEquals(ps.parameters.length, 3);
509+
510+
ps.setString(1, "testId");
511+
ps.setString(2, "testName");
512+
ps.setInt(3, 18);
513+
String compiledSql1 = ps.compileSql(ps.sqlSegments);
514+
Assert.assertEquals(compiledSql1, "insert into `test_issue_2299` (`id`, `name`, `age`) values ('testId', 'testName', 18)");
515+
516+
517+
Assert.assertEquals(ps2.parameters.length, 3);
518+
ps2.clearParameters();
519+
Assert.assertEquals(ps2.parameters.length, 4);
520+
521+
ps2.setString(1, "testId");
522+
ps2.setString(2, "testName");
523+
ps2.setInt(3, 18);
524+
525+
String compiledSql2 = ps2.compileSql(ps2.sqlSegments);
526+
Assert.assertEquals(compiledSql2, "insert into `test_issue_2299` (`id`, `name`, `age`) values ('testId', 'testName', 18)null");
527+
}
528+
}
529+
530+
static class TestPreparedStatementImpl extends PreparedStatementImpl {
531+
532+
public TestPreparedStatementImpl(ConnectionImpl connection, String sql) throws SQLException {
533+
super(connection, sql);
534+
}
535+
536+
@Override
537+
public void clearParameters() throws SQLException {
538+
checkClosed();
539+
if (originalSql.contains("?")) {
540+
this.parameters = new Object[sqlSegments.length];
541+
} else {
542+
this.parameters = new Object[0];
543+
}
544+
}
545+
546+
@Override
547+
String compileSql(String[] segments) {
548+
StringBuilder sb = new StringBuilder();
549+
for (int i = 0; i < segments.length; i++) {
550+
sb.append(segments[i]);
551+
if (i < parameters.length) {
552+
sb.append(parameters[i]);
553+
}
554+
}
555+
return sb.toString();
556+
}
557+
}
494558
}

0 commit comments

Comments
 (0)