|
10 | 10 | import static org.junit.jupiter.api.Assertions.assertEquals; |
11 | 11 | import static org.junit.jupiter.api.Assertions.assertTrue; |
12 | 12 | import static org.junit.jupiter.api.Assertions.fail; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
13 | 14 |
|
14 | 15 | import java.lang.reflect.Field; |
15 | 16 | import java.sql.BatchUpdateException; |
@@ -72,6 +73,8 @@ public class BatchExecutionTest extends AbstractTest { |
72 | 73 | .escapeIdentifier(RandomUtil.getIdentifier("timestamptable1")); |
73 | 74 | private static String timestampTable2 = AbstractSQLGenerator |
74 | 75 | .escapeIdentifier(RandomUtil.getIdentifier("timestamptable2")); |
| 76 | + private static String caseSensitiveDatabase = "BD_Collation_SQL_Latin1_General_CP1_CS_AS"; |
| 77 | + private static String caseInsensitiveDatabase = "BD_Collation_SQL_Latin1_General_CP1_CI_AS"; |
75 | 78 |
|
76 | 79 | /** |
77 | 80 | * This tests the updateCount when the error query does cause a SQL state HY008. |
@@ -568,6 +571,114 @@ public void testBatchStatementCancellation() throws Exception { |
568 | 571 | } |
569 | 572 | } |
570 | 573 |
|
| 574 | + @Test |
| 575 | + @Tag(Constants.xAzureSQLDB) |
| 576 | + public void testExecuteBatchColumnCaseMismatch_CI() throws Exception { |
| 577 | + String connectionStringCollationCaseInsensitive = TestUtils.addOrOverrideProperty(connectionString, "databaseName", caseInsensitiveDatabase); |
| 578 | + String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("caseInsensitiveTable")); |
| 579 | + |
| 580 | + try (Connection conTemp = DriverManager.getConnection(connectionString); Statement stmt = conTemp.createStatement()) { |
| 581 | + TestUtils.dropDatabaseIfExists(caseInsensitiveDatabase, connectionString); |
| 582 | + stmt.executeUpdate("CREATE DATABASE " + caseInsensitiveDatabase); |
| 583 | + stmt.executeUpdate("ALTER DATABASE " + caseInsensitiveDatabase + " COLLATE SQL_Latin1_General_CP1_CI_AS;"); |
| 584 | + |
| 585 | + // Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true |
| 586 | + try (Connection con = DriverManager.getConnection(connectionStringCollationCaseInsensitive |
| 587 | + + ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) { |
| 588 | + try (Statement statement = con.createStatement()) { |
| 589 | + TestUtils.dropTableIfExists(tableName, statement); |
| 590 | + String createSql = "CREATE TABLE" + tableName + " (c1 varchar(10))"; |
| 591 | + statement.execute(createSql); |
| 592 | + } |
| 593 | + // upper case C1 |
| 594 | + try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + tableName + "(C1) VALUES(?)")) { |
| 595 | + preparedStatement.setObject(1, "value1"); |
| 596 | + preparedStatement.addBatch(); |
| 597 | + preparedStatement.setObject(1, "value2"); |
| 598 | + preparedStatement.addBatch(); |
| 599 | + preparedStatement.executeBatch(); |
| 600 | + } |
| 601 | + } |
| 602 | + } finally { |
| 603 | + TestUtils.dropDatabaseIfExists(caseInsensitiveDatabase, connectionString); |
| 604 | + } |
| 605 | + } |
| 606 | + |
| 607 | + // adapter Azure pipeline CI, need to add a new environment variable `mssql_jdbc_test_connection_properties_collation_cs` |
| 608 | + @Test |
| 609 | + @Tag(Constants.xAzureSQLDB) |
| 610 | + public void testExecuteBatchColumnCaseMismatch_CS_throwException() throws Exception { |
| 611 | + String connectionStringCollationCaseSensitive = TestUtils.addOrOverrideProperty(connectionString, "databaseName", caseSensitiveDatabase); |
| 612 | + String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("caseSensitiveTable")); |
| 613 | + |
| 614 | + try (Connection conTemp = DriverManager.getConnection(connectionString); Statement stmt = conTemp.createStatement()) { |
| 615 | + TestUtils.dropDatabaseIfExists(caseSensitiveDatabase, connectionString); |
| 616 | + stmt.executeUpdate("CREATE DATABASE " + caseSensitiveDatabase); |
| 617 | + stmt.executeUpdate("ALTER DATABASE " + caseSensitiveDatabase + " COLLATE SQL_Latin1_General_CP1_CS_AS;"); |
| 618 | + |
| 619 | + try (Connection con = DriverManager.getConnection(connectionStringCollationCaseSensitive |
| 620 | + + ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) { |
| 621 | + try (Statement statement = con.createStatement()) { |
| 622 | + TestUtils.dropTableIfExists(tableName, statement); |
| 623 | + String createSql = "CREATE TABLE" + tableName + " (c1 varchar(10))"; |
| 624 | + statement.execute(createSql); |
| 625 | + } |
| 626 | + // upper case C1 |
| 627 | + try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + tableName + "(C1) VALUES(?)")) { |
| 628 | + preparedStatement.setObject(1, "value1"); |
| 629 | + preparedStatement.addBatch(); |
| 630 | + preparedStatement.setObject(1, "value2"); |
| 631 | + preparedStatement.addBatch(); |
| 632 | + try { |
| 633 | + preparedStatement.executeBatch(); |
| 634 | + fail("Should have failed"); |
| 635 | + } catch (Exception ex) { |
| 636 | + assertInstanceOf(java.sql.BatchUpdateException.class, ex); |
| 637 | + assertEquals("Unable to retrieve column metadata.", ex.getMessage()); |
| 638 | + } |
| 639 | + } |
| 640 | + } |
| 641 | + } finally { |
| 642 | + TestUtils.dropDatabaseIfExists(caseSensitiveDatabase, connectionString); |
| 643 | + } |
| 644 | + } |
| 645 | + |
| 646 | + // adapter Azure pipeline CI, need to add a new environment variable `mssql_jdbc_test_connection_properties_collation_cs` |
| 647 | + @Test |
| 648 | + @Tag(Constants.xAzureSQLDB) |
| 649 | + public void testExecuteBatchColumnCaseMismatch_CS() throws Exception { |
| 650 | + String connectionStringCollationCaseSensitive = TestUtils.addOrOverrideProperty(connectionString, "databaseName", caseSensitiveDatabase); |
| 651 | + String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("caseSensitiveTable")); |
| 652 | + |
| 653 | + try (Connection conTemp = DriverManager.getConnection(connectionString); Statement stmt = conTemp.createStatement()) { |
| 654 | + TestUtils.dropDatabaseIfExists(caseSensitiveDatabase, connectionString); |
| 655 | + stmt.executeUpdate("CREATE DATABASE " + caseSensitiveDatabase); |
| 656 | + stmt.executeUpdate("ALTER DATABASE " + caseSensitiveDatabase + " COLLATE SQL_Latin1_General_CP1_CS_AS;"); |
| 657 | + |
| 658 | + // Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true |
| 659 | + try (Connection con = DriverManager.getConnection(connectionStringCollationCaseSensitive |
| 660 | + + ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) { |
| 661 | + try (Statement statement = con.createStatement()) { |
| 662 | + TestUtils.dropTableIfExists(tableName, statement); |
| 663 | + String createSql = "CREATE TABLE" + tableName + " (c1 varchar(10), C1 varchar(10))"; |
| 664 | + statement.execute(createSql); |
| 665 | + } |
| 666 | + // upper case C1 |
| 667 | + try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + tableName + "(c1, C1) VALUES(?,?)")) { |
| 668 | + preparedStatement.setObject(1, "value1-1"); |
| 669 | + preparedStatement.setObject(2, "value1-2"); |
| 670 | + preparedStatement.addBatch(); |
| 671 | + preparedStatement.setObject(1, "value2-1"); |
| 672 | + preparedStatement.setObject(2, "value2-2"); |
| 673 | + preparedStatement.addBatch(); |
| 674 | + preparedStatement.executeBatch(); |
| 675 | + } |
| 676 | + } |
| 677 | + } finally { |
| 678 | + TestUtils.dropDatabaseIfExists(caseSensitiveDatabase, connectionString); |
| 679 | + } |
| 680 | + } |
| 681 | + |
571 | 682 | /** |
572 | 683 | * Get a PreparedStatement object and call the addBatch() method with 3 SQL statements and call the executeBatch() |
573 | 684 | * method and it should return array of Integer values of length 3 |
|
0 commit comments