Search in sources :

Example 6 with BindTarget

use of org.springframework.r2dbc.core.binding.BindTarget in project spring-framework by spring-projects.

the class NamedParameterUtilsUnitTests method multipleEqualCollectionParameterReferencesBindsValueOnce.

@Test
public void multipleEqualCollectionParameterReferencesBindsValueOnce() {
    String sql = "SELECT * FROM person where name IN (:ids) or lastname IN (:ids)";
    BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
    MultiValueMap<Integer, Object> bindings = new LinkedMultiValueMap<>();
    PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, factory, new MapBindParameterSource(Collections.singletonMap("ids", Parameter.from(Arrays.asList("foo", "bar", "baz")))));
    assertThat(operation.toQuery()).isEqualTo("SELECT * FROM person where name IN ($0, $1, $2) or lastname IN ($0, $1, $2)");
    operation.bindTo(new BindTarget() {

        @Override
        public void bind(String identifier, Object value) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void bind(int index, Object value) {
            assertThat(index).isIn(0, 1, 2);
            assertThat(value).isIn("foo", "bar", "baz");
            bindings.add(index, value);
        }

        @Override
        public void bindNull(String identifier, Class<?> type) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void bindNull(int index, Class<?> type) {
            throw new UnsupportedOperationException();
        }
    });
    // 
    assertThat(bindings).containsEntry(0, Collections.singletonList("foo")).containsEntry(1, // 
    Collections.singletonList("bar")).containsEntry(2, Collections.singletonList("baz"));
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) BindMarkersFactory(org.springframework.r2dbc.core.binding.BindMarkersFactory) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 BindTarget (org.springframework.r2dbc.core.binding.BindTarget)6 BindMarkersFactory (org.springframework.r2dbc.core.binding.BindMarkersFactory)4 Statement (io.r2dbc.spi.Statement)1 MockResult (io.r2dbc.spi.test.MockResult)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 StepVerifier (reactor.test.StepVerifier)1