Re-refactored repository query execution after polishing of the core query builder API.

Pagination information is set on the query, but this still has to be brought to execution inside the template.
This commit is contained in:
Oliver Gierke
2011-02-03 21:44:47 +00:00
parent 7d948e28f3
commit eafed37283
4 changed files with 39 additions and 38 deletions

View File

@@ -76,20 +76,18 @@ public class MongoQuery implements RepositoryQuery {
SimpleParameterAccessor accessor =
new SimpleParameterAccessor(method.getParameters(), parameters);
Query spec = new Query();
MongoQueryCreator creator =
new MongoQueryCreator(spec, tree, accessor,
template.getConverter());
creator.createQuery();
new MongoQueryCreator(tree, accessor, template.getConverter());
Query query = creator.createQuery();
if (method.isCollectionQuery()) {
return new CollectionExecution().execute(spec);
return new CollectionExecution().execute(query);
} else if (method.isPageQuery()) {
return new PagedExecution(creator, accessor.getPageable())
.execute(spec);
.execute(query);
} else {
return new SingleEntityExecution().execute(spec);
return new SingleEntityExecution().execute(query);
}
}
@@ -161,7 +159,8 @@ public class MongoQuery implements RepositoryQuery {
@SuppressWarnings({ "rawtypes", "unchecked" })
Object execute(Query query) {
int count = getCollectionCursor(query.getQueryObject()).count();
Query countQuery = creator.createQuery();
int count = getCollectionCursor(countQuery.getQueryObject()).count();
List<?> result =
template.find(applyPagination(query, pageable),

View File

@@ -21,9 +21,8 @@ import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.document.mongodb.MongoConverter;
import org.springframework.data.document.mongodb.builder.CriteriaDefinition;
import org.springframework.data.document.mongodb.builder.Criteria;
import org.springframework.data.document.mongodb.builder.QueryDefinition;
import org.springframework.data.document.mongodb.builder.CriteriaDefinition;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.SimpleParameterAccessor;
@@ -42,11 +41,10 @@ import com.mongodb.DBObject;
*
* @author Oliver Gierke
*/
class MongoQueryCreator extends AbstractQueryCreator<Void, Criteria> {
class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
private static final Log LOG = LogFactory.getLog(MongoQueryCreator.class);
private final MongoConverter converter;
private final Query querySpec;
/**
@@ -56,11 +54,10 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, Criteria> {
* @param tree
* @param accessor
*/
public MongoQueryCreator(Query querySpec, PartTree tree,
public MongoQueryCreator(PartTree tree,
SimpleParameterAccessor accessor, MongoConverter converter) {
super(tree, accessor);
this.querySpec = querySpec;
this.converter = converter;
}
@@ -78,7 +75,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, Criteria> {
protected Criteria create(Part part, BindableParameterIterator iterator) {
return from(part.getType(),
querySpec.find(part.getProperty().toDotPath()), iterator);
new Query().find(part.getProperty().toDotPath()), iterator);
}
@@ -123,7 +120,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, Criteria> {
* #complete(java.lang.Object, org.springframework.data.domain.Sort)
*/
@Override
protected Void complete(Criteria criteria, Sort sort) {
protected Query complete(Criteria criteria, Sort sort) {
Query query = criteria.build();
@@ -131,7 +128,7 @@ class MongoQueryCreator extends AbstractQueryCreator<Void, Criteria> {
LOG.debug("Created query " + query);
}
return null;
return query;
}

View File

@@ -18,6 +18,7 @@ package org.springframework.data.document.mongodb.repository;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import com.mongodb.DBCursor;
@@ -36,29 +37,29 @@ abstract class QueryUtils {
/**
* Applies the given {@link Pageable} to the given {@link Query}. Will
* do nothing if {@link Pageable} is {@literal null}.
* Applies the given {@link Pageable} to the given {@link Query}. Will do
* nothing if {@link Pageable} is {@literal null}.
*
* @param spec
* @param query
* @param pageable
* @return
*/
public static Query applyPagination(Query spec, Pageable pageable) {
public static Query applyPagination(Query query, Pageable pageable) {
if (pageable == null) {
return spec;
return query;
}
spec.limit(pageable.getPageSize());
// spec.skip(pageable.getOffset());
query.limit(pageable.getPageSize());
query.slip(pageable.getOffset());
return applySorting(spec, pageable.getSort());
return applySorting(query, pageable.getSort());
}
/**
* Applies the given {@link Sort} to the {@link Query}. Will do nothing
* if {@link Sort} is {@literal null}.
* Applies the given {@link Sort} to the {@link Query}. Will do nothing if
* {@link Sort} is {@literal null}.
*
* @param spec
* @param sort
@@ -70,8 +71,15 @@ abstract class QueryUtils {
return spec;
}
// TODO apply sorting
// spec.
org.springframework.data.document.mongodb.builder.Sort bSort =
spec.sort();
for (Order order : sort) {
bSort.on(
order.getProperty(),
order.isAscending() ? org.springframework.data.document.mongodb.builder.Sort.Order.ASCENDING
: org.springframework.data.document.mongodb.builder.Sort.Order.DESCENDING);
}
return spec;
}

View File

@@ -25,7 +25,6 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.document.mongodb.MongoConverter;
import org.springframework.data.document.mongodb.Person;
import org.springframework.data.document.mongodb.builder.Query;
import org.springframework.data.repository.query.Parameters;
import org.springframework.data.repository.query.SimpleParameterAccessor;
import org.springframework.data.repository.query.parser.PartTree;
@@ -64,18 +63,16 @@ public class MongoQueryCreatorUnitTests {
PartTree tree = new PartTree("findByFirstName", Person.class);
MongoQueryCreator creator =
new MongoQueryCreator(new Query(), tree,
new SimpleParameterAccessor(new Parameters(
findByFirstname), new Object[] { "Oliver" }),
converter);
new MongoQueryCreator(tree, new SimpleParameterAccessor(
new Parameters(findByFirstname),
new Object[] { "Oliver" }), converter);
creator.createQuery();
creator =
new MongoQueryCreator(new Query(), new PartTree(
"findByFirstNameAndFriend", Person.class),
new SimpleParameterAccessor(new Parameters(
findByFirstnameAndFriend), new Object[] {
new MongoQueryCreator(new PartTree("findByFirstNameAndFriend",
Person.class), new SimpleParameterAccessor(
new Parameters(findByFirstnameAndFriend), new Object[] {
"Oliver", new Person() }), converter);
creator.createQuery();
}