Correctly process Postgresql ?| and ?& operator

Issue: SPR-15382
This commit is contained in:
Juergen Hoeller 2017-04-13 15:59:07 +02:00
parent 3c8fc46568
commit 65d52a4b97
2 changed files with 25 additions and 5 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -153,8 +153,8 @@ public abstract class NamedParameterUtils {
}
if (c == '?') {
int j = i + 1;
if (j < statement.length && statement[j] == '?') {
// Postgres-style "??" operator should be skipped
if (j < statement.length && (statement[j] == '?' || statement[j] == '|' || statement[j] == '&')) {
// Postgres-style "??", "?|", "?&" operator should be skipped
i = i + 2;
continue;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -70,7 +70,7 @@ public class NamedParameterUtilsTests {
@Test
public void convertParamMapToArray() {
Map<String, String> paramMap = new HashMap<String, String>();
Map<String, String> paramMap = new HashMap<>();
paramMap.put("a", "a");
paramMap.put("b", "b");
paramMap.put("c", "c");
@ -189,6 +189,26 @@ public class NamedParameterUtilsTests {
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
}
@Test // SPR-15382
public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() throws Exception {
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
String sql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertEquals(0, parsedSql.getTotalParameterCount());
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
}
@Test // SPR-15382
public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() throws Exception {
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND ? = 'Back in Black'";
String sql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND :album = 'Back in Black'";
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
assertEquals(1, parsedSql.getTotalParameterCount());
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
}
@Test // SPR-7476
public void parseSqlStatementWithEscapedColon() throws Exception {
String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE(? 23:59:59) and baz = ?";