Skip to content

Commit 0543129

Browse files
committed
Add a test to bind the collection multiple times
Signed-off-by: ChanHyeongLee <[email protected]>
1 parent 59299b9 commit 0543129

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

spring-r2dbc/src/test/java/org/springframework/r2dbc/core/NamedParameterUtilsTests.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.r2dbc.core;
1818

19+
import java.util.Collections;
1920
import java.util.HashMap;
2021
import java.util.List;
2122
import java.util.Map;
@@ -411,6 +412,64 @@ void multipleEqualParameterReferencesBindsNullOnce() {
411412
.containsEntry(0, Parameters.in(String.class));
412413
}
413414

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+
414473

415474
private static String expand(ParsedSql sql) {
416475
return NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS,

0 commit comments

Comments
 (0)