postgresql-jdbc.git / summary / log / commit / refs
commit 3a1dc686ce548b4541012de755d9a9e17b4e278f
Author: Philippe Marschall <philippe.marschall@gmail.com>
Commit: GitHub <noreply@github.com>
Date: Mon Jan 03 14:37:17 2022 +0000
fix: throw SQLException for #getBoolean BIT(>1) (#2386)
Throw SQLException instead of ClassCastException when calling
CallableStatement#getBoolean(int) on BIT(>1).
.../org/postgresql/jdbc/PgCallableStatement.java | 3 +-
.../test/jdbc3/Jdbc3CallableStatementTest.java | 47 ++++++++++++++++++++++
2 files changed, 48 insertions(+), 2 deletions(-)
diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/PgCallableStatement.java b/pgjdbc/src/main/java/org/postgresql/jdbc/PgCallableStatement.java
index f73088c6..a4105461 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 @@ class PgCallableStatement extends PgPreparedStatement implements CallableStateme
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 9dfa192f..822bdf80 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 class Jdbc3CallableStatementTest extends BaseTest4 {
+ "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 class Jdbc3CallableStatementTest extends BaseTest4 {
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 class Jdbc3CallableStatementTest extends BaseTest4 {
}
}
+ @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 class Jdbc3CallableStatementTest extends BaseTest4 {
}
}
+ @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();
[parent: 54e5598ab34e]