Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] PreparedStatement.executeBatch() When the Insert Sql Column Name Case Mismatch, Throws SQLServerException "Unable to retrieve column metadata." #2589

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2210,7 +2210,14 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
jdbctype = ti.getSSType().getJDBCType().getIntValue();
}
if (null != bcOperationColumnList && !bcOperationColumnList.isEmpty()) {
int columnIndex = bcOperationColumnList.indexOf(c.getColumnName());
// find index ignore case
int columnIndex = -1;
for (int opi = 0; opi < bcOperationColumnList.size(); opi++) {
if (bcOperationColumnList.get(opi).equalsIgnoreCase(c.getColumnName())) {
columnIndex = opi;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
columnIndex = opi;
columnIndex = opi;
break;

}
}

if (columnIndex > -1) {
columnMappings.put(columnIndex + 1, i);
batchRecord.addColumnMetadata(columnIndex + 1, c.getColumnName(), jdbctype,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public class BatchExecutionTest extends AbstractTest {
.escapeIdentifier(RandomUtil.getIdentifier("timestamptable1"));
private static String timestampTable2 = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("timestamptable2"));
private static String caseSensitiveTable = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("caseSensitiveTable"));

/**
* This tests the updateCount when the error query does cause a SQL state HY008.
Expand Down Expand Up @@ -568,6 +570,27 @@ public void testBatchStatementCancellation() throws Exception {
}
}

@Test
public void testExecuteBatchColumnCaseMismatch() throws Exception {
// Insert Timestamp using prepared statement when useBulkCopyForBatchInsert=true
try (Connection con = DriverManager.getConnection(connectionString
+ ";useBulkCopyForBatchInsert=true;sendTemporalDataTypesAsStringForBulkCopy=false;")) {
try (Statement statement = con.createStatement()) {
TestUtils.dropTableIfExists(caseSensitiveTable, statement);
String createSql = "CREATE TABLE" + caseSensitiveTable + " (c1 varchar(10))";
statement.execute(createSql);
}
// upper case C1
try (PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO " + caseSensitiveTable + "(C1) VALUES(?)")) {
preparedStatement.setObject(1, "value1");
preparedStatement.addBatch();
preparedStatement.setObject(1, "value2");
preparedStatement.addBatch();
preparedStatement.executeBatch();
}
}
}
wooln marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get a PreparedStatement object and call the addBatch() method with 3 SQL statements and call the executeBatch()
* method and it should return array of Integer values of length 3
Expand Down Expand Up @@ -839,6 +862,7 @@ private static void dropTable() throws SQLException {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(ctstable4), stmt);
TestUtils.dropTableIfExists(timestampTable1, stmt);
TestUtils.dropTableIfExists(timestampTable2, stmt);
TestUtils.dropTableIfExists(caseSensitiveTable, stmt);
}
}

Expand Down
Loading