Search in sources :

Example 1 with BindTarget

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

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

the class DefaultDatabaseClientUnitTests method shouldApplyPreparedOperation.

@Test
void shouldApplyPreparedOperation() {
    MockResult result = mockSingleColumnResult(MockRow.builder().identified(0, Object.class, "Walter"));
    Statement statement = mockStatementFor("SELECT * FROM person", result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql(new PreparedOperation<String>() {

        @Override
        public String toQuery() {
            return "SELECT * FROM person";
        }

        @Override
        public String getSource() {
            return "SELECT";
        }

        @Override
        public void bindTo(BindTarget target) {
            target.bind("index", "value");
        }
    }).fetch().all().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verify(statement).bind("index", "value");
}
Also used : MockResult(io.r2dbc.spi.test.MockResult) Statement(io.r2dbc.spi.Statement) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 3 with BindTarget

use of org.springframework.r2dbc.core.binding.BindTarget 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 4 with BindTarget

use of org.springframework.r2dbc.core.binding.BindTarget 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 5 with BindTarget

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

the class NamedParameterUtilsUnitTests method shouldBindObjectArray.

@Test
public void shouldBindObjectArray() {
    MapBindParameterSource namedParams = new MapBindParameterSource(new HashMap<>());
    namedParams.addValue("a", Arrays.asList(new Object[] { "Walter", "Heisenberg" }, new Object[] { "Walt Jr.", "Flynn" }));
    BindTarget bindTarget = mock(BindTarget.class);
    PreparedOperation<?> operation = NamedParameterUtils.substituteNamedParameters("xxx :a", BIND_MARKERS, namedParams);
    operation.bindTo(bindTarget);
    verify(bindTarget).bind(0, "Walter");
    verify(bindTarget).bind(1, "Heisenberg");
    verify(bindTarget).bind(2, "Walt Jr.");
    verify(bindTarget).bind(3, "Flynn");
}
Also used : 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