DATACOUCH-265 - Support pageable and sort for String based SpEL queries

Original pull request: #128.
This commit is contained in:
Subhashni Balakrishnan
2017-01-03 15:07:25 -08:00
parent 162d8124ab
commit 5d36ecc12b
4 changed files with 61 additions and 2 deletions

View File

@@ -20,14 +20,23 @@ package org.springframework.data.couchbase.repository.query;
import com.couchbase.client.java.document.json.JsonValue;
import com.couchbase.client.java.query.N1qlQuery;
import com.couchbase.client.java.query.Statement;
import com.couchbase.client.java.query.dsl.path.DefaultLimitPath;
import com.couchbase.client.java.query.dsl.path.DefaultOrderByPath;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.repository.query.support.N1qlUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.query.EvaluationContextProvider;
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.ReturnedType;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.util.Assert;
/**
* A {@link RepositoryQuery} for Couchbase, based on N1QL and a String statement.
@@ -76,7 +85,24 @@ public class StringN1qlBasedQuery extends AbstractN1qlBasedQuery {
public Statement getStatement(ParameterAccessor accessor, Object[] runtimeParameters, ReturnedType returnedType) {
EvaluationContext evaluationContext = evaluationContextProvider.getEvaluationContext(getQueryMethod().getParameters(), runtimeParameters);
String parsedStatement = this.queryParser.doParse(parser, evaluationContext, false);
return N1qlQuery.simple(parsedStatement).statement();
String orderByPart = "";
String limitByPart = "";
Sort sort = accessor.getSort();
if (sort != null) {
com.couchbase.client.java.query.dsl.Sort[] cbSorts = N1qlUtils.createSort(sort, getCouchbaseOperations().getConverter());
orderByPart = " " + new DefaultOrderByPath(null).orderBy(cbSorts).toString();
}
if (queryMethod.isPageQuery()) {
Pageable pageable = accessor.getPageable();
Assert.notNull(pageable);
limitByPart = " " + new DefaultLimitPath(null).limit(pageable.getPageSize()).offset(pageable.getOffset()).toString();
} else if (queryMethod.isSliceQuery()) {
Pageable pageable = accessor.getPageable();
Assert.notNull(pageable);
limitByPart = " " + new DefaultLimitPath(null).limit(pageable.getPageSize() + 1).offset(pageable.getOffset()).toString();
}
return N1qlQuery.simple(parsedStatement + orderByPart + limitByPart).statement();
}
@Override