|
31 | 31 | import org.springframework.data.r2dbc.dialect.PostgresDialect;
|
32 | 32 | import org.springframework.data.r2dbc.dialect.SqlServerDialect;
|
33 | 33 | import org.springframework.data.r2dbc.mapping.SettableValue;
|
| 34 | +import org.springframework.util.LinkedMultiValueMap; |
| 35 | +import org.springframework.util.MultiValueMap; |
34 | 36 |
|
35 | 37 | /**
|
36 | 38 | * Unit tests for {@link NamedParameterUtils}.
|
@@ -335,6 +337,52 @@ public void bindNull(int index, Class<?> type) {
|
335 | 337 | });
|
336 | 338 | }
|
337 | 339 |
|
| 340 | + @Test // gh-310 |
| 341 | + public void multipleEqualCollectionParameterReferencesBindsValueOnce() { |
| 342 | + |
| 343 | + String sql = "SELECT * FROM person where name IN (:ids) or lastname IN (:ids)"; |
| 344 | + |
| 345 | + BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0); |
| 346 | + |
| 347 | + MultiValueMap<Integer, Object> bindings = new LinkedMultiValueMap<>(); |
| 348 | + |
| 349 | + PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, factory, |
| 350 | + new MapBindParameterSource( |
| 351 | + Collections.singletonMap("ids", SettableValue.from(Arrays.asList("foo", "bar", "baz"))))); |
| 352 | + |
| 353 | + assertThat(operation.toQuery()) |
| 354 | + .isEqualTo("SELECT * FROM person where name IN ($0, $1, $2) or lastname IN ($0, $1, $2)"); |
| 355 | + |
| 356 | + operation.bindTo(new BindTarget() { |
| 357 | + @Override |
| 358 | + public void bind(String identifier, Object value) { |
| 359 | + throw new UnsupportedOperationException(); |
| 360 | + } |
| 361 | + |
| 362 | + @Override |
| 363 | + public void bind(int index, Object value) { |
| 364 | + assertThat(index).isIn(0, 1, 2); |
| 365 | + assertThat(value).isIn("foo", "bar", "baz"); |
| 366 | + |
| 367 | + bindings.add(index, value); |
| 368 | + } |
| 369 | + |
| 370 | + @Override |
| 371 | + public void bindNull(String identifier, Class<?> type) { |
| 372 | + throw new UnsupportedOperationException(); |
| 373 | + } |
| 374 | + |
| 375 | + @Override |
| 376 | + public void bindNull(int index, Class<?> type) { |
| 377 | + throw new UnsupportedOperationException(); |
| 378 | + } |
| 379 | + }); |
| 380 | + |
| 381 | + assertThat(bindings).containsEntry(0, Collections.singletonList("foo")) // |
| 382 | + .containsEntry(1, Collections.singletonList("bar")) // |
| 383 | + .containsEntry(2, Collections.singletonList("baz")); |
| 384 | + } |
| 385 | + |
338 | 386 | @Test // gh-138
|
339 | 387 | public void multipleEqualParameterReferencesForAnonymousMarkersBindsValueMultipleTimes() {
|
340 | 388 |
|
|
0 commit comments