DATACOUCH-580 - Do not automatically insert n1ql.filter predicate in string query.

Since it is difficult to determine where the n1ql.filter (which typically
filters on _class), and also because it is not mandator, including
the n1ql.filter in the @query is left up to the author of the query.
Example:
@query("#{#n1ql.selectEntity} where #{#n1ql.filter} and lastname = $1")
This commit is contained in:
mikereiche
2020-07-14 14:48:09 -07:00
parent d2456bb166
commit e2444403a7
5 changed files with 30 additions and 34 deletions

View File

@@ -21,16 +21,17 @@ import org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate;
import org.springframework.data.couchbase.repository.query.StringBasedN1qlQueryParser;
/**
* @author Michael Reiche
*
* Query created from the string in @Query annotation in the repository interface.
*
* @Query("#{#n1ql.selectEntity} where firstname = $1 and lastname = $2")
* List<User> getByFirstnameAndLastname(String firstname, String lastname);
*
* It must include the SELECT ... FROM ... preferably via the #n1ql expression
* In addition to any predicates in the string, a predicate for the domainType (class)
* will be added.
*
* <pre>
* &#64;Query("#{#n1ql.selectEntity} where #{#n1ql.filter} and firstname = $1 and lastname = $2")
* List<User> getByFirstnameAndLastname(String firstname, String lastname);
* </pre>
*
* It must include the SELECT ... FROM ... preferably via the #n1ql expression, in addition to any predicates required,
* including the n1ql.filter (for _class = className)
*
* @author Michael Reiche
*/
public class StringQuery extends Query {
@@ -41,10 +42,10 @@ public class StringQuery extends Query {
}
/**
* inlineN1qlQuery (Query Annotation)
* append the string query to the provided StringBuilder.
* To be used along with the other append*() methods to construct the N1QL statement
* @param sb - StringBuilder
* inlineN1qlQuery (Query Annotation) append the string query to the provided StringBuilder. To be used along with the
* other append*() methods to construct the N1QL statement
*
* @param sb - StringBuilder
*/
private void appendInlineN1qlStatement(final StringBuilder sb) {
sb.append(inlineN1qlQuery);
@@ -52,11 +53,8 @@ public class StringQuery extends Query {
@Override
public String toN1qlString(ReactiveCouchbaseTemplate template, Class domainClass, boolean isCount) {
StringBasedN1qlQueryParser.N1qlSpelValues n1ql = getN1qlSpelValues(template, domainClass, isCount);
final StringBuilder statement = new StringBuilder();
appendInlineN1qlStatement(statement); // apply the string statement
appendWhereString(statement, n1ql.filter); // typeKey = typeValue
// To use generated parameters for literals
// we need to figure out if we must use positional or named parameters
// If we are using positional parameters, we need to start where