Skip to content

Commit

Permalink
fix: Issue #1680 updating a boolean field requires special handling t…
Browse files Browse the repository at this point in the history
…o set it to t or f instead of true or false (#1682)

* fix: Issue #1680 updating a boolean field requries special handling to set it to t or f instead of true or false
  • Loading branch information
davecramer committed Jan 29, 2020
1 parent 799e78d commit c266b08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1674,11 +1674,16 @@ private void updateRowBuffer() throws SQLException {
} else {
switch (getSQLType(columnIndex + 1)) {

//
// toString() isn't enough for date and time types; we must format it correctly
// or we won't be able to re-parse it.
//

// boolean needs to be formatted as t or f instead of true or false
case Types.BIT:
case Types.BOOLEAN:
rowBuffer[columnIndex] =
connection.encodeString(((Boolean)valueObject).booleanValue() ? "t" : "f");
break;
//
// toString() isn't enough for date and time types; we must format it correctly
// or we won't be able to re-parse it.
//
case Types.DATE:
rowBuffer[columnIndex] = connection
.encodeString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -41,6 +42,8 @@ public void setUp() throws Exception {
TestUtil.createTable(con, "second", "id1 int primary key, name1 text");
TestUtil.createTable(con, "stream", "id int primary key, asi text, chr text, bin bytea");
TestUtil.createTable(con, "multicol", "id1 int not null, id2 int not null, val text");
TestUtil.createTable(con, "booltable", "id int not null primary key, b boolean default false");
TestUtil.execute( "insert into booltable (id) values (1)", con);

Statement st2 = con.createStatement();
// create pk for multicol table
Expand All @@ -56,6 +59,7 @@ public void tearDown() throws SQLException {
TestUtil.dropTable(con, "updateable");
TestUtil.dropTable(con, "second");
TestUtil.dropTable(con, "stream");
TestUtil.dropTable(con, "booltable");
super.tearDown();
}

Expand Down Expand Up @@ -572,4 +576,17 @@ public void simpleAndUpdateableSameQuery() throws Exception {
}
}

@Test
public void testUpdateBoolean() throws Exception {

Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery("SELECT * FROM booltable WHERE id=1");
assertTrue(rs.next());
assertFalse(rs.getBoolean("b"));
rs.updateBoolean("b", true);
rs.updateRow();
//rs.refreshRow(); //fetches the value stored
assertTrue(rs.getBoolean("b"));
}
}

0 comments on commit c266b08

Please sign in to comment.