16
16
package org .springframework .data .couchbase .repository .query ;
17
17
18
18
import static org .springframework .data .couchbase .core .query .N1QLExpression .i ;
19
+ import static org .springframework .data .couchbase .core .query .N1QLExpression .s ;
19
20
import static org .springframework .data .couchbase .core .query .N1QLExpression .x ;
20
21
import static org .springframework .data .couchbase .core .support .TemplateUtils .SELECT_CAS ;
21
22
import static org .springframework .data .couchbase .core .support .TemplateUtils .SELECT_ID ;
@@ -199,30 +200,30 @@ public StringBasedN1qlQueryParser(String bucketName, String scope, String collec
199
200
}
200
201
201
202
/**
202
- * Create the n1ql spel values. The domainClass is needed, but not the returnClass. Mapping the domainClass to the
203
- * returnClass is the responsibility of decoding.
204
- *
205
- * @param bucketName
206
- * @param scope
207
- * @param collection
208
- * @param domainClass
209
- * @param typeField
210
- * @param typeValue
211
- * @param isCount
212
- * @param distinctFields
213
- * @param fields
214
- * @return
215
- */
203
+ * Create the n1ql spel values. The domainClass is needed, but not the returnClass. Mapping the domainClass to the
204
+ * returnClass is the responsibility of decoding.
205
+ *
206
+ * @param bucketName
207
+ * @param scope
208
+ * @param collection
209
+ * @param domainClass
210
+ * @param typeKey
211
+ * @param typeValue
212
+ * @param isCount
213
+ * @param distinctFields
214
+ * @param fields
215
+ * @return
216
+ */
216
217
public N1qlSpelValues createN1qlSpelValues (String bucketName , String scope , String collection , Class domainClass ,
217
- String typeField , String typeValue , boolean isCount , String [] distinctFields , String [] fields ) {
218
+ String typeKey , String typeValue , boolean isCount , String [] distinctFields , String [] fields ) {
218
219
String b = bucketName ;
219
220
String keyspace = collection != null ? collection : bucketName ;
220
221
Assert .isTrue (!(distinctFields != null && fields != null ),
221
222
"only one of project(fields) and distinct(distinctFields) can be specified" );
222
223
String entityFields = "" ;
223
224
String selectEntity ;
224
225
if (distinctFields != null ) {
225
- String distinctFieldsStr = getProjectedOrDistinctFields (b , domainClass , typeField , fields , distinctFields );
226
+ String distinctFieldsStr = getProjectedOrDistinctFields (b , domainClass , typeKey , fields , distinctFields );
226
227
if (isCount ) {
227
228
selectEntity = N1QLExpression .select (N1QLExpression .count (N1QLExpression .distinct (x (distinctFieldsStr )))
228
229
.as (i (CountFragment .COUNT_ALIAS )).from (keyspace )).toString ();
@@ -233,11 +234,12 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String scope, Stri
233
234
selectEntity = N1QLExpression .select (N1QLExpression .count (x ("\" *\" " )).as (i (CountFragment .COUNT_ALIAS )))
234
235
.from (keyspace ).toString ();
235
236
} else {
236
- String projectedFields = getProjectedOrDistinctFields (keyspace , domainClass , typeField , fields , distinctFields );
237
+ String projectedFields = getProjectedOrDistinctFields (keyspace , domainClass , typeKey , fields ,
238
+ distinctFields );
237
239
entityFields = projectedFields ;
238
240
selectEntity = N1QLExpression .select (x (projectedFields )).from (keyspace ).toString ();
239
241
}
240
- String typeSelection = "`" + typeField + "` = \" " + typeValue + " \" " ;
242
+ String typeSelection = ! empty ( typeKey ) && ! empty ( typeValue ) ? i ( typeKey ). eq ( s ( typeValue )). toString () : null ;
241
243
242
244
String delete = N1QLExpression .delete ().from (keyspace ).toString ();
243
245
String returning = " returning " + N1qlUtils .createReturningExpressionForDelete (keyspace );
@@ -246,6 +248,10 @@ public N1qlSpelValues createN1qlSpelValues(String bucketName, String scope, Stri
246
248
i (collection ).toString (), typeSelection , delete , returning );
247
249
}
248
250
251
+ private static boolean empty (String s ) {
252
+ return s == null || s .length () == 0 ;
253
+ }
254
+
249
255
private String getProjectedOrDistinctFields (String b , Class resultClass , String typeField , String [] fields ,
250
256
String [] distinctFields ) {
251
257
if (distinctFields != null && distinctFields .length != 0 ) {
0 commit comments