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

Bulk insert with random column list #1992

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -26,6 +26,7 @@
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;
import java.util.logging.Level;

Expand Down Expand Up @@ -2091,6 +2092,7 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
SQLServerResultSet rs = stmt
.executeQueryInternal("sp_executesql N'SET FMTONLY ON SELECT * FROM "
+ Util.escapeSingleQuotes(tableName) + " '")) {
Map<Integer, Integer> columnMappings = null;
if (null != columnList && columnList.size() > 0) {
if (columnList.size() != valueList.size()) {

Expand All @@ -2099,6 +2101,7 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
Object[] msgArgs = {columnList.size(), valueList.size()};
throw new IllegalArgumentException(form.format(msgArgs));
}
columnMappings = new HashMap<>(columnList.size());
} else {
if (rs.getColumnCount() != valueList.size()) {
MessageFormat form = new MessageFormat(
Expand All @@ -2122,15 +2125,29 @@ public int[] executeBatch() throws SQLServerException, BatchUpdateException, SQL
} else {
jdbctype = ti.getSSType().getJDBCType().getIntValue();
}
batchRecord.addColumnMetadata(i, c.getColumnName(), jdbctype, ti.getPrecision(),
ti.getScale());
if (null != columnList && columnList.size() > 0) {
int columnIndex = columnList.indexOf(c.getColumnName());
if (columnIndex > -1) {
columnMappings.put(columnIndex + 1, i);
batchRecord.addColumnMetadata(columnIndex + 1, c.getColumnName(), jdbctype,
ti.getPrecision(), ti.getScale());
}
} else {
batchRecord.addColumnMetadata(i, c.getColumnName(), jdbctype, ti.getPrecision(),
ti.getScale());
}
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved
}

SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(connection);
SQLServerBulkCopyOptions option = new SQLServerBulkCopyOptions();
option.setBulkCopyTimeout(queryTimeout);
bcOperation.setBulkCopyOptions(option);
bcOperation.setDestinationTableName(tableName);
if (columnMappings != null) {
for (Entry<Integer, Integer> pair : columnMappings.entrySet()) {
bcOperation.addColumnMapping(pair.getKey(), pair.getValue());
}
}
bcOperation.setStmtColumnEncriptionSetting(this.getStmtColumnEncriptionSetting());
bcOperation.setDestinationTableMetadata(rs);
bcOperation.writeToServer(batchRecord);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,45 @@ public void testNonSupportedColumns() throws Exception {
}
}

@Test
public void testReverseColumnOrder() throws Exception {
String valid = "insert into " + AbstractSQLGenerator.escapeIdentifier(tableName) + " (c2, c1) values "
Jeffery-Wasty marked this conversation as resolved.
Show resolved Hide resolved
+ "(" + "?, " + "? " + ")";

try (Connection connection = PrepUtil.getConnection(connectionString + ";useBulkCopyForBatchInsert=true;");
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement(valid);
Statement stmt = (SQLServerStatement) connection.createStatement();) {
tkyc marked this conversation as resolved.
Show resolved Hide resolved
Field f1 = SQLServerConnection.class.getDeclaredField("isAzureDW");
f1.setAccessible(true);
f1.set(connection, true);

TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
String createTable = "create table " + AbstractSQLGenerator.escapeIdentifier(tableName)
+ " (c1 varchar(1), c2 varchar(3))";
stmt.execute(createTable);

pstmt.setString(1, "One");
pstmt.setString(2, "1");
pstmt.addBatch();

pstmt.executeBatch();

try (ResultSet rs = stmt
.executeQuery("select c1, c2 from " + AbstractSQLGenerator.escapeIdentifier(tableName))) {

Object[] expected = new Object[2];

expected[0] = "1";
expected[1] = "One";
rs.next();

for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], rs.getObject(i + 1));
}
}
}
}

@BeforeAll
public static void setupTests() throws Exception {
setConnection();
Expand Down