Search in sources :

Example 1 with ISQLServerPreparedStatement

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

the class WrapperTest method wrapTest.

/**
 * Wrapper tests
 * @throws Exception
 */
@Test
public void wrapTest() throws Exception {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection con = DriverManager.getConnection(connectionString);
    Statement stmt = con.createStatement();
    try {
        // First make sure that a statement can be unwrapped
        boolean isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
        assertEquals(isWrapper, true, "SQLServerStatement should be a wrapper for self");
        isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerStatement"));
        assertEquals(isWrapper, true, "SQLServerStatement should be a wrapper for ISQLServerStatement");
        isWrapper = ((SQLServerStatement) stmt).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerConnection"));
        assertEquals(isWrapper, false, "SQLServerStatement should not be a wrapper for SQLServerConnection");
        // Now make sure that we can unwrap a SQLServerCallableStatement to a SQLServerStatement
        CallableStatement cs = con.prepareCall("{  ? = CALL " + "ProcName" + " (?, ?, ?, ?) }");
        // Test the class first
        isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
        assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for SQLServerStatement");
        // Now unwrap the Callable to a statement and call a SQLServerStatement specific function and make sure it succeeds.
        SQLServerStatement stmt2 = (SQLServerStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerStatement"));
        stmt2.setResponseBuffering("adaptive");
        // now test the interface
        isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerCallableStatement"));
        assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for ISQLServerCallableStatement");
        // Now unwrap the Callable to a statement and call a SQLServerStatement specific function and make sure it succeeds.
        ISQLServerPreparedStatement stmt4 = (ISQLServerPreparedStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement"));
        stmt4.setResponseBuffering("adaptive");
        if (isKatmaiServer())
            stmt4.setDateTimeOffset(1, null);
        // Try Unwrapping CallableStatement to a callableStatement
        isWrapper = ((SQLServerCallableStatement) cs).isWrapperFor(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerCallableStatement"));
        assertEquals(isWrapper, true, "SQLServerCallableStatement should be a wrapper for SQLServerCallableStatement");
        // Now unwrap the Callable to a SQLServerCallableStatement and call a SQLServerStatement specific function and make sure it succeeds.
        SQLServerCallableStatement stmt3 = (SQLServerCallableStatement) ((SQLServerCallableStatement) cs).unwrap(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerCallableStatement"));
        stmt3.setResponseBuffering("adaptive");
        if (isKatmaiServer()) {
            stmt3.setDateTimeOffset(1, null);
        }
        if (null != stmt4) {
            stmt4.close();
        }
        if (null != cs) {
            cs.close();
        }
    } catch (UnsupportedOperationException e) {
        assertEquals(System.getProperty("java.specification.version"), "1.5", "isWrapperFor should be supported in anything other than 1.5");
        assertTrue(e.getMessage().equalsIgnoreCase("This operation is not supported."), "Wrong exception message");
    } finally {
        if (null != stmt) {
            stmt.close();
        }
        if (null != con) {
            con.close();
        }
    }
}
Also used : SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) ISQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) ISQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

ISQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.ISQLServerPreparedStatement)1 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)1 SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)1 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)1 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)1 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 Test (org.junit.jupiter.api.Test)1