From 83d991c964cf483802f854c6dab77aee13401c66 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 | 24 +++++++++++++++++++ 2 files changed, 30 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..3c15a7f54 100644 --- a/driver/src/main/java/com/impossibl/postgres/jdbc/SQLText.java +++ b/driver/src/main/java/com/impossibl/postgres/jdbc/SQLText.java @@ -330,6 +330,12 @@ private static int consumeDollar(final String sql, final int start, final Compos } } while (++ndx < sql.length()); + // Treat as a grammar piece of only one char if within paren expression + if (parent instanceof ParenGroupNode) { + parent.add(new GrammarPiece(sql.substring(start, start + 1), start)); + return start + 1; + } + // Just treat as a grammar piece parent.add(new GrammarPiece(sql.substring(start, ndx), start)); return 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 c5a4944c5..a819cde3e 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,30 @@ 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", + }, + new String[] { + "call help ( $$ { me } $$ )", + "call help ( $$ { me } $$ )", + }, + new String[] { + "call help ( $delim$ { me } $delim$ )", + "call help ( $delim$ { me } $delim$ )", + }, }; /**