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: preserve unquoted unicode whitespace in array literals #1266

Merged
merged 25 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8281083
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 21, 2018
8b5072d
Revert "fix: don't ignore unquoted non-ascii whitespace"
bokken Jul 21, 2018
e407aaf
Revert "Revert "fix: don't ignore unquoted non-ascii whitespace""
bokken Jul 21, 2018
06c58fb
Revert "Revert "Revert "fix: don't ignore unquoted non-ascii whitespa…
bokken Jul 21, 2018
c1325e2
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 21, 2018
e828d76
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 21, 2018
06837da
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 21, 2018
5fa5dd0
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 22, 2018
c95fd4b
test: fix comment in ArrayTest.testDirectFieldString
vlsi Jul 22, 2018
3bbff4e
fix: revert spaces in array literal
vlsi Jul 22, 2018
c89a54b
add changelog entry
vlsi Jul 22, 2018
aae0d48
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 24, 2018
afdbe46
fix: don't ignore unquoted non-ascii whitespace
bokken Aug 13, 2018
51d9be9
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 21, 2018
86646fa
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 21, 2018
9b952f9
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 21, 2018
aa4aa03
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 22, 2018
5bdf256
test: fix comment in ArrayTest.testDirectFieldString
vlsi Jul 22, 2018
8a3402d
fix: revert spaces in array literal
vlsi Jul 22, 2018
12a83a4
add changelog entry
vlsi Jul 22, 2018
c70db61
fix: don't ignore unquoted non-ascii whitespace
bokken Jul 24, 2018
7560646
fix: don't ignore unquoted non-ascii whitespace
bokken Aug 13, 2018
af3006f
Merge remote-tracking branch 'origin/arrays_whitespace' into arrays_w…
bokken Aug 13, 2018
a13f7f1
Merge remote-tracking branch 'upstream/master' into arrays_whitespace
bokken Apr 15, 2019
047f1a5
Merge remote-tracking branch 'upstream/master' into arrays_whitespace
Aug 6, 2020
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
28 changes: 27 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ private synchronized void buildArrayList() throws SQLException {
insideString = !insideString;
wasInsideString = true;
continue;
} else if (!insideString && Character.isWhitespace(chars[i])) {
} else if (!insideString && isArrayWhiteSpace(chars[i])) {
// white space
continue;
} else if ((!insideString && (chars[i] == delim || chars[i] == '}'))
Expand Down Expand Up @@ -529,6 +529,32 @@ private synchronized void buildArrayList() throws SQLException {
}
}

/**
* Is whitespace which postgresql will force quotes.
Copy link
Member

Choose a reason for hiding this comment

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

Could we elaborate a bit here. I my not have enough coffee yet but I have difficulty reading this line.

*
* <p>
* https://github.com/postgres/postgres/blob/f2c587067a8eb9cf1c8f009262381a6576ba3dd0/src/backend/utils/adt/arrayfuncs.c#L421-L438
* </p>
*
* @param c
* Character to examine.
* @return Indication if the character is a whitespace which back end will
* escape.
*/
private static boolean isArrayWhiteSpace(char c) {
switch (c) {
case '\t':
case '\n':
// vertical tab
case 0x0B:
case '\f':
case '\r':
case ' ':
return true;
}
return false;
}

/**
* Convert {@link ArrayList} to array.
*
Expand Down
32 changes: 28 additions & 4 deletions pgjdbc/src/test/java/org/postgresql/test/jdbc2/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.postgresql.PGConnection;
import org.postgresql.core.BaseConnection;
import org.postgresql.core.Oid;
import org.postgresql.geometric.PGbox;
import org.postgresql.geometric.PGpoint;
import org.postgresql.jdbc.PgArray;
Expand Down Expand Up @@ -87,10 +88,12 @@ public void testSetNull() throws SQLException {

@Test
public void testSetPrimitiveObjects() throws SQLException {

final String stringWithNonAsciiWhiteSpace = "a\u2001b";
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO arrtest VALUES (?,?,?)");
pstmt.setObject(1, new int[]{1,2,3}, Types.ARRAY);
pstmt.setObject(2, new double[]{3.1d, 1.4d}, Types.ARRAY);
pstmt.setObject(3, new String[]{"abc", "f'a", "fa\"b"}, Types.ARRAY);
pstmt.setObject(1, new int[] { 1, 2, 3 }, Types.ARRAY);
pstmt.setObject(2, new double[] { 3.1d, 1.4d }, Types.ARRAY);
pstmt.setObject(3, new String[] { stringWithNonAsciiWhiteSpace, "f'a", "fa\"b" }, Types.ARRAY);
pstmt.executeUpdate();
pstmt.close();

Expand Down Expand Up @@ -120,6 +123,9 @@ public void testSetPrimitiveObjects() throws SQLException {
assertEquals("f'a", strarr[0]);
assertEquals("fa\"b", strarr[1]);

strarr = (String[]) arr.getArray();
assertEquals(stringWithNonAsciiWhiteSpace, strarr[0]);

rs.close();
}

Expand Down Expand Up @@ -242,9 +248,10 @@ public void testRetrieveArrays() throws SQLException {
public void testRetrieveResultSets() throws SQLException {
Statement stmt = conn.createStatement();

final String stringWithNonAsciiWhiteSpace = "a\u2001b";
// you need a lot of backslashes to get a double quote in.
stmt.executeUpdate("INSERT INTO arrtest VALUES ('{1,2,3}','{3.1,1.4}', '"
+ TestUtil.escapeString(conn, "{abc,f'a,\"fa\\\"b\",def}") + "')");
+ TestUtil.escapeString(conn, "{\"a\u2001b\",f'a,\"fa\\\"b\",def}") + "')");

ResultSet rs = stmt.executeQuery("SELECT intarr, decarr, strarr FROM arrtest");
Assert.assertTrue(rs.next());
Expand Down Expand Up @@ -289,6 +296,13 @@ public void testRetrieveResultSets() throws SQLException {
Assert.assertTrue(!arrrs.next());
arrrs.close();

arrrs = arr.getResultSet(1, 1);
Assert.assertTrue(arrrs.next());
Assert.assertEquals(1, arrrs.getInt(1));
Assert.assertEquals(stringWithNonAsciiWhiteSpace, arrrs.getString(2));
Assert.assertFalse(arrrs.next());
arrrs.close();

rs.close();
stmt.close();
}
Expand Down Expand Up @@ -392,6 +406,16 @@ public void testNullFieldString() throws SQLException {
Assert.assertNull(arr.toString());
}

@Test
public void testDirectFieldString() throws SQLException {
Array arr = new PgArray((BaseConnection) conn, Oid.VARCHAR_ARRAY, "{\" lead\t\", un quot , \" new \n\"}");
final String[] array = (String[]) arr.getArray();
assertEquals(3, array.length);
assertEquals(" lead\t", array[0]);
assertEquals("unquot", array[1]);
assertEquals(" new \n", array[2]);
}

@Test
public void testUnknownArrayType() throws SQLException {
Statement stmt = conn.createStatement();
Expand Down