Skip to content

Commit 81e8c4d

Browse files
committed
Added test to verify fallback to normal execution for bulk copy api
2 parents f32cb53 + 44d6010 commit 81e8c4d

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkBatchInsertRecord.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import java.math.BigDecimal;
99
import java.math.RoundingMode;
10-
import java.nio.charset.Charset;
1110
import java.sql.Types;
1211
import java.text.DecimalFormat;
1312
import java.text.MessageFormat;
@@ -35,7 +34,6 @@ class SQLServerBulkBatchInsertRecord extends SQLServerBulkRecord {
3534
private int batchParamIndex = -1;
3635
private List<String> columnList;
3736
private List<String> valueList;
38-
private Charset charset;
3937

4038
/*
4139
* Class name for logging.
@@ -46,10 +44,10 @@ class SQLServerBulkBatchInsertRecord extends SQLServerBulkRecord {
4644
* Constructs a SQLServerBulkBatchInsertRecord with the batch parameter, column list, value list, and encoding
4745
*/
4846
SQLServerBulkBatchInsertRecord(ArrayList<Parameter[]> batchParam, ArrayList<String> columnList,
49-
ArrayList<String> valueList, Charset charset, boolean columnNameCaseSensitive) throws SQLServerException {
47+
ArrayList<String> valueList, String encoding, boolean columnNameCaseSensitive) throws SQLServerException {
5048
initLoggerResources();
5149
if (loggerExternal.isLoggable(java.util.logging.Level.FINER)) {
52-
loggerExternal.entering(loggerPackageName, loggerClassName, new Object[] {batchParam, charset.name()});
50+
loggerExternal.entering(loggerPackageName, loggerClassName, new Object[] {batchParam, encoding});
5351
}
5452

5553
if (null == batchParam) {
@@ -64,7 +62,6 @@ class SQLServerBulkBatchInsertRecord extends SQLServerBulkRecord {
6462
this.columnList = columnList;
6563
this.valueList = valueList;
6664
this.columnNameCaseSensitive = columnNameCaseSensitive;
67-
this.charset = charset;
6865
columnMetadata = new HashMap<>();
6966

7067
loggerExternal.exiting(loggerPackageName, loggerClassName);

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerPreparedStatement.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,8 +2218,7 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
22182218
}
22192219

22202220
SQLServerBulkBatchInsertRecord batchRecord = new SQLServerBulkBatchInsertRecord(
2221-
batchParamValues, bcOperationColumnList, bcOperationValueList,
2222-
connection.getDatabaseCollation().getCharset(), isDBColationCaseSensitive());
2221+
batchParamValues, bcOperationColumnList, bcOperationValueList, null, isDBColationCaseSensitive());
22232222

22242223
for (int i = 1; i <= rs.getColumnCount(); i++) {
22252224
Column c = rs.getColumn(i);
@@ -2427,8 +2426,7 @@ public long[] executeLargeBatch() throws SQLServerException, BatchUpdateExceptio
24272426
}
24282427

24292428
SQLServerBulkBatchInsertRecord batchRecord = new SQLServerBulkBatchInsertRecord(
2430-
batchParamValues, bcOperationColumnList, bcOperationValueList,
2431-
connection.getDatabaseCollation().getCharset(), isDBColationCaseSensitive());
2429+
batchParamValues, bcOperationColumnList, bcOperationValueList, null, isDBColationCaseSensitive());
24322430

24332431
for (int i = 1; i <= rs.getColumnCount(); i++) {
24342432
Column c = rs.getColumn(i);

src/test/java/com/microsoft/sqlserver/jdbc/preparedStatement/BatchExecutionWithBulkCopyTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.UUID;
3333

3434
import org.junit.jupiter.api.AfterAll;
35+
import org.junit.jupiter.api.Assertions;
3536
import org.junit.jupiter.api.BeforeEach;
3637
import org.junit.jupiter.api.BeforeAll;
3738
import org.junit.jupiter.api.Tag;
@@ -1407,6 +1408,54 @@ private void getCreateTableTemporalSQL(String tableName) throws SQLException {
14071408
}
14081409
}
14091410

1411+
/**
1412+
* Test insert-select fallback to normal execution for bulk copy API
1413+
*/
1414+
@Test
1415+
public void testInsertSelectFallbackToNormalExecution() throws Exception {
1416+
String tableNameSource = AbstractSQLGenerator.escapeIdentifier("SourceTable");
1417+
String tableNameDestination = AbstractSQLGenerator.escapeIdentifier("DestinationTable");
1418+
1419+
String connectStringUrl = connectionString
1420+
+ ";useBulkCopyForBatchInsert=true;sendStringParametersAsUnicode=false;";
1421+
1422+
try (Connection connection = PrepUtil.getConnection(connectStringUrl);
1423+
Statement stmt = connection.createStatement()) {
1424+
1425+
TestUtils.dropTableIfExists(tableNameSource, stmt);
1426+
String createSourceTableSQL = "CREATE TABLE " + tableNameSource + " (id INT, value VARCHAR(50))";
1427+
stmt.execute(createSourceTableSQL);
1428+
1429+
String insertSourceDataSQL = "INSERT INTO " + tableNameSource + " VALUES (1, 'TestValue1'), (2, 'TestValue2')";
1430+
stmt.execute(insertSourceDataSQL);
1431+
1432+
TestUtils.dropTableIfExists(tableNameDestination, stmt);
1433+
String createDestinationTableSQL = "CREATE TABLE " + tableNameDestination + " (id INT, value VARCHAR(50))";
1434+
stmt.execute(createDestinationTableSQL);
1435+
1436+
// Attempt unsupported INSERT-SELECT query for bulk copy api
1437+
String insertSelectSQL = "INSERT INTO " + tableNameDestination + " SELECT * FROM " + tableNameSource
1438+
+ " WHERE id = ?";
1439+
1440+
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection
1441+
.prepareStatement(insertSelectSQL)) {
1442+
pstmt.setInt(1, 1);
1443+
pstmt.addBatch();
1444+
pstmt.executeBatch(); // This should fall back to normal execution flow
1445+
}
1446+
1447+
// Validate inserted data in destination table
1448+
String selectSQL = "SELECT * FROM " + tableNameDestination;
1449+
try (ResultSet rs = stmt.executeQuery(selectSQL)) {
1450+
assertTrue(rs.next(), "Expected at least one row in result set");
1451+
assertEquals(1, rs.getInt("id"));
1452+
assertEquals("TestValue1", rs.getString("value"));
1453+
1454+
Assertions.assertFalse(rs.next(), "No more rows expected");
1455+
}
1456+
}
1457+
}
1458+
14101459
/**
14111460
* Test string values using prepared statement using accented and unicode characters.
14121461
* This test covers all combinations of useBulkCopyForBatchInsert and sendStringParametersAsUnicode.

0 commit comments

Comments
 (0)