Skip to content

Commit fd91313

Browse files
committed
DATACMNS-303 - Added support for count projects in query parsing.
PartTree now supports query methods starting with "count…By" and exposes that fact through an isCountProjection() method to be used by query creators upstream
1 parent 31618f9 commit fd91313

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

src/main/java/org/springframework/data/repository/query/parser/PartTree.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2012 the original author or authors.
2+
* Copyright 2008-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@
3737
*/
3838
public class PartTree implements Iterable<OrPart> {
3939

40-
private static final Pattern PREFIX_TEMPLATE = Pattern.compile("^(find|read|get)(\\p{Lu}.*?)??By");
40+
private static final Pattern PREFIX_TEMPLATE = Pattern.compile("^(find|read|get|count)(\\p{Lu}.*?)??By");
4141
private static final String KEYWORD_TEMPLATE = "(%s)(?=\\p{Lu})";
4242

4343
/**
@@ -67,7 +67,7 @@ public PartTree(String source, Class<?> domainClass) {
6767
this.subject = new Subject(null);
6868
this.predicate = new Predicate(source, domainClass);
6969
} else {
70-
this.subject = new Subject(matcher.group(2));
70+
this.subject = new Subject(matcher.group(0));
7171
this.predicate = new Predicate(source.substring(matcher.group().length()), domainClass);
7272
}
7373
}
@@ -94,13 +94,21 @@ public Sort getSort() {
9494
/**
9595
* Returns whether we indicate distinct lookup of entities.
9696
*
97-
* @return <tt>true<tt> if distinct
97+
* @return {@literal true} if distinct
9898
*/
9999
public boolean isDistinct() {
100-
101100
return subject.isDistinct();
102101
}
103102

103+
/**
104+
* Returns whether a count projection shall be applied.
105+
*
106+
* @return
107+
*/
108+
public Boolean isCountProjection() {
109+
return subject.isCountProjection();
110+
}
111+
104112
/**
105113
* Returns an {@link Iterable} of all parts contained in the {@link PartTree}.
106114
*
@@ -141,7 +149,7 @@ public String toString() {
141149

142150
OrderBySource orderBySource = predicate.getOrderBySource();
143151
return String.format("%s%s", StringUtils.collectionToDelimitedString(predicate.nodes, " or "),
144-
(orderBySource == null ? "" : " " + orderBySource));
152+
orderBySource == null ? "" : " " + orderBySource);
145153
}
146154

147155
/**
@@ -198,15 +206,24 @@ public String toString() {
198206
* {@code DistinctUser}.
199207
*
200208
* @author Phil Webb
209+
* @author Oliver Gierke
201210
*/
202211
private static class Subject {
203212

204213
private static final String DISTINCT = "Distinct";
214+
private static final Pattern COUNT_BY_TEMPLATE = Pattern.compile("^count(\\p{Lu}.*?)??By");
205215

206-
private boolean distinct;
216+
private final boolean distinct;
217+
private final boolean count;
207218

208219
public Subject(String subject) {
209-
this.distinct = (subject == null ? false : subject.contains(DISTINCT));
220+
221+
this.distinct = subject == null ? false : subject.contains(DISTINCT);
222+
this.count = subject == null ? false : COUNT_BY_TEMPLATE.matcher(subject).find();
223+
}
224+
225+
public boolean isCountProjection() {
226+
return count;
210227
}
211228

212229
public boolean isDistinct() {
@@ -269,4 +286,5 @@ public OrderBySource getOrderBySource() {
269286
return orderBySource;
270287
}
271288
}
289+
272290
}

src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2010 the original author or authors.
2+
* Copyright 2008-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -318,6 +318,37 @@ public void parsesSpecialCharactersCorrectly() {
318318
assertTrue(tree.getSort().getOrderFor("år").isAscending());
319319
}
320320

321+
/**
322+
* @see DATACMNS-303
323+
*/
324+
@Test
325+
public void identifiesSimpleCountByCorrectly() {
326+
327+
PartTree tree = new PartTree("countByLastname", User.class);
328+
assertThat(tree.isCountProjection(), is(true));
329+
}
330+
331+
/**
332+
* @see DATACMNS-303
333+
*/
334+
@Test
335+
public void identifiesExtendedCountByCorrectly() {
336+
337+
PartTree tree = new PartTree("countUserByLastname", User.class);
338+
assertThat(tree.isCountProjection(), is(true));
339+
}
340+
341+
/**
342+
* @see DATACMNS-303
343+
*/
344+
@Test
345+
public void identifiesCountAndDistinctByCorrectly() {
346+
347+
PartTree tree = new PartTree("countDistinctUserByLastname", User.class);
348+
assertThat(tree.isCountProjection(), is(true));
349+
assertThat(tree.isDistinct(), is(true));
350+
}
351+
321352
private static void assertType(Iterable<String> sources, Type type, String property) {
322353
assertType(sources, type, property, 1, true);
323354
}

0 commit comments

Comments
 (0)