io.katharsis.jpa.internal.query.backend.criteria.JpaCriteriaQueryExecutorImpl Maven / Gradle / Ivy
package io.katharsis.jpa.internal.query.backend.criteria;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import io.katharsis.jpa.internal.meta.MetaDataObject;
import io.katharsis.jpa.internal.query.AbstractQueryExecutorImpl;
import io.katharsis.jpa.internal.query.QueryUtil;
import io.katharsis.jpa.internal.query.backend.querydsl.ObjectArrayTupleImpl;
import io.katharsis.jpa.query.criteria.JpaCriteriaQueryExecutor;
public class JpaCriteriaQueryExecutorImpl extends AbstractQueryExecutorImpl implements JpaCriteriaQueryExecutor {
private CriteriaQuery query;
public JpaCriteriaQueryExecutorImpl(EntityManager em, MetaDataObject meta, CriteriaQuery criteriaQuery,
int numAutoSelections, Map selectionBindings) {
super(em, meta, numAutoSelections, selectionBindings);
this.query = criteriaQuery;
}
public CriteriaQuery getQuery() {
return query;
}
@Override
protected List> executeQuery() {
TypedQuery typedQuery = em.createQuery(query);
return executeQuery(typedQuery);
}
@Override
protected boolean isCompoundSelection() {
return query.getSelection().isCompoundSelection();
}
@Override
protected boolean isDistinct() {
return query.isDistinct();
}
@Override
protected boolean hasManyRootsFetchesOrJoins() {
return QueryUtil.hasManyRootsFetchesOrJoins(query);
}
@Override
@SuppressWarnings({ "rawtypes" })
public long getTotalRowCount() {
Selection selection = query.getSelection();
List orderList = query.getOrderList();
try {
CriteriaBuilder builder = em.getCriteriaBuilder();
Expression countExpr;
Set> roots = query.getRoots();
if (roots.size() != 1) {
throw new IllegalStateException("cannot compute totalRowCount in case of multiple query roots");
}
if (!query.getGroupList().isEmpty()) {
throw new IllegalStateException("cannot compute totalRowCount for grouped queries");
}
// transform query to a count query
Root root = roots.iterator().next();
countExpr = builder.count(root);
query.multiselect(countExpr);
query.orderBy(new ArrayList());
TypedQuery countQuery = em.createQuery(query);
return (Long) countQuery.getSingleResult();
}
finally {
// transform count query back to regular query
query.multiselect(selection);
query.orderBy(orderList);
}
}
@Override
public List getResultTuples() {
List> results = executeQuery();
List tuples = new ArrayList<>();
for (Object result : results) {
if (result instanceof Object[]) {
tuples.add(new CriteriaTupleImpl((Object[]) result, selectionBindings));
}
else {
tuples.add(new ObjectArrayTupleImpl(result));
}
}
return tuples;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy