Search in sources :

Example 1 with BindMarkersFactory

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

the class NamedParameterUtilsUnitTests method multipleEqualParameterReferencesBindsNullOnce.

@Test
public void multipleEqualParameterReferencesBindsNullOnce() {
    String sql = "SELECT * FROM person where name = :id or lastname = :id";
    BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
    PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, factory, new MapBindParameterSource(Collections.singletonMap("id", Parameter.empty(String.class))));
    assertThat(operation.toQuery()).isEqualTo("SELECT * FROM person where name = $0 or lastname = $0");
    operation.bindTo(new BindTarget() {

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

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

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

        @Override
        public void bindNull(int index, Class<?> type) {
            assertThat(index).isEqualTo(0);
            assertThat(type).isEqualTo(String.class);
        }
    });
}
Also used : BindMarkersFactory(org.springframework.r2dbc.core.binding.BindMarkersFactory) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) Test(org.junit.jupiter.api.Test)

Example 2 with BindMarkersFactory

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

the class NamedParameterUtilsUnitTests method multipleEqualParameterReferencesForAnonymousMarkersBindsValueMultipleTimes.

@Test
public void multipleEqualParameterReferencesForAnonymousMarkersBindsValueMultipleTimes() {
    String sql = "SELECT * FROM person where name = :id or lastname = :id";
    BindMarkersFactory factory = BindMarkersFactory.anonymous("?");
    PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, factory, new MapBindParameterSource(Collections.singletonMap("id", Parameter.from("foo"))));
    assertThat(operation.toQuery()).isEqualTo("SELECT * FROM person where name = ? or lastname = ?");
    Map<Integer, Object> bindValues = new LinkedHashMap<>();
    operation.bindTo(new BindTarget() {

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

        @Override
        public void bind(int index, Object value) {
            bindValues.put(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(bindValues).hasSize(2).containsEntry(0, "foo").containsEntry(1, "foo");
}
Also used : LinkedHashMap(java.util.LinkedHashMap) BindMarkersFactory(org.springframework.r2dbc.core.binding.BindMarkersFactory) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) Test(org.junit.jupiter.api.Test)

Example 3 with BindMarkersFactory

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

the class NamedParameterUtilsUnitTests method multipleEqualParameterReferencesBindsValueOnce.

@Test
public void multipleEqualParameterReferencesBindsValueOnce() {
    String sql = "SELECT * FROM person where name = :id or lastname = :id";
    BindMarkersFactory factory = BindMarkersFactory.indexed("$", 0);
    PreparedOperation<String> operation = NamedParameterUtils.substituteNamedParameters(sql, factory, new MapBindParameterSource(Collections.singletonMap("id", Parameter.from("foo"))));
    assertThat(operation.toQuery()).isEqualTo("SELECT * FROM person where name = $0 or lastname = $0");
    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).isEqualTo(0);
            assertThat(value).isEqualTo("foo");
        }

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

        @Override
        public void bindNull(int index, Class<?> type) {
            throw new UnsupportedOperationException();
        }
    });
}
Also used : BindMarkersFactory(org.springframework.r2dbc.core.binding.BindMarkersFactory) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) Test(org.junit.jupiter.api.Test)

Example 4 with BindMarkersFactory

use of org.springframework.r2dbc.core.binding.BindMarkersFactory 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)

Example 5 with BindMarkersFactory

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

the class DefaultDatabaseClientBuilder method build.

@Override
public DatabaseClient build() {
    Assert.notNull(this.connectionFactory, "ConnectionFactory must not be null");
    BindMarkersFactory bindMarkers = this.bindMarkers;
    if (bindMarkers == null) {
        if (this.namedParameters) {
            bindMarkers = BindMarkersFactoryResolver.resolve(this.connectionFactory);
        } else {
            bindMarkers = BindMarkersFactory.anonymous("?");
        }
    }
    return new DefaultDatabaseClient(bindMarkers, this.connectionFactory, this.executeFunction, this.namedParameters);
}
Also used : BindMarkersFactory(org.springframework.r2dbc.core.binding.BindMarkersFactory)

Aggregations

BindMarkersFactory (org.springframework.r2dbc.core.binding.BindMarkersFactory)5 Test (org.junit.jupiter.api.Test)4 BindTarget (org.springframework.r2dbc.core.binding.BindTarget)4 LinkedHashMap (java.util.LinkedHashMap)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1