|
1 | 1 | package com.clickhouse.jdbc; |
2 | 2 |
|
3 | 3 | import org.apache.commons.lang3.RandomStringUtils; |
| 4 | +import org.testng.Assert; |
4 | 5 | import org.testng.annotations.Ignore; |
5 | 6 | import org.testng.annotations.Test; |
6 | 7 |
|
7 | 8 | import java.sql.Array; |
8 | 9 | import java.sql.Connection; |
9 | | -import java.sql.JDBCType; |
10 | 10 | import java.sql.PreparedStatement; |
11 | 11 | import java.sql.ResultSet; |
12 | | -import java.sql.ResultSetMetaData; |
| 12 | +import java.sql.SQLException; |
13 | 13 | import java.sql.Statement; |
14 | 14 | import java.sql.Types; |
15 | 15 | import java.util.Arrays; |
@@ -491,4 +491,68 @@ void testStatementSplit() throws Exception { |
491 | 491 | } |
492 | 492 | } |
493 | 493 | } |
| 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 | + } |
494 | 558 | } |
0 commit comments