mirror of https://github.com/apache/openjpa.git
OPENJPA-1013: Constructor support in Criteria Query. Test case will be checked in later.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@776136 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
2ce5299a73
commit
db43c7443d
|
@ -540,7 +540,7 @@ public class CriteriaBuilder implements QueryBuilder, ExpressionParser {
|
|||
|
||||
public <Y> Selection<Y> select(Class<Y> result,
|
||||
Selection<?>... selections) {
|
||||
throw new AbstractMethodError();
|
||||
return new SelectionImpl(result).setSelections(selections);
|
||||
}
|
||||
|
||||
public <R> Case<R> selectCase() {
|
||||
|
|
|
@ -62,6 +62,9 @@ public class CriteriaQueryImpl implements CriteriaQuery {
|
|||
private PredicateImpl _having;
|
||||
private Boolean _distinct;
|
||||
private LinkedMap _parameterTypes;
|
||||
private Class _resultClass;
|
||||
private Value[] _projections;
|
||||
private int _aliasCount = 0;
|
||||
|
||||
public CriteriaQueryImpl(MetamodelImpl model) {
|
||||
this._model = model;
|
||||
|
@ -177,6 +180,14 @@ public class CriteriaQueryImpl implements CriteriaQuery {
|
|||
_parameterTypes = parameterTypes;
|
||||
}
|
||||
|
||||
public void setResultClass(Class resultClass) {
|
||||
_resultClass = resultClass;
|
||||
}
|
||||
|
||||
public void setProjections(Value[] projections) {
|
||||
_projections = projections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate kernel expressions.
|
||||
*/
|
||||
|
@ -206,14 +217,13 @@ public class CriteriaQueryImpl implements CriteriaQuery {
|
|||
evalOrdering(exps, factory);
|
||||
// exps.operation = QueryOperations.OP_SELECT;
|
||||
|
||||
// exps.parameterTypes = null; // LinkedMap<>
|
||||
// exps.projectionAliases = null; // String[]
|
||||
// exps.projectionClauses = null; // String[]
|
||||
exps.projections = toValues(factory, getSelectionList());
|
||||
evalProjection(exps, factory);
|
||||
|
||||
// exps.range = null; // Value[]
|
||||
// exps.resultClass = null; // Class
|
||||
if (_parameterTypes != null)
|
||||
exps.parameterTypes = _parameterTypes;
|
||||
if (_parameterTypes != null)
|
||||
exps.parameterTypes = _parameterTypes;
|
||||
exps.resultClass = _resultClass;
|
||||
return exps;
|
||||
}
|
||||
|
||||
|
@ -250,18 +260,30 @@ public class CriteriaQueryImpl implements CriteriaQuery {
|
|||
}
|
||||
}
|
||||
|
||||
void evalProjection(QueryExpressions exps, ExpressionFactory factory) {
|
||||
Value [] projs = toValues(exps, factory, getSelectionList());
|
||||
if (projs.length == 1 && projs[0] == null)
|
||||
exps.projections = _projections;
|
||||
else
|
||||
exps.projections = projs;
|
||||
//exps.projectionClauses = String[];
|
||||
}
|
||||
|
||||
|
||||
|
||||
Value[] toValues(ExpressionFactory factory, List<Selection<?>> sels) {
|
||||
Value[] toValues(QueryExpressions exps, ExpressionFactory factory,
|
||||
List<Selection<?>> sels) {
|
||||
if (sels == null || (sels.size() == 1 && sels.get(0) == getRoot()))
|
||||
return new Value[0];
|
||||
Value[] result = new Value[sels.size()];
|
||||
String[] aliases = new String[sels.size()];
|
||||
int i = 0;
|
||||
for (Selection<?> s : sels) {
|
||||
result[i++] = ((ExpressionImpl<?>)s).toValue(factory, _model,
|
||||
result[i] = ((SelectionImpl<?>)s).toValue(factory, _model,
|
||||
this);
|
||||
}
|
||||
aliases[i] = nextAlias();
|
||||
i++;
|
||||
}
|
||||
exps.projectionAliases = aliases;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -323,4 +345,9 @@ public class CriteriaQueryImpl implements CriteriaQuery {
|
|||
if (_parameterTypes.containsKey(paramKey))
|
||||
_parameterTypes.put(paramKey, type);
|
||||
}
|
||||
|
||||
private String nextAlias() {
|
||||
return "jpqlalias" + (++_aliasCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,9 +18,16 @@
|
|||
*/
|
||||
package org.apache.openjpa.persistence.criteria;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Selection;
|
||||
|
||||
import org.apache.openjpa.kernel.exps.ExpressionFactory;
|
||||
import org.apache.openjpa.kernel.exps.Value;
|
||||
import org.apache.openjpa.persistence.ResultItemImpl;
|
||||
import org.apache.openjpa.persistence.meta.MetamodelImpl;
|
||||
|
||||
/**
|
||||
* An item selected in the projection clause of Criteria query.
|
||||
|
@ -32,7 +39,31 @@ import org.apache.openjpa.persistence.ResultItemImpl;
|
|||
public class SelectionImpl<X> extends ResultItemImpl<X>
|
||||
implements Selection<X> {
|
||||
|
||||
private List<Selection<?>> _sels;
|
||||
|
||||
public SelectionImpl(Class<X> cls) {
|
||||
super(cls);
|
||||
}
|
||||
|
||||
public SelectionImpl setSelections(Selection<?>... selections) {
|
||||
_sels = Arrays.asList(selections);
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<Selection<?>> getSelections() {
|
||||
return _sels;
|
||||
}
|
||||
|
||||
Value toValue(ExpressionFactory factory, MetamodelImpl model,
|
||||
CriteriaQuery q) {
|
||||
((CriteriaQueryImpl)q).setResultClass(getJavaType());
|
||||
Value[] result = new Value[_sels.size()];
|
||||
int i = 0;
|
||||
for (Selection<?> s : _sels) {
|
||||
result[i++] = ((ExpressionImpl<?>)s).toValue(factory, model,
|
||||
q);
|
||||
}
|
||||
((CriteriaQueryImpl)q).setProjections(result);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue