Skip to content

Commit 9479ab2

Browse files
committed
Add a test to bind the collection multiple times.
Signed-off-by: ChanHyeongLee <[email protected]>
1 parent 28f4579 commit 9479ab2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

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

Lines changed: 61 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;
@@ -497,6 +498,66 @@ public void bindNull(int index, Class<?> type) {
497498
.containsEntry(0, Parameters.in(String.class));
498499
}
499500

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+
}
500561

501562
private static String expand(ParsedSql sql) {
502563
return NamedParameterUtils.substituteNamedParameters(sql, INDEXED_MARKERS,

0 commit comments

Comments
 (0)