DATACOUCH-211 - Applying sort that's passed in via the Pagenate object in call to N1qlCouchbaseRepository::findAll

This commit is contained in:
James Thomson
2016-03-10 21:31:23 +00:00
parent 0149144bc0
commit 8357122950
2 changed files with 34 additions and 1 deletions

View File

@@ -25,6 +25,8 @@ import com.couchbase.client.java.query.SimpleN1qlQuery;
import com.couchbase.client.java.query.Statement;
import com.couchbase.client.java.query.consistency.ScanConsistency;
import com.couchbase.client.java.query.dsl.Expression;
import com.couchbase.client.java.query.dsl.path.GroupByPath;
import com.couchbase.client.java.query.dsl.path.LimitPath;
import com.couchbase.client.java.query.dsl.path.WherePath;
import org.springframework.data.couchbase.core.CouchbaseOperations;
@@ -93,11 +95,22 @@ public class N1qlCouchbaseRepository<T, ID extends Serializable>
//prepare elements of the data query
WherePath selectFrom = N1qlUtils.createSelectFromForEntity(getCouchbaseOperations().getCouchbaseBucket().name());
//add where criteria
Expression whereCriteria = N1qlUtils.createWhereFilterForEntity(null, getCouchbaseOperations().getConverter(),
getEntityInformation());
GroupByPath groupBy = selectFrom.where(whereCriteria);
//apply the sort if available
LimitPath limitPath = groupBy;
if (pageable.getSort() != null) {
com.couchbase.client.java.query.dsl.Sort[] orderings = N1qlUtils.createSort(pageable.getSort(),
getCouchbaseOperations().getConverter());
limitPath = groupBy.orderBy(orderings);
}
//apply the paging
Statement pageStatement = selectFrom.where(whereCriteria).limit(pageable.getPageSize()).offset(pageable.getOffset());
Statement pageStatement = limitPath.limit(pageable.getPageSize()).offset(pageable.getOffset());
//fire the query
N1qlQuery query = N1qlQuery.simple(pageStatement, N1qlParams.build().consistency(consistency));