Skip to content

Commit c01a004

Browse files
committed
DATAMONGO-1454 - Derive string query count projection from return type.
1 parent 8779056 commit c01a004

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryMethod.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ public boolean isExistsQuery() {
200200
return unwrappedReturnType.isAssignableFrom(Boolean.class) || unwrappedReturnType.isAssignableFrom(Boolean.TYPE);
201201
}
202202

203+
/**
204+
* Returns whether the query is a count projection by checking the return type.
205+
*
206+
* @return
207+
* @since 1.10
208+
*/
209+
public boolean isCountQuery() {
210+
return unwrappedReturnType.isAssignableFrom(Long.class) || unwrappedReturnType.isAssignableFrom(Long.TYPE)
211+
|| unwrappedReturnType.isAssignableFrom(Number.class);
212+
}
213+
203214
/**
204215
* Returns the {@link Query} annotation that is applied to the method or {@code null} if none available.
205216
*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public StringBasedMongoQuery(String query, MongoQueryMethod method, MongoOperati
9797
this.fieldSpec = BINDING_PARSER.parseAndCollectParameterBindingsFromQueryIntoBindings(
9898
method.getFieldSpecification(), this.fieldSpecParameterBindings);
9999

100-
this.isCountQuery = method.hasAnnotatedQuery() && method.getQueryAnnotation().count();
100+
this.isCountQuery = method.isCountQuery() || (method.hasAnnotatedQuery() && method.getQueryAnnotation().count());
101101
this.isExistsQuery = method.isExistsQuery();
102102
this.isDeleteQuery = method.hasAnnotatedQuery() && method.getQueryAnnotation().delete();
103103

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryMethodUnitTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,17 @@ public void createsMongoQueryMethodWithWrappedExistsProjection() throws Exceptio
235235
assertThat(method.isExistsQuery(), is(true));
236236
}
237237

238+
/**
239+
* @see DATAMONGO-1454
240+
*/
241+
@Test
242+
public void createsMongoQueryMethodWithCountProjection() throws Exception {
243+
244+
MongoQueryMethod method = queryMethod(PersonRepository.class, "countBy");
245+
246+
assertThat(method.isCountQuery(), is(true));
247+
}
248+
238249
private MongoQueryMethod queryMethod(Class<?> repository, String name, Class<?>... parameters) throws Exception {
239250

240251
Method method = repository.getMethod(name, parameters);
@@ -281,6 +292,8 @@ interface PersonRepository extends Repository<User, Long> {
281292
boolean existsBy();
282293

283294
Future<Boolean> existsByFuture();
295+
296+
Integer countBy();
284297
}
285298

286299
interface SampleRepository extends Repository<Contact, Long> {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/StringBasedMongoQueryUnitTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,16 @@ public void shouldSupportExistsProjection() throws Exception {
373373
assertThat(mongoQuery.isExistsQuery(), is(true));
374374
}
375375

376+
/**
377+
* @see DATAMONGO-1454
378+
*/
379+
@Test
380+
public void shouldDeriveCountProjectionFromReturnType() throws Exception {
381+
382+
assertThat(createQueryForMethod("countMethod", String.class).isCountQuery(), is(true));
383+
assertThat(createQueryForMethod("countNumberMethod", String.class).isCountQuery(), is(true));
384+
}
385+
376386
private StringBasedMongoQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
377387

378388
Method method = SampleRepository.class.getMethod(name, parameters);
@@ -408,6 +418,12 @@ private interface SampleRepository extends Repository<Person, Long> {
408418
@Query(value = "{ 'lastname' : ?0 }", delete = true, count = true)
409419
void invalidMethod(String lastname);
410420

421+
@Query(value = "{ 'lastname' : ?0 }")
422+
long countMethod(String lastname);
423+
424+
@Query(value = "{ 'lastname' : ?0 }")
425+
Number countNumberMethod(String lastname);
426+
411427
@Query(value = "?0", fields = "?1")
412428
DBObject findByParameterizedCriteriaAndFields(DBObject criteria, Map<String, Integer> fields);
413429

0 commit comments

Comments
 (0)