Skip to content

Commit 65d52a4

Browse files
committed
Correctly process Postgresql ?| and ?& operator
Issue: SPR-15382
1 parent 3c8fc46 commit 65d52a4

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/namedparam/NamedParameterUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,8 +153,8 @@ public static ParsedSql parseSqlStatement(final String sql) {
153153
}
154154
if (c == '?') {
155155
int j = i + 1;
156-
if (j < statement.length && statement[j] == '?') {
157-
// Postgres-style "??" operator should be skipped
156+
if (j < statement.length && (statement[j] == '?' || statement[j] == '|' || statement[j] == '&')) {
157+
// Postgres-style "??", "?|", "?&" operator should be skipped
158158
i = i + 2;
159159
continue;
160160
}

spring-jdbc/src/test/java/org/springframework/jdbc/core/namedparam/NamedParameterUtilsTests.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -70,7 +70,7 @@ public void substituteNamedParameters() {
7070

7171
@Test
7272
public void convertParamMapToArray() {
73-
Map<String, String> paramMap = new HashMap<String, String>();
73+
Map<String, String> paramMap = new HashMap<>();
7474
paramMap.put("a", "a");
7575
paramMap.put("b", "b");
7676
paramMap.put("c", "c");
@@ -189,6 +189,26 @@ public void parseSqlStatementWithPostgresContainedOperator() throws Exception {
189189
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
190190
}
191191

192+
@Test // SPR-15382
193+
public void parseSqlStatementWithPostgresAnyArrayStringsExistsOperator() throws Exception {
194+
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
195+
String sql = "select '[\"3\", \"11\"]'::jsonb ?| '{1,3,11,12,17}'::text[]";
196+
197+
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
198+
assertEquals(0, parsedSql.getTotalParameterCount());
199+
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
200+
}
201+
202+
@Test // SPR-15382
203+
public void parseSqlStatementWithPostgresAllArrayStringsExistsOperator() throws Exception {
204+
String expectedSql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND ? = 'Back in Black'";
205+
String sql = "select '[\"3\", \"11\"]'::jsonb ?& '{1,3,11,12,17}'::text[] AND :album = 'Back in Black'";
206+
207+
ParsedSql parsedSql = NamedParameterUtils.parseSqlStatement(sql);
208+
assertEquals(1, parsedSql.getTotalParameterCount());
209+
assertEquals(expectedSql, NamedParameterUtils.substituteNamedParameters(parsedSql, null));
210+
}
211+
192212
@Test // SPR-7476
193213
public void parseSqlStatementWithEscapedColon() throws Exception {
194214
String expectedSql = "select '0\\:0' as a, foo from bar where baz < DATE(? 23:59:59) and baz = ?";

0 commit comments

Comments
 (0)