Skip to content

Commit

Permalink
escape ? jsonb operator (used in postgresql 9.4+ https://www.postgres…
Browse files Browse the repository at this point in the history
…ql.org/docs/9.4/static/functions-json.html) with double ??

- support for ?| and ?& added

- test for ??, ?| and ?& operators

- run testQuestionMarkExcaping only on postgresql 9.4+
  • Loading branch information
pkolaric committed Jan 6, 2017
1 parent 8e95543 commit b41dac8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/com/impossibl/postgres/jdbc/SQLText.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,22 @@ public static MultiStatementNode parse(final String sql, boolean standardConform
ndx = consumeQuotedIdentifier(sql, ndx, parents.peek());
continue;
case '?':
ParameterPiece parameterPiece = new ParameterPiece(paramId++, ndx);
parents.peek().add(parameterPiece);
char nextChar = lookAhead(sql, ndx);
if (nextChar == '|' || nextChar == '&' || nextChar == '?') {
final GrammarPiece grammarPiece;
if (nextChar == '?') {
grammarPiece = new GrammarPiece("?", ndx);
}
else {
grammarPiece = new GrammarPiece("?" + nextChar, ndx);
}
parents.peek().add(grammarPiece);
++ndx;
}
else {
ParameterPiece parameterPiece = new ParameterPiece(paramId++, ndx);
parents.peek().add(parameterPiece);
}
break;
case '$':
ndx = consumeDollar(sql, ndx, parents.peek());
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/impossibl/postgres/jdbc/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -609,4 +609,28 @@ public void testDefaultFetchSize() throws SQLException {

((PGConnection)con).setDefaultFetchSize(oldValue);
}

@Test
public void testQuestionMarkExcaping() throws SQLException {
if (!((PGConnection)con).isServerMinimumVersion(9, 4))
return;

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT '{\"a\":1, \"b\":2}'::jsonb ?? 'b'");
assertTrue(rs.next());
assertEquals(true, rs.getBoolean(1));
rs.close();

rs = stmt.executeQuery("SELECT '{\"a\":1, \"b\":2, \"c\":3}'::jsonb ?| array['b', 'd']");
assertTrue(rs.next());
assertEquals(true, rs.getBoolean(1));
rs.close();

rs = stmt.executeQuery("SELECT '{\"a\":1, \"b\":2, \"c\":3}'::jsonb ?& array['b', 'd']");
assertTrue(rs.next());
assertEquals(false, rs.getBoolean(1));
rs.close();

stmt.close();
}
}

0 comments on commit b41dac8

Please sign in to comment.