|
16 | 16 |
|
17 | 17 | package org.springframework.r2dbc.core;
|
18 | 18 |
|
| 19 | +import java.util.Collections; |
19 | 20 | import java.util.HashMap;
|
20 | 21 | import java.util.List;
|
21 | 22 | import java.util.Map;
|
@@ -411,6 +412,64 @@ void multipleEqualParameterReferencesBindsNullOnce() {
|
411 | 412 | .containsEntry(0, Parameters.in(String.class));
|
412 | 413 | }
|
413 | 414 |
|
| 415 | + @Test |
| 416 | + void multipleInParameterReferencesBindsMultiple(){ |
| 417 | + List<String> userIds = List.of("user1", "user2", "user3"); |
| 418 | + String sql = """ |
| 419 | + SELECT * FROM PERSON |
| 420 | + WHERE name IN (:ids) |
| 421 | + GROUP BY address |
| 422 | + UNION |
| 423 | + SELECT * FROM PERSON |
| 424 | + WHERE name NOT IN (:ids) |
| 425 | + """; |
| 426 | + |
| 427 | + BindMarkersFactory factory = BindMarkersFactory.anonymous("?"); |
| 428 | + |
| 429 | + PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters( |
| 430 | + sql, factory, new MapBindParameterSource( |
| 431 | + Collections.singletonMap("ids", Parameters.in(userIds)))); |
| 432 | + |
| 433 | + assertThat(operation.toQuery()).isEqualTo( |
| 434 | + """ |
| 435 | + SELECT * FROM PERSON |
| 436 | + WHERE name IN (?, ?, ?) |
| 437 | + GROUP BY address |
| 438 | + UNION |
| 439 | + SELECT * FROM PERSON |
| 440 | + WHERE name NOT IN (?, ?, ?) |
| 441 | + """); |
| 442 | + final String[] expectedValues = {"user1", "user2", "user3", "user1", "user2", "user3"}; |
| 443 | + final int[] bindingCount = {0}; |
| 444 | + final boolean[] isBinding = new boolean[6]; |
| 445 | + final String[] bindingValue = new String[6]; |
| 446 | + operation.bindTo(new BindTarget() { |
| 447 | + |
| 448 | + @Override |
| 449 | + public void bind(String identifier, Object value) { |
| 450 | + throw new UnsupportedOperationException(); |
| 451 | + } |
| 452 | + @Override |
| 453 | + public void bind(int index, Object value) { |
| 454 | + bindingCount[0]++; |
| 455 | + isBinding[index] = true; |
| 456 | + bindingValue[index] = (String) value; |
| 457 | + } |
| 458 | + @Override |
| 459 | + public void bindNull(String identifier, Class<?> type) { |
| 460 | + throw new UnsupportedOperationException(); |
| 461 | + } |
| 462 | + @Override |
| 463 | + public void bindNull(int index, Class<?> type) { |
| 464 | + throw new UnsupportedOperationException(); |
| 465 | + } |
| 466 | + }); |
| 467 | + assertThat(bindingCount[0]).isEqualTo(6); |
| 468 | + assertThat(isBinding).containsExactly(true, true, true, true, true, true); |
| 469 | + assertThat(bindingValue).containsExactly(expectedValues); |
| 470 | + } |
| 471 | + |
| 472 | + |
414 | 473 |
|
415 | 474 | private static String expand(ParsedSql sql) {
|
416 | 475 | return NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS,
|
|
0 commit comments