Search in sources :

Example 6 with DBStatement

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

the class bvtTest method testStmtScrollSensitiveReadOnly.

/**
 * Create a statement ResultSet.SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY, executeQuery verify cursor by using next and absolute and verify
 * data
 *
 * @throws SQLException
 */
@Test
public void testStmtScrollSensitiveReadOnly() throws SQLException {
    String query = "SELECT * FROM " + table1.getEscapedTableName();
    try (DBConnection conn = new DBConnection(connectionString);
        DBStatement stmt = conn.createStatement(DBResultSetTypes.TYPE_SCROLL_SENSITIVE_CONCUR_READ_ONLY);
        DBResultSet rs = stmt.executeQuery(query)) {
        rs.next();
        rs.next();
        rs.verifyCurrentRow(table1);
        rs.absolute(3);
        rs.verifyCurrentRow(table1);
        rs.absolute(1);
        rs.verify(table1);
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) Test(org.junit.jupiter.api.Test)

Example 7 with DBStatement

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

the class bvtTest method testStmtSSScrollDynamicOptimisticCC.

/**
 * Create a statement TYPE_SS_SCROLL_DYNAMIC, CONCUR_SS_OPTIMISTIC_CC, executeQuery verify cursor by using next and previous and verify data
 *
 * @throws SQLException
 */
@Test
public void testStmtSSScrollDynamicOptimisticCC() throws SQLException {
    try (DBConnection conn = new DBConnection(connectionString);
        DBStatement stmt = conn.createStatement(DBResultSetTypes.TYPE_DYNAMIC_CONCUR_OPTIMISTIC);
        DBResultSet rs = stmt.selectAll(table1)) {
        // Verify resultset behavior
        rs.next();
        rs.afterLast();
        rs.previous();
        rs.verify(table1);
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) Test(org.junit.jupiter.api.Test)

Example 8 with DBStatement

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

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

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

Aggregations

DBStatement (com.microsoft.sqlserver.testframework.DBStatement)24 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)21 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)15 Test (org.junit.jupiter.api.Test)12 DBTable (com.microsoft.sqlserver.testframework.DBTable)6 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)3 ResultSetMetaData (java.sql.ResultSetMetaData)3 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)2 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 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 SqlType (com.microsoft.sqlserver.testframework.sqlType.SqlType)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 Connection (java.sql.Connection)1