|
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; |
@@ -497,6 +498,66 @@ public void bindNull(int index, Class<?> type) { |
497 | 498 | .containsEntry(0, Parameters.in(String.class)); |
498 | 499 | } |
499 | 500 |
|
| 501 | + @Test |
| 502 | + void multipleInParameterReferencesBindsMultiple(){ |
| 503 | + List<String> userIds = List.of("user1", "user2", "user3"); |
| 504 | + String sql = """ |
| 505 | + SELECT * FROM PERSON |
| 506 | + WHERE name IN (:ids) |
| 507 | + GROUP BY address |
| 508 | +
|
| 509 | + UNION |
| 510 | +
|
| 511 | + SELECT * FROM PERSON |
| 512 | + WHERE name NOT IN (:ids) |
| 513 | + """; |
| 514 | + |
| 515 | + BindMarkersFactory factory = BindMarkersFactory.anonymous("?"); |
| 516 | + |
| 517 | + PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters( |
| 518 | + sql, factory, new MapBindParameterSource( |
| 519 | + Collections.singletonMap("ids", Parameters.in(userIds)))); |
| 520 | + |
| 521 | + assertThat(operation.toQuery()).isEqualTo( |
| 522 | + """ |
| 523 | + SELECT * FROM PERSON |
| 524 | + WHERE name IN (?, ?, ?) |
| 525 | + GROUP BY address |
| 526 | +
|
| 527 | + UNION |
| 528 | +
|
| 529 | + SELECT * FROM PERSON |
| 530 | + WHERE name NOT IN (?, ?, ?) |
| 531 | + """); |
| 532 | + final String[] expectedValues = {"user1", "user2", "user3", "user1", "user2", "user3"}; |
| 533 | + final int[] bindingCount = {0}; |
| 534 | + final boolean[] isBinding = new boolean[6]; |
| 535 | + final String[] bindingValue = new String[6]; |
| 536 | + operation.bindTo(new BindTarget() { |
| 537 | + |
| 538 | + @Override |
| 539 | + public void bind(String identifier, Object value) { |
| 540 | + throw new UnsupportedOperationException(); |
| 541 | + } |
| 542 | + @Override |
| 543 | + public void bind(int index, Object value) { |
| 544 | + bindingCount[0]++; |
| 545 | + isBinding[index] = true; |
| 546 | + bindingValue[index] = (String) value; |
| 547 | + } |
| 548 | + @Override |
| 549 | + public void bindNull(String identifier, Class<?> type) { |
| 550 | + throw new UnsupportedOperationException(); |
| 551 | + } |
| 552 | + @Override |
| 553 | + public void bindNull(int index, Class<?> type) { |
| 554 | + throw new UnsupportedOperationException(); |
| 555 | + } |
| 556 | + }); |
| 557 | + assertThat(bindingCount[0]).isEqualTo(6); |
| 558 | + assertThat(isBinding).containsExactly(true, true, true, true, true, true); |
| 559 | + assertThat(bindingValue).containsExactly(expectedValues); |
| 560 | + } |
500 | 561 |
|
501 | 562 | private static String expand(ParsedSql sql) { |
502 | 563 | return NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS, |
|
0 commit comments