Skip to content

Commit

Permalink
fix: throw SQLException for #getBoolean BIT(>1) (#2386)
Browse files Browse the repository at this point in the history
Throw SQLException instead of ClassCastException when calling
CallableStatement#getBoolean(int) on BIT(>1).
  • Loading branch information
marschall committed Jan 3, 2022
1 parent 54e5598 commit 3a1dc68
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Expand Up @@ -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 {
Expand Down
Expand Up @@ -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");
Expand All @@ -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)");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3a1dc68

Please sign in to comment.