|
102 | 102 | import java.util.concurrent.Future;
|
103 | 103 | import java.util.concurrent.TimeUnit;
|
104 | 104 | import java.util.concurrent.TimeoutException;
|
| 105 | +import java.util.function.Consumer; |
105 | 106 | import java.util.function.Supplier;
|
106 | 107 | import java.util.function.ToIntFunction;
|
107 | 108 |
|
@@ -12285,4 +12286,98 @@ private int countValues(String query) {
|
12285 | 12286 | }
|
12286 | 12287 | }
|
12287 | 12288 |
|
| 12289 | + /** |
| 12290 | + * Tests for Bug#21978230, COMMENT PARSING NOT PROPER IN PREPSTMT.EXECUTEBATCH(). |
| 12291 | + * |
| 12292 | + * @throws Exception |
| 12293 | + */ |
| 12294 | + @Test |
| 12295 | + public void testBug21978230() throws Exception { |
| 12296 | + createTable("testBug21978230", "(c1 INT, c2 INT, t VARCHAR(100))"); |
| 12297 | + |
| 12298 | + Properties props = new Properties(); |
| 12299 | + props.setProperty(PropertyKey.sslMode.getKeyName(), SslMode.DISABLED.name()); |
| 12300 | + props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true"); |
| 12301 | + props.setProperty(PropertyKey.rewriteBatchedStatements.getKeyName(), "true"); |
| 12302 | + props.setProperty(PropertyKey.continueBatchOnError.getKeyName(), "false"); |
| 12303 | + props.setProperty(PropertyKey.emulateUnsupportedPstmts.getKeyName(), "true"); |
| 12304 | + Connection testConn = getConnectionWithProps(props); |
| 12305 | + |
| 12306 | + Consumer<String> runQueryAndAssertResults = (query) -> { |
| 12307 | + try { |
| 12308 | + this.pstmt = testConn.prepareStatement(query); |
| 12309 | + this.pstmt.setInt(1, 1); |
| 12310 | + this.pstmt.setInt(2, 10); |
| 12311 | + this.pstmt.setString(3, "A"); |
| 12312 | + this.pstmt.setString(4, "A"); |
| 12313 | + this.pstmt.addBatch(); |
| 12314 | + this.pstmt.setInt(1, 2); |
| 12315 | + this.pstmt.setInt(2, 20); |
| 12316 | + this.pstmt.setString(3, "B"); |
| 12317 | + this.pstmt.setString(4, "B"); |
| 12318 | + this.pstmt.addBatch(); |
| 12319 | + this.pstmt.setInt(1, 3); |
| 12320 | + this.pstmt.setInt(2, 30); |
| 12321 | + this.pstmt.setString(3, "C"); |
| 12322 | + this.pstmt.setString(4, "C"); |
| 12323 | + this.pstmt.addBatch(); |
| 12324 | + this.pstmt.setInt(1, 4); |
| 12325 | + this.pstmt.setInt(2, 40); |
| 12326 | + this.pstmt.setString(3, "D"); |
| 12327 | + this.pstmt.setString(4, "D"); |
| 12328 | + this.pstmt.addBatch(); |
| 12329 | + this.pstmt.executeBatch(); |
| 12330 | + |
| 12331 | + this.rs = this.stmt.executeQuery("SELECT * FROM testBug21978230"); |
| 12332 | + for (int i = 1; i <= 4; i++) { |
| 12333 | + assertTrue(this.rs.next()); |
| 12334 | + assertEquals(i, this.rs.getInt(1)); |
| 12335 | + assertEquals(i * 10, this.rs.getInt(2)); |
| 12336 | + char chr = (char) ('A' + i - 1); |
| 12337 | + assertEquals(chr + "^^^^" + chr, this.rs.getString(3)); |
| 12338 | + } |
| 12339 | + assertFalse(this.rs.next()); |
| 12340 | + } catch (SQLException e) { |
| 12341 | + throw new RuntimeException(e); |
| 12342 | + } |
| 12343 | + }; |
| 12344 | + |
| 12345 | + /* |
| 12346 | + * Expected: "Parameter index out of range". |
| 12347 | + */ |
| 12348 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12349 | + Exception ex = assertThrows(RuntimeException.class, ".*Parameter index out of range \\(3 > number of parameters, which is 2\\)\\.", () -> { |
| 12350 | + runQueryAndAssertResults.accept("REPLACE INTO testBug21978230 VALUES (?, ?, /*/ CONCAT(?, '^^^^', ?)) /*ON DUPLICATE KEY UPDATE v=concat(v,?)*/"); |
| 12351 | + return null; |
| 12352 | + }); |
| 12353 | + assertEquals(ex.getCause().getClass(), SQLException.class); |
| 12354 | + |
| 12355 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12356 | + ex = assertThrows(RuntimeException.class, ".*Parameter index out of range \\(3 > number of parameters, which is 2\\)\\.", () -> { |
| 12357 | + runQueryAndAssertResults.accept("INSERT INTO testBug21978230 VALUES (?, ?, /*/ CONCAT(?, '^^^^', ?)) /*ON DUPLICATE KEY UPDATE v=concat(v,?)*/"); |
| 12358 | + return null; |
| 12359 | + }); |
| 12360 | + assertEquals(ex.getCause().getClass(), SQLException.class); |
| 12361 | + |
| 12362 | + /* |
| 12363 | + * Expected: 4 records inserted in each query |
| 12364 | + */ |
| 12365 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12366 | + runQueryAndAssertResults.accept("REPLACE INTO testBug21978230 VALUES (?, ?, /**/ CONCAT(?, '^^^^', ?)) /*ON DUPLICATE KEY UPDATE v=concat(v,?)*/"); |
| 12367 | + |
| 12368 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12369 | + runQueryAndAssertResults.accept("INSERT INTO testBug21978230 VALUES (?, ?, /**/ CONCAT(?, '^^^^', ?)) /*ON DUPLICATE KEY UPDATE v=concat(v,?)*/"); |
| 12370 | + |
| 12371 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12372 | + runQueryAndAssertResults.accept("REPLACE INTO testBug21978230 VALUES (?, ?, concat(?, '^^^^', ?)) /*ON DUPLICATE KEY UPDaTE v=concat(v,?)*/"); |
| 12373 | + |
| 12374 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12375 | + runQueryAndAssertResults.accept("INSERT INTO testBug21978230 VALUES (?, ?, concat(?, '^^^^', ?)) /*ON DUPLICATE KEY UPDaTE v=concat(v,?)*/"); |
| 12376 | + |
| 12377 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12378 | + runQueryAndAssertResults.accept("REPLACE INTO testBug21978230 VALUES(?, ?, /* comment */CONCAT(?, '^^^^', ?))"); |
| 12379 | + |
| 12380 | + this.stmt.execute("TRUNCATE TABLE testBug21978230"); |
| 12381 | + runQueryAndAssertResults.accept("INSERT INTO testBug21978230 VALUES(?, ?, /* comment */CONCAT(?, '^^^^', ?))"); |
| 12382 | + } |
12288 | 12383 | }
|
0 commit comments