Search in sources :

Example 41 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class AlterTableTest method checkWarning.

private void checkWarning(Statement st, String expectedWarning) throws Exception {
    SQLWarning sqlWarn = (st == null) ? getConnection().getWarnings() : st.getWarnings();
    assertNotNull("Expected warning but found none", sqlWarn);
    assertSQLState(expectedWarning, sqlWarn);
}
Also used : SQLWarning(java.sql.SQLWarning)

Example 42 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class RolesConferredPrivilegesTest method testConstraintInvalidation.

/**
 * @see #testViewInvalidation
 */
public void testConstraintInvalidation() throws SQLException {
    Connection dboConn = getConnection();
    Statement s = dboConn.createStatement();
    Connection c = openUserConnection("DonaldDuck");
    Statement cStmt = c.createStatement();
    SQLWarning w;
    /*
        * 3-dimensional search space:
        *
        * Which role we grant the role to (direct to a role or to a role it
        * inherits)
        *    X
        * Whether the role is granted directly to the session user or to PUBLIC.
        *    X
        * Whether we grant the entire underlying table or just the column
        * needed.
        */
    String[] grantToThisRole = new String[] { "a2", "h" };
    String[] roleGrantees = new String[] { "DonaldDuck", "public" };
    String[][] tabAndColReferencesPerms = new String[][] { { g_r }, { g_r_c1, g_r_c2, g_r_c3 } };
    String createTableString = "create table t (i int not null, j int, k int)";
    String dropTableString = "drop table t";
    String addConstraintString = "alter table t add constraint fk " + "foreign key(i,j,k) references s1.t1";
    cStmt.executeUpdate(createTableString);
    for (int r = 0; r < grantToThisRole.length; r++) {
        for (int gNo = 0; gNo < roleGrantees.length; gNo++) {
            for (int i = 0; i < tabAndColReferencesPerms.length; i++) {
                /*
                     * Create a foreign key constraint on the basis of a
                     * references privilege via a role.
                     */
                s.executeUpdate("grant h to " + roleGrantees[gNo]);
                doGrantRevoke(GRANT, "test_dbo", tabAndColReferencesPerms[i], grantToThisRole[r]);
                setRole(c, "h");
                cStmt.executeUpdate(addConstraintString);
                assertFkConstraintExists(true, c, "t");
                /*
                     * Setting another role does not affect the constraint once
                     * defined.
                     */
                setRole(c, "none");
                assertFkConstraintExists(true, c, "t");
                // Remove privileges from role, and the constraint should be
                // gone.
                doGrantRevoke(REVOKE, "test_dbo", tabAndColReferencesPerms[i], grantToThisRole[r], new String[] { CONSTRAINTDROPPED, null, null });
                assertFkConstraintExists(false, c, "t");
                /*
                     * Revoking the role should also invalidate constraint
                     */
                doGrantRevoke(GRANT, "test_dbo", tabAndColReferencesPerms[i], grantToThisRole[r]);
                setRole(c, "h");
                cStmt.executeUpdate(addConstraintString);
                assertFkConstraintExists(true, c, "t");
                s.executeUpdate("revoke h from " + roleGrantees[gNo]);
                assertFkConstraintExists(false, c, "t");
                /*
                     * Check that user privilege and/or PUBLIC privilege is
                     * preferred over role privilege if available. This is not
                     * standard SQL, but useful behavior IMHO as long as Derby
                     * can't revalidate via another path (DERBY-1632) - lest a
                     * role revoke or drop causes an invalidation when user has
                     * discretionary privilege. Cf. also comment on priority of
                     * user vs public in DERBY-1611.
                     */
                String[] directGrantee = roleGrantees;
                for (int u = 0; u < directGrantee.length; u++) {
                    s.executeUpdate("grant h to " + roleGrantees[gNo]);
                    doGrantRevoke(GRANT, "test_dbo", tabAndColReferencesPerms[i], directGrantee[u]);
                    setRole(c, "h");
                    // Now we have references privilege two ways, via role
                    // and via user.
                    cStmt.executeUpdate(addConstraintString);
                    // Now revoke role priv and see that constraints is
                    // still unaffected.
                    s.executeUpdate("revoke h from " + roleGrantees[gNo]);
                    assertFkConstraintExists(true, c, "t");
                    // take away user privilege, too
                    doGrantRevoke(REVOKE, "test_dbo", tabAndColReferencesPerms[i], directGrantee[u], new String[] { CONSTRAINTDROPPED, null, null });
                    assertFkConstraintExists(false, c, "t");
                }
                // clean up
                doGrantRevoke(REVOKE, "test_dbo", tabAndColReferencesPerms[i], grantToThisRole[r]);
            }
        }
    }
    /*
         * Dropping a role should also invalidate a dependent constraint.
         *
         * (We do this test outside the loop above for simplicity of
         * reestablish role graph after the drop..)
         */
    doGrantRevoke(GRANT, "test_dbo", g_r, "h");
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    cStmt.executeUpdate(addConstraintString);
    assertFkConstraintExists(true, c, "t");
    s.executeUpdate("drop role h");
    w = s.getWarnings();
    assertSQLState(CONSTRAINTDROPPED, w);
    assertFkConstraintExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_s, "h");
    // re-establish role graph
    s.executeUpdate("create role h");
    s.executeUpdate("grant e to h");
    s.executeUpdate("grant f to h");
    /*
         * For FOREIGN KEY constraint, check that dependency on role and
         * subesquent invalidation happens for a mix of column privileges
         * granted to user, public and role (due to tricky logic in this
         * implementation,
         * cf. DDLConstantAction#storeConstraintDependenciesOnPrivileges
         */
    // {role, role}
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    doGrantRevoke(GRANT, "test_dbo", new String[] { g_r_c1, g_r_c2, g_r_c3 }, "h");
    cStmt.executeUpdate("alter table t add constraint fk foreign key(i,j,k) " + "references s1.t1");
    s.executeUpdate("revoke h from DonaldDuck");
    w = s.getWarnings();
    assertSQLState(CONSTRAINTDROPPED, w);
    assertFkConstraintExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", new String[] { g_r_c1, g_r_c2, g_r_c3 }, "h");
    // {public, role}
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    doGrantRevoke(GRANT, "test_dbo", g_r_c1, "public");
    doGrantRevoke(GRANT, "test_dbo", g_r_c2, "h");
    doGrantRevoke(GRANT, "test_dbo", g_r_c3, "h");
    cStmt.executeUpdate("alter table t add constraint fk " + "foreign key(i,j,k) references s1.t1");
    s.executeUpdate("revoke h from DonaldDuck");
    w = s.getWarnings();
    assertSQLState(CONSTRAINTDROPPED, w);
    assertFkConstraintExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c1, "public");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c2, "h");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c3, "h");
    // {user, role}
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    doGrantRevoke(GRANT, "test_dbo", g_r_c1, "DonaldDuck");
    doGrantRevoke(GRANT, "test_dbo", g_r_c2, "h");
    doGrantRevoke(GRANT, "test_dbo", g_r_c3, "h");
    cStmt.executeUpdate("alter table t add constraint fk " + "foreign key(i,j,k) references s1.t1");
    s.executeUpdate("revoke h from DonaldDuck");
    w = s.getWarnings();
    assertSQLState(CONSTRAINTDROPPED, w);
    assertFkConstraintExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c1, "DonaldDuck");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c2, "h");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c3, "h");
    // {user, public, role}
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    doGrantRevoke(GRANT, "test_dbo", g_r_c1, "DonaldDuck");
    doGrantRevoke(GRANT, "test_dbo", g_r_c2, "public");
    doGrantRevoke(GRANT, "test_dbo", g_r_c3, "h");
    cStmt.executeUpdate("alter table t add constraint fk " + "foreign key(i,j,k) references s1.t1");
    s.executeUpdate("revoke h from DonaldDuck");
    w = s.getWarnings();
    assertSQLState(CONSTRAINTDROPPED, w);
    assertFkConstraintExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c1, "DonaldDuck");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c2, "public");
    doGrantRevoke(REVOKE, "test_dbo", g_r_c3, "h");
    // Try the same as above but with EXECUTE privilege instead of
    // REFERENCES for a CHECK constraint
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    doGrantRevoke(GRANT, "test_dbo", g_e, "h");
    cStmt.executeUpdate("alter table t add constraint ch " + "check(i < s1.f1())");
    assertCheckConstraintExists(true, c, "t");
    s.executeUpdate("revoke h from DonaldDuck");
    w = s.getWarnings();
    assertSQLState(CONSTRAINTDROPPED, w);
    assertCheckConstraintExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
    // Try the same as above but with two EXECUTE privileges
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    doGrantRevoke(GRANT, "test_dbo", g_e, "h");
    doGrantRevoke(GRANT, "test_dbo", g_e_f2, "DonaldDuck");
    cStmt.executeUpdate("alter table t add constraint ch " + "check(i < (s1.f1() + s1.f2()))");
    assertCheckConstraintExists(true, c, "t");
    s.executeUpdate("revoke h from DonaldDuck");
    w = s.getWarnings();
    assertSQLState(CONSTRAINTDROPPED, w);
    assertCheckConstraintExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
    doGrantRevoke(REVOKE, "test_dbo", g_e_f2, "DonaldDuck");
    // Try the same as above but with multiple CHECK constraints to verify
    // that only those affected by a revoke are impacted.
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    doGrantRevoke(GRANT, "test_dbo", g_e, "h");
    doGrantRevoke(GRANT, "test_dbo", g_e_f2, "DonaldDuck");
    cStmt.executeUpdate("create table tmp(i int constraint ct1 check(i < s1.f1())," + "                 j int constraint ct2 check(j < s1.f2()))");
    s.executeUpdate("revoke h from DonaldDuck");
    // This should only impact ct1
    try {
        cStmt.executeUpdate("insert into tmp values (6, -1)");
    } catch (SQLException e) {
        fail("expected success", e);
    }
    try {
        cStmt.executeUpdate("insert into tmp values (6, 6)");
        fail("ct2 should remain");
    } catch (SQLException e) {
        assertSQLState(CHECKCONSTRAINTVIOLATED, e);
    }
    cStmt.executeUpdate("alter table tmp drop constraint ct2");
    doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
    doGrantRevoke(REVOKE, "test_dbo", g_e_f2, "DonaldDuck");
    cStmt.executeUpdate("drop table tmp");
    cStmt.executeUpdate(dropTableString);
    cStmt.close();
    c.close();
    s.close();
    dboConn.close();
}
Also used : SQLWarning(java.sql.SQLWarning) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 43 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class RolesConferredPrivilegesTest method testTriggerInvalidation.

/**
 * @see #testViewInvalidation
 */
public void testTriggerInvalidation() throws SQLException {
    Connection dboConn = getConnection();
    Statement s = dboConn.createStatement();
    Connection c = openUserConnection("DonaldDuck");
    Statement cStmt = c.createStatement();
    SQLWarning w;
    /*
        * 2-dimensional search space:
        *
        * Which role we grant the role to (direct to a role or to a role it
        * inherits)
        *    X
        * Whether the role is granted directly to the session user or to PUBLIC.
        */
    String[] grantToThisRole = new String[] { "a2", "h" };
    String[] roleGrantees = new String[] { "DonaldDuck", "public" };
    String createTriggerString = "create trigger t after insert on s1.t1 values 1";
    for (int r = 0; r < grantToThisRole.length; r++) {
        for (int gNo = 0; gNo < roleGrantees.length; gNo++) {
            /*
                 * Create a trigger on the basis of a trigger privilege via a
                 * role.
                 */
            s.executeUpdate("grant h to " + roleGrantees[gNo]);
            doGrantRevoke(GRANT, "test_dbo", g_t, grantToThisRole[r]);
            setRole(c, "h");
            cStmt.executeUpdate(createTriggerString);
            assertTriggerExists(true, c, "t");
            cStmt.executeUpdate(createTriggerString);
            /*
                 * Setting another role does not affect the trigger once
                 * defined.
                 */
            setRole(c, "none");
            assertTriggerExists(true, c, "t");
            setRole(c, "h");
            cStmt.executeUpdate(createTriggerString);
            // Remove privileges from role, and the trigger should be
            // gone.
            doGrantRevoke(REVOKE, "test_dbo", g_t, grantToThisRole[r], TRIGGERDROPPED);
            assertTriggerExists(false, c, "t");
            /*
                 * Revoking the role should also invalidate trigger
                 */
            doGrantRevoke(GRANT, "test_dbo", g_t, grantToThisRole[r]);
            setRole(c, "h");
            cStmt.executeUpdate(createTriggerString);
            assertTriggerExists(true, c, "t");
            cStmt.executeUpdate(createTriggerString);
            s.executeUpdate("revoke h from " + roleGrantees[gNo]);
            w = s.getWarnings();
            assertSQLState(TRIGGERDROPPED, w);
            assertTriggerExists(false, c, "t");
            /*
                 * Check that user privilege and/or PUBLIC privilege is
                 * preferred over role privilege if available. This is not
                 * standard SQL, but useful behavior IMHO as long as Derby
                 * can't revalidate via another path (DERBY-1632) - lest a
                 * role revoke or drop causes an invalidation when user has
                 * discretionary privilege. Cf. also comment on priority of
                 * user vs public in DERBY-1611.
                 */
            String[] directGrantee = roleGrantees;
            for (int u = 0; u < directGrantee.length; u++) {
                s.executeUpdate("grant h to " + roleGrantees[gNo]);
                doGrantRevoke(GRANT, "test_dbo", g_t, directGrantee[u]);
                setRole(c, "h");
                // Now we have trigger privilege two ways,a via role and
                // via user.
                cStmt.executeUpdate(createTriggerString);
                // Now revoke role priv and see that trigger is still
                // unaffected.
                s.executeUpdate("revoke h from " + roleGrantees[gNo]);
                assertTriggerExists(true, c, "t");
                cStmt.executeUpdate(createTriggerString);
                // take away user privilege, too
                doGrantRevoke(REVOKE, "test_dbo", g_t, directGrantee[u], TRIGGERDROPPED);
                assertTriggerExists(false, c, "t");
            }
            // clean up
            doGrantRevoke(REVOKE, "test_dbo", g_t, grantToThisRole[r]);
        }
    }
    /*
         * Dropping a role should also invalidate a dependent trigger.
         *
         * (We do this test outside the loop above for simplicity of
         * reestablish role graph after the drop..)
         */
    doGrantRevoke(GRANT, "test_dbo", g_t, "h");
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    cStmt.executeUpdate(createTriggerString);
    assertTriggerExists(true, c, "t");
    cStmt.executeUpdate(createTriggerString);
    s.executeUpdate("drop role h");
    w = s.getWarnings();
    assertSQLState(TRIGGERDROPPED, w);
    assertTriggerExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_t, "h");
    // re-establish role graph
    s.executeUpdate("create role h");
    s.executeUpdate("grant e to h");
    s.executeUpdate("grant f to h");
    /*
         * Dropping an EXECUTE privilege used in a trigger body will not drop
         * the trigger if the EXECUTE privilege is revoked from a user
         * directly, since this currently requires the RESTRICT
         * keyword. However, revoking a role does not carry the RESTRICT
         * keyword, so any execution privilege conferred through a role is
         * revoked, too, and any dependent object, for example a trigger in
         * example below, will be dropped.
         */
    doGrantRevoke(GRANT, "test_dbo", g_t, "h");
    doGrantRevoke(GRANT, "test_dbo", g_e, "h");
    s.executeUpdate("grant h to DonaldDuck");
    setRole(c, "h");
    cStmt.executeUpdate("create trigger t after insert on s1.t1 values s1.f1()");
    assertTriggerExists(true, c, "t");
    cStmt.executeUpdate("create trigger t after insert on s1.t1 values s1.f1()");
    s.executeUpdate("revoke h from DonaldDuck");
    w = s.getWarnings();
    assertSQLState(TRIGGERDROPPED, w);
    assertTriggerExists(false, c, "t");
    doGrantRevoke(REVOKE, "test_dbo", g_t, "h");
    doGrantRevoke(REVOKE, "test_dbo", g_e, "h");
    /*
         * Check that dependency on role and subsequent invalidation happens
         * for a mix of column SELECT privileges granted to user, public and
         * role (due to tricky logic in this implementation,
         * cf. DDLConstantAction#storeViewTriggerDependenciesOnPrivileges
         */
    // SELECT privileges to {public, role} x
    // TRIGGER privilege to {user, role}
    String[] triggerPrivGrantees = new String[] { "h", "DonaldDuck" };
    for (int i = 0; i < triggerPrivGrantees.length; i++) {
        s.executeUpdate("grant h to DonaldDuck");
        setRole(c, "h");
        doGrantRevoke(GRANT, "test_dbo", g_t, triggerPrivGrantees[i]);
        doGrantRevoke(GRANT, "test_dbo", g_s_c1, "public");
        doGrantRevoke(GRANT, "test_dbo", g_s_c2, "h");
        cStmt.executeUpdate("create trigger t after insert on s1.t1 " + "select c1,c2 from s1.t1");
        s.executeUpdate("revoke h from DonaldDuck");
        w = s.getWarnings();
        assertSQLState(TRIGGERDROPPED, w);
        assertTriggerExists(false, c, "t");
        doGrantRevoke(REVOKE, "test_dbo", g_t, triggerPrivGrantees[i]);
        doGrantRevoke(REVOKE, "test_dbo", g_s_c1, "public");
        doGrantRevoke(REVOKE, "test_dbo", g_s_c2, "h");
    }
    // TRIGGER privilege to {user, role}
    for (int i = 0; i < triggerPrivGrantees.length; i++) {
        s.executeUpdate("grant h to DonaldDuck");
        setRole(c, "h");
        doGrantRevoke(GRANT, "test_dbo", g_t, triggerPrivGrantees[i]);
        doGrantRevoke(GRANT, "test_dbo", g_s_c1, "DonaldDuck");
        doGrantRevoke(GRANT, "test_dbo", g_s_c2, "h");
        cStmt.executeUpdate("create trigger t after insert on s1.t1 " + "select c1,c2 from s1.t1");
        s.executeUpdate("revoke h from DonaldDuck");
        w = s.getWarnings();
        assertSQLState(TRIGGERDROPPED, w);
        assertTriggerExists(false, c, "t");
        doGrantRevoke(REVOKE, "test_dbo", g_t, triggerPrivGrantees[i]);
        doGrantRevoke(REVOKE, "test_dbo", g_s_c1, "DonaldDuck");
        doGrantRevoke(REVOKE, "test_dbo", g_s_c2, "h");
    }
    // TRIGGER privilege to {user, role}
    for (int i = 0; i < triggerPrivGrantees.length; i++) {
        s.executeUpdate("grant h to DonaldDuck");
        setRole(c, "h");
        doGrantRevoke(GRANT, "test_dbo", g_t, triggerPrivGrantees[i]);
        doGrantRevoke(GRANT, "test_dbo", g_s_c1, "DonaldDuck");
        doGrantRevoke(GRANT, "test_dbo", g_s_c2, "public");
        doGrantRevoke(GRANT, "test_dbo", g_s_c3, "h");
        cStmt.executeUpdate("create trigger t after insert on s1.t1 " + "select c1,c2,c3 from s1.t1");
        s.executeUpdate("revoke h from DonaldDuck");
        w = s.getWarnings();
        assertSQLState(TRIGGERDROPPED, w);
        assertTriggerExists(false, c, "t");
        doGrantRevoke(REVOKE, "test_dbo", g_t, triggerPrivGrantees[i]);
        doGrantRevoke(REVOKE, "test_dbo", g_s_c1, "DonaldDuck");
        doGrantRevoke(REVOKE, "test_dbo", g_s_c2, "public");
        doGrantRevoke(REVOKE, "test_dbo", g_s_c3, "h");
    }
    cStmt.close();
    c.close();
    s.close();
    dboConn.close();
}
Also used : SQLWarning(java.sql.SQLWarning) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 44 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class RolesTest method doStmtWithWarnings.

// Minion to analyze outcome. If state string is empty, we expect success
// for that combination of authentication level and user (dbo or not).
// State arrays: element 0: expected error, element 1: expected warning
private ResultSet doStmtWithWarnings(String stmt, String[] noAuthState, String[] authDboState, String[] authNotDboState, boolean query) {
    ResultSet result = null;
    try {
        if (query) {
            result = _stm.executeQuery(stmt);
        } else {
            _stm.execute(stmt);
        }
        if (_authLevel == NO_SQLAUTHORIZATION) {
            if (noAuthState[0] != null) {
                fail("exception " + noAuthState[0] + " expected: (" + stmt);
            }
            if (noAuthState[1] != null) {
                SQLWarning w = _stm.getWarnings();
                assertNotNull("Expected warning but found none", w);
                assertSQLState(noAuthState[1], w);
            }
        } else {
            // SQLAUTHORIZATION
            if (isDbo()) {
                if (authDboState[0] != null) {
                    fail("exception " + authDboState[0] + " expected: (" + stmt);
                }
                if (authDboState[1] != null) {
                    SQLWarning w = _stm.getWarnings();
                    assertNotNull("Expected warning but found none", w);
                    assertSQLState(authDboState[1], w);
                }
            } else {
                if (authNotDboState[0] != null) {
                    fail("exception " + authNotDboState[0] + " expected: (" + stmt);
                }
                if (authNotDboState[1] != null) {
                    SQLWarning w = _stm.getWarnings();
                    assertNotNull("Expected warning but found none", w);
                    assertSQLState(authNotDboState[1], w);
                }
            }
        }
    } catch (SQLException e) {
        if (_authLevel == NO_SQLAUTHORIZATION) {
            if (noAuthState[0] == null) {
                fail("stmt " + stmt + " failed with exception " + e.getSQLState(), e);
            } else {
                assertSQLState("Stmt " + stmt, noAuthState[0], e);
            }
        } else {
            // SQLAUTHORIZATION
            if (isDbo()) {
                if (authDboState[0] == null) {
                    fail("stmt " + stmt + " failed with exception " + e.getSQLState(), e);
                } else {
                    assertSQLState("Stmt " + stmt, authDboState[0], e);
                }
            } else {
                if (authNotDboState[0] == null) {
                    fail("stmt " + stmt + " failed with exception " + e.getSQLState(), e);
                } else {
                    assertSQLState("Stmt " + stmt, authNotDboState[0], e);
                }
            }
        }
    }
    return result;
}
Also used : SQLWarning(java.sql.SQLWarning) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet)

Example 45 with SQLWarning

use of java.sql.SQLWarning in project derby by apache.

the class ScrollCursors2Test method testCallableStatements.

/**
 * CallableStatement tests.
 *
 * @exception SQLException
 *                Thrown if some unexpected error happens
 */
public void testCallableStatements() throws SQLException {
    Connection conn = getConnection();
    SQLWarning warning;
    // sensitive, read only
    CallableStatement cs_s_r = null;
    // sensitive, updatable
    CallableStatement cs_s_u = null;
    // insensitive, read only
    CallableStatement cs_i_r = null;
    // forward only, read only
    CallableStatement cs_f_r = null;
    cs_s_r = prepareCall("values cast (? as Integer)", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
    // We should have gotten 1 warnings
    warning = conn.getWarnings();
    assertNotNull(warning);
    if (!isDerbyNetClient)
        assertEquals("01J02", warning.getSQLState());
    else
        assertEquals("01J10", warning.getSQLState());
    JDBC.assertNoWarnings(warning.getNextWarning());
    conn.clearWarnings();
    cs_s_r.close();
    cs_s_u = prepareCall("values cast (? as Integer)", ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    // We should have gotten 1 warning
    warning = conn.getWarnings();
    assertNotNull(warning);
    if (!isDerbyNetClient)
        assertEquals("01J02", warning.getSQLState());
    else
        assertEquals("01J10", warning.getSQLState());
    JDBC.assertNoWarnings(warning.getNextWarning());
    conn.clearWarnings();
    cs_s_u.close();
    cs_i_r = prepareCall("values cast (? as Integer)", ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    // We should have gotten 0 warnings
    JDBC.assertNoWarnings(conn.getWarnings());
    conn.clearWarnings();
    cs_i_r.close();
    cs_f_r = prepareCall("values cast (? as Integer)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    // We should have gotten 0 warnings
    JDBC.assertNoWarnings(conn.getWarnings());
    conn.clearWarnings();
    cs_f_r.close();
}
Also used : SQLWarning(java.sql.SQLWarning) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection)

Aggregations

SQLWarning (java.sql.SQLWarning)147 SQLException (java.sql.SQLException)47 Statement (java.sql.Statement)35 PreparedStatement (java.sql.PreparedStatement)30 Connection (java.sql.Connection)24 ResultSet (java.sql.ResultSet)23 Test (org.testng.annotations.Test)19 Test (org.junit.Test)17 BaseTest (util.BaseTest)15 CallableStatement (java.sql.CallableStatement)13 ArrayList (java.util.ArrayList)9 JDBCStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement)8 ResultSetMetaData (java.sql.ResultSetMetaData)6 Expectations (org.jmock.Expectations)6 IOException (java.io.IOException)4 FilterChainImpl (com.alibaba.druid.filter.FilterChainImpl)3 PrestoWarning (com.facebook.presto.spi.PrestoWarning)3 HashSet (java.util.HashSet)3 Properties (java.util.Properties)3 LanguageConnectionContext (org.apache.derby.iapi.sql.conn.LanguageConnectionContext)3