Skip to content

Package: CriteriaQueryImpl

CriteriaQueryImpl

nameinstructionbranchcomplexitylinemethod
CriteriaQueryImpl(CriteriaQueryHolder, Metamodel, CriteriaBuilderImpl)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
distinct()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
distinct(boolean)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
from(Class)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
from(EntityType)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getOrderList()
M: 0 C: 13
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getRestriction()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getResultType()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSelection()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
groupBy(Expression[])
M: 17 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
groupBy(List)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
having(Expression)
M: 17 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
having(Predicate[])
M: 9 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
isDistinct()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
orderBy(List)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
orderBy(Order[])
M: 4 C: 13
76%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 3
75%
M: 0 C: 1
100%
select(Selection)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
translateQuery(CriteriaParameterFiller)
M: 38 C: 122
76%
M: 6 C: 16
73%
M: 4 C: 8
67%
M: 6 C: 21
78%
M: 0 C: 1
100%
where(Expression)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
where(List)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
where(Predicate[])
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2022 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.model;
16:
17: import cz.cvut.kbss.jopa.model.metamodel.EntityType;
18: import cz.cvut.kbss.jopa.model.metamodel.Metamodel;
19: import cz.cvut.kbss.jopa.model.query.criteria.*;
20: import cz.cvut.kbss.jopa.query.criteria.*;
21: import cz.cvut.kbss.jopa.query.criteria.expressions.AbstractExpression;
22: import cz.cvut.kbss.jopa.utils.ErrorUtils;
23:
24: import java.util.*;
25:
26:
27: public class CriteriaQueryImpl<T> implements CriteriaQuery<T> {
28:
29: protected final CriteriaQueryHolder<T> query;
30: private final Metamodel metamodel;
31: private final CriteriaBuilderImpl cb;
32:
33:
34: public CriteriaQueryImpl(CriteriaQueryHolder<T> query, Metamodel metamodel, CriteriaBuilderImpl cb) {
35: this.query = Objects.requireNonNull(query, ErrorUtils.getNPXMessageSupplier("query"));
36: this.metamodel = metamodel;
37: this.cb = cb;
38: }
39:
40: @Override
41: public <X> Root<X> from(Class<X> entityClass) {
42: RootImpl<X> root = new RootImpl<>(metamodel, null, entityClass, this.cb);
43: query.setRoot(root);
44: return root;
45: }
46:
47: @Override
48: public <X> Root<X> from(EntityType<X> entity) {
49: RootImpl<X> root = new RootImpl<>(metamodel, null, entity.getBindableJavaType(), this.cb);
50: query.setRoot(root);
51: return root;
52: }
53:
54: @Override
55: public CriteriaQuery<T> select(Selection<? extends T> selection) {
56: query.setSelection(selection);
57: return this;
58: }
59:
60: @Override
61: public CriteriaQuery<T> where(Expression<Boolean> expression) {
62: query.setWhere(cb.wrapExpressionToPredicateWithRepair(expression));
63: return this;
64: }
65:
66: @Override
67: public CriteriaQuery<T> where(Predicate... predicates) {
68: query.setWhere(cb.and(predicates));
69: return this;
70: }
71:
72: @Override
73: public CriteriaQuery<T> where(List<Predicate> predicates) {
74: return this.where(predicates.toArray(new Predicate[0]));
75: }
76:
77: @Override
78: public Class<T> getResultType() {
79: return query.getResultType();
80: }
81:
82: @Override
83: public CriteriaQuery<T> distinct(boolean b) {
84: query.setDistinct(b);
85: return this;
86: }
87:
88: @Override
89: public CriteriaQuery<T> distinct() {
90: return this.distinct(true);
91: }
92:
93: @Override
94: public boolean isDistinct() {
95: return query.isDistinct();
96: }
97:
98: @Override
99: public Selection<T> getSelection() {
100: return (Selection<T>) query.getSelection();
101: }
102:
103: @Override
104: public Predicate getRestriction() {
105: return query.getWhere();
106: }
107:
108: @Override
109: public CriteriaQuery<T> orderBy(List<Order> o) {
110:• if (!o.isEmpty()) {
111: query.setOrderBy(o);
112: } else {
113: query.setOrderBy(null);
114: }
115: return this;
116: }
117:
118: @Override
119: public CriteriaQuery<T> orderBy(Order... o) {
120:• if (o != null && o.length > 0) {
121: query.setOrderBy(Arrays.asList(o));
122: } else {
123: query.setOrderBy(null);
124: }
125: return this;
126: }
127:
128: @Override
129: public List<Order> getOrderList() {
130:• if (query.getOrderBy() == null) {
131: return Collections.emptyList();
132: }
133: return new ArrayList<>(query.getOrderBy());
134: }
135:
136:
137: @Override
138: public CriteriaQuery<T> groupBy(Expression<?>... grouping) {
139:• if (grouping != null && grouping.length > 0) {
140: query.setGroupBy(Arrays.asList(grouping));
141: } else {
142: query.setGroupBy(null);
143: }
144: return this;
145: }
146:
147: @Override
148: public CriteriaQuery<T> groupBy(List<Expression<?>> grouping) {
149:• if (!grouping.isEmpty()) {
150: query.setGroupBy(grouping);
151: } else {
152: query.setGroupBy(null);
153: }
154: return this;
155: }
156:
157: @Override
158: public CriteriaQuery<T> having(Expression<Boolean> restriction) {
159: query.setHaving(cb.and(cb.wrapExpressionToPredicateWithRepair(restriction)));
160: return this;
161: }
162:
163: @Override
164: public CriteriaQuery<T> having(Predicate... restrictions) {
165: query.setHaving(cb.and(restrictions));
166: return this;
167: }
168:
169: /**
170: * Method translates criteria query to SOQL query and returns its string representation.
171: *
172: * @param parameterFiller Generator of parameter values in the query string
173: * @return string representation of SOQL query
174: */
175: public String translateQuery(CriteriaParameterFiller parameterFiller) {
176: StringBuilder stringBuilder = new StringBuilder();
177: stringBuilder.append("SELECT ");
178:• if (isDistinct()) {
179: stringBuilder.append("DISTINCT ");
180: }
181: ((AbstractExpression) query.getSelection()).setExpressionToQuery(stringBuilder, parameterFiller);
182:
183: stringBuilder.append(" FROM ").append(((RootImpl) query.getRoot()).getJavaType().getSimpleName()).append(' ');
184: ((RootImpl) query.getRoot()).setExpressionToQuery(stringBuilder, parameterFiller);
185:
186:• if (query.getWhere() != null) {
187: stringBuilder.append(" WHERE ");
188: ((AbstractPredicate) query.getWhere()).setExpressionToQuery(stringBuilder, parameterFiller);
189: }
190:
191:• if (query.getGroupBy() != null && !query.getGroupBy().isEmpty()) {
192: stringBuilder.append(" GROUP BY ");
193:• for (Expression groupBy : query.getGroupBy()) {
194: ((AbstractExpression) groupBy).setExpressionToQuery(stringBuilder, parameterFiller);
195: }
196: }
197:
198:• if (query.getHaving() != null) {
199: stringBuilder.append(" HAVING ");
200: ((AbstractPredicate) query.getHaving()).setExpressionToQuery(stringBuilder, parameterFiller);
201: }
202:
203:• if (!getOrderList().isEmpty()) {
204: stringBuilder.append(" ORDER BY ");
205: List<Order> orders = getOrderList();
206:• for (int i = 0; i < orders.size(); i++) {
207: ((AbstractExpression) orders.get(i).getExpression()).setExpressionToQuery(stringBuilder, parameterFiller);
208:• stringBuilder.append(orders.get(i).isAscending() ? " ASC" : " DESC");
209:• if (orders.size() > 1 && (i + 1) != orders.size()) {
210: stringBuilder.append(", ");
211: }
212: }
213: }
214: return stringBuilder.toString();
215: }
216: }