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.
27
27
import org .springframework .data .couchbase .core .convert .CouchbaseConverter ;
28
28
import org .springframework .lang .Nullable ;
29
29
30
+ import static org .springframework .data .couchbase .core .query .N1QLExpression .*;
31
+
30
32
/**
31
33
* @author Michael Nitschinger
32
34
* @author Michael Reiche
35
+ * @author Mauro Monti
33
36
*/
34
37
public class QueryCriteria implements QueryCriteriaDefinition {
35
38
36
- private final String key ;
39
+ private final N1QLExpression key ;
37
40
/**
38
41
* Holds the chain itself, the current operator being always the last one.
39
42
*/
@@ -46,15 +49,15 @@ public class QueryCriteria implements QueryCriteriaDefinition {
46
49
private Object [] value ;
47
50
private String format ;
48
51
49
- QueryCriteria (List <QueryCriteria > chain , String key , Object [] value , ChainOperator chainOperator ) {
52
+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object [] value , ChainOperator chainOperator ) {
50
53
this (chain , key , value , chainOperator , null , null );
51
54
}
52
55
53
- QueryCriteria (List <QueryCriteria > chain , String key , Object value , ChainOperator chainOperator ) {
56
+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object value , ChainOperator chainOperator ) {
54
57
this (chain , key , new Object [] { value }, chainOperator , null , null );
55
58
}
56
59
57
- QueryCriteria (List <QueryCriteria > chain , String key , Object [] value , ChainOperator chainOperator , String operator ,
60
+ QueryCriteria (List <QueryCriteria > chain , N1QLExpression key , Object [] value , ChainOperator chainOperator , String operator ,
58
61
String format ) {
59
62
this .criteriaChain = chain ;
60
63
criteriaChain .add (this );
@@ -70,9 +73,16 @@ Object[] getValue() {
70
73
}
71
74
72
75
/**
73
- * Static factory method to create a Criteria using the provided key.
76
+ * Static factory method to create a Criteria using the provided String key.
74
77
*/
75
78
public static QueryCriteria where (String key ) {
79
+ return where (x (key ));
80
+ }
81
+
82
+ /**
83
+ * Static factory method to create a Criteria using the provided N1QLExpression key.
84
+ */
85
+ public static QueryCriteria where (N1QLExpression key ) {
76
86
return new QueryCriteria (new ArrayList <>(), key , null , null );
77
87
}
78
88
@@ -83,21 +93,29 @@ private static QueryCriteria wrap(QueryCriteria criteria) {
83
93
}
84
94
85
95
public QueryCriteria and (String key ) {
96
+ return and (x (key ));
97
+ }
98
+
99
+ public QueryCriteria and (N1QLExpression key ) {
86
100
return new QueryCriteria (this .criteriaChain , key , null , ChainOperator .AND );
87
101
}
88
102
89
103
public QueryCriteria and (QueryCriteria criteria ) {
90
104
return new QueryCriteria (this .criteriaChain , null , criteria , ChainOperator .AND );
91
105
}
92
106
93
- public QueryCriteria or (QueryCriteria criteria ) {
94
- return new QueryCriteria ( this . criteriaChain , null , criteria , ChainOperator . OR );
107
+ public QueryCriteria or (String key ) {
108
+ return or ( x ( key ) );
95
109
}
96
110
97
- public QueryCriteria or (String key ) {
111
+ public QueryCriteria or (N1QLExpression key ) {
98
112
return new QueryCriteria (this .criteriaChain , key , null , ChainOperator .OR );
99
113
}
100
114
115
+ public QueryCriteria or (QueryCriteria criteria ) {
116
+ return new QueryCriteria (this .criteriaChain , null , criteria , ChainOperator .OR );
117
+ }
118
+
101
119
public QueryCriteria eq (@ Nullable Object o ) {
102
120
return is (o );
103
121
}
@@ -343,7 +361,7 @@ public String export() { // used only by tests
343
361
*/
344
362
private StringBuilder exportSingle (StringBuilder sb , int [] paramIndexPtr , JsonValue parameters ,
345
363
CouchbaseConverter converter ) {
346
- String fieldName = maybeBackTic (key );
364
+ String fieldName = key == null ? null : key . toString (); // maybeBackTic(key);
347
365
int valueLen = value == null ? 0 : value .length ;
348
366
Object [] v = new Object [valueLen + 2 ];
349
367
v [0 ] = fieldName ;
@@ -377,7 +395,7 @@ private StringBuilder exportSingle(StringBuilder sb, int[] paramIndexPtr, JsonVa
377
395
* @param parameters - parameters of the query. If operands are parameterized, their values are added to parameters
378
396
* @return string containing part of N1QL query
379
397
*/
380
- private String maybeWrapValue (String key , Object value , int [] paramIndexPtr , JsonValue parameters ,
398
+ private String maybeWrapValue (N1QLExpression key , Object value , int [] paramIndexPtr , JsonValue parameters ,
381
399
CouchbaseConverter converter ) {
382
400
if (paramIndexPtr != null ) {
383
401
if (paramIndexPtr [0 ] >= 0 ) {
@@ -397,10 +415,10 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
397
415
JsonObject params = (JsonObject ) parameters ;
398
416
// from StringBasedN1qlQueryParser.getNamedPlaceholderValues()
399
417
try {
400
- params .put (key , convert (converter , value ));
418
+ params .put (key . toString () , convert (converter , value ));
401
419
} catch (InvalidArgumentException iae ) {
402
420
if (value instanceof Object []) {
403
- params .put (key , JsonArray .from ((Object []) value ));
421
+ params .put (key . toString () , JsonArray .from ((Object []) value ));
404
422
} else {
405
423
throw iae ;
406
424
}
@@ -416,7 +434,7 @@ private String maybeWrapValue(String key, Object value, int[] paramIndexPtr, Jso
416
434
} else if (value == null ) {
417
435
return "null" ;
418
436
} else if (value instanceof Object []) { // convert array into sequence of comma-separated values
419
- StringBuffer l = new StringBuffer ();
437
+ StringBuilder l = new StringBuilder ();
420
438
l .append ("[" );
421
439
Object [] array = (Object []) value ;
422
440
for (int i = 0 ; i < array .length ; i ++) {
0 commit comments