Search in sources :

Example 1 with SqlType

use of com.microsoft.sqlserver.testframework.sqlType.SqlType 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)

Example 2 with SqlType

use of com.microsoft.sqlserver.testframework.sqlType.SqlType in project mssql-jdbc by Microsoft.

the class lobsTest method createLob.

private Object createLob(Class lobClass) {
    // Randomly indicate negative length
    streamLength = ThreadLocalRandom.current().nextInt(3) < 2 ? datasize : -1 - ThreadLocalRandom.current().nextInt(datasize);
    // For streams -1 means any length, avoid to ensure that an exception is always thrown
    if (streamLength == -1 && (lobClass == DBCharacterStream.class || lobClass == DBBinaryStream.class))
        streamLength = datasize;
    log.fine("Length passed into update : " + streamLength);
    byte[] data = new byte[datasize];
    ThreadLocalRandom.current().nextBytes(data);
    if (lobClass == DBCharacterStream.class)
        return new DBInvalidUtil().new InvalidCharacterStream(new String(data), streamLength < -1);
    else if (lobClass == DBBinaryStream.class)
        return new DBInvalidUtil().new InvalidBinaryStream(data, streamLength < -1);
    if (lobClass == Clob.class) {
        ArrayList<SqlType> types = Utils.types();
        SqlType type = Utils.find(String.class);
        Object expected = type.createdata(String.class, data);
        return new DBInvalidUtil().new InvalidClob(expected, false);
    } else {
        ArrayList<SqlType> types = Utils.types();
        SqlType type = Utils.find(byte[].class);
        Object expected = type.createdata(type.getClass(), data);
        return new DBInvalidUtil().new InvalidBlob(expected, false);
    }
}
Also used : DBCharacterStream(com.microsoft.sqlserver.testframework.Utils.DBCharacterStream) DBBinaryStream(com.microsoft.sqlserver.testframework.Utils.DBBinaryStream) SqlType(com.microsoft.sqlserver.testframework.sqlType.SqlType) DBInvalidUtil(com.microsoft.sqlserver.testframework.DBInvalidUtil)

Example 3 with SqlType

use of com.microsoft.sqlserver.testframework.sqlType.SqlType in project mssql-jdbc by Microsoft.

the class DBTable method addColumns.

/**
 * adds a columns for each SQL type in DBSchema
 */
private void addColumns() {
    totalColumns = schema.getNumberOfSqlTypes();
    columns = new ArrayList<>(totalColumns);
    for (int i = 0; i < totalColumns; i++) {
        SqlType sqlType = schema.getSqlType(i);
        DBColumn column = new DBColumn(RandomUtil.getIdentifier(sqlType.getName()), sqlType);
        columns.add(column);
    }
}
Also used : SqlType(com.microsoft.sqlserver.testframework.sqlType.SqlType)

Example 4 with SqlType

use of com.microsoft.sqlserver.testframework.sqlType.SqlType in project mssql-jdbc by Microsoft.

the class DBTable method addColumns.

/**
 * adds a columns for each SQL type in DBSchema
 */
private void addColumns(boolean unicode) {
    totalColumns = schema.getNumberOfSqlTypes();
    columns = new ArrayList<>(totalColumns);
    for (int i = 0; i < totalColumns; i++) {
        SqlType sqlType = schema.getSqlType(i);
        DBColumn column;
        if (unicode)
            column = new DBColumn(RandomUtil.getIdentifier(sqlType.getName()) + "ĀĂŎՖએДЕЖЗИЙਟਖਞ", sqlType);
        else
            column = new DBColumn(RandomUtil.getIdentifier(sqlType.getName()), sqlType);
        columns.add(column);
    }
}
Also used : SqlType(com.microsoft.sqlserver.testframework.sqlType.SqlType)

Example 5 with SqlType

use of com.microsoft.sqlserver.testframework.sqlType.SqlType in project mssql-jdbc by Microsoft.

the class BulkCopyColumnMappingTest method testRepetativeCM.

@Test
@DisplayName("BulkCopy:test repetative column mapping")
void testRepetativeCM() {
    // create source table
    DBTable sourceTable1 = new DBTable(true);
    stmt.createTable(sourceTable1);
    stmt.populateTable(sourceTable1);
    // create destication table with same shcema as source
    DBTable destTable = sourceTable1.cloneSchema();
    // add 1 column to destination which will be duplicate of first source column
    SqlType sqlType = sourceTable1.getSqlType(0);
    destTable.addColumn(sqlType);
    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 <= sourceTable1.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(sourceTable1.getColumnName(i - 1), destTable.getColumnName(i - 1));
                break;
            case 3:
                bulkWrapper.setColumnMapping(sourceTable1.getColumnName(i - 1), i);
                break;
        }
    }
    // add column mapping for duplicate column in destination
    bulkWrapper.setColumnMapping(1, 24);
    // perform bulkCopy without validating results or dropping destination table
    BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable1, destTable, false, false, false);
    try {
        validateValuesRepetativeCM(con, sourceTable1, destTable);
    } catch (SQLException e) {
        fail("failed to validate values in " + sourceTable1.getTableName() + " and " + destTable.getTableName() + "\n" + e.getMessage());
    }
    dropTable(sourceTable1.getEscapedTableName());
    dropTable(destTable.getEscapedTableName());
}
Also used : DBTable(com.microsoft.sqlserver.testframework.DBTable) SQLException(java.sql.SQLException) SqlType(com.microsoft.sqlserver.testframework.sqlType.SqlType) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)7 DBTable (com.microsoft.sqlserver.testframework.DBTable)3 SQLException (java.sql.SQLException)2 Test (org.junit.jupiter.api.Test)2 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)1 SQLServerDataTable (com.microsoft.sqlserver.jdbc.SQLServerDataTable)1 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)1 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)1 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)1 DBInvalidUtil (com.microsoft.sqlserver.testframework.DBInvalidUtil)1 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)1 DBBinaryStream (com.microsoft.sqlserver.testframework.Utils.DBBinaryStream)1 DBCharacterStream (com.microsoft.sqlserver.testframework.Utils.DBCharacterStream)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 DisplayName (org.junit.jupiter.api.DisplayName)1