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
|
/*
* Copyright (c) 2011, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2021 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Gordon Yorke - Initial development
// 02/03/2017 - Dalia Abo Sheasha
// - 509693 : EclipseLink generates inconsistent SQL statements for SubQuery
package org.eclipse.persistence.internal.jpa.querydef;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.criteria.AbstractQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.Metamodel;
import org.eclipse.persistence.expressions.ExpressionBuilder;
/**
* <p>
* <b>Purpose</b>: Contains the implementation of the AbstractQuery interface of
* the JPA criteria API.
* <p>
* <b>Description</b>: This is the container class for the components that
* define a query. This is the superclass of both the CriteriaQuery and the
* SubQuery.
* <p>
*
* @see javax.persistence.criteria CriteriaQuery
*
* @author gyorke
* @since EclipseLink 1.2
*/
public abstract class AbstractQueryImpl<T> extends CommonAbstractCriteriaImpl<T> implements AbstractQuery<T> {
private static final long serialVersionUID = -5270020290752637882L;
protected ResultType queryResult;
protected boolean distinct;
protected Predicate havingClause;
protected List<Expression<?>> groupBy;
protected Set<Root<?>> roots;
protected org.eclipse.persistence.expressions.Expression baseExpression;
protected enum ResultType{
UNKNOWN, OBJECT_ARRAY, PARTIAL, TUPLE, ENTITY, CONSTRUCTOR, OTHER
}
public AbstractQueryImpl(Metamodel metamodel, ResultType queryResult, CriteriaBuilderImpl queryBuilder, Class<T> resultType){
super(metamodel, queryBuilder, resultType);
this.roots = new HashSet<Root<?>>();
this.queryResult = queryResult;
this.baseExpression = new ExpressionBuilder();
}
/**
* Specify the expressions that are used to form groups over
* the query results.
* Replaces the previous specified grouping expressions, if any.
* If no grouping expressions are specified, any previously
* added grouping expressions are simply removed.
* @param grouping list of zero or more grouping expressions
* @return the modified query
*/
@Override
public AbstractQuery<T> groupBy(List<Expression<?>> grouping){
this.groupBy = grouping;
return this;
}
/**
* Specify the expressions that are used to form groups over the query
* results. Replaces the previous specified grouping expressions, if any. If
* no grouping expressions are specified, any previously added grouping
* expressions are simply removed.
*
* @param grouping
* zero or more grouping expressions
* @return the modified query
*/
@Override
public AbstractQuery<T> groupBy(Expression<?>... grouping){
this.groupBy = new ArrayList<Expression<?>>();
for (Expression<?> exp : grouping){
this.groupBy.add(exp);
}
return this;
}
/**
* Specify a restriction over the groups of the query. Replaces the previous
* having restriction(s), if any.
*
* @param restriction
* a simple or compound boolean expression
* @return the modified query
*/
@Override
public AbstractQuery<T> having(Expression<Boolean> restriction) {
findRootAndParameters(restriction);
if (((InternalExpression)restriction).isCompoundExpression() || ((InternalExpression)restriction).isPredicate()) {
this.havingClause = (Predicate) restriction;
} else {
this.havingClause = queryBuilder.isTrue(restriction);
}
return this;
}
/**
* Specify restrictions over the groups of the query according the
* conjunction of the specified restriction predicates. Replaces the
* previously added restriction(s), if any. If no restrictions are
* specified, any previously added restrictions are simply removed.
*
* @param restrictions
* zero or more restriction predicates
* @return the modified query
*/
@Override
public AbstractQuery<T> having(Predicate... restrictions){
if (restrictions != null && restrictions.length > 0) {
Predicate conjunction = this.queryBuilder.conjunction();
for (Predicate predicate : restrictions) {
conjunction = this.queryBuilder.and(conjunction, predicate);
}
findRootAndParameters(conjunction);
this.havingClause = conjunction;
}
return this;
}
public abstract void addJoin(FromImpl join);
/**
* Specify whether duplicate query results will be eliminated. A true value
* will cause duplicates to be eliminated. A false value will cause
* duplicates to be retained. If distinct has not been specified, duplicate
* results must be retained. This method only overrides the return type of
* the corresponding AbstractQuery method.
*
* @param distinct
* boolean value specifying whether duplicate results must be
* eliminated from the query result or whether they must be
* retained
* @return the modified query.
*/
@Override
public AbstractQuery<T> distinct(boolean distinct){
this.distinct= distinct;
return this;
}
@Override
protected org.eclipse.persistence.expressions.Expression getBaseExpression() {
return getBaseExpression(null);
}
protected org.eclipse.persistence.expressions.Expression getBaseExpression(Root root) {
if (this.roots.isEmpty()) {
baseExpression = new ExpressionBuilder();
} else if (this.roots.size() == 1) {
baseExpression = ((RootImpl) this.roots.iterator().next()).getCurrentNode();
} else if (root != null) {
for (Root r : this.roots) {
if (r == root) {
baseExpression = ((RootImpl) r).getCurrentNode();
}
}
}
return baseExpression;
}
/**
* Return a list of the grouping expressions
* @return the list of grouping expressions
*/
@Override
public List<Expression<?>> getGroupList(){
if (this.groupBy == null){
this.groupBy = new ArrayList<Expression<?>>();
}
return this.groupBy;
}
/**
* Return the predicate that corresponds to the restriction(s) over the
* grouping items.
*
* @return having clause predicate
*/
@Override
public Predicate getGroupRestriction(){
return this.havingClause;
}
/**
* Return the query roots.
*
* @return the set of query roots
*/
@Override
public Set<Root<?>> getRoots(){
return this.roots;
}
@Override
protected void integrateRoot(RootImpl root) {
if (!this.roots.contains(root)) {
this.roots.add(root);
}
}
/**
* Return whether duplicate query results must be eliminated or retained.
*
* @return boolean indicating whether duplicate query results must be
* eliminated
*/
@Override
public boolean isDistinct(){
return this.distinct;
}
protected void findJoins(FromImpl root) {
root.findJoins(this);
}
/**
* Add a query root corresponding to the given entity, forming a Cartesian
* product with any existing roots.
*
* @param entity
* metamodel entity representing the entity of type X
* @return query root corresponding to the given entity
*/
@Override
public <X> Root<X> from(EntityType<X> entity) {
return this.internalFrom(entity);
}
/**
* Add a query root corresponding to the given entity, forming a Cartesian
* product with any existing roots.
*
* @param entityClass
* the entity class
* @return query root corresponding to the given entity
*/
@Override
public <X> Root<X> from(Class<X> entityClass) {
return this.internalFrom(entityClass);
}
// override the return type only:
/**
* Modify the query to restrict the query result according to the specified
* boolean expression. Replaces the previously added restriction(s), if any.
* This method only overrides the return type of the corresponding
* AbstractQuery method.
*
* @param restriction
* a simple or compound boolean expression
* @return the modified query
*/
@Override
public AbstractQuery<T> where(Expression<Boolean> restriction){
return (AbstractQuery<T>)super.where(restriction);
}
/**
* Modify the query to restrict the query result according to the
* conjunction of the specified restriction predicates. Replaces the
* previously added restriction(s), if any. If no restrictions are
* specified, any previously added restrictions are simply removed. This
* method only overrides the return type of the corresponding AbstractQuery
* method.
*
* @param restrictions
* zero or more restriction predicates
* @return the modified query
*/
@Override
public AbstractQuery<T> where(Predicate... restrictions) {
return (AbstractQuery<T>) super.where(restrictions);
}
}
|