Package: CriteriaBuilderImpl
CriteriaBuilderImpl
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
CriteriaBuilderImpl(UnitOfWorkImpl) |
|
|
|
|
|
||||||||||||||||||||
abs(Expression) |
|
|
|
|
|
||||||||||||||||||||
and(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
and(Predicate[]) |
|
|
|
|
|
||||||||||||||||||||
asc(Expression) |
|
|
|
|
|
||||||||||||||||||||
ceil(Expression) |
|
|
|
|
|
||||||||||||||||||||
count(Expression) |
|
|
|
|
|
||||||||||||||||||||
createQuery(Class) |
|
|
|
|
|
||||||||||||||||||||
desc(Expression) |
|
|
|
|
|
||||||||||||||||||||
equal(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
equal(Expression, Object) |
|
|
|
|
|
||||||||||||||||||||
equal(Expression, String, String) |
|
|
|
|
|
||||||||||||||||||||
floor(Expression) |
|
|
|
|
|
||||||||||||||||||||
greaterThan(Expression, Comparable) |
|
|
|
|
|
||||||||||||||||||||
greaterThan(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
greaterThanOrEqual(Expression, Comparable) |
|
|
|
|
|
||||||||||||||||||||
greaterThanOrEqual(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
in(Expression) |
|
|
|
|
|
||||||||||||||||||||
isMember(Object, Expression) |
|
|
|
|
|
||||||||||||||||||||
isNotMember(Object, Expression) |
|
|
|
|
|
||||||||||||||||||||
lang(Path) |
|
|
|
|
|
||||||||||||||||||||
length(Expression) |
|
|
|
|
|
||||||||||||||||||||
lessThan(Expression, Comparable) |
|
|
|
|
|
||||||||||||||||||||
lessThan(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
lessThanOrEqual(Expression, Comparable) |
|
|
|
|
|
||||||||||||||||||||
lessThanOrEqual(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
like(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
like(Expression, String) |
|
|
|
|
|
||||||||||||||||||||
literal(Object) |
|
|
|
|
|
||||||||||||||||||||
literal(String, String) |
|
|
|
|
|
||||||||||||||||||||
lower(Expression) |
|
|
|
|
|
||||||||||||||||||||
not(Expression) |
|
|
|
|
|
||||||||||||||||||||
notEqual(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
notEqual(Expression, Object) |
|
|
|
|
|
||||||||||||||||||||
notIn(Expression) |
|
|
|
|
|
||||||||||||||||||||
notLike(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
notLike(Expression, String) |
|
|
|
|
|
||||||||||||||||||||
or(Expression, Expression) |
|
|
|
|
|
||||||||||||||||||||
or(Predicate[]) |
|
|
|
|
|
||||||||||||||||||||
parameter(Class) |
|
|
|
|
|
||||||||||||||||||||
parameter(Class, String) |
|
|
|
|
|
||||||||||||||||||||
upper(Expression) |
|
|
|
|
|
||||||||||||||||||||
validateFunctionArgument(Expression) |
|
|
|
|
|
||||||||||||||||||||
wrapExpressionToPredicateWithRepair(Expression) |
|
|
|
|
|
Coverage
1: /*
2: * Copyright (C) 2023 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.criteria;
16:
17: import cz.cvut.kbss.jopa.model.CriteriaQueryImpl;
18: import cz.cvut.kbss.jopa.model.query.criteria.Expression;
19: import cz.cvut.kbss.jopa.model.query.criteria.Order;
20: import cz.cvut.kbss.jopa.model.query.criteria.ParameterExpression;
21: import cz.cvut.kbss.jopa.model.query.criteria.Path;
22: import cz.cvut.kbss.jopa.model.query.criteria.Predicate;
23: import cz.cvut.kbss.jopa.query.criteria.expressions.AbsFunction;
24: import cz.cvut.kbss.jopa.query.criteria.expressions.AbstractExpression;
25: import cz.cvut.kbss.jopa.query.criteria.expressions.AbstractPathExpression;
26: import cz.cvut.kbss.jopa.query.criteria.expressions.CeilFunction;
27: import cz.cvut.kbss.jopa.query.criteria.expressions.CountFunction;
28: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionEqualImpl;
29: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionGreaterThanImpl;
30: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionGreaterThanOrEqualImpl;
31: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionInImpl;
32: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionLessThanImpl;
33: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionLessThanOrEqualImpl;
34: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionLikeImpl;
35: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionLiteralImpl;
36: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionNotEqualImpl;
37: import cz.cvut.kbss.jopa.query.criteria.expressions.ExpressionNotLikeImpl;
38: import cz.cvut.kbss.jopa.query.criteria.expressions.FloorFunction;
39: import cz.cvut.kbss.jopa.query.criteria.expressions.IsMemberExpression;
40: import cz.cvut.kbss.jopa.query.criteria.expressions.LangFunction;
41: import cz.cvut.kbss.jopa.query.criteria.expressions.LengthFunction;
42: import cz.cvut.kbss.jopa.query.criteria.expressions.LowerFunction;
43: import cz.cvut.kbss.jopa.query.criteria.expressions.OrderImpl;
44: import cz.cvut.kbss.jopa.query.criteria.expressions.ParameterExpressionImpl;
45: import cz.cvut.kbss.jopa.query.criteria.expressions.UpperFunction;
46: import cz.cvut.kbss.jopa.sessions.CriteriaBuilder;
47: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
48:
49: import java.util.Arrays;
50: import java.util.Collection;
51:
52: public class CriteriaBuilderImpl implements CriteriaBuilder {
53:
54: private final UnitOfWorkImpl uow;
55:
56: public CriteriaBuilderImpl(UnitOfWorkImpl uow) {
57: this.uow = uow;
58: }
59:
60: @Override
61: public <T> CriteriaQueryImpl<T> createQuery(Class<T> resultClass) {
62: return new CriteriaQueryImpl<>(new CriteriaQueryHolder<>(resultClass), uow.getMetamodel(), this);
63: }
64:
65: @Override
66: public <N extends Number> Expression<N> abs(Expression<N> x) {
67: validateFunctionArgument(x);
68: return new AbsFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
69: }
70:
71: private void validateFunctionArgument(Expression<?> x) {
72:• if (!(x instanceof AbstractPathExpression)) {
73: throw new IllegalArgumentException("Function can be applied only to path expressions.");
74: }
75: }
76:
77: @Override
78: public Expression<Integer> count(Expression<?> x) {
79: validateFunctionArgument(x);
80: return new CountFunction((AbstractPathExpression) x, this);
81: }
82:
83: @Override
84: public <N extends Number> Expression<N> ceil(Expression<N> x) {
85: validateFunctionArgument(x);
86: return new CeilFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
87: }
88:
89: @Override
90: public <N extends Number> Expression<N> floor(Expression<N> x) {
91: validateFunctionArgument(x);
92: return new FloorFunction<>((Class<N>) x.getJavaType(), (AbstractPathExpression) x, this);
93: }
94:
95: @Override
96: public Expression<Integer> length(Expression<String> x) {
97: validateFunctionArgument(x);
98: return new LengthFunction((AbstractPathExpression) x, this);
99: }
100:
101: @Override
102: public <T> ParameterExpression<T> parameter(Class<T> paramClass) {
103:• if (paramClass == null) {
104: throw new IllegalArgumentException("Class must be defined.");
105: }
106: return new ParameterExpressionImpl<>(paramClass, null, this);
107: }
108:
109: @Override
110: public <T> ParameterExpression<T> parameter(Class<T> paramClass, String name) {
111:• if (paramClass == null) {
112: throw new IllegalArgumentException("Class must be defined.");
113: }
114: return new ParameterExpressionImpl<>(paramClass, name, this);
115: }
116:
117: @Override
118: public <T> Expression<T> literal(T value) {
119:• if (value == null) {
120: throw new IllegalArgumentException("Literal cannot be null.");
121: }
122: return new ExpressionLiteralImpl<>(value, this);
123: }
124:
125: @Override
126: public Expression<String> literal(String value, String languageTag) {
127:• if (value == null) {
128: throw new IllegalArgumentException("Literal cannot be null.");
129: }
130: return new ExpressionLiteralImpl<>(value, languageTag, this);
131: }
132:
133: @Override
134: public Expression<String> lower(Expression<String> x) {
135: validateFunctionArgument(x);
136: return new LowerFunction((AbstractPathExpression) x, this);
137: }
138:
139: @Override
140: public Expression<String> upper(Expression<String> x) {
141: validateFunctionArgument(x);
142: return new UpperFunction((AbstractPathExpression) x, this);
143: }
144:
145: @Override
146: public Expression<String> lang(Path<String> x) {
147: validateFunctionArgument(x);
148: return new LangFunction((AbstractPathExpression) x, this);
149: }
150:
151: @Override
152: public Order asc(Expression<?> x) {
153: return new OrderImpl(x);
154: }
155:
156: @Override
157: public Order desc(Expression<?> x) {
158: return new OrderImpl(x, false);
159: }
160:
161:
162: @Override
163: public Predicate and(Expression<Boolean> x, Expression<Boolean> y) {
164: return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(x, y), this);
165: }
166:
167: @Override
168: public Predicate and(Predicate... restrictions) {
169:• if (restrictions.length == 1) {
170: return new SimplePredicateImpl(restrictions[0], this);
171: } else {
172: return new CompoundedPredicateImpl(Predicate.BooleanOperator.AND, Arrays.asList(restrictions), this);
173: }
174: }
175:
176: @Override
177: public Predicate or(Expression<Boolean> x, Expression<Boolean> y) {
178: return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(x, y), this);
179: }
180:
181: @Override
182: public Predicate or(Predicate... restrictions) {
183:• if (restrictions.length == 1) {
184: return new SimplePredicateImpl(Predicate.BooleanOperator.OR, restrictions[0], this);
185: } else {
186: return new CompoundedPredicateImpl(Predicate.BooleanOperator.OR, Arrays.asList(restrictions), this);
187: }
188: }
189:
190: @Override
191: public Predicate equal(Expression<?> x, Expression<?> y) {
192: return new SimplePredicateImpl(
193: new ExpressionEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
194: }
195:
196: @Override
197: public Predicate equal(Expression<?> x, Object y) {
198: return new SimplePredicateImpl(
199: new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this), this);
200: }
201:
202: @Override
203: public Predicate equal(Expression<?> x, String y, String languageTag) {
204: return new SimplePredicateImpl(
205: new ExpressionEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, languageTag, this),
206: this), this);
207: }
208:
209: @Override
210: public Predicate notEqual(Expression<?> x, Expression<?> y) {
211: return new SimplePredicateImpl(
212: new ExpressionNotEqualImpl((AbstractExpression<?>) x, (AbstractExpression<?>) y, this), this);
213: }
214:
215: @Override
216: public Predicate notEqual(Expression<?> x, Object y) {
217: return new SimplePredicateImpl(
218: new ExpressionNotEqualImpl((AbstractExpression<?>) x, new ExpressionLiteralImpl<>(y, this), this),
219: this);
220:
221: }
222:
223: @Override
224: public Predicate like(Expression<String> x, Expression<String> pattern) {
225: return new SimplePredicateImpl(
226: new ExpressionLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this),
227: this);
228: }
229:
230: @Override
231: public Predicate like(Expression<String> x, String pattern) {
232: return new SimplePredicateImpl(
233: new ExpressionLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this),
234: this), this);
235: }
236:
237: @Override
238: public Predicate notLike(Expression<String> x, Expression<String> pattern) {
239: return new SimplePredicateImpl(
240: new ExpressionNotLikeImpl((AbstractExpression<String>) x, (AbstractExpression<String>) pattern, this),
241: this);
242: }
243:
244: @Override
245: public Predicate notLike(Expression<String> x, String pattern) {
246: return new SimplePredicateImpl(
247: new ExpressionNotLikeImpl((AbstractExpression<String>) x, new ExpressionLiteralImpl<>(pattern, this),
248: this), this);
249: }
250:
251: @Override
252: public Predicate not(Expression<Boolean> restriction) {
253: return wrapExpressionToPredicateWithRepair(restriction).not();
254: }
255:
256: @Override
257: public <T> In<T> in(Expression<? extends T> expression) {
258: return new ExpressionInImpl<>(expression, this);
259: }
260:
261: @Override
262: public <T> In<T> notIn(Expression<? extends T> expression) {
263: In<T> inExpression = new ExpressionInImpl<>(expression, this);
264: inExpression.not();
265: return inExpression;
266: }
267:
268: @Override
269: public <E, C extends Collection<E>> Predicate isMember(E elem, Expression<C> collection) {
270: return new IsMemberExpression<>(elem, collection, this);
271: }
272:
273: @Override
274: public <E, C extends Collection<E>> Predicate isNotMember(E elem, Expression<C> collection) {
275: final IsMemberExpression<E> expr = new IsMemberExpression<>(elem, collection, this);
276: expr.not();
277: return expr;
278: }
279:
280: @Override
281: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x,
282: Expression<? extends Y> y) {
283: return new SimplePredicateImpl(
284: new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
285: }
286:
287: @Override
288: public <Y extends Comparable<? super Y>> Predicate greaterThan(Expression<? extends Y> x, Y y) {
289: return new SimplePredicateImpl(
290: new ExpressionGreaterThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this),
291: this);
292: }
293:
294: @Override
295: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x,
296: Expression<? extends Y> y) {
297: return new SimplePredicateImpl(
298: new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
299: }
300:
301: @Override
302: public <Y extends Comparable<? super Y>> Predicate greaterThanOrEqual(Expression<? extends Y> x, Y y) {
303: return new SimplePredicateImpl(
304: new ExpressionGreaterThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this),
305: this), this);
306: }
307:
308: @Override
309: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Expression<? extends Y> y) {
310: return new SimplePredicateImpl(
311: new ExpressionLessThanImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
312: }
313:
314: @Override
315: public <Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y) {
316: return new SimplePredicateImpl(
317: new ExpressionLessThanImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this), this),
318: this);
319: }
320:
321: @Override
322: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x,
323: Expression<? extends Y> y) {
324: return new SimplePredicateImpl(
325: new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, (AbstractExpression<Y>) y, this), this);
326: }
327:
328: @Override
329: public <Y extends Comparable<? super Y>> Predicate lessThanOrEqual(Expression<? extends Y> x, Y y) {
330: return new SimplePredicateImpl(
331: new ExpressionLessThanOrEqualImpl((AbstractExpression<Y>) x, new ExpressionLiteralImpl<>(y, this),
332: this), this);
333: }
334:
335:
336: /**
337: * Method wraps given boolean expression to Predicate and if path expression occur, it wrap it to
338: * ExpressionEqualsImpl before. For example: {@literal Expression<Boolean> expression =
339: * factory.get("attributeName");} Looks like boolean expression but in fact it is not boolean expression, so we need
340: * to fix this.
341: *
342: * @param expression - boolean or path expression
343: * @return Expression wrapped in Predicate
344: */
345: public Predicate wrapExpressionToPredicateWithRepair(Expression<Boolean> expression) {
346:• if (expression instanceof Predicate) {
347: return (Predicate) expression;
348:• } else if (expression instanceof AbstractPathExpression) {
349: return new SimplePredicateImpl(
350: new ExpressionEqualImpl((AbstractExpression) expression, (AbstractExpression) this.literal(true),
351: this), this);
352: } else {
353: return new SimplePredicateImpl(expression, this);
354: }
355: }
356:
357: }