1
1
/*
2
- * Copyright 2012-2020 the original author or authors
2
+ * Copyright 2012-2021 the original author or authors
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
package org .springframework .data .couchbase .core .query ;
17
17
18
+ import static org .springframework .data .couchbase .core .query .N1QLExpression .x ;
19
+
18
20
import java .util .ArrayList ;
19
21
import java .util .Formatter ;
20
22
import java .util .LinkedList ;
21
23
import java .util .List ;
22
24
25
+ import org .springframework .data .couchbase .core .convert .CouchbaseConverter ;
26
+ import org .springframework .lang .Nullable ;
27
+
23
28
import com .couchbase .client .core .error .InvalidArgumentException ;
24
29
import com .couchbase .client .java .json .JsonArray ;
25
30
import com .couchbase .client .java .json .JsonObject ;
26
31
import com .couchbase .client .java .json .JsonValue ;
27
- import org .springframework .data .couchbase .core .convert .CouchbaseConverter ;
28
- import org .springframework .lang .Nullable ;
29
32
30
33
/**
31
34
* @author Michael Nitschinger
32
35
* @author Michael Reiche
36
+ * @author Mauro Monti
33
37
*/
34
38
public class QueryCriteria implements QueryCriteriaDefinition {
35
39
36
- private final String key ;
40
+ private final N1QLExpression key ;
37
41
/**
38
42
* Holds the chain itself, the current operator being always the last one.
39
43
*/
@@ -46,16 +50,16 @@ public class QueryCriteria implements QueryCriteriaDefinition {
46
50
private Object [] value ;
47
51
private String format ;
48
52
49
- QueryCriteria (List <QueryCriteria > chain , String key , Object [] value , ChainOperator chainOperator ) {
53
+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object [] value , ChainOperator chainOperator ) {
50
54
this (chain , key , value , chainOperator , null , null );
51
55
}
52
56
53
- QueryCriteria (List <QueryCriteria > chain , String key , Object value , ChainOperator chainOperator ) {
57
+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object value , ChainOperator chainOperator ) {
54
58
this (chain , key , new Object [] { value }, chainOperator , null , null );
55
59
}
56
60
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 ) {
59
63
this .criteriaChain = chain ;
60
64
criteriaChain .add (this );
61
65
this .key = key ;
@@ -70,9 +74,16 @@ Object[] getValue() {
70
74
}
71
75
72
76
/**
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.
74
78
*/
75
79
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 ) {
76
87
return new QueryCriteria (new ArrayList <>(), key , null , null );
77
88
}
78
89
@@ -83,21 +94,29 @@ private static QueryCriteria wrap(QueryCriteria criteria) {
83
94
}
84
95
85
96
public QueryCriteria and (String key ) {
97
+ return and (x (key ));
98
+ }
99
+
100
+ public QueryCriteria and (N1QLExpression key ) {
86
101
return new QueryCriteria (this .criteriaChain , key , null , ChainOperator .AND );
87
102
}
88
103
89
104
public QueryCriteria and (QueryCriteria criteria ) {
90
105
return new QueryCriteria (this .criteriaChain , null , criteria , ChainOperator .AND );
91
106
}
92
107
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 ) );
95
110
}
96
111
97
- public QueryCriteria or (String key ) {
112
+ public QueryCriteria or (N1QLExpression key ) {
98
113
return new QueryCriteria (this .criteriaChain , key , null , ChainOperator .OR );
99
114
}
100
115
116
+ public QueryCriteria or (QueryCriteria criteria ) {
117
+ return new QueryCriteria (this .criteriaChain , null , criteria , ChainOperator .OR );
118
+ }
119
+
101
120
public QueryCriteria eq (@ Nullable Object o ) {
102
121
return is (o );
103
122
}
@@ -343,7 +362,7 @@ public String export() { // used only by tests
343
362
*/
344
363
private StringBuilder exportSingle (StringBuilder sb , int [] paramIndexPtr , JsonValue parameters ,
345
364
CouchbaseConverter converter ) {
346
- String fieldName = maybeBackTic (key );
365
+ String fieldName = key == null ? null : key . toString (); // maybeBackTic(key);
347
366
int valueLen = value == null ? 0 : value .length ;
348
367
Object [] v = new Object [valueLen + 2 ];
349
368
v [0 ] = fieldName ;
@@ -377,7 +396,7 @@ private StringBuilder exportSingle(StringBuilder sb, int[] paramIndexPtr, JsonVa
377
396
* @param parameters - parameters of the query. If operands are parameterized, their values are added to parameters
378
397
* @return string containing part of N1QL query
379
398
*/
380
- private String maybeWrapValue (String key , Object value , int [] paramIndexPtr , JsonValue parameters ,
399
+ private String maybeWrapValue (N1QLExpression key , Object value , int [] paramIndexPtr , JsonValue parameters ,
381
400
CouchbaseConverter converter ) {
382
401
if (paramIndexPtr != null ) {
383
402
if (paramIndexPtr [0 ] >= 0 ) {
@@ -397,10 +416,10 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
397
416
JsonObject params = (JsonObject ) parameters ;
398
417
// from StringBasedN1qlQueryParser.getNamedPlaceholderValues()
399
418
try {
400
- params .put (key , convert (converter , value ));
419
+ params .put (key . toString () , convert (converter , value ));
401
420
} catch (InvalidArgumentException iae ) {
402
421
if (value instanceof Object []) {
403
- params .put (key , JsonArray .from ((Object []) value ));
422
+ params .put (key . toString () , JsonArray .from ((Object []) value ));
404
423
} else {
405
424
throw iae ;
406
425
}
@@ -416,7 +435,7 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
416
435
} else if (value == null ) {
417
436
return "null" ;
418
437
} else if (value instanceof Object []) { // convert array into sequence of comma-separated values
419
- StringBuffer l = new StringBuffer ();
438
+ StringBuilder l = new StringBuilder ();
420
439
l .append ("[" );
421
440
Object [] array = (Object []) value ;
422
441
for (int i = 0 ; i < array .length ; i ++) {
0 commit comments