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: binary decoding of bool values #2640

Merged
merged 3 commits into from Oct 12, 2022
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
2 changes: 1 addition & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Expand Up @@ -2297,7 +2297,7 @@ public boolean getBoolean(@Positive int columnIndex) throws SQLException {
int col = columnIndex - 1;
if (Oid.BOOL == fields[col].getOID()) {
final byte[] v = value;
return (1 == v.length) && (116 == v[0]); // 116 = 't'
return (1 == v.length) && ((116 == v[0] && !isBinary(columnIndex)) || (1 == v[0] && isBinary(columnIndex))); // 116 = 't'
}

if (isBinary(columnIndex)) {
Expand Down
Expand Up @@ -7,6 +7,7 @@

import org.postgresql.PGConnection;
import org.postgresql.PGProperty;
import org.postgresql.core.Oid;
import org.postgresql.core.Version;
import org.postgresql.jdbc.PreferQueryMode;
import org.postgresql.test.TestUtil;
Expand Down Expand Up @@ -39,7 +40,7 @@ public enum StringType {
}

protected Connection con;
private BinaryMode binaryMode;
protected BinaryMode binaryMode;
private ReWriteBatchedInserts reWriteBatchedInserts;
protected PreferQueryMode preferQueryMode;
private StringType stringType;
Expand All @@ -58,6 +59,7 @@ protected void updateProperties(Properties props) {

protected void forceBinary(Properties props) {
PGProperty.PREPARE_THRESHOLD.set(props, -1);
PGProperty.BINARY_TRANSFER_ENABLE.set(props, Oid.BOOL);
}

public final void setBinaryMode(BinaryMode binaryMode) {
Expand Down
Expand Up @@ -505,14 +505,17 @@ public void testDoubleQuestionMark() throws SQLException {
st = con.prepareStatement("select ??- lseg '((-1,0),(1,0))';");
rs = st.executeQuery();
assertTrue(rs.next());
assertEquals("t", rs.getString(1));
// Bool values in binary mode are first converted to their Java type (Boolean), and then
// converted to String, which means that we receive 'true'. Bool values in text mode are
// returned as the same text value that was returned by the server, i.e. 't'.
assertEquals(binaryMode == BinaryMode.FORCE && preferQueryMode != PreferQueryMode.SIMPLE ? "true" : "t", rs.getString(1));
assertFalse(rs.next());
st.close();

st = con.prepareStatement("select lseg '((-1,0),(1,0))' ??# box '((-2,-2),(2,2))';");
rs = st.executeQuery();
assertTrue(rs.next());
assertEquals("t", rs.getString(1));
assertEquals(binaryMode == BinaryMode.FORCE && preferQueryMode != PreferQueryMode.SIMPLE ? "true" : "t", rs.getString(1));
assertFalse(rs.next());
st.close();
}
Expand Down
Expand Up @@ -72,6 +72,10 @@ public void setUp() throws Exception {
stmt.executeUpdate("INSERT INTO testint VALUES (12345)");

// Boolean Tests
TestUtil.createTable(con, "testbool", "a boolean, b int");
stmt.executeUpdate("INSERT INTO testbool VALUES(true, 1)");
stmt.executeUpdate("INSERT INTO testbool VALUES(false, 0)");

TestUtil.createTable(con, "testboolstring", "a varchar(30), b boolean");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('1 ', true)");
stmt.executeUpdate("INSERT INTO testboolstring VALUES('0', false)");
Expand Down Expand Up @@ -284,6 +288,14 @@ public void testMaxFieldSize() throws SQLException {
assertEquals("12", new String(rs.getBytes(1)));
}

@Test
public void testBooleanBool() throws SQLException {
testBoolean("testbool", 0);
testBoolean("testbool", 1);
testBoolean("testbool", 5);
testBoolean("testbool", -1);
}

@Test
public void testBooleanString() throws SQLException {
testBoolean("testboolstring", 0);
Expand Down Expand Up @@ -332,7 +344,7 @@ public void testBoolean(String table, int prepareThreshold) throws SQLException
}

@Test
public void testgetBooleanJDBCCompliance() throws SQLException {
public void testGetBooleanJDBCCompliance() throws SQLException {
// The JDBC specification in Table B-6 "Use of ResultSet getter Methods to Retrieve JDBC Data Types"
// the getBoolean have this Supported JDBC Type: TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT,
// DOUBLE, DECIMAL, NUMERIC, BIT, BOOLEAN, CHAR, VARCHAR, LONGVARCHAR
Expand Down