Search in sources :

Example 6 with SQLServerDatabaseMetaData

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

the class DatabaseMetaDataForeignKeyTest method testGetExportedKeys.

/**
 * test getExportedKeys() methods
 *
 * @throws SQLException
 * @throws SQLTimeoutException
 */
@Test
public void testGetExportedKeys() throws SQLException {
    String[] tableNames = { table2, table3, table4, table5 };
    int[][] values = { // expected UPDATE_RULE, expected DELETE_RULE
    { 4, 3 }, { 2, 0 }, { 0, 2 }, { 3, 4 } };
    SQLServerDatabaseMetaData dmd = (SQLServerDatabaseMetaData) connection.getMetaData();
    for (int i = 0; i < tableNames.length; i++) {
        String pkTable = tableNames[i];
        SQLServerResultSet rs1 = (SQLServerResultSet) dmd.getExportedKeys(null, null, pkTable);
        rs1.next();
        assertEquals(values[i][0], rs1.getInt("UPDATE_RULE"));
        assertEquals(values[i][1], rs1.getInt("DELETE_RULE"));
        SQLServerResultSet rs2 = (SQLServerResultSet) dmd.getExportedKeys(catalog, schema, pkTable);
        rs2.next();
        assertEquals(values[i][0], rs2.getInt("UPDATE_RULE"));
        assertEquals(values[i][1], rs2.getInt("DELETE_RULE"));
        SQLServerResultSet rs3 = (SQLServerResultSet) dmd.getExportedKeys(catalog, "", pkTable);
        rs3.next();
        assertEquals(values[i][0], rs3.getInt("UPDATE_RULE"));
        assertEquals(values[i][1], rs3.getInt("DELETE_RULE"));
        try {
            dmd.getExportedKeys("", schema, pkTable);
            fail("Exception is not thrown.");
        } catch (SQLException e) {
            assertTrue(e.getMessage().startsWith(EXPECTED_ERROR_MESSAGE));
        }
    }
}
Also used : SQLServerResultSet(com.microsoft.sqlserver.jdbc.SQLServerResultSet) SQLServerDatabaseMetaData(com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

SQLServerDatabaseMetaData (com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData)6 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)6 Test (org.junit.jupiter.api.Test)6 SQLException (java.sql.SQLException)4 SQLServerResultSet (com.microsoft.sqlserver.jdbc.SQLServerResultSet)3 DatabaseMetaData (java.sql.DatabaseMetaData)3 ResultSet (java.sql.ResultSet)2 IOException (java.io.IOException)1