postgresql-jdbc.git / summary / log / commit / refs
commit 39d510c096cf435ff0f47bb510ae893a0c2b96d2
Author: Laurenz Albe <laurenz.albe@wien.gv.at>
Date: Fri Nov 20 12:34:11 2015 +0000
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 ++++++++++--------
org/postgresql/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 0c1083f3..f69b1815 100644
--- a/org/postgresql/core/Parser.java
+++ b/org/postgresql/core/Parser.java
@@ -83,9 +83,6 @@ public class Parser {
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 class Parser {
i++; // make sure the coming ? is not treated as a bind
} else
{
- if (bindPositions == null)
- bindPositions = new ArrayList<Integer>();
- bindPositions.add(nativeSql.length());
- int bindIndex = bindPositions.size();
- nativeSql.append(NativeQuery.bindName(bindIndex));
+ if (withParameters)
+ {
+ if (bindPositions == null)
+ bindPositions = new ArrayList<Integer>();
+ 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 14b32eaf..7291fe21 100644
--- a/org/postgresql/test/jdbc3/CompositeQueryParseTest.java
+++ b/org/postgresql/test/jdbc3/CompositeQueryParseTest.java
@@ -30,7 +30,12 @@ public class CompositeQueryParseTest extends TestCase {
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 class CompositeQueryParseTest extends TestCase {
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 class CompositeQueryParseTest extends TestCase {
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()
[parent: 3d30a4c76b44]