Skip to content

Commit 7c135fe

Browse files
committed
spring-projects#282 - Add support of 'CONTAINING' conditions
1 parent cca24c8 commit 7c135fe

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public Condition createCondition(Part part) {
9595
}
9696
case STARTING_WITH:
9797
case ENDING_WITH:
98+
case CONTAINING:
9899
case LIKE: {
99100
Expression pathExpression = createPropertyPathExpression(part.getProperty());
100101
BindMarker bindMarker = createBindMarker(parameterMetadataProvider.next(part));

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

+26
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,30 @@ public void prependsLikeOperatorParameterWithPercentSymbolForStartingWithQuery()
307307
verify(bindSpecMock, times(1)).bind(0, "%hn");
308308
}
309309

310+
@Test
311+
public void createsQueryToFindAllEntitiesByStringAttributeContaining() throws Exception {
312+
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByFirstNameContaining", String.class);
313+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
314+
dataAccessStrategy);
315+
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] {"oh"});
316+
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
317+
String expectedSql = "SELECT " + ALL_FIELDS + " FROM " + TABLE + " WHERE " + TABLE + ".first_name LIKE ?";
318+
assertThat(bindableQuery.get()).isEqualTo(expectedSql);
319+
}
320+
321+
@SuppressWarnings({"rawtypes", "unchecked"})
322+
@Test
323+
public void wrapsLikeOperatorParameterWithPercentSymbolsForContainingQuery() throws Exception {
324+
R2dbcQueryMethod queryMethod = getQueryMethod("findAllByFirstNameContaining", String.class);
325+
PartTreeR2dbcQuery r2dbcQuery = new PartTreeR2dbcQuery(queryMethod, databaseClient, r2dbcConverter,
326+
dataAccessStrategy);
327+
RelationalParametersParameterAccessor accessor = getAccessor(queryMethod, new Object[] {"hn"});
328+
BindableQuery bindableQuery = r2dbcQuery.createQuery(accessor);
329+
DatabaseClient.BindSpec bindSpecMock = mock(DatabaseClient.BindSpec.class);
330+
bindableQuery.bind(bindSpecMock);
331+
verify(bindSpecMock, times(1)).bind(0, "%hn%");
332+
}
333+
310334
private R2dbcQueryMethod getQueryMethod(String methodName, Class<?>... parameterTypes) throws Exception {
311335
Method method = UserRepository.class.getMethod(methodName, parameterTypes);
312336
return new R2dbcQueryMethod(method, new DefaultRepositoryMetadata(UserRepository.class),
@@ -351,6 +375,8 @@ private interface UserRepository extends Repository<User, Long> {
351375
Flux<User> findAllByFirstNameStartingWith(String starting);
352376

353377
Flux<User> findAllByFirstNameEndingWith(String ending);
378+
379+
Flux<User> findAllByFirstNameContaining(String containing);
354380
}
355381

356382
@Table("users")

0 commit comments

Comments
 (0)