|
7 | 7 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
8 | 8 | import static org.junit.jupiter.api.Assertions.assertTrue; |
9 | 9 |
|
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.io.InputStream; |
10 | 12 | import java.lang.reflect.Field; |
11 | 13 | import java.lang.reflect.Method; |
12 | 14 | import java.math.BigDecimal; |
13 | 15 | import java.math.RoundingMode; |
| 16 | +import java.nio.charset.StandardCharsets; |
14 | 17 | import java.sql.BatchUpdateException; |
15 | 18 | import java.sql.Connection; |
16 | 19 | import java.sql.Date; |
@@ -1696,6 +1699,63 @@ private void getCreateTableWithStringData() throws SQLException { |
1696 | 1699 | } |
1697 | 1700 | } |
1698 | 1701 |
|
| 1702 | + /** |
| 1703 | + * Test bulk insert with InputStream data when useBulkCopyForBatchInsert is true. |
| 1704 | + * Added support for InputStream data type with bulk copy batch insert. |
| 1705 | + * |
| 1706 | + * @throws Exception |
| 1707 | + */ |
| 1708 | + @Test |
| 1709 | + public void testBulkCopyBatchInsertForInputStreamData() throws Exception { |
| 1710 | + String tableName = RandomUtil.getIdentifier("BulkTableForInputStream"); |
| 1711 | + String insertSQL = "INSERT INTO " + AbstractSQLGenerator.escapeIdentifier(tableName) + |
| 1712 | + " (Id, Data) VALUES (?, ?)"; |
| 1713 | + String selectSQL = "SELECT Id, Data FROM " + AbstractSQLGenerator.escapeIdentifier(tableName); |
| 1714 | + |
| 1715 | + try (Connection connection = PrepUtil.getConnection(connectionString + ";useBulkCopyForBatchInsert=true;"); |
| 1716 | + Statement stmt = connection.createStatement(); |
| 1717 | + SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement(insertSQL)) { |
| 1718 | + |
| 1719 | + getCreateTableInputStream(tableName); |
| 1720 | + |
| 1721 | + String data = "Sample string to be inserted as binary data."; |
| 1722 | + byte[] bytes = data.getBytes(StandardCharsets.UTF_8); |
| 1723 | + InputStream inputStream = new ByteArrayInputStream(bytes); |
| 1724 | + |
| 1725 | + pstmt.setObject(1, 1); |
| 1726 | + pstmt.setBinaryStream(2, inputStream); |
| 1727 | + |
| 1728 | + pstmt.addBatch(); |
| 1729 | + pstmt.executeBatch(); |
| 1730 | + |
| 1731 | + // Validate inserted data |
| 1732 | + try (ResultSet rs = stmt.executeQuery(selectSQL)) { |
| 1733 | + assertTrue(rs.next()); |
| 1734 | + |
| 1735 | + assertEquals(1, rs.getInt(1)); |
| 1736 | + byte[] retrievedBytes = rs.getBytes(2); |
| 1737 | + String retrievedData = new String(retrievedBytes, StandardCharsets.UTF_8); |
| 1738 | + assertEquals(data, retrievedData); |
| 1739 | + |
| 1740 | + } |
| 1741 | + } finally { |
| 1742 | + try (Statement stmt = connection.createStatement()) { |
| 1743 | + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt); |
| 1744 | + } |
| 1745 | + } |
| 1746 | + } |
| 1747 | + |
| 1748 | + private void getCreateTableInputStream(String tableName) throws SQLException { |
| 1749 | + try (Statement stmt = connection.createStatement()) { |
| 1750 | + TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt); |
| 1751 | + String createTableSQL = "CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName) + " (" + |
| 1752 | + "Id INT PRIMARY KEY, " + |
| 1753 | + "Data VARBINARY(MAX) NULL" + ")"; |
| 1754 | + |
| 1755 | + stmt.execute(createTableSQL); |
| 1756 | + } |
| 1757 | + } |
| 1758 | + |
1699 | 1759 | @BeforeAll |
1700 | 1760 | public static void setupTests() throws Exception { |
1701 | 1761 | setConnection(); |
|
0 commit comments