Search in sources :

Example 36 with SQLWarning

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

the class SURTest method testCursorOperationConflictWarning1.

/**
 * Test that you get cursor operation conflict warning if updating
 * a row which has been deleted from the table.
 */
public void testCursorOperationConflictWarning1() throws SQLException {
    Statement s = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    s.setCursorName(getNextCursorName());
    ResultSet rs = s.executeQuery("select * from t1");
    rs.next();
    createStatement().executeUpdate("delete from t1 where id=" + rs.getString("ID"));
    final int newValue = -3333;
    final int oldValue = rs.getInt(2);
    rs.updateInt(2, newValue);
    rs.updateRow();
    SQLWarning warn = rs.getWarnings();
    assertWarning(warn, CURSOR_OPERATION_CONFLICT);
    assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
    assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
    assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
    rs.clearWarnings();
    rs.deleteRow();
    warn = rs.getWarnings();
    assertWarning(warn, CURSOR_OPERATION_CONFLICT);
    rs.relative(0);
    assertTrue("Expected rs.rowUpdated() to be false", !rs.rowUpdated());
    assertTrue("Expected rs.rowDeleted() to be false", !rs.rowDeleted());
    assertEquals("Did not expect the resultset to be updated", oldValue, rs.getInt(2));
    rs.close();
    s.close();
}
Also used : SQLWarning(java.sql.SQLWarning) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet)

Example 37 with SQLWarning

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

the class SURTest method testConcurrencyModeWarning2.

/**
 * Test that you get a warning when specifying a query which is not
 * updatable and concurrency mode CONCUR_UPDATABLE.
 * In this case, the query contains a join.
 */
public void testConcurrencyModeWarning2() throws SQLException {
    Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
    s.setCursorName(getNextCursorName());
    ResultSet rs = s.executeQuery("select * from t1 as table1,t1 as table2 where " + "table1.a=table2.a");
    SQLWarning warn = rs.getWarnings();
    assertEquals("Expected resultset to be read only", ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
    assertWarning(warn, QUERY_NOT_QUALIFIED_FOR_UPDATABLE_RESULTSET);
    scrollForward(rs);
    rs.close();
    s.close();
}
Also used : SQLWarning(java.sql.SQLWarning) Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet)

Example 38 with SQLWarning

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

the class NativeAuthenticationServiceTest method passwordExpiring.

// connect but expect a warning that the password is about to expire
private Connection passwordExpiring(boolean expiring, String dbName, String user) throws Exception {
    Connection conn = null;
    reportConnectionAttempt(dbName, user, getPassword(user), true);
    String expectedSQLState = DBO.equals(user) ? DBO_PASSWORD_EXPIRING : PASSWORD_EXPIRING;
    conn = openConnection(dbName, user, true, null);
    SQLWarning warning = conn.getWarnings();
    if (expiring) {
        assertNotNull(tagError("Should have seen a warning"), warning);
        assertSQLState(expectedSQLState, warning);
    } else {
        assertNull(tagError("Should not have seen a warning"), warning);
    }
    return conn;
}
Also used : SQLWarning(java.sql.SQLWarning) Connection(java.sql.Connection)

Example 39 with SQLWarning

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

the class MemoryDbManager method createDatabase.

/**
 * Creates a new database and keeps track of it to delete it when the
 * clean up is invoked.
 * <p>
 * If the database already exists, a connection to the existing
 * database is returned.
 *
 * @param dbName the database name
 * @param dbAttributes database attributes (i.e. encryption)
 * @param user user name
 * @param password user password
 * @return A connection to the database.
 * @throws SQLException if creating or connecting to the database fails
 */
public Connection createDatabase(String dbName, String dbAttributes, String user, String password) throws SQLException {
    String userAttr = "";
    if (user != null) {
        userAttr = ";user=" + user;
    }
    if (password != null) {
        userAttr += ";password=" + password;
    }
    String url = dbName;
    if (dbAttributes != null) {
        url += ";" + dbAttributes;
    }
    if (!userAttr.equals("")) {
        url += userAttr;
    }
    if (url.indexOf(ATTR_CREATE) == -1) {
        url += ATTR_CREATE;
    }
    Connection con = getConnection(url);
    if (con.getWarnings() != null) {
        // See if there are more than one warning.
        SQLWarning w = con.getWarnings();
        String warnings = w.getMessage();
        while ((w = w.getNextWarning()) != null) {
            warnings += " || " + w.getMessage();
        }
        BaseJDBCTestCase.fail("Warning(s) when creating database: " + warnings);
    }
    // Keep track of the database we just created, so that we can
    // delete it.
    DATABASES.add(dbName + userAttr);
    return con;
}
Also used : SQLWarning(java.sql.SQLWarning) Connection(java.sql.Connection)

Example 40 with SQLWarning

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

the class CastingTest method checkDataTruncationResult.

/**
 * <p>
 * Check the results for the queries in testDataTruncation().
 * </p>
 *
 * <p>
 * The method expects a query that returns three rows with columns of a
 * character string or binary string data type, where some of the values
 * are cast to a narrower data type.
 * </p>
 *
 * <p>
 * Expect the following truncations to have taken place:
 * </p>
 *
 * <ol>
 * <li>Row 1, column 1: truncated from 3 to 2 bytes</li>
 * <li>Row 3, column 1: truncated from 3 to 2 bytes</li>
 * <li>Row 3, column 2: truncated from 4 to 2 bytes</li>
 * </ol>
 */
private void checkDataTruncationResult(Statement s, String sql) throws SQLException {
    ResultSet rs = s.executeQuery(sql);
    // First row should have one warning (column 1)
    assertTrue(rs.next());
    SQLWarning w = rs.getWarnings();
    assertDataTruncation(w, -1, true, false, 3, 2);
    w = w.getNextWarning();
    assertNull(w);
    // workaround for DERBY-5765
    rs.clearWarnings();
    // Second row should have no warnings
    assertTrue(rs.next());
    assertNull(rs.getWarnings());
    // Third row should have two warnings (column 1 and 2)
    assertTrue(rs.next());
    w = rs.getWarnings();
    assertDataTruncation(w, -1, true, false, 3, 2);
    // Client driver doesn't support nested warnings
    if (usingEmbedded()) {
        w = w.getNextWarning();
        assertDataTruncation(w, -1, true, false, 4, 2);
    }
    w = w.getNextWarning();
    assertNull(w);
    // workaround for DERBY-5765
    rs.clearWarnings();
    // No more rows
    assertFalse(rs.next());
    rs.close();
    // There should be no warnings on the statement or the connection
    assertNull(s.getWarnings());
    assertNull(getConnection().getWarnings());
}
Also used : SQLWarning(java.sql.SQLWarning) ResultSet(java.sql.ResultSet)

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