GeezerVison is showing github.com — fix: binary decoding of bool values (#2640) · pgjdbc/pgjdbc@56a487c
File treeExpand file treeCollapse file tree Open diff view settings main/java/org/postgresql/jdbc test/java/org/postgresql/test/jdbc2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2297,7 +2297,7 @@ public boolean getBoolean(@Positive int columnIndex) throws SQLException { | 2297 | 2297 | int col = columnIndex - 1; | 2298 | 2298 | if (Oid.BOOL == fields[col].getOID()) { | 2299 | 2299 | final byte[] v = value; | 2300 | | - return (1 == v.length) && (116 == v[0]); // 116 = 't' | | 2300 | + return (1 == v.length) && ((116 == v[0] && !isBinary(columnIndex)) || (1 == v[0] && isBinary(columnIndex))); // 116 = 't' | 2301 | 2301 | } | 2302 | 2302 |
| 2303 | 2303 | if (isBinary(columnIndex)) { | |
Collapse file Copy file name to clipboardExpand all lines: pgjdbc/src/test/java/org/postgresql/test/jdbc2/BaseTest4.java +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 |
| 8 | 8 | import org.postgresql.PGConnection; | 9 | 9 | import org.postgresql.PGProperty; | | 10 | +import org.postgresql.core.Oid; | 10 | 11 | import org.postgresql.core.Version; | 11 | 12 | import org.postgresql.jdbc.PreferQueryMode; | 12 | 13 | import org.postgresql.test.TestUtil; | @@ -39,7 +40,7 @@ public enum StringType { | 39 | 40 | } | 40 | 41 |
| 41 | 42 | protected Connection con; | 42 | | - private BinaryMode binaryMode; | | 43 | + protected BinaryMode binaryMode; | 43 | 44 | private ReWriteBatchedInserts reWriteBatchedInserts; | 44 | 45 | protected PreferQueryMode preferQueryMode; | 45 | 46 | private StringType stringType; | @@ -58,6 +59,7 @@ protected void updateProperties(Properties props) { | 58 | 59 |
| 59 | 60 | protected void forceBinary(Properties props) { | 60 | 61 | PGProperty.PREPARE_THRESHOLD.set(props, -1); | | 62 | + PGProperty.BINARY_TRANSFER_ENABLE.set(props, Oid.BOOL); | 61 | 63 | } | 62 | 64 |
| 63 | 65 | public final void setBinaryMode(BinaryMode binaryMode) { | |
Collapse file Copy file name to clipboardExpand all lines: pgjdbc/src/test/java/org/postgresql/test/jdbc2/PreparedStatementTest.java +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -505,14 +505,17 @@ public void testDoubleQuestionMark() throws SQLException { | 505 | 505 | st = con.prepareStatement("select ??- lseg '((-1,0),(1,0))';"); | 506 | 506 | rs = st.executeQuery(); | 507 | 507 | assertTrue(rs.next()); | 508 | | - assertEquals("t", rs.getString(1)); | | 508 | + // Bool values in binary mode are first converted to their Java type (Boolean), and then | | 509 | + // converted to String, which means that we receive 'true'. Bool values in text mode are | | 510 | + // returned as the same text value that was returned by the server, i.e. 't'. | | 511 | + assertEquals(binaryMode == BinaryMode.FORCE && preferQueryMode != PreferQueryMode.SIMPLE ? "true" : "t", rs.getString(1)); | 509 | 512 | assertFalse(rs.next()); | 510 | 513 | st.close(); | 511 | 514 |
| 512 | 515 | st = con.prepareStatement("select lseg '((-1,0),(1,0))' ??# box '((-2,-2),(2,2))';"); | 513 | 516 | rs = st.executeQuery(); | 514 | 517 | assertTrue(rs.next()); | 515 | | - assertEquals("t", rs.getString(1)); | | 518 | + assertEquals(binaryMode == BinaryMode.FORCE && preferQueryMode != PreferQueryMode.SIMPLE ? "true" : "t", rs.getString(1)); | 516 | 519 | assertFalse(rs.next()); | 517 | 520 | st.close(); | 518 | 521 | } | |
Collapse file Copy file name to clipboardExpand all lines: pgjdbc/src/test/java/org/postgresql/test/jdbc2/ResultSetTest.java +13-1Lines changed: 13 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,6 +72,10 @@ public void setUp() throws Exception { | 72 | 72 | stmt.executeUpdate("INSERT INTO testint VALUES (12345)"); | 73 | 73 |
| 74 | 74 | // Boolean Tests | | 75 | + TestUtil.createTable(con, "testbool", "a boolean, b int"); | | 76 | + stmt.executeUpdate("INSERT INTO testbool VALUES(true, 1)"); | | 77 | + stmt.executeUpdate("INSERT INTO testbool VALUES(false, 0)"); | | 78 | + | 75 | 79 | TestUtil.createTable(con, "testboolstring", "a varchar(30), b boolean"); | 76 | 80 | stmt.executeUpdate("INSERT INTO testboolstring VALUES('1 ', true)"); | 77 | 81 | stmt.executeUpdate("INSERT INTO testboolstring VALUES('0', false)"); | @@ -284,6 +288,14 @@ public void testMaxFieldSize() throws SQLException { | 284 | 288 | assertEquals("12", new String(rs.getBytes(1))); | 285 | 289 | } | 286 | 290 |
| | 291 | + @Test | | 292 | + public void testBooleanBool() throws SQLException { | | 293 | + testBoolean("testbool", 0); | | 294 | + testBoolean("testbool", 1); | | 295 | + testBoolean("testbool", 5); | | 296 | + testBoolean("testbool", -1); | | 297 | + } | | 298 | + | 287 | 299 | @Test | 288 | 300 | public void testBooleanString() throws SQLException { | 289 | 301 | testBoolean("testboolstring", 0); | @@ -332,7 +344,7 @@ public void testBoolean(String table, int prepareThreshold) throws SQLException | 332 | 344 | } | 333 | 345 |
| 334 | 346 | @Test | 335 | | - public void testgetBooleanJDBCCompliance() throws SQLException { | | 347 | + public void testGetBooleanJDBCCompliance() throws SQLException { | 336 | 348 | // The JDBC specification in Table B-6 "Use of ResultSet getter Methods to Retrieve JDBC Data Types" | 337 | 349 | // the getBoolean have this Supported JDBC Type: TINYINT, SMALLINT, INTEGER, BIGINT, REAL, FLOAT, | 338 | 350 | // DOUBLE, DECIMAL, NUMERIC, BIT, BOOLEAN, CHAR, VARCHAR, LONGVARCHAR | |
|