mirror of https://github.com/apache/openjpa.git
OPENJPA-1013,OPENJPA-1014: Support In() and IsNull()
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@772058 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
51224e586c
commit
89182580bb
|
@ -25,6 +25,7 @@ import org.apache.openjpa.persistence.test.SQLListenerTestCase;
|
|||
*/
|
||||
public class TestTypesafeCriteria extends SQLListenerTestCase {
|
||||
CriteriaBuilder cb;
|
||||
CriteriaQuery c;
|
||||
EntityManager em;
|
||||
|
||||
public void setUp() {
|
||||
|
@ -32,6 +33,8 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
|
|||
setDictionary();
|
||||
cb = (CriteriaBuilder)emf.getQueryBuilder();
|
||||
em = emf.createEntityManager();
|
||||
|
||||
c = cb.create();
|
||||
}
|
||||
|
||||
void setDictionary() {
|
||||
|
@ -50,6 +53,14 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
|
|||
assertEquivalence(c, jpql);
|
||||
}
|
||||
|
||||
public void testImplicitRoot() {
|
||||
String jpql = "select a from Account a";
|
||||
CriteriaQuery c = cb.create();
|
||||
c.from(Account.class);
|
||||
|
||||
assertEquivalence(c, jpql);
|
||||
}
|
||||
|
||||
public void testEqual() {
|
||||
String jpql = "select a from Account a where a.balance=100";
|
||||
|
||||
|
@ -98,6 +109,13 @@ public class TestTypesafeCriteria extends SQLListenerTestCase {
|
|||
assertEquivalence(c, jpql);
|
||||
}
|
||||
|
||||
public void testInPredicate() {
|
||||
String jpql = "select a from Account a where a.owner in ('X','Y','Z')";
|
||||
CriteriaQuery c = cb.create();
|
||||
Root<Account> account = c.from(Account.class);
|
||||
c.where(cb.in(account.get(Account_.owner)).value("X").value("Y").value("Z"));
|
||||
assertEquivalence(c, jpql);
|
||||
}
|
||||
|
||||
public void testBinaryPredicate() {
|
||||
String jpql = "select a from Account a where a.balance>100 and a.balance<200";
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.util.Collection;
|
|||
|
||||
import javax.persistence.criteria.Expression;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.QueryBuilder.In;
|
||||
|
||||
import org.apache.openjpa.kernel.exps.ExpressionFactory;
|
||||
import org.apache.openjpa.kernel.exps.Value;
|
||||
|
@ -60,32 +61,37 @@ public abstract class ExpressionImpl<X> extends SelectionImpl<X>
|
|||
}
|
||||
|
||||
public Predicate in(Object... values) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new AbstractMethodError();
|
||||
In<X> result = new Expressions.In<X>(this);
|
||||
for (Object v : values)
|
||||
result.value((X)v);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Predicate in(Expression<?>... values) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new AbstractMethodError();
|
||||
In<X> result = new Expressions.In<X>(this);
|
||||
for (Expression<?> e : values)
|
||||
result.value((Expression<? extends X>)e);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Predicate in(Collection<?> values) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new AbstractMethodError();
|
||||
In<X> result = new Expressions.In<X>(this);
|
||||
for (Object e : values)
|
||||
result.value((X)e);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Predicate in(Expression<?> values) {
|
||||
// TODO Auto-generated method stub
|
||||
throw new AbstractMethodError();
|
||||
In<X> result = new Expressions.In<X>(this);
|
||||
result.value((Expression<? extends X>)values);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Predicate isNotNull() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new AbstractMethodError();
|
||||
return new Expressions.IsNotNull(this);
|
||||
}
|
||||
|
||||
public Predicate isNull() {
|
||||
// TODO Auto-generated method stub
|
||||
throw new AbstractMethodError();
|
||||
return new Expressions.IsNull(this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import java.util.Collection;
|
|||
import java.util.List;
|
||||
|
||||
import javax.persistence.criteria.Expression;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.QueryBuilder;
|
||||
import javax.persistence.criteria.QueryBuilder.Trimspec;
|
||||
|
||||
|
@ -700,12 +701,45 @@ public class Expressions {
|
|||
}
|
||||
}
|
||||
|
||||
public static class In<T> extends PredicateImpl
|
||||
public static class IsNull extends PredicateImpl {
|
||||
ExpressionImpl<?> e;
|
||||
public IsNull(ExpressionImpl<?> e) {
|
||||
super();
|
||||
this.e = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
|
||||
ExpressionFactory factory, MetamodelImpl model) {
|
||||
return factory.equal(
|
||||
Expressions.toValue(e, factory, model),
|
||||
factory.getNull());
|
||||
}
|
||||
}
|
||||
|
||||
public static class IsNotNull extends PredicateImpl {
|
||||
ExpressionImpl<?> e;
|
||||
public IsNotNull(ExpressionImpl<?> e) {
|
||||
super();
|
||||
this.e = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
|
||||
ExpressionFactory factory, MetamodelImpl model) {
|
||||
return factory.notEqual(
|
||||
Expressions.toValue(e, factory, model),
|
||||
factory.getNull());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class In<T> extends PredicateImpl.Or
|
||||
implements QueryBuilder.In<T> {
|
||||
private List<Expression<? extends T>> values =
|
||||
new ArrayList<Expression<? extends T>>();
|
||||
ExpressionImpl<?> e;
|
||||
public In(Expression<?> e) {
|
||||
super(null);
|
||||
super((Predicate[])null);
|
||||
this.e = (ExpressionImpl<?>)e;
|
||||
}
|
||||
|
||||
public Expression<T> getExpression() {
|
||||
|
@ -713,12 +747,22 @@ public class Expressions {
|
|||
}
|
||||
|
||||
public In<T> value(T value) {
|
||||
return value(new Constant<T>(value));
|
||||
add(new Expressions.Equal(e,value));
|
||||
return this;
|
||||
}
|
||||
|
||||
public In<T> value(Expression<? extends T> value) {
|
||||
values.add(value);
|
||||
add(new Expressions.Equal(e,value));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
|
||||
ExpressionFactory factory, MetamodelImpl model) {
|
||||
IsNotNull notNull = new Expressions.IsNotNull(e);
|
||||
return factory.and(
|
||||
super.toKernelExpression(factory, model),
|
||||
notNull.toKernelExpression(factory, model));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -44,8 +44,10 @@ public class PredicateImpl extends ExpressionImpl<Boolean>
|
|||
|
||||
protected PredicateImpl(BooleanOperator op, Predicate...restrictions) {
|
||||
this(op);
|
||||
for (Predicate p : restrictions)
|
||||
add((PredicateImpl)p);
|
||||
if (restrictions != null) {
|
||||
for (Predicate p : restrictions)
|
||||
add((PredicateImpl)p);
|
||||
}
|
||||
}
|
||||
|
||||
public PredicateImpl add(Expression<Boolean> s) {
|
||||
|
@ -70,7 +72,8 @@ public class PredicateImpl extends ExpressionImpl<Boolean>
|
|||
public PredicateImpl negate() {
|
||||
PredicateImpl not = new PredicateImpl(_op);
|
||||
not._negated = true;
|
||||
not._exps = new ArrayList<Expression<Boolean>>(this._exps);
|
||||
if (_exps != null)
|
||||
not._exps = new ArrayList<Expression<Boolean>>(this._exps);
|
||||
not._op = this._op;
|
||||
return not;
|
||||
}
|
||||
|
@ -90,13 +93,14 @@ public class PredicateImpl extends ExpressionImpl<Boolean>
|
|||
org.apache.openjpa.kernel.exps.Expression ke2 =
|
||||
e2.toKernelExpression(factory, model);
|
||||
org.apache.openjpa.kernel.exps.Expression result =
|
||||
_op == BooleanOperator.AND ?
|
||||
factory.and(ke1,ke2) : factory.or(ke1, ke2);
|
||||
_op == BooleanOperator.AND
|
||||
? factory.and(ke1,ke2) : factory.or(ke1, ke2);
|
||||
|
||||
for (int i = 2; i < _exps.size(); i++) {
|
||||
ExpressionImpl<?> e = (ExpressionImpl<?>)_exps.get(i);
|
||||
result = factory.and(result,
|
||||
e.toKernelExpression(factory, model));
|
||||
result = _op == BooleanOperator.AND
|
||||
? factory.and(result, e.toKernelExpression(factory, model))
|
||||
: factory.or(result, e.toKernelExpression(factory, model));
|
||||
}
|
||||
return _negated ? factory.not(result) : result;
|
||||
}
|
||||
|
@ -119,7 +123,7 @@ public class PredicateImpl extends ExpressionImpl<Boolean>
|
|||
}
|
||||
|
||||
public Or(Predicate...restrictions) {
|
||||
super(BooleanOperator.AND, restrictions);
|
||||
super(BooleanOperator.OR, restrictions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue