From 39d510c096cf435ff0f47bb510ae893a0c2b96d2 Mon Sep 17 00:00:00 2001 From: Laurenz Albe Date: Fri, 20 Nov 2015 13:34:11 +0100 Subject: [PATCH] Allow both single and double question marks in simple statements This is an improvement over the previous commit: Now you can either use a single question mark in a java.sql.Statement which will be interpreted as such, or you can use escaped (doubled) question marks. This way code that used to work will continue to work, while people who do not expect that question marks need to be escaped in simple statements will not be surprised. This commit also fixes the regression tests and adds a new one. --- org/postgresql/core/Parser.java | 18 ++++++++++-------- .../test/jdbc3/CompositeQueryParseTest.java | 11 ++++++++--- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/org/postgresql/core/Parser.java b/org/postgresql/core/Parser.java index 0c1083f357..f69b1815c1 100644 --- a/org/postgresql/core/Parser.java +++ b/org/postgresql/core/Parser.java @@ -83,9 +83,6 @@ public static List parseJdbcSql(String query, boolean standardConfo break; case '?': - if (!withParameters) - break; - nativeSql.append(aChars, fragmentStart, i - fragmentStart); if (i + 1 < aChars.length && aChars[i + 1] == '?') /* replace ?? with ? */ { @@ -93,11 +90,16 @@ public static List parseJdbcSql(String query, boolean standardConfo i++; // make sure the coming ? is not treated as a bind } else { - if (bindPositions == null) - bindPositions = new ArrayList(); - bindPositions.add(nativeSql.length()); - int bindIndex = bindPositions.size(); - nativeSql.append(NativeQuery.bindName(bindIndex)); + if (withParameters) + { + if (bindPositions == null) + bindPositions = new ArrayList(); + bindPositions.add(nativeSql.length()); + int bindIndex = bindPositions.size(); + nativeSql.append(NativeQuery.bindName(bindIndex)); + } + else + nativeSql.append('?'); } fragmentStart = i + 1; break; diff --git a/org/postgresql/test/jdbc3/CompositeQueryParseTest.java b/org/postgresql/test/jdbc3/CompositeQueryParseTest.java index 14b32eaf44..7291fe21fe 100644 --- a/org/postgresql/test/jdbc3/CompositeQueryParseTest.java +++ b/org/postgresql/test/jdbc3/CompositeQueryParseTest.java @@ -30,7 +30,12 @@ public void testSimpleQuery() public void testSimpleBind() { - assertEquals("select $1", reparse("select ?", true, false, true)); + assertEquals("select $1", reparse("select ?", true, true, true)); + } + + public void testUnquotedQuestionmark() + { + assertEquals("select '{\"key\": \"val\"}'::jsonb ? 'key'", reparse("select '{\"key\": \"val\"}'::jsonb ? 'key'", true, false, true)); } public void testQuotedQuestionmark() @@ -40,7 +45,7 @@ public void testQuotedQuestionmark() public void testDoubleQuestionmark() { - assertEquals("select '?', $1 ?=> $2", reparse("select '?', ? ??=> ?", true, false, true)); + assertEquals("select '?', $1 ?=> $2", reparse("select '?', ? ??=> ?", true, true, true)); } public void testCompositeBasic() @@ -50,7 +55,7 @@ public void testCompositeBasic() public void testCompositeWithBinds() { - assertEquals("select $1;/*cut*/\n select $1", reparse("select ?; select ?", true, false, true)); + assertEquals("select $1;/*cut*/\n select $1", reparse("select ?; select ?", true, true, true)); } public void testTrailingSemicolon()