Skip to content

Commit 47788e4

Browse files
committed
Updated logic for null handling
1 parent 4666e11 commit 47788e4

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,7 +2193,7 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
21932193
}
21942194

21952195
SQLServerBulkBatchInsertRecord batchRecord = new SQLServerBulkBatchInsertRecord(
2196-
batchParamValues, bcOperationColumnList, bcOperationValueList, null, connection.getDatabaseCollation().getIsCaseSensitive());
2196+
batchParamValues, bcOperationColumnList, bcOperationValueList, null, isDBColationCaseSensitive());
21972197

21982198
for (int i = 1; i <= rs.getColumnCount(); i++) {
21992199
Column c = rs.getColumn(i);
@@ -2211,15 +2211,15 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
22112211
}
22122212
if (null != bcOperationColumnList && !bcOperationColumnList.isEmpty()) {
22132213
// connection contains database name
2214-
boolean isCaseSensitive = connection.getDatabaseCollation().getIsCaseSensitive();
2215-
int columnIndex;
2214+
boolean isCaseSensitive = isDBColationCaseSensitive();
2215+
int columnIndex = -1;
22162216
if (isCaseSensitive) {
22172217
columnIndex = bcOperationColumnList.indexOf(c.getColumnName());
22182218
} else {
22192219
// find index ignore case
2220-
columnIndex = -1;
22212220
for (int opi = 0; opi < bcOperationColumnList.size(); opi++) {
2222-
if (bcOperationColumnList.get(opi).equalsIgnoreCase(c.getColumnName())) {
2221+
String opCol = bcOperationColumnList.get(opi);
2222+
if (opCol != null && opCol.equalsIgnoreCase(c.getColumnName())) {
22232223
columnIndex = opi;
22242224
break;
22252225
}
@@ -2401,7 +2401,7 @@ public long[] executeLargeBatch() throws SQLServerException, BatchUpdateExceptio
24012401
}
24022402

24032403
SQLServerBulkBatchInsertRecord batchRecord = new SQLServerBulkBatchInsertRecord(
2404-
batchParamValues, bcOperationColumnList, bcOperationValueList, null, connection.getDatabaseCollation().getIsCaseSensitive());
2404+
batchParamValues, bcOperationColumnList, bcOperationValueList, null, isDBColationCaseSensitive());
24052405

24062406
for (int i = 1; i <= rs.getColumnCount(); i++) {
24072407
Column c = rs.getColumn(i);
@@ -2493,6 +2493,12 @@ public long[] executeLargeBatch() throws SQLServerException, BatchUpdateExceptio
24932493
}
24942494
}
24952495

2496+
private boolean isDBColationCaseSensitive() throws SQLServerException {
2497+
if (null == connection.getDatabaseCollation())
2498+
return false;
2499+
return connection.getDatabaseCollation().getIsCaseSensitive();
2500+
}
2501+
24962502
private void checkValidColumns(TypeInfo ti) throws SQLServerException {
24972503
int jdbctype = ti.getSSType().getJDBCType().getIntValue();
24982504
String typeName;

src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/BatchExecutionTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ public void testBatchStatementCancellation() throws Exception {
572572
}
573573

574574
@Test
575+
@Tag(Constants.xAzureSQLDB)
575576
public void testExecuteBatchColumnCaseMismatch_CI() throws Exception {
576577
String connectionStringCollationCaseInsensitive = TestUtils.addOrOverrideProperty(connectionString, "databaseName", caseInsensitiveDatabase);
577578
String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("caseInsensitiveTable"));
@@ -605,6 +606,7 @@ public void testExecuteBatchColumnCaseMismatch_CI() throws Exception {
605606

606607
// adapter Azure pipeline CI, need to add a new environment variable `mssql_jdbc_test_connection_properties_collation_cs`
607608
@Test
609+
@Tag(Constants.xAzureSQLDB)
608610
public void testExecuteBatchColumnCaseMismatch_CS_throwException() throws Exception {
609611
String connectionStringCollationCaseSensitive = TestUtils.addOrOverrideProperty(connectionString, "databaseName", caseSensitiveDatabase);
610612
String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("caseSensitiveTable"));
@@ -643,6 +645,7 @@ public void testExecuteBatchColumnCaseMismatch_CS_throwException() throws Except
643645

644646
// adapter Azure pipeline CI, need to add a new environment variable `mssql_jdbc_test_connection_properties_collation_cs`
645647
@Test
648+
@Tag(Constants.xAzureSQLDB)
646649
public void testExecuteBatchColumnCaseMismatch_CS() throws Exception {
647650
String connectionStringCollationCaseSensitive = TestUtils.addOrOverrideProperty(connectionString, "databaseName", caseSensitiveDatabase);
648651
String tableName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("caseSensitiveTable"));

0 commit comments

Comments
 (0)