Skip to content

Commit d552ec1

Browse files
committed
spring-projects#282 - Implement conjunction of conditions
1 parent 497e604 commit d552ec1

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

Diff for: src/main/java/org/springframework/data/r2dbc/repository/query/R2dbcQueryCreator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected Condition create(Part part, Iterator<Object> iterator) {
8080

8181
@Override
8282
protected Condition and(Part part, Condition condition, Iterator<Object> iterator) {
83-
throw new UnsupportedOperationException();
83+
return condition.and(conditionFactory.createCondition(part));
8484
}
8585

8686
@Override

Diff for: src/test/java/org/springframework/data/r2dbc/repository/query/PartTreeR2dbcQueryIntegrationTests.java

+25-8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
*/
5151
@RunWith(MockitoJUnitRunner.class)
5252
public class PartTreeR2dbcQueryIntegrationTests {
53+
private static final String TABLE = "users";
54+
private static final String ALL_FIELDS = TABLE + ".id, " + TABLE + ".first_name, " + TABLE + ".last_name";
55+
5356
@Mock
5457
private ConnectionFactory connectionFactory;
5558
@Mock
@@ -82,9 +85,9 @@ public void createsQueryToFindAllEntitiesByStringAttribute() throws Exception {
8285
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByFirstName", String.class);
8386
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
8487
dataAccessStrategy);
85-
BindableQuery bindableQuery = r2dbcQuery.createQuery(getAccessor(queryMethod, new Object[]{"Matthews"}));
86-
assertThat(bindableQuery.get())
87-
.isEqualTo("SELECT users.id, users.first_name FROM users WHERE users.first_name = ?");
88+
BindableQuery bindableQuery = r2dbcQuery.createQuery(getAccessor(queryMethod, new Object[]{"John"}));
89+
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name = ?";
90+
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
8891
}
8992

9093
@Test
@@ -93,18 +96,29 @@ public void createsQueryWithIsNullCondition() throws Exception {
9396
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
9497
dataAccessStrategy);
9598
BindableQuery bindableQuery = r2dbcQuery.createQuery((getAccessor(queryMethod, new Object[]{null})));
96-
assertThat(bindableQuery.get())
97-
.isEqualTo("SELECT users.id, users.first_name FROM users WHERE users.first_name IS NULL");
99+
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name IS NULL";
100+
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
98101
}
99102

100103
@Test
101104
public void createsQueryWithLimitForExistsProjection() throws Exception {
102105
R2dbcQueryMethod queryMethod = getQueryMethod("existsByFirstName", String.class);
103106
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
104107
dataAccessStrategy);
105-
BindableQuery query = r2dbcQuery.createQuery((getAccessor(queryMethod, new Object[]{"Matthews"})));
106-
assertThat(query.get())
107-
.isEqualTo("SELECT users.id FROM users WHERE users.first_name = ? LIMIT 1");
108+
BindableQuery query = r2dbcQuery.createQuery((getAccessor(queryMethod, new Object[]{"John"})));
109+
String expectedSql = "SELECT " + TABLE + ".id FROM " + TABLE + " WHERE " + TABLE + ".first_name = ? LIMIT 1";
110+
assertThat(query.get()).isEqualTo(expectedSql);
111+
}
112+
113+
@Test
114+
public void createsQueryToFindAllEntitiesByTwoStringAttributes() throws Exception {
115+
R2dbcQueryMethod queryMethod = getQueryMethod("findByLastNameAndFirstName", String.class, String.class);
116+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
117+
dataAccessStrategy);
118+
BindableQuery bindableQuery = r2dbcQuery.createQuery(getAccessor(queryMethod, new Object[]{"Doe", "John"}));
119+
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE
120+
+ " WHERE " + TABLE + ".last_name = ? AND " + TABLE + ".first_name = ?";
121+
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
108122
}
109123

110124
private R2dbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
@@ -120,6 +134,8 @@ private RelationalParametersParameterAccessor getAccessor(R2dbcQueryMethod query
120134
private interface UserRepository extends Repository<User, Long> {
121135
Flux<User> findAllByFirstName(String firstName);
122136

137+
Flux<User> findByLastNameAndFirstName(String lastName, String firstName);
138+
123139
Mono<Boolean> existsByFirstName(String firstName);
124140
}
125141

@@ -129,5 +145,6 @@ private static class User {
129145
@Id
130146
private Long id;
131147
private String firstName;
148+
private String lastName;
132149
}
133150
}

0 commit comments

Comments
 (0)