Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
fix: Binary handling of empty arrays.
The type of the empty array was not properly read as a result
getting the type of the array caused an NPE
fixes Issue #421 reported by Juha Komulainen
- Loading branch information
|
@@ -264,15 +264,30 @@ private ResultSet readBinaryResultSet(int index, int count) throws SQLException |
|
|
} |
|
|
List rows = new ArrayList(); |
|
|
Field[] fields = new Field[2]; |
|
|
if (dimensions > 0) { |
|
|
storeValues(rows, fields, elementOid, dims, pos, 0, index); |
|
|
} |
|
|
|
|
|
storeValues(rows, fields, elementOid, dims, pos, 0, index); |
|
|
|
|
|
BaseStatement stat = (BaseStatement) connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
|
|
return stat.createDriverResultSet(fields, rows); |
|
|
} |
|
|
|
|
|
private int storeValues(List rows, Field[] fields, int elementOid, final int[] dims, int pos, final int thisDimension, int index) throws SQLException { |
|
|
if (thisDimension == dims.length - 1) { |
|
|
private int storeValues(List rows, Field[] fields, int elementOid, final int[] dims, int pos, final int thisDimension, int index) throws SQLException |
|
|
{ |
|
|
// handle an empty array |
|
|
if (dims.length == 0) |
|
|
{ |
|
|
fields[0] = new Field("INDEX", Oid.INT4); |
|
|
fields[0].setFormat(Field.BINARY_FORMAT); |
|
|
fields[1] = new Field("VALUE", elementOid); |
|
|
fields[1].setFormat(Field.BINARY_FORMAT); |
|
|
for (int i = 1; i < index; ++i) { |
|
|
int len = ByteConverter.int4(fieldBytes, pos); pos += 4; |
|
|
if (len != -1) { |
|
|
pos += len; |
|
|
} |
|
|
} |
|
|
} |
|
|
else if (thisDimension == dims.length - 1) { |
|
|
fields[0] = new Field("INDEX", Oid.INT4); |
|
|
fields[0].setFormat(Field.BINARY_FORMAT); |
|
|
fields[1] = new Field("VALUE", elementOid); |
|
|
|
@@ -413,6 +413,33 @@ public void testNonStandardDelimiter() throws SQLException |
|
|
|
|
|
assertTrue(!arrRS.next()); |
|
|
} |
|
|
|
|
|
public void testEmptyBinaryArray() throws SQLException |
|
|
{ |
|
|
|
|
|
PreparedStatement pstmt = conn.prepareStatement("SELECT '{}'::int[]"); |
|
|
// force binary |
|
|
((org.postgresql.PGStatement)pstmt).setPrepareThreshold(-1); |
|
|
|
|
|
try (ResultSet rs = pstmt.executeQuery()) { |
|
|
while (rs.next()) { |
|
|
Array array = rs.getArray(1); |
|
|
if (!rs.wasNull()) |
|
|
{ |
|
|
ResultSet ars = array.getResultSet(); |
|
|
try |
|
|
{ |
|
|
ars.getMetaData().getColumnType(1); // this throws NPE |
|
|
} |
|
|
catch (NullPointerException ex) |
|
|
{ |
|
|
assertTrue("should not get here",true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|