DATACMNS-516 - Add support for limiting the query result in the query derivation mechanism.

We can now detect whether a given query method wants to limit the query results based on two new supported prefixes (findFirstBy…(…) and findFirstKBy…(…)) where "First" part can also be replaced by "Top" and an following optional K (which must be a decimal int) that denotes the maximal number of result rows to be retuned by the query.

This can be used by the stores to allow queries like:

- T findFirstBy…OrderBy…Asc -> smallest
- T findFirstBy…OrderBy…Desc -> greatest
- T findTopBy…OrderBy…Desc -> greatest
- T findFirstBy…(Sort sort) -> general purpose
- T findTopBy…(Sort sort) -> general purpose
- List<T> findFirstKBy…OrderBy…Asc -> smallest K
- List<T> findFirstKBy…OrderBy…Desc -> biggest K
- List<T> findFirstKBy…(Sort sort) -> general purpose TOP K
- List<T> findTopKBy…(Sort sort) -> general purpose TOP K

The limiting expressions also support the Distinct expression. Also, for the queries limiting the result set to one instance, wrapping the result into an Optional is supported.

If pagination or slicing is applied to a limiting query pagination (and the calculation of the number of pages available) is happening within the limited result.

Original pull request: #85.
This commit is contained in:
Thomas Darimont
2014-06-05 12:18:46 +02:00
committed by Oliver Gierke
parent d41da583e9
commit 1697c77f31
2 changed files with 166 additions and 3 deletions

View File

@@ -553,6 +553,118 @@ public class PartTreeUnitTests {
assertThat(tree.isDelete(), is(true));
}
/**
* @see DATACMNS-516
*/
@Test
public void disablesFindFirstKImplicitIfNotPresent() {
assertLimiting("findByLastname", User.class, false, null);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindFirstImplicit() {
assertLimiting("findFirstByLastname", User.class, true, 1);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindFirst1Explicit() {
assertLimiting("findFirstByLastname", User.class, true, 1);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindFirstKExplicit() {
assertLimiting("findFirst10ByLastname", User.class, true, 10);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindFirstKUsersExplicit() {
assertLimiting("findFirst10UsersByLastname", User.class, true, 10);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindFirstKDistinctUsersExplicit() {
assertLimiting("findFirst10DistinctUsersByLastname", User.class, true, 10, true);
assertLimiting("findDistinctFirst10UsersByLastname", User.class, true, 10, true);
assertLimiting("findFirst10UsersDistinctByLastname", User.class, true, 10, true);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindTopImplicit() {
assertLimiting("findTopByLastname", User.class, true, 1);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindTop1Explicit() {
assertLimiting("findTop1ByLastname", User.class, true, 1);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindTopKExplicit() {
assertLimiting("findTop10ByLastname", User.class, true, 10);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindTopKUsersExplicit() {
assertLimiting("findTop10UsersByLastname", User.class, true, 10);
}
/**
* @see DATACMNS-516
*/
@Test
public void identifiesFindTopKDistinctUsersExplicit() {
assertLimiting("findTop10DistinctUsersByLastname", User.class, true, 10, true);
assertLimiting("findDistinctTop10UsersByLastname", User.class, true, 10, true);
assertLimiting("findTop10UsersDistinctByLastname", User.class, true, 10, true);
}
@Test
public void shouldNotSupportLimitingCountQueries() {
assertLimiting("countFirst10DistinctUsersByLastname", User.class, false, null, true);
assertLimiting("countTop10DistinctUsersByLastname", User.class, false, null, true);
}
private static void assertLimiting(String methodName, Class<?> entityType, boolean limiting, Integer maxResults) {
assertLimiting(methodName, entityType, limiting, maxResults, false);
}
private static void assertLimiting(String methodName, Class<?> entityType, boolean limiting, Integer maxResults,
boolean distinct) {
PartTree tree = new PartTree(methodName, entityType);
assertThat(tree.isLimiting(), is(limiting));
assertThat(tree.getMaxResults(), is(maxResults));
assertThat(tree.isDistinct(), is(distinct));
}
private static void assertType(Iterable<String> sources, Type type, String property) {
assertType(sources, type, property, 1, true);
}