Search in sources :

Example 21 with SQLServerBulkCopy

use of com.microsoft.sqlserver.jdbc.SQLServerBulkCopy 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)

Example 22 with SQLServerBulkCopy

use of com.microsoft.sqlserver.jdbc.SQLServerBulkCopy in project mssql-jdbc by Microsoft.

the class BulkData method testSmalldatetime.

/**
 * Testing that setting scale and precision 0 in column meta data for smalldatetime should work
 *
 * @throws Exception
 */
@Test
public void testSmalldatetime() throws Exception {
    variation = "testSmalldatetime";
    BulkData bData = new BulkData(variation);
    String value = ("1954-05-22 02:44:00.0").toString();
    query = "CREATE TABLE " + destTable + " (smallDATA smalldatetime)";
    stmt.executeUpdate(query);
    try (SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(connectionString)) {
        bcOperation.setDestinationTableName(destTable);
        bcOperation.writeToServer(bData);
        try (ResultSet rs = stmt.executeQuery("select * from " + destTable)) {
            while (rs.next()) {
                assertEquals(rs.getString(1), value);
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 23 with SQLServerBulkCopy

use of com.microsoft.sqlserver.jdbc.SQLServerBulkCopy in project mssql-jdbc by Microsoft.

the class BulkData method testVarchar.

/**
 * Testing that sending a bigger varchar(3) to varchar(2) is thowing the proper error message.
 *
 * @throws Exception
 */
@Test
public void testVarchar() throws Exception {
    variation = "testVarchar";
    BulkData bData = new BulkData(variation);
    query = "CREATE TABLE " + destTable + " (smallDATA varchar(2))";
    stmt.executeUpdate(query);
    try (SQLServerBulkCopy bcOperation = new SQLServerBulkCopy(connectionString)) {
        bcOperation.setDestinationTableName(destTable);
        bcOperation.writeToServer(bData);
        bcOperation.close();
        fail("BulkCopy executed for testVarchar when it it was expected to fail");
    } catch (Exception e) {
        if (e instanceof SQLException) {
            assertTrue(e.getMessage().contains("The given value of type"), "Invalid Error message: " + e.toString());
        } else {
            fail(e.getMessage());
        }
    }
}
Also used : SQLException(java.sql.SQLException) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) SQLException(java.sql.SQLException) IOException(java.io.IOException) SQLServerException(com.microsoft.sqlserver.jdbc.SQLServerException) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 24 with SQLServerBulkCopy

use of com.microsoft.sqlserver.jdbc.SQLServerBulkCopy in project mssql-jdbc by Microsoft.

the class BulkCopyWithSqlVariantTest method bulkCopyTestTwoCols.

/**
 * Test bulkcoping two column with sql_variant datatype
 *
 * @throws SQLException
 */
@Test
public void bulkCopyTestTwoCols() throws SQLException {
    String col1Value = "2015-05-05";
    String col2Value = "126.1230";
    String destTableName = "dest_sqlVariant";
    Utils.dropTableIfExists(tableName, stmt);
    Utils.dropTableIfExists(destTableName, stmt);
    stmt.executeUpdate("create table " + tableName + " (col1 sql_variant, col2 sql_variant)");
    stmt.executeUpdate("INSERT into " + tableName + "(col1, col2) values (CAST ('" + col1Value + "' AS " + "date" + ")" + ",CAST (" + col2Value + " AS " + "smallmoney" + ")   )");
    stmt.executeUpdate("create table " + destTableName + " (col1 sql_variant, col2 sql_variant)");
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + tableName);
    SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
    bulkCopy.setDestinationTableName(destTableName);
    bulkCopy.writeToServer(rs);
    bulkCopy.close();
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTableName);
    while (rs.next()) {
        assertEquals("" + rs.getDate(1), col1Value);
        assertEquals(rs.getSmallMoney(2), new BigDecimal(col2Value));
    }
}
Also used : SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 25 with SQLServerBulkCopy

use of com.microsoft.sqlserver.jdbc.SQLServerBulkCopy in project mssql-jdbc by Microsoft.

the class BulkCopyWithSqlVariantTest method bulkCopyTestChar.

/**
 * test char value
 *
 * @throws SQLException
 */
@Test
public void bulkCopyTestChar() throws SQLException {
    String col1Value = "'sample'";
    beforeEachSetup("char", col1Value);
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + tableName);
    SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(con);
    bulkCopy.setDestinationTableName(destTableName);
    bulkCopy.writeToServer(rs);
    bulkCopy.close();
    rs = (SQLServerResultSet) stmt.executeQuery("SELECT * FROM " + destTableName);
    while (rs.next()) {
        // adds space between
        assertEquals("'" + rs.getString(1).trim() + "'", col1Value);
    }
}
Also used : SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)43 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)34 Test (org.junit.jupiter.api.Test)34 SQLException (java.sql.SQLException)8 ResultSet (java.sql.ResultSet)6 SQLServerException (com.microsoft.sqlserver.jdbc.SQLServerException)5 IOException (java.io.IOException)5 Connection (java.sql.Connection)5 SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)4 Statement (java.sql.Statement)4 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)3 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)3 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)3 BigDecimal (java.math.BigDecimal)3 ColumnMap (com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap)1 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)1 DBTable (com.microsoft.sqlserver.testframework.DBTable)1 SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1