Skip to content

Commit

Permalink
fix: ArrayIndexOutOfBoundsException when using the same SQL for regul…
Browse files Browse the repository at this point in the history
…ar and updateable resultset (#1123)

fixes #1116
  • Loading branch information
vlsi committed Feb 18, 2018
1 parent 43d80cd commit 45c32bc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/core/v3/QueryExecutorImpl.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1489,6 +1489,17 @@ private void sendBind(SimpleQuery query, SimpleParameterList params, Portal port
} }
} }
} }
// If text-only results are required (e.g. updateable resultset), and the query has binary columns,
// flip to text format.
if (noBinaryTransfer && query.hasBinaryFields()) {
for (Field field : fields) {
if (field.getFormat() != Field.TEXT_FORMAT) {
field.setFormat(Field.TEXT_FORMAT);
}
}
query.resetNeedUpdateFieldFormats();
query.setHasBinaryFields(false);
}


// This is not the number of binary fields, but the total number // This is not the number of binary fields, but the total number
// of fields if any of them are binary or zero if all of them // of fields if any of them are binary or zero if all of them
Expand Down
3 changes: 3 additions & 0 deletions pgjdbc/src/main/java/org/postgresql/core/v3/SimpleQuery.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ boolean needUpdateFieldFormats() {
return false; return false;
} }


public void resetNeedUpdateFieldFormats() {
needUpdateFieldFormats = fields != null;
}


public boolean hasBinaryFields() { public boolean hasBinaryFields() {
return hasBinaryFields; return hasBinaryFields;
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;


import org.postgresql.PGConnection;
import org.postgresql.test.TestUtil; import org.postgresql.test.TestUtil;


import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test; import org.junit.Test;


import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -529,4 +532,49 @@ public void testMultiColumnUpdate() throws Exception {
st.close(); st.close();
} }


@Test
public void simpleAndUpdateableSameQuery() throws Exception {
PGConnection unwrap = con.unwrap(PGConnection.class);
Assume.assumeNotNull(unwrap);
int prepareThreshold = unwrap.getPrepareThreshold();
String sql = "select * from second where id1=?";
for (int i = 0; i <= prepareThreshold; i++) {
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(sql);
ps.setInt(1, 1);
rs = ps.executeQuery();
rs.next();
String name1 = rs.getString("name1");
Assert.assertEquals("anyvalue", name1);
int id1 = rs.getInt("id1");
Assert.assertEquals(1, id1);
} finally {
TestUtil.closeQuietly(rs);
TestUtil.closeQuietly(ps);
}
}
// The same SQL, and use updateable ResultSet
{
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
ps.setInt(1, 1);
rs = ps.executeQuery();
rs.next();
String name1 = rs.getString("name1");
Assert.assertEquals("anyvalue", name1);
int id1 = rs.getInt("id1");
Assert.assertEquals(1, id1);
rs.updateString("name1", "updatedValue");
rs.updateRow();
} finally {
TestUtil.closeQuietly(rs);
TestUtil.closeQuietly(ps);
}
}
}

} }

0 comments on commit 45c32bc

Please sign in to comment.