OPENJPA-1444: distinct query

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@893447 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-12-23 09:33:28 +00:00
parent 3dee418d36
commit 29e919f2cc
9 changed files with 32 additions and 58 deletions

View File

@ -464,6 +464,10 @@ public class QueryCacheStoreQuery
return _ex.isAggregate(unwrap(q));
}
public boolean isDistinct(StoreQuery q) {
return _ex.isDistinct(unwrap(q));
}
public boolean hasGrouping(StoreQuery q) {
return _ex.hasGrouping(unwrap(q));
}

View File

@ -185,6 +185,10 @@ public abstract class AbstractStoreQuery
return false;
}
public boolean isDistinct(StoreQuery q) {
return false;
}
public boolean hasGrouping(StoreQuery q) {
return false;
}

View File

@ -343,15 +343,6 @@ public class DelegatingQuery
}
}
public void setDistinct(boolean distinct) {
try {
_query.setDistinct(distinct);
} catch (RuntimeException re) {
throw translate(re);
}
}
public Class getResultType() {
try {
return _query.getResultType();

View File

@ -383,6 +383,10 @@ public class ExpressionStoreQuery
return assertQueryExpression().isAggregate();
}
public final boolean isDistinct(StoreQuery q) {
return assertQueryExpression().isDistinct();
}
public final boolean hasGrouping(StoreQuery q) {
return assertQueryExpression().grouping.length > 0;
}

View File

@ -126,13 +126,6 @@ public interface QueryContext {
*/
public boolean isDistinct();
/**
* Specify that the query will return distinct instances.
*
* @since 2.0.0
*/
public void setDistinct(boolean distinct);
/**
* Scope of a mapping from the result data to its object representation.

View File

@ -119,7 +119,6 @@ public class QueryImpl
// these fields should only be used directly after we have a compilation,
// because their values may be encoded in the query string
private Boolean _unique = null;
private boolean _distinct = false;
private Class<?> _resultClass = null;
private transient long _startIdx = 0;
private transient long _endIdx = Long.MAX_VALUE;
@ -471,27 +470,6 @@ public class QueryImpl
}
}
/**
* Sets this query to return distinct result.
*/
public void setDistinct(boolean flag) {
lock();
try {
assertOpen();
// allowed modification: no read-only check
_distinct = flag;
} finally {
unlock();
}
}
/**
* Affirms if this query will return distinct elements.
*/
public boolean isDistinct() {
return _distinct;
}
/**
* Affirms if this query has originated by parsing a string-based query.
*/
@ -1549,6 +1527,16 @@ public class QueryImpl
}
}
public boolean isDistinct() {
lock();
try {
return compileForExecutor().isDistinct(_storeQuery);
} finally {
unlock();
}
}
public boolean hasGrouping() {
lock();
try {
@ -2015,6 +2003,10 @@ public class QueryImpl
q.getContext().getQueryString()));
}
public boolean isDistinct(StoreQuery q) {
return _executors[0].isDistinct(q);
}
public int getOperation(StoreQuery q) {
return _executors[0].getOperation(q);
}

View File

@ -106,6 +106,10 @@ public class QueryExpressions
return _aggregate.booleanValue();
}
public boolean isDistinct() {
return distinct != DISTINCT_FALSE;
}
/**
* Add an update.
*/

View File

@ -168,13 +168,9 @@ class CriteriaExpressionBuilder {
}
protected void evalDistinct(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) {
if (q.hasFetchJoins()) {
exps.distinct = QueryExpressions.DISTINCT_FALSE;
} else {
exps.distinct = q.isDistinct() ? QueryExpressions.DISTINCT_TRUE | QueryExpressions.DISTINCT_AUTO
: QueryExpressions.DISTINCT_FALSE;
}
}
protected void evalCrossJoinRoots(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) {
Set<Root<?>> roots = q.getRoots();
@ -267,7 +263,6 @@ class CriteriaExpressionBuilder {
selections = new ArrayList<Selection<?>>(1);
selections.add(r);
}
boolean usingFetchJoin = q.hasFetchJoins();
for (Selection<?> s : selections) {
if (s.isCompoundSelection()) {
getProjections(exps, s.getCompoundSelectionItems(), projections, aliases,
@ -275,8 +270,6 @@ class CriteriaExpressionBuilder {
} else {
Value val = (exp2Vals != null && exp2Vals.containsKey(s)
? exp2Vals.get(s) : ((ExpressionImpl<?>)s).toValue(factory, q));
if (q.isDistinct() && usingFetchJoin)
val = factory.distinct(val);
String alias = s.getAlias();
val.setAlias(alias);
projections.add(val);

View File

@ -649,17 +649,6 @@ class CriteriaQueryImpl<T> implements OpenJPACriteriaQuery<T>, AliasContext {
}
}
boolean hasFetchJoins() {
Set<Root<?>> roots = getRoots();
if (roots == null || roots.isEmpty())
return false;
for (Root<?> root : roots) {
if (!root.getFetches().isEmpty())
return true;
}
return false;
}
/**
* Gets the string representation of the query.
*/