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);
}
});
}
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");
}
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");
}
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();
}
});
}
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");
}
Aggregations