From f2a9fc13113f12a579777491f9c7e1db39c13d02 Mon Sep 17 00:00:00 2001 From: Kurt Westerfeld Date: Wed, 11 Sep 2024 06:54:10 +0100 Subject: [PATCH] fixes #608 - when a $1 or $$foo$$ is within () expression, like a function invocation, parse the $ as plain grammar piece - add a test for this situation which does not parse otherwise and throws ParseException --- .../com/impossibl/postgres/jdbc/SQLText.java | 6 ++++++ .../impossibl/postgres/jdbc/SQLTextTests.java | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/driver/src/main/java/com/impossibl/postgres/jdbc/SQLText.java b/driver/src/main/java/com/impossibl/postgres/jdbc/SQLText.java index 766395a50..550e9aa4d 100644 --- a/driver/src/main/java/com/impossibl/postgres/jdbc/SQLText.java +++ b/driver/src/main/java/com/impossibl/postgres/jdbc/SQLText.java @@ -313,6 +313,12 @@ private static int consumeParens(final String sql, final int start, final Deque< } private static int consumeDollar(final String sql, final int start, final CompositeNode parent) throws ParseException { + + if (parent instanceof ParenGroupNode) { + parent.add(new GrammarPiece(sql.substring(start, start + 1), start)); + return start + 1; + } + int ndx = start; do { if (lookAhead(sql, ndx) == '$') { diff --git a/driver/src/test/java/com/impossibl/postgres/jdbc/SQLTextTests.java b/driver/src/test/java/com/impossibl/postgres/jdbc/SQLTextTests.java index 1a5fa7236..47cb92521 100644 --- a/driver/src/test/java/com/impossibl/postgres/jdbc/SQLTextTests.java +++ b/driver/src/test/java/com/impossibl/postgres/jdbc/SQLTextTests.java @@ -97,6 +97,22 @@ public class SQLTextTests { "--\n--", "--\n--", }, + new String[] { + "PREPARE test_plan AS SELECT hashtext($1)", + "PREPARE test_plan AS SELECT hashtext($1)", + }, + new String[] { + "PREPARE test_plan AS SELECT hashtext($$foo$$)", + "PREPARE test_plan AS SELECT hashtext($$foo$$)", + }, + new String[] { + "PREPARE test_plan AS SELECT hashtext($$fo;o$$)", + "PREPARE test_plan AS SELECT hashtext($$fo;o$$)", + }, + new String[] { + "select * from flatten('{\"a\":1,\"b\":2}', $ as root) as f", + "select * from flatten('{\"a\":1,\"b\":2}', $ as root) as f", + }, }; /**