Skip to content
Permalink
Browse files
fix: Issue #1680 updating a boolean field requires special handling t…
…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 c266b0885141f48912d7883ec17855c420641a72
Showing with 27 additions and 5 deletions.
  1. +10 −5 pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
  2. +17 −0 pgjdbc/src/test/java/org/postgresql/test/jdbc2/UpdateableResultTest.java
@@ -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(
@@ -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;
@@ -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
@@ -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();
}

@@ -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.