use of com.microsoft.sqlserver.testframework.DBTable in project mssql-jdbc by Microsoft.
the class BulkCopyAllTypes method setupVariation.
private void setupVariation() throws SQLException {
try (DBConnection dbConnection = new DBConnection(connectionString);
DBStatement dbStmt = dbConnection.createStatement()) {
tableSrc = new DBTable(true);
tableDest = tableSrc.cloneSchema();
dbStmt.createTable(tableSrc);
dbStmt.createTable(tableDest);
dbStmt.populateTable(tableSrc);
}
}
use of com.microsoft.sqlserver.testframework.DBTable in project mssql-jdbc by Microsoft.
the class BulkCopyColumnMappingTest method testExplicitCM.
@Test
@DisplayName("BulkCopy:test explicit column mapping")
void testExplicitCM() {
// create dest table
DBTable destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
for (int i = 1; i <= destTable.totalColumns(); i++) {
int select = i % 4;
switch(select) {
case 0:
bulkWrapper.setColumnMapping(i, i);
break;
case 1:
bulkWrapper.setColumnMapping(i, destTable.getColumnName(i - 1));
break;
case 2:
bulkWrapper.setColumnMapping(sourceTable.getColumnName(i - 1), destTable.getColumnName(i - 1));
break;
case 3:
bulkWrapper.setColumnMapping(sourceTable.getColumnName(i - 1), i);
break;
}
}
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable);
}
use of com.microsoft.sqlserver.testframework.DBTable in project mssql-jdbc by Microsoft.
the class BulkCopyColumnMappingTest method testInvalidCM.
@Test
@DisplayName("BulkCopy:test invalid column mapping")
void testInvalidCM() {
// create dest table
DBTable destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with wrong column names
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping("wrongFirst", "wrongSecond");
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with invalid ordinal, column no 65 does not exist
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping(sourceTable.getColumnName(1), 65);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with invalid ordinal, column no 42 does not exist
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping(42, destTable.getColumnName(1));
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with invalid ordinal, column no 42 and 65 do not exist
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping(42, 65);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy while passing empty string as column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping(sourceTable.getColumnName(1), " ");
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with 0 ordinal column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping(0, 0);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with negative ordinal column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping(-3, -6);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with Integer.MIN_VALUE and Integer.MAX_VALUE column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
bulkWrapper.setColumnMapping(Integer.MIN_VALUE, Integer.MAX_VALUE);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
}
use of com.microsoft.sqlserver.testframework.DBTable in project mssql-jdbc by Microsoft.
the class BulkCopyISQLServerBulkRecordTest method testISQLServerBulkRecord.
@Test
void testISQLServerBulkRecord() throws SQLException {
try (DBConnection con = new DBConnection(connectionString);
DBStatement stmt = con.createStatement()) {
DBTable dstTable = new DBTable(true);
stmt.createTable(dstTable);
BulkData Bdata = new BulkData(dstTable);
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, Bdata, dstTable);
}
}
use of com.microsoft.sqlserver.testframework.DBTable in project mssql-jdbc by Microsoft.
the class BulkCopyCSVTest method testBulkCopyCSV.
private void testBulkCopyCSV(SQLServerBulkCSVFileRecord fileRecord, boolean firstLineIsColumnNames) {
DBTable destTable = null;
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath + inputFile), encoding))) {
// read the first line from csv and parse it to get datatypes to create destination column
String[] columnTypes = br.readLine().substring(1).split(delimiter, -1);
br.close();
int numberOfColumns = columnTypes.length;
destTable = new DBTable(false);
try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy((Connection) con.product())) {
bulkCopy.setDestinationTableName(destTable.getEscapedTableName());
// add a column in destTable for each datatype in csv
for (int i = 0; i < numberOfColumns; i++) {
SqlType sqlType = null;
int precision = -1;
int scale = -1;
String columnType = columnTypes[i].trim().toLowerCase();
int indexOpenParenthesis = columnType.lastIndexOf("(");
// skip the parenthesis in case of precision and scale type
if (-1 != indexOpenParenthesis) {
String precision_scale = columnType.substring(indexOpenParenthesis + 1, columnType.length() - 1);
columnType = columnType.substring(0, indexOpenParenthesis);
sqlType = SqlTypeMapping.valueOf(columnType.toUpperCase()).sqlType;
// add scale if exist
int indexPrecisionScaleSeparator = precision_scale.indexOf("-");
if (-1 != indexPrecisionScaleSeparator) {
scale = Integer.parseInt(precision_scale.substring(indexPrecisionScaleSeparator + 1));
sqlType.setScale(scale);
precision_scale = precision_scale.substring(0, indexPrecisionScaleSeparator);
}
// add precision
precision = Integer.parseInt(precision_scale);
sqlType.setPrecision(precision);
} else {
sqlType = SqlTypeMapping.valueOf(columnType.toUpperCase()).sqlType;
}
destTable.addColumn(sqlType);
fileRecord.addColumnMetadata(i + 1, "", sqlType.getJdbctype().getVendorTypeNumber(), (-1 == precision) ? 0 : precision, (-1 == scale) ? 0 : scale);
}
stmt.createTable(destTable);
bulkCopy.writeToServer((ISQLServerBulkRecord) fileRecord);
}
if (firstLineIsColumnNames)
validateValuesFromCSV(destTable, inputFile);
else
validateValuesFromCSV(destTable, inputFileNoColumnName);
} catch (Exception e) {
fail(e.getMessage());
} finally {
if (null != destTable) {
stmt.dropTable(destTable);
}
}
}
Aggregations