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: Issue #1680 updating a boolean field requires special handling to set it to t or f instead of true or false #1682

Merged
merged 2 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 13 additions & 9 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1674,16 +1674,20 @@ 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(
connection.getTimestampUtils().toString(
getDefaultCalendar(), (Date) valueObject));
rowBuffer[columnIndex] =
connection.encodeString(connection.getTimestampUtils().toString(getDefaultCalendar(),
(Date) valueObject));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the DATE handling changed right? If so let's leave it untouched by the PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct. will revert. Didn't like the formatting

break;

case Types.TIME:
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"));
}
}