NamedParameterUtils skips Postgres-style ?? operator

Issue: SPR-13582
This commit is contained in:
Juergen Hoeller 2015-10-26 22:47:12 +01:00
parent 5386e8a9e9
commit 11d1f050e9
2 changed files with 29 additions and 40 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -103,7 +103,7 @@ public abstract class NamedParameterUtils {
if (c == ':' || c == '&') { if (c == ':' || c == '&') {
int j = i + 1; int j = i + 1;
if (j < statement.length && statement[j] == ':' && c == ':') { if (j < statement.length && statement[j] == ':' && c == ':') {
// Postgres-style "::" casting operator - to be skipped. // Postgres-style "::" casting operator should be skipped
i = i + 2; i = i + 2;
continue; continue;
} }
@ -144,7 +144,7 @@ public abstract class NamedParameterUtils {
if (c == '\\') { if (c == '\\') {
int j = i + 1; int j = i + 1;
if (j < statement.length && statement[j] == ':') { if (j < statement.length && statement[j] == ':') {
// this is an escaped : and should be skipped // escaped ":" should be skipped
sqlToUse = sqlToUse.substring(0, i - escapes) + sqlToUse.substring(i - escapes + 1); sqlToUse = sqlToUse.substring(0, i - escapes) + sqlToUse.substring(i - escapes + 1);
escapes++; escapes++;
i = i + 2; i = i + 2;
@ -152,6 +152,12 @@ public abstract class NamedParameterUtils {
} }
} }
if (c == '?') { if (c == '?') {
int j = i + 1;
if (j < statement.length && statement[j] == '?') {
// Postgres-style "??" operator should be skipped
i = i + 2;
continue;
}
unnamedParameterCount++; unnamedParameterCount++;
totalParameterCount++; totalParameterCount++;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,6 +30,7 @@ import static org.junit.Assert.*;
* @author Thomas Risberg * @author Thomas Risberg
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rick Evans * @author Rick Evans
* @author Artur Geraschenko
*/ */
public class NamedParameterUtilsTests { public class NamedParameterUtilsTests {
@ -137,10 +138,7 @@ public class NamedParameterUtilsTests {
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null)); assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
} }
/* @Test // SPR-4789
* SPR-4789
*/
@Test
public void parseSqlContainingComments() { public void parseSqlContainingComments() {
String sql1 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz -- :xx XX\n"; String sql1 = "/*+ HINT */ xxx /* comment ? */ :a yyyy :b :c :a zzzzz -- :xx XX\n";
ParsedSql psql1 = NamedParameterUtils.parseSqlStatement(sql1); ParsedSql psql1 = NamedParameterUtils.parseSqlStatement(sql1);
@ -174,10 +172,7 @@ public class NamedParameterUtilsTests {
NamedParameterUtils.substituteNamedParameters(psql4, new MapSqlParameterSource(parameters))); NamedParameterUtils.substituteNamedParameters(psql4, new MapSqlParameterSource(parameters)));
} }
/* @Test // SPR-4612
* SPR-4612
*/
@Test
public void parseSqlStatementWithPostgresCasting() throws Exception { public void parseSqlStatementWithPostgresCasting() throws Exception {
String expectedSql = "select 'first name' from artists where id = ? and birth_date=?::timestamp"; String expectedSql = "select 'first name' from artists where id = ? and birth_date=?::timestamp";
String sql = "select 'first name' from artists where id = :id and birth_date=:birthDate::timestamp"; String sql = "select 'first name' from artists where id = :id and birth_date=:birthDate::timestamp";
@ -185,10 +180,16 @@ public class NamedParameterUtilsTests {
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null)); assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
} }
/* @Test // SPR-13582
* SPR-7476 public void parseSqlStatementWithPostgresContainedOperator() throws Exception {
*/ String expectedSql = "select 'first name' from artists where info->'stat'->'albums' = ?? ? and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
@Test String sql = "select 'first name' from artists where info->'stat'->'albums' = ?? :album and '[\"1\",\"2\",\"3\"]'::jsonb ?? '4'";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertEquals(1, parsedSql.getTotalParameterCount());
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
}
@Test // SPR-7476
public void parseSqlStatementWithEscapedColon() throws Exception { public void parseSqlStatementWithEscapedColon() throws Exception {
String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE(? 23:59:59) and baz = ?"; String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE(? 23:59:59) and baz = ?";
String sql = "select '0\\:0' as a, foo from bar where baz < DATE(:p1 23\\:59\\:59) and baz = :p2"; String sql = "select '0\\:0' as a, foo from bar where baz < DATE(:p1 23\\:59\\:59) and baz = :p2";
@ -201,10 +202,7 @@ public class NamedParameterUtilsTests {
assertEquals(expectedSql, finalSql); assertEquals(expectedSql, finalSql);
} }
/* @Test // SPR-7476
* SPR-7476
*/
@Test
public void parseSqlStatementWithBracketDelimitedParameterNames() throws Exception { public void parseSqlStatementWithBracketDelimitedParameterNames() throws Exception {
String expectedSql = "select foo from bar where baz = b??z"; String expectedSql = "select foo from bar where baz = b??z";
String sql = "select foo from bar where baz = b:{p1}:{p2}z"; String sql = "select foo from bar where baz = b:{p1}:{p2}z";
@ -217,10 +215,7 @@ public class NamedParameterUtilsTests {
assertEquals(expectedSql, finalSql); assertEquals(expectedSql, finalSql);
} }
/* @Test // SPR-7476
* SPR-7476
*/
@Test
public void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() throws Exception { public void parseSqlStatementWithEmptyBracketsOrBracketsInQuotes() throws Exception {
String expectedSql = "select foo from bar where baz = b:{}z"; String expectedSql = "select foo from bar where baz = b:{}z";
String sql = "select foo from bar where baz = b:{}z"; String sql = "select foo from bar where baz = b:{}z";
@ -238,40 +233,28 @@ public class NamedParameterUtilsTests {
assertEquals(expectedSql2, finalSql2); assertEquals(expectedSql2, finalSql2);
} }
/* @Test // SPR-2544
* SPR-2544
*/
@Test
public void parseSqlStatementWithLogicalAnd() { public void parseSqlStatementWithLogicalAnd() {
String expectedSql = "xxx & yyyy"; String expectedSql = "xxx & yyyy";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(expectedSql); ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(expectedSql);
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null)); assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
} }
/* @Test // SPR-2544
* SPR-2544
*/
@Test
public void substituteNamedParametersWithLogicalAnd() throws Exception { public void substituteNamedParametersWithLogicalAnd() throws Exception {
String expectedSql = "xxx & yyyy"; String expectedSql = "xxx & yyyy";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource()); String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertEquals(expectedSql, newSql); assertEquals(expectedSql, newSql);
} }
/* @Test // SPR-3173
* SPR-3173
*/
@Test
public void variableAssignmentOperator() throws Exception { public void variableAssignmentOperator() throws Exception {
String expectedSql = "x := 1"; String expectedSql = "x := 1";
String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource()); String newSql = NamedParameterUtils.substituteNamedParameters(expectedSql, new MapSqlParameterSource());
assertEquals(expectedSql, newSql); assertEquals(expectedSql, newSql);
} }
/* @Test // SPR-8280
* SPR-8280
*/
@Test
public void parseSqlStatementWithQuotedSingleQuote() { public void parseSqlStatementWithQuotedSingleQuote() {
String sql = "SELECT ':foo'':doo', :xxx FROM DUAL"; String sql = "SELECT ':foo'':doo', :xxx FROM DUAL";
ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql); ParsedSql psql = NamedParameterUtils.parseSqlStatement(sql);