Search in sources :

Example 6 with DBResultSet

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

the class bvtTest method testTwoResultsetsSameStmt.

/**
 * Verify two concurrent resultsets from same connection, same statement
 *
 * @throws SQLException
 */
@Test
public void testTwoResultsetsSameStmt() throws SQLException {
    String query = "SELECT * FROM " + table1.getEscapedTableName();
    String query2 = "SELECT * FROM " + table2.getEscapedTableName();
    try (DBConnection conn = new DBConnection(connectionString);
        DBStatement stmt = conn.createStatement()) {
        DBResultSet rs1 = stmt.executeQuery(query);
        DBResultSet rs2 = stmt.executeQuery(query2);
        // Interleave resultset calls. rs is expected to be closed
        try {
            rs1.next();
        } catch (SQLException e) {
            assertEquals(e.toString(), "com.microsoft.sqlserver.jdbc.SQLServerException: The result set is closed.");
        }
        rs2.next();
        rs2.verifyCurrentRow(table2);
        try {
            rs1.next();
        } catch (SQLException e) {
            assertEquals(e.toString(), "com.microsoft.sqlserver.jdbc.SQLServerException: The result set is closed.");
        }
        rs1.close();
        rs2.next();
        rs2.verify(table2);
        rs2.close();
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLException(java.sql.SQLException) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) Test(org.junit.jupiter.api.Test)

Example 7 with DBResultSet

use of com.microsoft.sqlserver.testframework.DBResultSet 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 8 with DBResultSet

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

the class BulkCopyTestUtil method validateValues.

/**
 * @param con
 * @param srcData
 * @param destinationTable
 * @throws Exception
 */
static void validateValues(DBConnection con, ISQLServerBulkRecord srcData, DBTable destinationTable) throws Exception {
    try (DBStatement dstStmt = con.createStatement();
        DBResultSet dstResultSet = dstStmt.executeQuery("SELECT * FROM " + destinationTable.getEscapedTableName() + ";")) {
        ResultSetMetaData destMeta = ((ResultSet) dstResultSet.product()).getMetaData();
        int totalColumns = destMeta.getColumnCount();
        // reset the counter in ISQLServerBulkRecord, which was incremented during read by BulkCopy
        java.lang.reflect.Method method = srcData.getClass().getMethod("reset");
        method.invoke(srcData);
        // verify data from sourceType and resultSet
        while (srcData.next() && dstResultSet.next()) {
            Object[] srcValues = srcData.getRowData();
            for (int i = 1; i <= totalColumns; i++) {
                Object srcValue, dstValue;
                srcValue = srcValues[i - 1];
                if (srcValue.getClass().getName().equalsIgnoreCase("java.lang.Double")) {
                    // in case of SQL Server type Float (ie java type double), in float(n) if n is <=24 ie precsion is <=7 SQL Server type Real is returned(ie java type float)
                    if (destMeta.getPrecision(i) < 8)
                        srcValue = new Float(((Double) srcValue));
                }
                dstValue = dstResultSet.getObject(i);
                int dstType = destMeta.getColumnType(i);
                if (java.sql.Types.TIMESTAMP != dstType && java.sql.Types.TIME != dstType && microsoft.sql.Types.DATETIMEOFFSET != dstType) {
                    // skip validation for temporal types due to rounding eg 7986-10-21 09:51:15.114 is rounded as 7986-10-21 09:51:15.113 in server
                    ComparisonUtil.compareExpectedAndActual(dstType, srcValue, dstValue);
                }
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) ResultSet(java.sql.ResultSet) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet)

Example 9 with DBResultSet

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

the class BulkCopyTestUtil method validateValues.

/**
 * validate if same values are in both source and destination table
 *
 * @param con
 * @param sourceTable
 * @param destinationTable
 * @throws SQLException
 */
static void validateValues(DBConnection con, DBTable sourceTable, DBTable destinationTable) throws SQLException {
    try (DBStatement srcStmt = con.createStatement();
        DBStatement dstStmt = con.createStatement();
        DBResultSet srcResultSet = srcStmt.executeQuery("SELECT * FROM " + sourceTable.getEscapedTableName() + ";");
        DBResultSet dstResultSet = dstStmt.executeQuery("SELECT * FROM " + destinationTable.getEscapedTableName() + ";")) {
        ResultSetMetaData destMeta = ((ResultSet) dstResultSet.product()).getMetaData();
        int totalColumns = destMeta.getColumnCount();
        // verify data from sourceType and resultSet
        while (srcResultSet.next() && dstResultSet.next()) {
            for (int i = 1; i <= totalColumns; i++) {
                // TODO: check row and column count in both the tables
                Object srcValue, dstValue;
                srcValue = srcResultSet.getObject(i);
                dstValue = dstResultSet.getObject(i);
                ComparisonUtil.compareExpectedAndActual(destMeta.getColumnType(i), srcValue, dstValue);
            }
        }
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) ResultSet(java.sql.ResultSet) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet)

Example 10 with DBResultSet

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

the class lobsTest method testMultipleClose.

/**
 * Tests stream closures
 *
 * @param streamClass
 * @throws Exception
 */
private void testMultipleClose(Class streamClass) throws Exception {
    DBConnection conn = new DBConnection(connectionString);
    String[] types = { "varchar(max)", "nvarchar(max)", "varbinary(max)" };
    try {
        table = this.createTable(table, types, true);
        DBStatement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
        String query = "select * from " + table.getEscapedTableName();
        DBResultSet rs = stmt.executeQuery(query);
        while (rs.next()) {
            for (int i = 0; i < 3; i++) {
                DBColumn col = table.getColumns().get(i);
                if (!col.getSqlType().canConvert(streamClass, DBCoercion.GET, new DBConnection(connectionString)))
                    continue;
                Object stream = rs.getXXX(i + 1, streamClass);
                if (stream == null) {
                    assertEquals(stream, rs.getObject(i + 1), "Stream is null when data is not");
                } else {
                    // close the stream twice
                    if (streamClass == DBCharacterStream.class) {
                        ((Reader) stream).close();
                        ((Reader) stream).close();
                    } else {
                        ((InputStream) stream).close();
                        ((InputStream) stream).close();
                    }
                }
            }
        }
    } finally {
        if (null != table)
            this.dropTables(table);
        if (null != null)
            conn.close();
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Reader(java.io.Reader) DBColumn(com.microsoft.sqlserver.testframework.DBColumn) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet)

Aggregations

DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)18 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)15 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)13 Test (org.junit.jupiter.api.Test)11 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)4 ResultSetMetaData (java.sql.ResultSetMetaData)4 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)1 SQLServerResultSetMetaData (com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData)1 ColumnMap (com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap)1 DBColumn (com.microsoft.sqlserver.testframework.DBColumn)1 DBPreparedStatement (com.microsoft.sqlserver.testframework.DBPreparedStatement)1 DBResultSetTypes (com.microsoft.sqlserver.testframework.DBResultSetTypes)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 BigDecimal (java.math.BigDecimal)1