SGF-172 - added support for countBy... methods
This commit is contained in:
@@ -31,4 +31,5 @@ import java.lang.annotation.Target;
|
||||
public @interface Query {
|
||||
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ class GemfireQueryCreator extends AbstractQueryCreator<QueryString, Predicates>
|
||||
|
||||
super(tree);
|
||||
|
||||
this.query = new QueryBuilder(entity);
|
||||
this.query = new QueryBuilder(entity,tree);
|
||||
this.indexes = new IndexProvider();
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.util.Assert;
|
||||
* Base class for GemFire specific {@link RepositoryQuery} implementations.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author David Turanski
|
||||
*/
|
||||
abstract class GemfireRepositoryQuery implements RepositoryQuery {
|
||||
|
||||
@@ -47,4 +48,6 @@ abstract class GemfireRepositoryQuery implements RepositoryQuery {
|
||||
public QueryMethod getQueryMethod() {
|
||||
return this.queryMethod;
|
||||
}
|
||||
|
||||
protected abstract boolean isCountQuery();
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ public class PartTreeGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
QueryString query = new GemfireQueryCreator(tree, method.getPersistentEntity()).createQuery(parameterAccessor
|
||||
.getSort());
|
||||
|
||||
RepositoryQuery repositoryQuery = new StringBasedGemfireRepositoryQuery(query.toString(), method, template);
|
||||
RepositoryQuery repositoryQuery = new StringBasedGemfireRepositoryQuery(query.toString(), method, template,isCountQuery());
|
||||
|
||||
return repositoryQuery.execute(prepareStringParameters(parameters));
|
||||
}
|
||||
@@ -102,4 +102,12 @@ public class PartTreeGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.gemfire.repository.query.GemfireRepositoryQuery#isCountQuery()
|
||||
*/
|
||||
@Override
|
||||
protected boolean isCountQuery() {
|
||||
return this.tree.isCountProjection();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,13 @@
|
||||
package org.springframework.data.gemfire.repository.query;
|
||||
|
||||
import org.springframework.data.gemfire.mapping.GemfirePersistentEntity;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author David Turanski
|
||||
*/
|
||||
class QueryBuilder {
|
||||
|
||||
@@ -33,8 +35,8 @@ class QueryBuilder {
|
||||
this.query = source;
|
||||
}
|
||||
|
||||
public QueryBuilder(GemfirePersistentEntity<?> entity) {
|
||||
this(String.format("SELECT * FROM /%s %s", entity.getRegionName(), DEFAULT_ALIAS));
|
||||
public QueryBuilder(GemfirePersistentEntity<?> entity, PartTree tree) {
|
||||
this(String.format(tree.isCountProjection()? "SELECT count(*) FROM /%s %s":"SELECT * FROM /%s %s", entity.getRegionName(), DEFAULT_ALIAS));
|
||||
}
|
||||
|
||||
public QueryString create(Predicate predicate) {
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.gemstone.gemfire.cache.Region;
|
||||
* Value object to work with OQL query strings.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author David Turanski
|
||||
*/
|
||||
class QueryString {
|
||||
|
||||
@@ -56,7 +57,18 @@ class QueryString {
|
||||
* @param domainClass must not be {@literal null}.
|
||||
*/
|
||||
public QueryString(Class<?> domainClass) {
|
||||
this(String.format("SELECT * FROM /%s", domainClass.getSimpleName()));
|
||||
this(domainClass, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@literal SELECT} query for the given domain class.
|
||||
*
|
||||
* @param domainClass must not be {@literal null}.
|
||||
* @param isCountQuery indicates if this is a count query
|
||||
*/
|
||||
public QueryString(Class<?> domainClass, boolean isCountQuery) {
|
||||
this(String
|
||||
.format(isCountQuery ? "SELECT count(*) FROM /%s" : "SELECT * FROM /%s", domainClass.getSimpleName()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -66,7 +78,7 @@ class QueryString {
|
||||
* @param region must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public QueryString forRegion(Class<?> domainClass, Region<?, ?> region) {
|
||||
public QueryString forRegion(Class<?> domainClass, Region<?, ?> region) {
|
||||
return new QueryString(query.replaceAll(REGION_PATTERN, region.getName()));
|
||||
}
|
||||
|
||||
|
||||
@@ -32,11 +32,13 @@ import com.gemstone.gemfire.cache.query.internal.ResultsBag;
|
||||
* {@link GemfireRepositoryQuery} using plain {@link String} based OQL queries.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author David Turanski
|
||||
*/
|
||||
public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
|
||||
private static final String INVALID_EXECUTION = "Paging and modifying queries are not supported!";
|
||||
|
||||
private final boolean isCountProjection;
|
||||
private final QueryString query;
|
||||
private final GemfireQueryMethod method;
|
||||
private final GemfireTemplate template;
|
||||
@@ -49,7 +51,7 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
* @param template must not be {@literal null}.
|
||||
*/
|
||||
public StringBasedGemfireRepositoryQuery(GemfireQueryMethod method, GemfireTemplate template) {
|
||||
this(method.getAnnotatedQuery(), method, template);
|
||||
this(method.getAnnotatedQuery(), method, template, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +63,8 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
* @param method must not be {@literal null}.
|
||||
* @param template must not be {@literal null}.
|
||||
*/
|
||||
public StringBasedGemfireRepositoryQuery(String query, GemfireQueryMethod method, GemfireTemplate template) {
|
||||
public StringBasedGemfireRepositoryQuery(String query, GemfireQueryMethod method, GemfireTemplate template,
|
||||
Boolean isCountProjection) {
|
||||
|
||||
super(method);
|
||||
|
||||
@@ -70,6 +73,11 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
this.query = new QueryString(StringUtils.hasText(query) ? query : method.getAnnotatedQuery());
|
||||
this.method = method;
|
||||
this.template = template;
|
||||
if (isCountProjection == null && StringUtils.hasText(method.getAnnotatedQuery())) {
|
||||
this.isCountProjection = method.getAnnotatedQuery().toLowerCase().contains("count(*)");
|
||||
} else {
|
||||
this.isCountProjection = isCountProjection;
|
||||
}
|
||||
|
||||
if (method.isPageQuery() || method.isModifyingQuery()) {
|
||||
throw new IllegalStateException(INVALID_EXECUTION);
|
||||
@@ -96,7 +104,6 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
if (method.isCollectionQuery()) {
|
||||
return result;
|
||||
} else if (method.isQueryForEntity()) {
|
||||
|
||||
if (result.isEmpty()) {
|
||||
return null;
|
||||
} else if (result.size() == 1) {
|
||||
@@ -104,9 +111,16 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
} else {
|
||||
throw new IncorrectResultSizeDataAccessException(1, result.size());
|
||||
}
|
||||
} else if (isCountProjection) {
|
||||
int count = (Integer) result.iterator().next();
|
||||
|
||||
if (long.class.isAssignableFrom(method.getReturnedObjectType())) {
|
||||
return (long) count;
|
||||
} else {
|
||||
return count;
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException(INVALID_EXECUTION);
|
||||
throw new IllegalStateException("Unsupported query: " + query.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,4 +144,12 @@ public class StringBasedGemfireRepositoryQuery extends GemfireRepositoryQuery {
|
||||
|
||||
return source.getClass().isArray() ? CollectionUtils.arrayToList(source) : Collections.singleton(source);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.gemfire.repository.query.GemfireRepositoryQuery#isCountQuery()
|
||||
*/
|
||||
@Override
|
||||
protected boolean isCountQuery() {
|
||||
return this.isCountProjection;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,7 @@ import com.gemstone.gemfire.cache.Region;
|
||||
* for Gemfire.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author David Turanski
|
||||
*/
|
||||
public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
|
||||
@@ -164,7 +165,7 @@ public class GemfireRepositoryFactory extends RepositoryFactorySupport {
|
||||
String namedQueryName = queryMethod.getNamedQueryName();
|
||||
if (namedQueries.hasQuery(namedQueryName)) {
|
||||
return new StringBasedGemfireRepositoryQuery(namedQueries.getQuery(namedQueryName), queryMethod,
|
||||
template);
|
||||
template,false);
|
||||
}
|
||||
|
||||
return new PartTreeGemfireRepositoryQuery(queryMethod, template);
|
||||
|
||||
Reference in New Issue
Block a user