Skip to content

Commit

Permalink
Allow both single and double question marks in simple statements
Browse files Browse the repository at this point in the history
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 committed Nov 20, 2015
1 parent 3d30a4c commit 39d510c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
18 changes: 10 additions & 8 deletions org/postgresql/core/Parser.java
Expand Up @@ -83,21 +83,23 @@ public static List<NativeQuery> 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 ? */
{
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;
Expand Down
11 changes: 8 additions & 3 deletions org/postgresql/test/jdbc3/CompositeQueryParseTest.java
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 39d510c

Please sign in to comment.