Search in sources :

Example 1 with SQLServerResultSetMetaData

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

the class ComparisonUtil method compareSrcTableAndDestTableIgnoreRowOrder.

/**
 * test if source table and destination table are the same
 *
 * @param con
 * @param srcTable
 * @param destTable
 * @throws SQLException
 */
public static void compareSrcTableAndDestTableIgnoreRowOrder(DBConnection con, DBTable srcTable, DBTable destTable) throws SQLException {
    DBResultSet srcResultSetCount = con.createStatement().executeQuery("SELECT COUNT(*) FROM " + srcTable.getEscapedTableName() + ";");
    DBResultSet dstResultSetCount = con.createStatement().executeQuery("SELECT COUNT(*) FROM " + destTable.getEscapedTableName() + ";");
    srcResultSetCount.next();
    dstResultSetCount.next();
    int srcRows = srcResultSetCount.getInt(1);
    int destRows = dstResultSetCount.getInt(1);
    if (srcRows != destRows) {
        fail("Souce table and Destination table have different number of rows.");
    }
    if (srcTable.getColumns().size() != destTable.getColumns().size()) {
        fail("Souce table and Destination table have different number of columns.");
    }
    DBResultSet srcResultSet = con.createStatement().executeQuery("SELECT * FROM " + srcTable.getEscapedTableName() + " ORDER BY [" + srcTable.getColumnName(1) + "], [" + srcTable.getColumnName(2) + "],[" + srcTable.getColumnName(3) + "];");
    DBResultSet dstResultSet = con.createStatement().executeQuery("SELECT * FROM " + destTable.getEscapedTableName() + " ORDER BY [" + destTable.getColumnName(1) + "], [" + destTable.getColumnName(2) + "],[" + destTable.getColumnName(3) + "];");
    while (srcResultSet.next() && dstResultSet.next()) {
        for (int i = 0; i < destTable.getColumns().size(); i++) {
            SQLServerResultSetMetaData srcMeta = (SQLServerResultSetMetaData) ((ResultSet) srcResultSet.product()).getMetaData();
            SQLServerResultSetMetaData destMeta = (SQLServerResultSetMetaData) ((ResultSet) dstResultSet.product()).getMetaData();
            int srcJDBCTypeInt = srcMeta.getColumnType(i + 1);
            int destJDBCTypeInt = destMeta.getColumnType(i + 1);
            // verify column types
            if (srcJDBCTypeInt != destJDBCTypeInt) {
                fail("Souce table and Destination table have different number of columns.");
            }
            Object expectedValue = srcResultSet.getObject(i + 1);
            Object actualValue = dstResultSet.getObject(i + 1);
            compareExpectedAndActual(destJDBCTypeInt, expectedValue, actualValue);
        }
    }
}
Also used : SQLServerResultSetMetaData(com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet)

Aggregations

SQLServerResultSetMetaData (com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData)1 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)1