Permalink
Browse files

fix: ArrayIndexOutOfBoundsException when using the same SQL for regul…

…ar and updateable resultset (#1123)

fixes #1116
  • Loading branch information...
vlsi committed Feb 18, 2018
1 parent 43d80cd commit 45c32bc6af2e140ff86dabd718344c74fc244394
@@ -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
// of fields if any of them are binary or zero if all of them
@@ -204,6 +204,9 @@ boolean needUpdateFieldFormats() {
return false;
}
public void resetNeedUpdateFieldFormats() {
needUpdateFieldFormats = fields != null;
}
public boolean hasBinaryFields() {
return hasBinaryFields;
@@ -12,8 +12,11 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.postgresql.PGConnection;
import org.postgresql.test.TestUtil;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import java.io.ByteArrayInputStream;
@@ -529,4 +532,49 @@ public void testMultiColumnUpdate() throws Exception {
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.