@@ -54,6 +54,7 @@ public Object handle(Expression<?> expression) {
54
54
return expression .accept (this , null );
55
55
}
56
56
57
+ // TODO
57
58
public Sort toSort (List <OrderSpecifier <?>> orderBys ) {
58
59
Sort sort = Sort .unsorted ();
59
60
for (OrderSpecifier <?> orderBy : orderBys ) {
@@ -99,12 +100,12 @@ private String regexValue(Operation<?> expr, int index) {
99
100
100
101
protected QueryCriteriaDefinition asDocument (String key , Object value ) {
101
102
QueryCriteria qc = null ;
102
- if (1 == 1 ) {
103
+ if (workInProgress ) {
103
104
throw new UnsupportedOperationException ("Wrong path to create this criteria " + key );
104
105
}
105
- if (key .equals ("$and" ) || key .equals ("$or" ) /* value instanceof QueryCriteria[] */ ) {
106
+ if (key .equals (Ops . AND ) || key .equals (Ops . OR ) ) {
106
107
throw new UnsupportedOperationException ("Wrong path to create this criteria " + key );
107
- } else if (key .equals ("$in" ) /* value instanceof QueryCriteria[] */ ) {
108
+ } else if (key .equals (Ops . IN ) ) {
108
109
throw new RuntimeException (("not supported" ));
109
110
} else {
110
111
qc = QueryCriteria .where (key ).is (value );
@@ -122,21 +123,14 @@ public Object visit(Operation<?> expr, Void context) {
122
123
Operation <?> lhs = (Operation <?>) expr .getArg (0 );
123
124
if (lhs .getOperator () == Ops .COL_SIZE || lhs .getOperator () == Ops .ARRAY_SIZE
124
125
|| lhs .getOperator () == Ops .STRING_LENGTH ) {
125
- // return asDocument(asDBKey(lhs, 0), asDocument("$size", asDBValue(expr, 1)));
126
126
return QueryCriteria .where (asDBKey (expr , 0 )).is (asDBValue (expr , 1 ));
127
127
} else {
128
128
throw new UnsupportedOperationException ("Illegal operation " + expr );
129
129
}
130
130
} else if (expr .getArg (0 ) instanceof Path ) {
131
- /*
132
- Path<?> path = (Path<?>) expr.getArg(0);
133
- Constant<?> constant = (Constant<?>) expr.getArg(1);
134
- return asDocument(asDBKey(expr, 0), convert(path, constant));
135
- */
136
131
return QueryCriteria .where (asDBKey (expr , 0 )).is (asDBValue (expr , 1 ));
137
132
}
138
133
} else if (op == Ops .STRING_IS_EMPTY ) {
139
- // return asDocument(asDBKey(expr, 0), "");
140
134
return QueryCriteria .where (asDBKey (expr , 0 )).isNotValued ().or (asDBKey (expr , 0 )).is ("" );
141
135
} else if (op == Ops .NOT ) {
142
136
// Handle the not's child
@@ -148,64 +142,41 @@ public Object visit(Operation<?> expr, Void context) {
148
142
context );
149
143
} else {
150
144
QueryCriteria arg = (QueryCriteria ) handle (expr .getArg (0 ));
151
- return arg .negate (); // negate(arg);
145
+ return arg .negate ();
152
146
}
153
147
154
148
} else if (op == Ops .AND ) {
155
- // return asDocument("$and", collectConnectorArgs("$and", expr));
156
- return collectConnectorArgs ("$and" , expr );
149
+ return collectConnectorArgs (Ops .AND , expr );
157
150
} else if (op == Ops .OR ) {
158
- // return asDocument("$or", collectConnectorArgs("$or", expr));
159
- return collectConnectorArgs ("$or" , expr );
151
+ return collectConnectorArgs (Ops .OR , expr );
160
152
} else if (op == Ops .NE ) {
161
- // Path<?> path = (Path<?>) expr.getArg(0);
162
- // Constant<?> constant = (Constant<?>) expr.getArg(1);
163
- // return asDocument(asDBKey(expr, 0), asDocument("$ne", convert(path, constant)));
164
153
return QueryCriteria .where (asDBKey (expr , 0 )).ne (asDBValue (expr , 1 ));
165
154
} else if (op == Ops .STARTS_WITH ) {
166
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression("^" + regexValue(expr, 1)));
167
155
return QueryCriteria .where (asDBKey (expr , 0 )).startingWith (asDBValue (expr , 1 ));
168
156
} else if (op == Ops .STARTS_WITH_IC ) {
169
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression("^" + regexValue(expr, 1), "i"));
170
157
return QueryCriteria .where (asDBKey (expr , 0 )).upper ()
171
158
.startingWith (asDBValue (expr , 1 ).toString ().toUpperCase (Locale .ROOT ));
172
159
} else if (op == Ops .ENDS_WITH ) {
173
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression(regexValue(expr, 1) + "$"));
174
160
return QueryCriteria .where (asDBKey (expr , 0 )).endingWith (asDBValue (expr , 1 ));
175
161
} else if (op == Ops .ENDS_WITH_IC ) {
176
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression(regexValue(expr, 1) + "$", "i"));
177
162
return QueryCriteria .where (asDBKey (expr , 0 )).upper ()
178
163
.endingWith (asDBValue (expr , 1 ).toString ().toUpperCase (Locale .ROOT ));
179
164
} else if (op == Ops .EQ_IGNORE_CASE ) {
180
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression("^" + regexValue(expr, 1) + "$", "i"));
181
165
return QueryCriteria .where (asDBKey (expr , 0 )).upper ().eq (asDBValue (expr , 1 ).toString ().toUpperCase (Locale .ROOT ));
182
166
} else if (op == Ops .STRING_CONTAINS ) {
183
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression(".*" + regexValue(expr, 1) + ".*"));
184
167
return QueryCriteria .where (asDBKey (expr , 0 )).containing (asDBValue (expr , 1 ));
185
168
} else if (op == Ops .STRING_CONTAINS_IC ) {
186
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression(".*" + regexValue(expr, 1) + ".*", "i"));
187
169
return QueryCriteria .where (asDBKey (expr , 0 )).upper ()
188
170
.containing (asDBValue (expr , 1 ).toString ().toUpperCase (Locale .ROOT ));
189
- /*
190
- } else if (op == Ops.MATCHES) {
191
- //return asDocument(asDBKey(expr, 0), new CBRegularExpression(asDBValue(expr, 1).toString()));
192
- return QueryCriteria.where(asDBKey(expr, 0)).like(asDBValue(expr,1));
193
- } else if (op == Ops.MATCHES_IC) {
194
- //return asDocument(asDBKey(expr, 0), new CBRegularExpression(asDBValue(expr, 1).toString(), "i"));
195
- return QueryCriteria.where("UPPER("+asDBKey(expr, 0)+")").like("UPPER("+asDBValue(expr,1)+")");
196
- */
171
+ } else if (op == Ops .MATCHES ) {
172
+ return QueryCriteria .where (asDBKey (expr , 0 )).regex (asDBValue (expr , 1 ));
173
+ } else if (op == Ops .MATCHES_IC ) {
174
+ return QueryCriteria .where ("UPPER(" + asDBKey (expr , 0 ) + ")" ).regex ("UPPER(" + asDBValue (expr , 1 ) + ")" );
197
175
} else if (op == Ops .LIKE ) {
198
- // String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
199
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression(regex));
200
176
return QueryCriteria .where (asDBKey (expr , 0 )).like (asDBValue (expr , 1 ));
201
177
} else if (op == Ops .LIKE_IC ) {
202
- // String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
203
- // return asDocument(asDBKey(expr, 0), new CBRegularExpression(regex, "i"));
204
178
return QueryCriteria .where (asDBKey (expr , 0 )).upper ().like (asDBValue (expr , 1 ).toString ().toUpperCase (Locale .ROOT ));
205
179
} else if (op == Ops .BETWEEN ) {
206
- // Document value = new Document("$gte", this.asDBValue(expr, 1));
207
- // value.append("$lte", this.asDBValue(expr, 2));
208
- // return this.asDocument(this.asDBKey(expr, 0), value);
209
180
return QueryCriteria .where (asDBKey (expr , 0 )).between (asDBValue (expr , 1 ), asDBValue (expr , 2 ));
210
181
} else if (op == Ops .IN ) {
211
182
int constIndex = 0 ;
@@ -217,16 +188,13 @@ public Object visit(Operation<?> expr, Void context) {
217
188
if (Collection .class .isAssignableFrom (expr .getArg (constIndex ).getType ())) {
218
189
@ SuppressWarnings ("unchecked" ) // guarded by previous check
219
190
Collection <?> values = ((Constant <? extends Collection <?>>) expr .getArg (constIndex )).getConstant ();
220
- // return asDocument(asDBKey(expr, exprIndex), asDocument("$in", values));
221
191
return QueryCriteria .where (asDBKey (expr , exprIndex )).in (values );
222
- } else { // I think framework already converts IN to EQ if arg is not a collection
223
- // Path<?> path = (Path<?>) expr.getArg(exprIndex);
224
- // Constant<?> constant = (Constant<?>) expr.getArg(constIndex);
225
- // return asDocument(asDBKey(expr, exprIndex), convert(path, constant));
192
+ } else { // framework already converts IN to EQ if arg is not a collection
193
+ if (workInProgress )
194
+ throw new RuntimeException ("good, we are testing in -> eq" );
226
195
Object value = expr .getArg (constIndex );
227
196
return QueryCriteria .where (asDBKey (expr , exprIndex )).eq (value );
228
197
}
229
-
230
198
} else if (op == Ops .NOT_IN ) {
231
199
int constIndex = 0 ;
232
200
int exprIndex = 1 ;
@@ -237,93 +205,35 @@ public Object visit(Operation<?> expr, Void context) {
237
205
if (Collection .class .isAssignableFrom (expr .getArg (constIndex ).getType ())) {
238
206
@ SuppressWarnings ("unchecked" ) // guarded by previous check
239
207
Collection <?> values = ((Constant <? extends Collection <?>>) expr .getArg (constIndex )).getConstant ();
240
- // return asDocument(asDBKey(expr, exprIndex), asDocument("$nin", values));
241
208
return QueryCriteria .where (asDBKey (expr , exprIndex )).notIn (values );
242
- } else { // I think framework already converts NOT_IN to NE if arg is not a collection
243
- // Path<?> path = (Path<?>) expr.getArg(exprIndex);
244
- // Constant<?> constant = (Constant<?>) expr.getArg(constIndex);
245
- // return asDocument(asDBKey(expr, exprIndex), asDocument("$ne", convert(path, constant)));
209
+ } else { // framework already converts NOT_IN to NE if arg is not a collection
210
+ if (workInProgress )
211
+ throw new RuntimeException ("good, we are testing not_in -> ne" );
246
212
Object value = expr .getArg (constIndex );
247
213
return QueryCriteria .where (asDBKey (expr , exprIndex )).ne (value );
248
214
}
249
-
250
215
} else if (op == Ops .COL_IS_EMPTY ) {
251
- // List<Object> list = new ArrayList<Object>(2);
252
- // list.add(asDocument(asDBKey(expr, 0), new ArrayList<Object>()));
253
- // list.add(asDocument(asDBKey(expr, 0), asDocument("$exists", false)));
254
- // return asDocument("$or", list);
255
216
return QueryCriteria .where (asDBKey (expr , 0 )).isNotValued ();
256
217
} else if (op == Ops .LT ) {
257
- // return asDocument(asDBKey(expr, 0), asDocument("$lt", asDBValue(expr, 1)));
258
218
return QueryCriteria .where (asDBKey (expr , 0 )).lt (asDBValue (expr , 1 ));
259
219
} else if (op == Ops .GT ) {
260
- // return asDocument(asDBKey(expr, 0), asDocument("$gt", asDBValue(expr, 1)));
261
220
return QueryCriteria .where (asDBKey (expr , 0 )).gt (asDBValue (expr , 1 ));
262
221
} else if (op == Ops .LOE ) {
263
- // return asDocument(asDBKey(expr, 0), asDocument("$lte", asDBValue(expr, 1)));
264
222
return QueryCriteria .where (asDBKey (expr , 0 )).lte (asDBValue (expr , 1 ));
265
223
} else if (op == Ops .GOE ) {
266
- // return asDocument(asDBKey(expr, 0), asDocument("$gte", asDBValue(expr, 1)));
267
224
return QueryCriteria .where (asDBKey (expr , 0 )).gte (asDBValue (expr , 1 ));
268
225
} else if (op == Ops .IS_NULL ) {
269
- // return asDocument(asDBKey(expr, 0), asDocument("$exists", false));
270
226
return QueryCriteria .where (asDBKey (expr , 0 )).isNull ();
271
227
} else if (op == Ops .IS_NOT_NULL ) {
272
- // return asDocument(asDBKey(expr, 0), asDocument("$exists", true));
273
228
return QueryCriteria .where (asDBKey (expr , 0 )).isNotNull ();
274
229
} else if (op == Ops .CONTAINS_KEY ) { // TODO not sure about this one
275
- Path <?> path = (Path <?>) expr .getArg (0 );
276
- // Expression<?> key = expr.getArg(1);
277
- // return asDocument(visit(path, context) + "." + key.toString(), asDocument("$exists", true));
278
230
return QueryCriteria .where ("meta().id" /*asDBKey(expr, 0)*/ ).eq (asDBKey (expr , 1 ));
279
231
} else if (op == Ops .STRING_LENGTH ) {
280
232
return "LENGTH(" + asDBKey (expr , 0 ) + ")" ;// QueryCriteria.where(asDBKey(expr, 0)).size();
281
233
}
282
-
283
- throw new UnsupportedOperationException ("Illegal operation " + expr );
234
+ throw new UnsupportedOperationException ("Unsupported operation " + expr );
284
235
}
285
236
286
- /* TODO -- need later
287
- private Object negate(QueryCriteriaDefinition arg) {
288
- List<Object> list = new ArrayList<Object>();
289
- for (Map.Entry<String, Object> entry : arg.entrySet()) {
290
- if (entry.getKey().equals("$or")) {
291
- list.add(asDocument("$nor", entry.getValue()));
292
-
293
- } else if (entry.getKey().equals("$and")) {
294
- List<Object> list2 = new ArrayList<Object>();
295
- for (Object o : ((Collection) entry.getValue())) {
296
- list2.add(negate((QueryCriteriaDefinition) o));
297
- }
298
- list.add(asDocument("$or", list2));
299
-
300
- } else if (entry.getValue() instanceof Pattern || entry.getValue() instanceof CBRegularExpression) {
301
- list.add(asDocument(entry.getKey(), asDocument("$not", entry.getValue())));
302
-
303
- } else if (entry.getValue() instanceof QueryCriteriaDefinition) {
304
- list.add(negate(entry.getKey(), (QueryCriteriaDefinition) entry.getValue()));
305
-
306
- } else {
307
- list.add(asDocument(entry.getKey(), asDocument("$ne", entry.getValue())));
308
- }
309
- }
310
- return list.size() == 1 ? list.get(0) : asDocument("$or", list);
311
- }
312
-
313
- private Object negate(String key, QueryCriteriaDefinition value) {
314
- if (value.size() == 1) {
315
- return asDocument(key, asDocument("$not", value));
316
- } else {
317
- List<Object> list2 = new ArrayList<Object>();
318
- for (Map.Entry<String, Object> entry2 : value.entrySet()) {
319
- list2.add(asDocument(key, asDocument("$not", asDocument(entry2.getKey(), entry2.getValue()))));
320
- }
321
- return asDocument("$or", list2);
322
- }
323
- }
324
- */
325
-
326
- /* TODO -- need later
327
237
protected Object convert (Path <?> property , Constant <?> constant ) {
328
238
if (isReference (property )) {
329
239
return asReference (constant .getConstant ());
@@ -332,12 +242,11 @@ protected Object convert(Path<?> property, Constant<?> constant) {
332
242
return asReferenceKey (property .getMetadata ().getParent ().getType (), constant .getConstant ());
333
243
} else if (constant .getType ().equals (String .class ) && isImplicitObjectIdConversion ()) {
334
244
String id = (String ) constant .getConstant ();
335
- return ObjectId.isValid(id) ? new ObjectId(id) : id;
245
+ return id ;
336
246
}
337
247
}
338
248
return visit (constant , null );
339
249
}
340
- */
341
250
342
251
protected boolean isImplicitObjectIdConversion () {
343
252
return true ;
@@ -390,16 +299,16 @@ public Object visit(ParamExpression<?> expr, Void context) {
390
299
throw new UnsupportedOperationException ();
391
300
}
392
301
393
- private QueryCriteriaDefinition collectConnectorArgs (String operator , Operation <?> operation ) {
302
+ private QueryCriteriaDefinition collectConnectorArgs (Ops operator , Operation <?> operation ) {
394
303
QueryCriteria first = null ;
395
304
for (Expression <?> exp : operation .getArgs ()) {
396
305
QueryCriteria document = (QueryCriteria ) handle (exp );
397
306
if (first == null ) {
398
307
first = document ;
399
308
} else {
400
- if (operator .equals ("$or" )) {
309
+ if (operator .equals (Ops . OR )) {
401
310
first = first .or (document );
402
- } else if (operator .equals ("$and" )) {
311
+ } else if (operator .equals (Ops . AND )) {
403
312
first = first .and (document );
404
313
}
405
314
}
0 commit comments