Skip to content

Commit c5e8654

Browse files
committed
Merge branch '6.2.x'
2 parents a185f15 + 018d3c9 commit c5e8654

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

Diff for: spring-jdbc/src/test/java/org/springframework/jdbc/core/simple/JdbcClientIntegrationTests.java

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.jdbc.core.simple;
1818

19+
import java.util.List;
20+
1921
import org.junit.jupiter.api.AfterEach;
2022
import org.junit.jupiter.api.BeforeEach;
2123
import org.junit.jupiter.api.Test;
@@ -143,6 +145,52 @@ void updateWithGeneratedKeysAndKeyColumnNamesUsingNamedParameters() {
143145
assertUser(expectedId, firstName, lastName);
144146
}
145147

148+
@Test // gh-34768
149+
void selectWithReusedNamedParameter() {
150+
this.jdbcClient.sql(INSERT_WITH_JDBC_PARAMS).params("John", "John").update();
151+
this.jdbcClient.sql(INSERT_WITH_JDBC_PARAMS).params("John", "Smith").update();
152+
this.jdbcClient.sql(INSERT_WITH_JDBC_PARAMS).params("Smith", "Smith").update();
153+
assertNumUsers(4);
154+
155+
String sql = """
156+
select * from users
157+
where
158+
first_name in ('Bogus', :name) or
159+
last_name in (:name, 'Bogus')
160+
order by last_name
161+
""";
162+
163+
List<User> users = this.jdbcClient.sql(sql)
164+
.param("name", "John")
165+
.query(User.class)
166+
.list();
167+
168+
assertThat(users).containsExactly(new User(2, "John", "John"), new User(3, "John", "Smith"));
169+
}
170+
171+
@Test // gh-34768
172+
void selectWithReusedNamedParameterList() {
173+
this.jdbcClient.sql(INSERT_WITH_JDBC_PARAMS).params("John", "John").update();
174+
this.jdbcClient.sql(INSERT_WITH_JDBC_PARAMS).params("John", "Smith").update();
175+
this.jdbcClient.sql(INSERT_WITH_JDBC_PARAMS).params("Smith", "Smith").update();
176+
assertNumUsers(4);
177+
178+
String sql = """
179+
select * from users
180+
where
181+
first_name in (:names) or
182+
last_name in (:names)
183+
order by last_name
184+
""";
185+
186+
List<User> users = this.jdbcClient.sql(sql)
187+
.param("names", List.of("John", "Bogus"))
188+
.query(User.class)
189+
.list();
190+
191+
assertThat(users).containsExactly(new User(2, "John", "John"), new User(3, "John", "Smith"));
192+
}
193+
146194

147195
private void assertNumUsers(long count) {
148196
long numUsers = this.jdbcClient.sql("select count(id) from users").query(Long.class).single();

Diff for: spring-r2dbc/src/test/java/org/springframework/r2dbc/core/AbstractDatabaseClientIntegrationTests.java

+64-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -211,6 +211,69 @@ void shouldEmitGeneratedKey() {
211211
.verifyComplete();
212212
}
213213

214+
@Test // gh-34768
215+
void executeInsertWithReusedNamedParameter() {
216+
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
217+
218+
Lego lego = new Lego(1, 42, "Star Wars", 42);
219+
220+
databaseClient.sql(() -> "INSERT INTO legoset (id, version, name, manual) VALUES(:id, :number, :name, :number)")
221+
.bind("id", lego.id)
222+
.bind("name", lego.name)
223+
.bind("number", lego.version)
224+
.fetch().rowsUpdated()
225+
.as(StepVerifier::create)
226+
.expectNext(1L)
227+
.verifyComplete();
228+
229+
databaseClient.sql("SELECT * FROM legoset")
230+
.mapProperties(Lego.class)
231+
.first()
232+
.as(StepVerifier::create)
233+
.assertNext(actual -> assertThat(actual).isEqualTo(lego))
234+
.verifyComplete();
235+
}
236+
237+
@Test // gh-34768
238+
void executeSelectWithReusedNamedParameterList() {
239+
DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
240+
241+
String insertSql = "INSERT INTO legoset (id, version, name, manual) VALUES(:id, :version, :name, :manual)";
242+
String selectSql = "SELECT * FROM legoset WHERE version IN (:numbers) OR manual IN (:numbers)";
243+
Lego lego = new Lego(1, 42, "Star Wars", 99);
244+
245+
databaseClient.sql(insertSql)
246+
.bind("id", lego.id)
247+
.bind("version", lego.version)
248+
.bind("name", lego.name)
249+
.bind("manual", lego.manual)
250+
.fetch().rowsUpdated()
251+
.as(StepVerifier::create)
252+
.expectNext(1L)
253+
.verifyComplete();
254+
255+
databaseClient.sql(selectSql)
256+
// match version
257+
.bind("numbers", List.of(2, 3, lego.version, 4))
258+
.mapProperties(Lego.class)
259+
.first()
260+
.as(StepVerifier::create)
261+
.assertNext(actual -> assertThat(actual).isEqualTo(lego))
262+
.verifyComplete();
263+
264+
databaseClient.sql(selectSql)
265+
// match manual
266+
.bind("numbers", List.of(2, 3, lego.manual, 4))
267+
.mapProperties(Lego.class)
268+
.first()
269+
.as(StepVerifier::create)
270+
.assertNext(actual -> assertThat(actual).isEqualTo(lego))
271+
.verifyComplete();
272+
}
273+
274+
275+
record Lego(int id, Integer version, String name, Integer manual) {
276+
}
214277

215278
record ParameterRecord(int id, String name, Integer manual) {
216279
}

0 commit comments

Comments
 (0)