11/*
2- * Copyright 2012-2020 the original author or authors
2+ * Copyright 2012-2021 the original author or authors
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1515 */
1616package org .springframework .data .couchbase .core .query ;
1717
18+ import static org .springframework .data .couchbase .core .query .N1QLExpression .x ;
19+
1820import java .util .ArrayList ;
1921import java .util .Formatter ;
2022import java .util .LinkedList ;
2123import java .util .List ;
2224
25+ import org .springframework .data .couchbase .core .convert .CouchbaseConverter ;
26+ import org .springframework .lang .Nullable ;
27+
2328import com .couchbase .client .core .error .InvalidArgumentException ;
2429import com .couchbase .client .java .json .JsonArray ;
2530import com .couchbase .client .java .json .JsonObject ;
2631import com .couchbase .client .java .json .JsonValue ;
27- import org .springframework .data .couchbase .core .convert .CouchbaseConverter ;
28- import org .springframework .lang .Nullable ;
2932
3033/**
3134 * @author Michael Nitschinger
3235 * @author Michael Reiche
36+ * @author Mauro Monti
3337 */
3438public class QueryCriteria implements QueryCriteriaDefinition {
3539
36- private final String key ;
40+ private final N1QLExpression key ;
3741 /**
3842 * Holds the chain itself, the current operator being always the last one.
3943 */
@@ -46,16 +50,16 @@ public class QueryCriteria implements QueryCriteriaDefinition {
4650 private Object [] value ;
4751 private String format ;
4852
49- QueryCriteria (List <QueryCriteria > chain , String key , Object [] value , ChainOperator chainOperator ) {
53+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object [] value , ChainOperator chainOperator ) {
5054 this (chain , key , value , chainOperator , null , null );
5155 }
5256
53- QueryCriteria (List <QueryCriteria > chain , String key , Object value , ChainOperator chainOperator ) {
57+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object value , ChainOperator chainOperator ) {
5458 this (chain , key , new Object [] { value }, chainOperator , null , null );
5559 }
5660
57- QueryCriteria (List <QueryCriteria > chain , String key , Object [] value , ChainOperator chainOperator , String operator ,
58- String format ) {
61+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object [] value , ChainOperator chainOperator ,
62+ String operator , String format ) {
5963 this .criteriaChain = chain ;
6064 criteriaChain .add (this );
6165 this .key = key ;
@@ -70,9 +74,16 @@ Object[] getValue() {
7074 }
7175
7276 /**
73- * Static factory method to create a Criteria using the provided key.
77+ * Static factory method to create a Criteria using the provided String key.
7478 */
7579 public static QueryCriteria where (String key ) {
80+ return where (x (key ));
81+ }
82+
83+ /**
84+ * Static factory method to create a Criteria using the provided N1QLExpression key.
85+ */
86+ public static QueryCriteria where (N1QLExpression key ) {
7687 return new QueryCriteria (new ArrayList <>(), key , null , null );
7788 }
7889
@@ -83,21 +94,29 @@ private static QueryCriteria wrap(QueryCriteria criteria) {
8394 }
8495
8596 public QueryCriteria and (String key ) {
97+ return and (x (key ));
98+ }
99+
100+ public QueryCriteria and (N1QLExpression key ) {
86101 return new QueryCriteria (this .criteriaChain , key , null , ChainOperator .AND );
87102 }
88103
89104 public QueryCriteria and (QueryCriteria criteria ) {
90105 return new QueryCriteria (this .criteriaChain , null , criteria , ChainOperator .AND );
91106 }
92107
93- public QueryCriteria or (QueryCriteria criteria ) {
94- return new QueryCriteria ( this . criteriaChain , null , criteria , ChainOperator . OR );
108+ public QueryCriteria or (String key ) {
109+ return or ( x ( key ) );
95110 }
96111
97- public QueryCriteria or (String key ) {
112+ public QueryCriteria or (N1QLExpression key ) {
98113 return new QueryCriteria (this .criteriaChain , key , null , ChainOperator .OR );
99114 }
100115
116+ public QueryCriteria or (QueryCriteria criteria ) {
117+ return new QueryCriteria (this .criteriaChain , null , criteria , ChainOperator .OR );
118+ }
119+
101120 public QueryCriteria eq (@ Nullable Object o ) {
102121 return is (o );
103122 }
@@ -343,7 +362,7 @@ public String export() { // used only by tests
343362 */
344363 private StringBuilder exportSingle (StringBuilder sb , int [] paramIndexPtr , JsonValue parameters ,
345364 CouchbaseConverter converter ) {
346- String fieldName = maybeBackTic (key );
365+ String fieldName = key == null ? null : key . toString (); // maybeBackTic(key);
347366 int valueLen = value == null ? 0 : value .length ;
348367 Object [] v = new Object [valueLen + 2 ];
349368 v [0 ] = fieldName ;
@@ -377,7 +396,7 @@ private StringBuilder exportSingle(StringBuilder sb, int[] paramIndexPtr, JsonVa
377396 * @param parameters - parameters of the query. If operands are parameterized, their values are added to parameters
378397 * @return string containing part of N1QL query
379398 */
380- private String maybeWrapValue (String key , Object value , int [] paramIndexPtr , JsonValue parameters ,
399+ private String maybeWrapValue (N1QLExpression key , Object value , int [] paramIndexPtr , JsonValue parameters ,
381400 CouchbaseConverter converter ) {
382401 if (paramIndexPtr != null ) {
383402 if (paramIndexPtr [0 ] >= 0 ) {
@@ -397,10 +416,10 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
397416 JsonObject params = (JsonObject ) parameters ;
398417 // from StringBasedN1qlQueryParser.getNamedPlaceholderValues()
399418 try {
400- params .put (key , convert (converter , value ));
419+ params .put (key . toString () , convert (converter , value ));
401420 } catch (InvalidArgumentException iae ) {
402421 if (value instanceof Object []) {
403- params .put (key , JsonArray .from ((Object []) value ));
422+ params .put (key . toString () , JsonArray .from ((Object []) value ));
404423 } else {
405424 throw iae ;
406425 }
@@ -416,7 +435,7 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
416435 } else if (value == null ) {
417436 return "null" ;
418437 } else if (value instanceof Object []) { // convert array into sequence of comma-separated values
419- StringBuffer l = new StringBuffer ();
438+ StringBuilder l = new StringBuilder ();
420439 l .append ("[" );
421440 Object [] array = (Object []) value ;
422441 for (int i = 0 ; i < array .length ; i ++) {
0 commit comments