From: <mro...@us...> - 2012-12-07 19:57:05
|
Revision: 57459 http://firebird.svn.sourceforge.net/firebird/?rev=57459&view=rev Author: mrotteveel Date: 2012-12-07 19:56:57 +0000 (Fri, 07 Dec 2012) Log Message: ----------- JDBC-288 : FBPreparedStatement and FBCallableStatement not compliant for java.sql.Statement methods Modified Paths: -------------- client-java/trunk/src/main/org/firebirdsql/jdbc/FBCallableStatement.java client-java/trunk/src/main/org/firebirdsql/jdbc/FBPreparedStatement.java client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBCallableStatement.java client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBPreparedStatement.java Modified: client-java/trunk/src/main/org/firebirdsql/jdbc/FBCallableStatement.java =================================================================== --- client-java/trunk/src/main/org/firebirdsql/jdbc/FBCallableStatement.java 2012-12-07 04:14:40 UTC (rev 57458) +++ client-java/trunk/src/main/org/firebirdsql/jdbc/FBCallableStatement.java 2012-12-07 19:56:57 UTC (rev 57459) @@ -1257,30 +1257,6 @@ throw new FBDriverNotCapableException(); } - public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { - throw new FBDriverNotCapableException(); - } - - public int executeUpdate(String sql, int[] columnIndexes) throws SQLException { - throw new FBDriverNotCapableException(); - } - - public int executeUpdate(String sql, String[] columnNames) throws SQLException { - throw new FBDriverNotCapableException(); - } - - public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { - throw new FBDriverNotCapableException(); - } - - public boolean execute(String sql, int[] columnIndexes) throws SQLException { - throw new FBDriverNotCapableException(); - } - - public boolean execute(String sql, String[] columnNames) throws SQLException { - throw new FBDriverNotCapableException(); - } - /** * Asserts if the current statement has data to return. It checks if the * result set has a row with data. Modified: client-java/trunk/src/main/org/firebirdsql/jdbc/FBPreparedStatement.java =================================================================== --- client-java/trunk/src/main/org/firebirdsql/jdbc/FBPreparedStatement.java 2012-12-07 04:14:40 UTC (rev 57458) +++ client-java/trunk/src/main/org/firebirdsql/jdbc/FBPreparedStatement.java 2012-12-07 19:56:57 UTC (rev 57459) @@ -44,6 +44,8 @@ public class FBPreparedStatement extends FBStatement implements FirebirdPreparedStatement { + static final String METHOD_NOT_SUPPORTED = "This method is only supported on Statement and not supported on PreparedStatement and CallableStatement"; + private boolean metaDataQuery; /** @@ -1354,4 +1356,55 @@ throw new FBDriverNotCapableException(); } + // Methods not allowed to be used on PreparedStatement and CallableStatement + + @Override + public ResultSet executeQuery(String sql) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public int executeUpdate(String sql) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public boolean execute(String sql) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public void addBatch(String sql) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public int executeUpdate(String sql, int[] columnIndex) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public int executeUpdate(String sql, String[] columnNames) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public boolean execute(String sql, int autoGeneratedKeys) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public boolean execute(String sql, int[] columnIndexes) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } + + @Override + public boolean execute(String sql, String[] columnNames) throws SQLException { + throw new FBSQLException(METHOD_NOT_SUPPORTED); + } } Modified: client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBCallableStatement.java =================================================================== --- client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBCallableStatement.java 2012-12-07 04:14:40 UTC (rev 57458) +++ client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBCallableStatement.java 2012-12-07 19:56:57 UTC (rev 57459) @@ -825,11 +825,11 @@ } public void testJdbc181() throws Exception { - PreparedStatement ps = con.prepareCall("{call factorial(?, ?)}"); //con.prepareStatement("EXECUTE PROCEDURE factorial(?, ?)"); + CallableStatement cs = con.prepareCall("{call factorial(?, ?)}"); //con.prepareStatement("EXECUTE PROCEDURE factorial(?, ?)"); try { - ps.setInt(1, 5); - ps.setInt(2, 1); - ResultSet rs = ps.executeQuery(); + cs.setInt(1, 5); + cs.setInt(2, 1); + ResultSet rs = cs.executeQuery(); int counter = 0; int factorial = 1; while(rs.next()) { @@ -840,7 +840,7 @@ factorial *= counter; } } finally { - closeQuietly(ps); + closeQuietly(cs); } } @@ -890,4 +890,161 @@ } // Other closeOnCompletion behavior considered to be sufficiently tested in TestFBStatement + + /** + * The method {@link java.sql.Statement#executeQuery(String)} should not work on CallabeStatement. + */ + public void testUnsupportedExecuteQuery_String() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.executeQuery("SELECT * FROM test_blob"); + fail("Expected SQLException when executing executeQuery(String) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#executeUpdate(String)} should not work on CallabeStatement. + */ + public void testUnsupportedExecuteUpdate_String() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.executeUpdate("SELECT * FROM test_blob"); + fail("Expected SQLException when executing executeUpdate(String) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#execute(String)} should not work on CallabeStatement. + */ + public void testUnsupportedExecute_String() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.execute("SELECT * FROM test_blob"); + fail("Expected SQLException when executing execute(String) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#addBatch(String)} should not work on CallabeStatement. + */ + public void testUnsupportedAddBatch_String() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.addBatch("SELECT * FROM test_blob"); + fail("Expected SQLException when executing addBatch(String) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#executeUpdate(String, int)} should not work on CallabeStatement. + */ + public void testUnsupportedExecuteUpdate_String_int() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.executeUpdate("SELECT * FROM test_blob", Statement.NO_GENERATED_KEYS); + fail("Expected SQLException when executing executeUpdate(String, int) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, int[])} should not work on CallabeStatement. + */ + public void testUnsupportedExecuteUpdate_String_intArr() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.executeUpdate("SELECT * FROM test_blob", new int[] { 1 }); + fail("Expected SQLException when executing executeUpdate(String, int[]) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#executeUpdate(String, String[])} should not work on CallabeStatement. + */ + public void testUnsupportedExecuteUpdate_String_StringArr() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.executeUpdate("SELECT * FROM test_blob", new String[] { "col" }); + fail("Expected SQLException when executing executeUpdate(String, String[]) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, int)} should not work on CallabeStatement. + */ + public void testUnsupportedExecute_String_int() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.execute("SELECT * FROM test_blob", Statement.NO_GENERATED_KEYS); + fail("Expected SQLException when executing execute(String, int) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, int[])} should not work on CallabeStatement. + */ + public void testUnsupportedExecute_String_intArr() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.execute("SELECT * FROM test_blob", new int[] { 1 }); + fail("Expected SQLException when executing execute(String, int[]) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, String[])} should not work on CallabeStatement. + */ + public void testUnsupportedExecute_String_StringArr() throws Exception { + CallableStatement cs = con.prepareCall(EXECUTE_SIMPLE_OUT_PROCEDURE); + try { + cs.execute("SELECT * FROM test_blob", new String[] { "col" }); + fail("Expected SQLException when executing execute(String, String[]) on CallabeStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(cs); + } + } + + private void assertStatementOnlyException(SQLException ex) { + assertEquals("Unexpected SQLState for statement only method called on FBCallableStatement", + FBSQLException.SQL_STATE_GENERAL_ERROR, ex.getSQLState()); + assertEquals("Unexpected exception message for statement only method called on FBCallableStatement", + FBPreparedStatement.METHOD_NOT_SUPPORTED, ex.getMessage()); + } } Modified: client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBPreparedStatement.java =================================================================== --- client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBPreparedStatement.java 2012-12-07 04:14:40 UTC (rev 57458) +++ client-java/trunk/src/test/org/firebirdsql/jdbc/TestFBPreparedStatement.java 2012-12-07 19:56:57 UTC (rev 57459) @@ -172,23 +172,164 @@ closeQuietly(updatePs); } } - - public void testMixedExecution() throws Throwable { - PreparedStatement ps = con - .prepareStatement("INSERT INTO test_blob (id, obj_data) VALUES(?, NULL)"); + + /** + * The method {@link java.sql.Statement#executeQuery(String)} should not work on PreparedStatement. + */ + public void testUnsupportedExecuteQuery_String() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); try { - ps.setInt(1, 100); - ps.execute(); - - ResultSet rs = ps.executeQuery("SELECT * FROM test_blob"); - while (rs.next()) { - // nothing - } + ps.executeQuery("SELECT * FROM test_blob"); + fail("Expected SQLException when executing executeQuery(String) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); } finally { closeQuietly(ps); } } + + /** + * The method {@link java.sql.Statement#executeUpdate(String)} should not work on PreparedStatement. + */ + public void testUnsupportedExecuteUpdate_String() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.executeUpdate("SELECT * FROM test_blob"); + fail("Expected SQLException when executing executeUpdate(String) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#execute(String)} should not work on PreparedStatement. + */ + public void testUnsupportedExecute_String() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.execute("SELECT * FROM test_blob"); + fail("Expected SQLException when executing execute(String) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#addBatch(String)} should not work on PreparedStatement. + */ + public void testUnsupportedAddBatch_String() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.addBatch("SELECT * FROM test_blob"); + fail("Expected SQLException when executing addBatch(String) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#executeUpdate(String, int)} should not work on PreparedStatement. + */ + public void testUnsupportedExecuteUpdate_String_int() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.executeUpdate("SELECT * FROM test_blob", Statement.NO_GENERATED_KEYS); + fail("Expected SQLException when executing executeUpdate(String, int) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, int[])} should not work on PreparedStatement. + */ + public void testUnsupportedExecuteUpdate_String_intArr() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.executeUpdate("SELECT * FROM test_blob", new int[] { 1 }); + fail("Expected SQLException when executing executeUpdate(String, int[]) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#executeUpdate(String, String[])} should not work on PreparedStatement. + */ + public void testUnsupportedExecuteUpdate_String_StringArr() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.executeUpdate("SELECT * FROM test_blob", new String[] { "col" }); + fail("Expected SQLException when executing executeUpdate(String, String[]) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, int)} should not work on PreparedStatement. + */ + public void testUnsupportedExecute_String_int() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.execute("SELECT * FROM test_blob", Statement.NO_GENERATED_KEYS); + fail("Expected SQLException when executing execute(String, int) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, int[])} should not work on PreparedStatement. + */ + public void testUnsupportedExecute_String_intArr() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.execute("SELECT * FROM test_blob", new int[] { 1 }); + fail("Expected SQLException when executing execute(String, int[]) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + + /** + * The method {@link java.sql.Statement#execute(String, String[])} should not work on PreparedStatement. + */ + public void testUnsupportedExecute_String_StringArr() throws Exception { + PreparedStatement ps = con.prepareStatement("SELECT 1 FROM RDB$DATABASE"); + try { + ps.execute("SELECT * FROM test_blob", new String[] { "col" }); + fail("Expected SQLException when executing execute(String, String[]) on PreparedStatement"); + } catch (SQLException ex) { + assertStatementOnlyException(ex); + } finally { + closeQuietly(ps); + } + } + private void assertStatementOnlyException(SQLException ex) { + assertEquals("Unexpected SQLState for statement only method called on FBPreparedStatement", + FBSQLException.SQL_STATE_GENERAL_ERROR, ex.getSQLState()); + assertEquals("Unexpected exception message for statement only method called on FBPreparedStatement", + FBPreparedStatement.METHOD_NOT_SUPPORTED, ex.getMessage()); + } + void checkSelectString(String stringToTest, int id) throws Exception { PreparedStatement selectPs = con .prepareStatement("SELECT obj_data FROM test_blob WHERE id = ?"); @@ -525,7 +666,7 @@ Statement stmt = con.createStatement(); try { - stmt.execute("SELECT * FROM rdb$database"); + stmt.execute("SELECT 1 FROM RDB$DATABASE"); } catch (Throwable t) { fail("Should not throw exception"); } finally { @@ -558,7 +699,7 @@ Statement stmt = con.createStatement(); try { - stmt.execute("SELECT * FROM rdb$database"); + stmt.execute("SELECT 1 FROM RDB$DATABASE"); } catch (Throwable t) { fail("Should not throw exception"); } finally { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |