-
Notifications
You must be signed in to change notification settings - Fork 862
/
JPQLSerializer.java
528 lines (458 loc) · 19.8 KB
/
JPQLSerializer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.jpa;
import java.util.*;
import javax.annotation.Nullable;
import javax.persistence.*;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.metamodel.SingularAttribute;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.querydsl.core.JoinExpression;
import com.querydsl.core.JoinType;
import com.querydsl.core.QueryMetadata;
import com.querydsl.core.support.SerializerBase;
import com.querydsl.core.types.*;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.util.MathUtils;
/**
* {@code JPQLSerializer} serializes Querydsl expressions into JPQL syntax.
*
* @author tiwe
*/
public class JPQLSerializer extends SerializerBase<JPQLSerializer> {
private static final Set<? extends Operator> NUMERIC = Sets.immutableEnumSet(
Ops.ADD, Ops.SUB, Ops.MULT, Ops.DIV,
Ops.LT, Ops.LOE, Ops.GT, Ops.GOE, Ops.BETWEEN);
private static final Set<? extends Operator> CASE_OPS = Sets.immutableEnumSet(
Ops.CASE_EQ_ELSE, Ops.CASE_ELSE);
private static final String COMMA = ", ";
private static final String DELETE = "delete from ";
private static final String FROM = "from ";
private static final String GROUP_BY = "\ngroup by ";
private static final String HAVING = "\nhaving ";
private static final String ORDER_BY = "\norder by ";
private static final String SELECT = "select ";
private static final String SELECT_COUNT = "select count(";
private static final String SELECT_COUNT_DISTINCT = "select count(distinct ";
private static final String SELECT_DISTINCT = "select distinct ";
private static final String SET = "\nset ";
private static final String UPDATE = "update ";
private static final String WHERE = "\nwhere ";
private static final String WITH = " with ";
private static final String ON = " on ";
private static final Map<JoinType, String> joinTypes = new EnumMap<JoinType, String>(JoinType.class);
private final JPQLTemplates templates;
private final EntityManager entityManager;
private boolean inProjection = false;
private boolean inCaseOperation = false;
static {
joinTypes.put(JoinType.DEFAULT, COMMA);
joinTypes.put(JoinType.FULLJOIN, "\n full join ");
joinTypes.put(JoinType.INNERJOIN, "\n inner join ");
joinTypes.put(JoinType.JOIN, "\n inner join ");
joinTypes.put(JoinType.LEFTJOIN, "\n left join ");
joinTypes.put(JoinType.RIGHTJOIN, "\n right join ");
}
private boolean wrapElements = false;
public JPQLSerializer(JPQLTemplates templates) {
this(templates, null);
}
public JPQLSerializer(JPQLTemplates templates, EntityManager em) {
super(templates);
this.templates = templates;
this.entityManager = em;
}
private String getEntityName(Class<?> clazz) {
final Entity entityAnnotation = clazz.getAnnotation(Entity.class);
if (entityAnnotation != null && entityAnnotation.name().length() > 0) {
return entityAnnotation.name();
} else if (clazz.getPackage() != null) {
String pn = clazz.getPackage().getName();
return clazz.getName().substring(pn.length() + 1);
} else {
return clazz.getName();
}
}
private void handleJoinTarget(JoinExpression je) {
// type specifier
if (je.getTarget() instanceof EntityPath<?>) {
final EntityPath<?> pe = (EntityPath<?>) je.getTarget();
if (pe.getMetadata().isRoot()) {
append(getEntityName(pe.getType()));
append(" ");
}
handle(je.getTarget());
} else if (je.getTarget() instanceof Operation) {
Operation<?> op = (Operation) je.getTarget();
if (op.getOperator() == Ops.ALIAS) {
boolean treat = false;
if (Collection.class.isAssignableFrom(op.getArg(0).getType())) {
if (op.getArg(0) instanceof CollectionExpression) {
Class<?> par = ((CollectionExpression) op.getArg(0)).getParameter(0);
treat = !par.equals(op.getArg(1).getType());
}
} else if (Map.class.isAssignableFrom(op.getArg(0).getType())) {
if (op.getArg(0) instanceof MapExpression) {
Class<?> par = ((MapExpression) op.getArg(0)).getParameter(1);
treat = !par.equals(op.getArg(1).getType());
}
} else {
treat = !op.getArg(0).getType().equals(op.getArg(1).getType());
}
if (treat) {
Expression<?> entityName = ConstantImpl.create(getEntityName(op.getArg(1).getType()));
Expression<?> t = ExpressionUtils.operation(op.getType(), JPQLOps.TREAT, op.getArg(0), entityName);
op = ExpressionUtils.operation(op.getType(), Ops.ALIAS, t, op.getArg(1));
}
}
handle(op);
} else {
handle(je.getTarget());
}
}
public void serialize(QueryMetadata metadata, boolean forCountRow, @Nullable String projection) {
final Expression<?> select = metadata.getProjection();
final List<JoinExpression> joins = metadata.getJoins();
final Predicate where = metadata.getWhere();
final List<? extends Expression<?>> groupBy = metadata.getGroupBy();
final Predicate having = metadata.getHaving();
final List<OrderSpecifier<?>> orderBy = metadata.getOrderBy();
// select
boolean inProjectionOrig = inProjection;
inProjection = true;
if (projection != null) {
append(SELECT).append(projection).append("\n");
} else if (forCountRow) {
if (!groupBy.isEmpty()) {
append(SELECT_COUNT_DISTINCT);
handle(", ", groupBy);
} else {
if (!metadata.isDistinct()) {
append(SELECT_COUNT);
} else {
append(SELECT_COUNT_DISTINCT);
}
if (select != null) {
if (select instanceof FactoryExpression) {
handle(joins.get(0).getTarget());
} else {
// TODO : make sure this works
handle(select);
}
} else {
handle(joins.get(0).getTarget());
}
}
append(")\n");
} else if (select != null || !joins.isEmpty()) {
if (!metadata.isDistinct()) {
append(SELECT);
} else {
append(SELECT_DISTINCT);
}
if (select != null) {
handle(select);
} else {
handle(joins.get(0).getTarget());
}
append("\n");
}
inProjection = inProjectionOrig;
// from
if (!joins.isEmpty()) {
append(FROM);
serializeSources(forCountRow, joins);
}
// where
if (where != null) {
append(WHERE).handle(where);
}
// group by
if (!groupBy.isEmpty() && !forCountRow) {
append(GROUP_BY).handle(COMMA, groupBy);
}
// having
if (having != null) {
append(HAVING).handle(having);
}
// order by
if (!orderBy.isEmpty() && !forCountRow) {
append(ORDER_BY);
boolean first = true;
for (final OrderSpecifier<?> os : orderBy) {
if (!first) {
append(COMMA);
}
handle(os.getTarget());
append(os.getOrder() == Order.ASC ? " asc" : " desc");
if (os.getNullHandling() == OrderSpecifier.NullHandling.NullsFirst) {
append(" nulls first");
} else if (os.getNullHandling() == OrderSpecifier.NullHandling.NullsLast) {
append(" nulls last");
}
first = false;
}
}
}
public void serializeForDelete(QueryMetadata md) {
append(DELETE);
handleJoinTarget(md.getJoins().get(0));
if (md.getWhere() != null) {
append(WHERE).handle(md.getWhere());
}
}
public void serializeForUpdate(QueryMetadata md, Map<Path<?>, Expression<?>> updates) {
append(UPDATE);
handleJoinTarget(md.getJoins().get(0));
append(SET);
boolean first = true;
for (Map.Entry<Path<?>, Expression<?>> entry : updates.entrySet()) {
if (!first) {
append(", ");
}
handle(entry.getKey());
append(" = ");
handle(entry.getValue());
first = false;
}
if (md.getWhere() != null) {
append(WHERE).handle(md.getWhere());
}
}
private void serializeSources(boolean forCountRow, List<JoinExpression> joins) {
for (int i = 0; i < joins.size(); i++) {
final JoinExpression je = joins.get(i);
if (i > 0) {
append(joinTypes.get(je.getType()));
}
if (je.hasFlag(JPAQueryMixin.FETCH) && !forCountRow) {
handle(JPAQueryMixin.FETCH);
}
handleJoinTarget(je);
// XXX Hibernate specific flag
if (je.hasFlag(JPAQueryMixin.FETCH_ALL_PROPERTIES) && !forCountRow) {
handle(JPAQueryMixin.FETCH_ALL_PROPERTIES);
}
if (je.getCondition() != null) {
append(templates.isWithForOn() ? WITH : ON);
handle(je.getCondition());
}
}
}
@Override
public void visitConstant(Object constant) {
if (inCaseOperation && templates.isCaseWithLiterals()) {
if (constant instanceof Collection) {
append("(");
boolean first = true;
for (Object o : (Collection) constant) {
if (!first) {
append(", ");
}
visitLiteral(o);
first = false;
}
append(")");
} else {
visitLiteral(constant);
}
} else {
boolean wrap = templates.wrapConstant(constant);
if (wrap) {
append("(");
}
append("?");
if (!getConstantToLabel().containsKey(constant)) {
final String constLabel = String.valueOf(getConstantToLabel().size() + 1);
getConstantToLabel().put(constant, constLabel);
append(constLabel);
} else {
append(getConstantToLabel().get(constant));
}
if (wrap) {
append(")");
}
}
}
public void visitLiteral(Object constant) {
append(templates.asLiteral(constant));
}
@Override
public Void visit(ParamExpression<?> param, Void context) {
append("?");
if (!getConstantToLabel().containsKey(param)) {
final String paramLabel = String.valueOf(getConstantToLabel().size() + 1);
getConstantToLabel().put(param, paramLabel);
append(paramLabel);
} else {
append(getConstantToLabel().get(param));
}
return null;
}
@Override
public Void visit(SubQueryExpression<?> query, Void context) {
append("(");
serialize(query.getMetadata(), false, null);
append(")");
return null;
}
@Override
public Void visit(Path<?> expr, Void context) {
// only wrap a PathCollection, if it the pathType is PROPERTY
boolean wrap = wrapElements
&& (Collection.class.isAssignableFrom(expr.getType()) || Map.class.isAssignableFrom(expr.getType()))
&& expr.getMetadata().getPathType().equals(PathType.PROPERTY);
if (wrap) {
append("elements(");
}
super.visit(expr, context);
if (wrap) {
append(")");
}
return null;
}
@Override
@SuppressWarnings("unchecked")
protected void visitOperation(Class<?> type, Operator operator, List<? extends Expression<?>> args) {
boolean oldInCaseOperation = inCaseOperation;
inCaseOperation = CASE_OPS.contains(operator);
boolean oldWrapElements = wrapElements;
wrapElements = templates.wrapElements(operator);
if (operator == Ops.EQ && args.get(1) instanceof Operation &&
((Operation) args.get(1)).getOperator() == Ops.QuantOps.ANY) {
args = ImmutableList.<Expression<?>>of(args.get(0), ((Operation) args.get(1)).getArg(0));
visitOperation(type, Ops.IN, args);
} else if (operator == Ops.NE && args.get(1) instanceof Operation &&
((Operation) args.get(1)).getOperator() == Ops.QuantOps.ANY) {
args = ImmutableList.<Expression<?>>of(args.get(0), ((Operation) args.get(1)).getArg(0));
visitOperation(type, Ops.NOT_IN, args);
} else if (operator == Ops.IN || operator == Ops.NOT_IN) {
if (args.get(1) instanceof Path) {
visitAnyInPath(type, operator, args);
} else if (args.get(0) instanceof Path && args.get(1) instanceof Constant<?>) {
visitPathInCollection(type, operator, args);
} else {
super.visitOperation(type, operator, args);
}
} else if (operator == Ops.NUMCAST) {
visitNumCast(args);
} else if (operator == Ops.EXISTS && args.get(0) instanceof SubQueryExpression) {
final SubQueryExpression subQuery = (SubQueryExpression) args.get(0);
append("exists (");
serialize(subQuery.getMetadata(), false, templates.getExistsProjection());
append(")");
} else if (operator == Ops.MATCHES || operator == Ops.MATCHES_IC) {
super.visitOperation(type, Ops.LIKE,
ImmutableList.of(args.get(0), ExpressionUtils.regexToLike((Expression<String>) args.get(1))));
} else if (operator == Ops.LIKE && args.get(1) instanceof Constant<?>) {
final String escape = String.valueOf(templates.getEscapeChar());
final String escaped = args.get(1).toString().replace(escape, escape + escape);
super.visitOperation(String.class, Ops.LIKE,
ImmutableList.of(args.get(0), ConstantImpl.create(escaped)));
} else if (NUMERIC.contains(operator)) {
super.visitOperation(type, operator, normalizeNumericArgs(args));
} else {
super.visitOperation(type, operator, args);
}
inCaseOperation = oldInCaseOperation;
wrapElements = oldWrapElements;
}
private void visitNumCast(List<? extends Expression<?>> args) {
@SuppressWarnings("unchecked") //this is the second argument's type
Constant<Class<?>> rightArg = (Constant<Class<?>>) args.get(1);
final Class<?> targetType = rightArg.getConstant();
final String typeName = templates.getTypeForCast(targetType);
visitOperation(targetType, JPQLOps.CAST, ImmutableList.of(args.get(0), ConstantImpl.create(typeName)));
}
private void visitPathInCollection(Class<?> type, Operator operator,
List<? extends Expression<?>> args) {
Path<?> lhs = (Path<?>) args.get(0);
@SuppressWarnings("unchecked")
Constant<? extends Collection<?>> rhs = (Constant<? extends Collection<?>>) args.get(1);
if (rhs.getConstant().isEmpty()) {
operator = operator == Ops.IN ? Ops.EQ : Ops.NE;
args = ImmutableList.of(Expressions.ONE, Expressions.TWO);
} else if (entityManager != null && !templates.isPathInEntitiesSupported() && args.get(0).getType().isAnnotationPresent(Entity.class)) {
final Metamodel metamodel = entityManager.getMetamodel();
final PersistenceUnitUtil util = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
final EntityType<?> entityType = metamodel.entity(args.get(0).getType());
if (entityType.hasSingleIdAttribute()) {
SingularAttribute<?,?> id = getIdProperty(entityType);
// turn lhs into id path
lhs = ExpressionUtils.path(id.getJavaType(), lhs, id.getName());
// turn rhs into id collection
Set<Object> ids = new HashSet<Object>();
for (Object entity : rhs.getConstant()) {
ids.add(util.getIdentifier(entity));
}
rhs = ConstantImpl.create(ids);
args = ImmutableList.of(lhs, rhs);
}
}
super.visitOperation(type, operator, args);
}
@SuppressWarnings({"rawtypes", "unchecked"})
private SingularAttribute<?,?> getIdProperty(EntityType entity) {
final Set<SingularAttribute> singularAttributes = entity.getSingularAttributes();
for (final SingularAttribute singularAttribute : singularAttributes) {
if (singularAttribute.isId()) {
return singularAttribute;
}
}
return null;
}
private void visitAnyInPath(Class<?> type, Operator operator, List<? extends Expression<?>> args) {
super.visitOperation(type,
operator == Ops.IN ? JPQLOps.MEMBER_OF : JPQLOps.NOT_MEMBER_OF,
args);
}
@SuppressWarnings("unchecked")
private List<? extends Expression<?>> normalizeNumericArgs(List<? extends Expression<?>> args) {
//we do not yet let it produce these types
//we verify the types with isAssignableFrom()
@SuppressWarnings("unchecked")
List<? extends Expression<? extends Number>> potentialArgs =
(List<? extends Expression<? extends Number>>) args;
boolean hasConstants = false;
Class<? extends Number> numType = null;
for (Expression<? extends Number> arg : potentialArgs) {
if (Number.class.isAssignableFrom(arg.getType())) {
if (arg instanceof Constant<?>) {
hasConstants = true;
} else {
numType = arg.getType();
}
}
}
if (hasConstants && numType != null) {
//now we do let the potentialArgs help us
final List<Expression<?>> newArgs = new ArrayList<Expression<?>>(args.size());
for (final Expression<? extends Number> arg : potentialArgs) {
if (arg instanceof Constant<?> && Number.class.isAssignableFrom(arg.getType())
&& !arg.getType().equals(numType)) {
final Number number = ((Constant<? extends Number>) arg).getConstant();
newArgs.add(ConstantImpl.create(MathUtils.cast(number, numType)));
} else {
newArgs.add(arg);
}
}
return newArgs;
} else {
//the types are all non-constants, or not Number expressions
return potentialArgs;
}
}
}