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());
}
}
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);
}
}
}
}
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());
}
}
}
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));
}
}
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);
}
}
Aggregations