Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw SQLException for #getBoolean BIT(>1) #2386

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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