Search in sources :

Example 1 with DBTable

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);
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBTable(com.microsoft.sqlserver.testframework.DBTable) DBStatement(com.microsoft.sqlserver.testframework.DBStatement)

Example 2 with DBTable

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);
}
Also used : DBTable(com.microsoft.sqlserver.testframework.DBTable) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with DBTable

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);
}
Also used : DBTable(com.microsoft.sqlserver.testframework.DBTable) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with DBTable

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);
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBTable(com.microsoft.sqlserver.testframework.DBTable) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 5 with DBTable

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);
        }
    }
}
Also used : DBTable(com.microsoft.sqlserver.testframework.DBTable) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) SqlType(com.microsoft.sqlserver.testframework.sqlType.SqlType) FileInputStream(java.io.FileInputStream) SQLServerBulkCopy(com.microsoft.sqlserver.jdbc.SQLServerBulkCopy) SQLException(java.sql.SQLException)

Aggregations

DBTable (com.microsoft.sqlserver.testframework.DBTable)13 Test (org.junit.jupiter.api.Test)7 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)6 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)6 DisplayName (org.junit.jupiter.api.DisplayName)6 SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)3 SQLException (java.sql.SQLException)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)1 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)1 DBPreparedStatement (com.microsoft.sqlserver.testframework.DBPreparedStatement)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1