diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/PgCallableStatement.java b/pgjdbc/src/main/java/org/postgresql/jdbc/PgCallableStatement.java index f73088c6b7..a4105461c4 100644 --- a/pgjdbc/src/main/java/org/postgresql/jdbc/PgCallableStatement.java +++ b/pgjdbc/src/main/java/org/postgresql/jdbc/PgCallableStatement.java @@ -248,8 +248,7 @@ public boolean getBoolean(@Positive int parameterIndex) throws SQLException { if (result == null) { return false; } - - return (Boolean) result; + return BooleanTypeUtil.castToBoolean(result); } public byte getByte(@Positive int parameterIndex) throws SQLException { diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc3/Jdbc3CallableStatementTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc3/Jdbc3CallableStatementTest.java index 9dfa192f64..822bdf8082 100644 --- a/pgjdbc/src/test/java/org/postgresql/test/jdbc3/Jdbc3CallableStatementTest.java +++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc3/Jdbc3CallableStatementTest.java @@ -85,6 +85,18 @@ public void setUp() throws Exception { + "end;'" + "LANGUAGE plpgsql VOLATILE;" ); + stmt.execute( + "CREATE OR REPLACE FUNCTION testspg__getBooleanWithoutArg() " + + "RETURNS boolean AS ' " + + "begin return true; end; ' LANGUAGE plpgsql;"); + stmt.execute( + "CREATE OR REPLACE FUNCTION testspg__getBit1WithoutArg() " + + "RETURNS bit(1) AS ' " + + "begin return B''1''; end; ' LANGUAGE plpgsql;"); + stmt.execute( + "CREATE OR REPLACE FUNCTION testspg__getBit2WithoutArg() " + + "RETURNS bit(2) AS ' " + + "begin return B''10''; end; ' LANGUAGE plpgsql;"); if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v11)) { stmt.execute( "CREATE OR REPLACE PROCEDURE inonlyprocedure(a IN int) AS 'BEGIN NULL; END;' LANGUAGE plpgsql"); @@ -104,6 +116,9 @@ public void tearDown() throws SQLException { stmt.execute("drop function myif(a INOUT int, b IN int)"); stmt.execute("drop function mynoparams()"); stmt.execute("drop function mynoparamsproc()"); + stmt.execute("drop function testspg__getBooleanWithoutArg ();"); + stmt.execute("drop function testspg__getBit1WithoutArg ();"); + stmt.execute("drop function testspg__getBit2WithoutArg ();"); if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v11)) { stmt.execute("drop procedure inonlyprocedure(a IN int)"); stmt.execute("drop procedure inoutprocedure(a INOUT int)"); @@ -378,6 +393,29 @@ public void testSetObjectBit() throws Throwable { } } + @Test + public void testGetBit1WithoutArg() throws SQLException { + try (CallableStatement call = con.prepareCall("{ ? = call testspg__getBit1WithoutArg () }")) { + call.registerOutParameter(1, Types.BOOLEAN); + call.execute(); + assertTrue(call.getBoolean(1)); + } + } + + @Test + public void testGetBit2WithoutArg() throws SQLException { + try (CallableStatement call = con.prepareCall("{ ? = call testspg__getBit2WithoutArg () }")) { + call.registerOutParameter(1, Types.BOOLEAN); + try { + call.execute(); + assertTrue(call.getBoolean(1)); + fail("#getBoolean(int) on bit(2) should throw"); + } catch (SQLException e) { + assertEquals(PSQLState.CANNOT_COERCE.getState(), e.getSQLState()); + } + } + } + @Test public void testGetObjectLongVarchar() throws Throwable { assumeCallableStatementsSupported(); @@ -940,6 +978,15 @@ public void testGetBoolean01() throws Throwable { } } + @Test + public void testGetBooleanWithoutArg() throws SQLException { + try (CallableStatement call = con.prepareCall("{ ? = call testspg__getBooleanWithoutArg () }")) { + call.registerOutParameter(1, Types.BOOLEAN); + call.execute(); + assertTrue(call.getBoolean(1)); + } + } + @Test public void testGetByte01() throws Throwable { assumeCallableStatementsSupported();