Search in sources :

Example 1 with ColumnMap

use of com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap in project mssql-jdbc by Microsoft.

the class BulkCopyTestUtil method performBulkCopy.

/**
 * perform bulk copy using source and destination tables
 *
 * @param wrapper
 * @param sourceTable
 * @param destTable
 * @param validateResult
 */
static void performBulkCopy(BulkCopyTestWrapper wrapper, DBTable sourceTable, DBTable destinationTable, boolean validateResult) {
    try (DBConnection con = new DBConnection(wrapper.getConnectionString());
        DBStatement stmt = con.createStatement();
        DBResultSet srcResultSet = stmt.executeQuery("SELECT * FROM " + sourceTable.getEscapedTableName() + ";");
        SQLServerBulkCopy bulkCopy = wrapper.isUsingConnection() ? new SQLServerBulkCopy((Connection) con.product()) : new SQLServerBulkCopy(wrapper.getConnectionString())) {
        if (wrapper.isUsingBulkCopyOptions()) {
            bulkCopy.setBulkCopyOptions(wrapper.getBulkOptions());
        }
        bulkCopy.setDestinationTableName(destinationTable.getEscapedTableName());
        if (wrapper.isUsingColumnMapping()) {
            for (int i = 0; i < wrapper.cm.size(); i++) {
                ColumnMap currentMap = wrapper.cm.get(i);
                if (currentMap.sourceIsInt && currentMap.destIsInt) {
                    bulkCopy.addColumnMapping(currentMap.srcInt, currentMap.destInt);
                } else if (currentMap.sourceIsInt && (!currentMap.destIsInt)) {
                    bulkCopy.addColumnMapping(currentMap.srcInt, currentMap.destString);
                } else if ((!currentMap.sourceIsInt) && currentMap.destIsInt) {
                    bulkCopy.addColumnMapping(currentMap.srcString, currentMap.destInt);
                } else if ((!currentMap.sourceIsInt) && (!currentMap.destIsInt)) {
                    bulkCopy.addColumnMapping(currentMap.srcString, currentMap.destString);
                }
            }
        }
        bulkCopy.writeToServer((ResultSet) srcResultSet.product());
        if (validateResult) {
            validateValues(con, sourceTable, destinationTable);
        }
    } catch (SQLException ex) {
        fail(ex.getMessage());
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) ColumnMap(com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap) SQLException(java.sql.SQLException) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)

Aggregations

SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)1 ColumnMap (com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap)1 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)1 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)1 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1