Skip to content
Permalink
Browse files

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.
  • Loading branch information
Laurenz Albe
Laurenz Albe committed Nov 20, 2015
1 parent 3d30a4c commit 39d510c096cf435ff0f47bb510ae893a0c2b96d2
Showing with 18 additions and 11 deletions.
  1. +10 −8 org/postgresql/core/Parser.java
  2. +8 −3 org/postgresql/test/jdbc3/CompositeQueryParseTest.java
@@ -83,21 +83,23 @@
break;

case '?':
if (!withParameters)
break;

nativeSql.append(aChars, fragmentStart, i - fragmentStart);
if (i + 1 < aChars.length && aChars[i + 1] == '?') /* replace ?? with ? */
{
nativeSql.append('?');
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;
@@ -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()

0 comments on commit 39d510c

Please sign in to comment.
You can’t perform that action at this time.