OPENJPA-1013,OPENJPA-1014: Type-strict Criteria API (skeletal) implementation as per JPA 2.0.

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@771948 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2009-05-05 18:03:31 +00:00
parent 6bccd91606
commit 0392538775
19 changed files with 2694 additions and 41 deletions

View File

@ -1091,43 +1091,7 @@ public class JPQLExpressionBuilder
if (node.children.length == 3)
setImplicitType(val3, Integer.TYPE);
// the semantics of the JPQL substring() function
// are that arg2 is the 1-based start index, and arg3 is
// the length of the string to be return; this is different
// than the semantics of the ExpressionFactory's substring,
// which matches the Java language (0-based start index,
// arg2 is the end index): we perform the translation by
// adding one to the first argument, and then adding the
// first argument to the second argument to get the endIndex
Value start = null;
Value end = null;
if (val2 instanceof Literal &&
(val3 == null || val3 instanceof Literal)) {
// optimize SQL for the common case of two literals
long jpqlStart = ((Number) ((Literal) val2).getValue())
.longValue();
start = factory.newLiteral(new Long(jpqlStart - 1),
Literal.TYPE_NUMBER);
if (val3 != null) {
long length = ((Number) ((Literal) val3).getValue())
.longValue();
long endIndex = length + (jpqlStart - 1);
end = factory.newLiteral(new Long(endIndex),
Literal.TYPE_NUMBER);
}
} else {
start = factory.subtract(val2, factory.newLiteral
(Numbers.valueOf(1), Literal.TYPE_NUMBER));
if (val3 != null)
end = factory.add(val3,
(factory.subtract(val2, factory.newLiteral
(Numbers.valueOf(1), Literal.TYPE_NUMBER))));
}
if (val3 != null)
return factory.substring(val1, factory.newArgumentList(
start, end));
else
return factory.substring(val1, start);
return convertSubstringArguments(factory, val1, val2, val3);
case JJTLOCATE:
// as with SUBSTRING (above), the semantics for LOCATE differ
@ -1220,7 +1184,56 @@ public class JPQLExpressionBuilder
new Object[]{ node }, null);
}
}
/**
* Converts JPQL substring() function to OpenJPA ExpressionFactory
* substring() arguments.
*
* The semantics of the JPQL substring() function are that arg2 is the
* 1-based start index, and arg3 is the length of the string to be return.
* This is different than the semantics of the ExpressionFactory's
* substring(), which matches the Java language (0-based start index,
* arg2 is the end index): we perform the translation by adding one to the
* first argument, and then adding the first argument to the second
* argument to get the endIndex.
*
* @param val1 the original String
* @param val2 the 1-based start index as per JPQL substring() semantics
* @param val3 the length of the returned string as per JPQL semantics
*
*/
public static Value convertSubstringArguments(ExpressionFactory factory,
Value val1, Value val2, Value val3) {
Value start = null;
Value end = null;
if (val2 instanceof Literal &&
(val3 == null || val3 instanceof Literal)) {
// optimize SQL for the common case of two literals
long jpqlStart = ((Number) ((Literal) val2).getValue())
.longValue();
start = factory.newLiteral(new Long(jpqlStart - 1),
Literal.TYPE_NUMBER);
if (val3 != null) {
long length = ((Number) ((Literal) val3).getValue())
.longValue();
long endIndex = length + (jpqlStart - 1);
end = factory.newLiteral(new Long(endIndex),
Literal.TYPE_NUMBER);
}
} else {
start = factory.subtract(val2, factory.newLiteral
(Numbers.valueOf(1), Literal.TYPE_NUMBER));
if (val3 != null)
end = factory.add(val3,
(factory.subtract(val2, factory.newLiteral
(Numbers.valueOf(1), Literal.TYPE_NUMBER))));
}
if (val3 != null)
return factory.substring(val1, factory.newArgumentList(start, end));
else
return factory.substring(val1, start);
}
private void assertQueryExtensions(String clause) {
OpenJPAConfiguration conf = resolver.getConfiguration();
switch(conf.getCompatibilityInstance().getJPQL()) {

View File

@ -37,4 +37,7 @@ public class Account {
private long id;
private int balance;
private Integer loan;
private String owner;
private String name;
}

View File

@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import javax.persistence.metamodel.Attribute;
/**
* This source is supposed to be automatically generated from Account.java
* during compilation.
* Hand coded to avoid runtime compiler invocation. Used for testing.
*
* @author Pinaki Poddar
*
*/
public class Account_ {
public static volatile Attribute<Account, Long> id;
public static volatile Attribute<Account, Integer> balance;
public static volatile Attribute<Account, Integer> loan;
public static volatile Attribute<Account, String> owner;
public static volatile Attribute<Account, String> name;
}

View File

@ -0,0 +1,149 @@
package org.apache.openjpa.persistence.criteria;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
import org.apache.openjpa.jdbc.sql.DBDictionary;
import org.apache.openjpa.persistence.test.SQLListenerTestCase;
/**
* Tests type-strict version of Criteria API.
*
* Most of the tests build Criteria Query and then execute the query as well
* as a reference JPQL query supplied as a string. The test is validated by
* asserting that the resultant SQL queries for these two alternative form
* of executing a query are the same.
*
*
*
* @author Pinaki Poddar
*
*/
public class TestTypesafeCriteria extends SQLListenerTestCase {
CriteriaBuilder cb;
EntityManager em;
public void setUp() {
super.setUp(Account.class);
setDictionary();
cb = (CriteriaBuilder)emf.getQueryBuilder();
em = emf.createEntityManager();
}
void setDictionary() {
JDBCConfiguration conf = (JDBCConfiguration)emf.getConfiguration();
DBDictionary dict = conf.getDBDictionaryInstance();
dict.requiresCastForComparisons = false;
dict.requiresCastForMathFunctions = false;
}
public void testRoot() {
String jpql = "select a from Account a";
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
c.select(account);
assertEquivalence(c, jpql);
}
public void testEqual() {
String jpql = "select a from Account a where a.balance=100";
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
c.select(account).where(cb.equal(account.get(Account_.balance), 100));
assertEquivalence(c, jpql);
}
public void testEqual2() {
String jpql = "select a from Account a where a.balance=a.loan";
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
c.select(account).where(cb.equal(account.get(Account_.balance), account.get(Account_.loan)));
assertEquivalence(c, jpql);
}
public void testProj() {
String jpql = "select a.balance,a.loan from Account a";
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
c.select(account.get(Account_.balance), account.get(Account_.loan));
assertEquivalence(c, jpql);
}
public void testAbs() {
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
String jpql = "select a from Account a where abs(a.balance)=100";
c.select(account).where(cb.equal(cb.abs(account.get(Account_.balance)), 100));
assertEquivalence(c, jpql);
}
public void testAvg() {
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
String jpql = "select avg(a.balance) from Account a";
c.select(cb.avg(account.get(Account_.balance)));
assertEquivalence(c, jpql);
}
public void testBinaryPredicate() {
String jpql = "select a from Account a where a.balance>100 and a.balance<200";
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
c.select(account).where(cb.and(
cb.greaterThan(account.get(Account_.balance), 100),
cb.lessThan(account.get(Account_.balance), 200)));
assertEquivalence(c, jpql);
}
public void testUnaryExpression() {
String jpql = "select a from Account a where a.balance=abs(a.balance)";
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
c.select(account).where(cb.equal(account.get(Account_.balance), cb.abs(account.get(Account_.balance))));
assertEquivalence(c, jpql);
}
public void testBetweenExpression() {
String jpql = "select a from Account a where a.balance between 100 and 200";
CriteriaQuery c = cb.create();
Root<Account> account = c.from(Account.class);
c.select(account).where(cb.between(account.get(Account_.balance), 100, 200));
assertEquivalence(c, jpql);
}
void assertEquivalence(CriteriaQuery c, String jpql) {
sql.clear();
List cList = em.createQuery(c).getResultList();
assertEquals(1, sql.size());
String cSQL = sql.get(0);
sql.clear();
List jList = em.createQuery(jpql).getResultList();
assertEquals(1, sql.size());
String jSQL = sql.get(0);
assertEquals(jSQL, cSQL);
}
}

View File

@ -47,6 +47,7 @@ import org.apache.openjpa.lib.conf.Value;
import org.apache.openjpa.lib.util.Localizer;
import org.apache.openjpa.lib.util.Closeable;
import org.apache.openjpa.meta.MetaDataRepository;
import org.apache.openjpa.persistence.criteria.CriteriaBuilder;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
import org.apache.openjpa.persistence.query.OpenJPAQueryBuilder;
import org.apache.openjpa.persistence.query.QueryBuilderImpl;
@ -357,7 +358,7 @@ public class EntityManagerFactoryImpl
}
public QueryBuilder getQueryBuilder() {
throw new UnsupportedOperationException();
return new CriteriaBuilder().setMetaModel(getMetamodel());
}
public OpenJPAQueryBuilder getDynamicQueryBuilder() {

View File

@ -70,6 +70,7 @@ import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.meta.FieldMetaData;
import org.apache.openjpa.meta.QueryMetaData;
import org.apache.openjpa.meta.SequenceMetaData;
import org.apache.openjpa.persistence.criteria.CriteriaBuilder;
import org.apache.openjpa.util.Exceptions;
import org.apache.openjpa.util.ImplHelper;
import org.apache.openjpa.util.RuntimeExceptionTranslator;
@ -1509,8 +1510,8 @@ public class EntityManagerImpl
}
public Query createQuery(CriteriaQuery criteriaQuery) {
throw new UnsupportedOperationException(
"JPA 2.0 - Method not yet implemented");
return new QueryImpl(this, _ret,
_broker.newQuery(CriteriaBuilder.LANG_CRITERIA, criteriaQuery));
}
public OpenJPAQuery createDynamicQuery(

View File

@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence;
import javax.persistence.ResultItem;
/**
* A single dimension of projection in query result.
*
* @author Pinaki Poddar
*
* @param <X> type of the result
*/
public class ResultItemImpl<X> implements ResultItem<X> {
protected String _alias;
protected Class<X> _cls;
protected ResultItemImpl(Class<X> cls) {
_cls = cls;
}
public String getAlias() {
return _alias;
}
public void setAlias(String alias) {
_alias = alias;
}
public Class<X> getJavaType() {
return _cls;
}
}

View File

@ -0,0 +1,675 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import javax.persistence.Parameter;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.QueryBuilder;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Subquery;
import org.apache.openjpa.kernel.ExpressionStoreQuery;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.ExpressionParser;
import org.apache.openjpa.kernel.exps.QueryExpressions;
import org.apache.openjpa.kernel.exps.Value;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
/**
* Factory for Criteria query expressions.
*
* Acts as an adapter to OpenJPA ExpressionFactory.
*
* @author Pinaki Poddar
*
* @since 2.0.0
*
*/
@SuppressWarnings("serial")
public class CriteriaBuilder implements QueryBuilder, ExpressionParser {
public static final String LANG_CRITERIA = "javax.persistence.criteria";
private MetamodelImpl _model;
public CriteriaBuilder setMetaModel(MetamodelImpl model) {
_model = model;
return this;
}
public QueryExpressions eval(Object parsed, ExpressionStoreQuery query,
ExpressionFactory factory, ClassMetaData candidate) {
CriteriaQueryImpl c = (CriteriaQueryImpl) parsed;
return c.getQueryExpressions(factory);
}
public Value[] eval(String[] vals, ExpressionStoreQuery query,
ExpressionFactory factory, ClassMetaData candidate) {
throw new AbstractMethodError();
}
public String getLanguage() {
return LANG_CRITERIA;
}
public Object parse(String ql, ExpressionStoreQuery query) {
throw new AbstractMethodError();
}
public void populate(Object parsed, ExpressionStoreQuery query) {
CriteriaQueryImpl c = (CriteriaQueryImpl) parsed;
query.getContext().setCandidateType(c.getRoot().getJavaType(), true);
query.setQuery(parsed);
}
public <N extends Number> Expression<N> abs(Expression<N> x) {
return new Expressions.Abs<N>(x);
}
public <Y> Expression<Y> all(Subquery<Y> subquery) {
throw new AbstractMethodError();
}
public Predicate and(Predicate... restrictions) {
return new PredicateImpl.And(restrictions);
}
public Predicate and(Expression<Boolean> x, Expression<Boolean> y) {
return new PredicateImpl.And(x,y);
}
public <Y> Expression<Y> any(Subquery<Y> subquery) {
throw new AbstractMethodError();
}
public Order asc(Expression<?> x) {
return new OrderImpl(x, true);
}
public <N extends Number> Expression<Double> avg(Expression<N> x) {
return new Expressions.Avg(x);
}
public <Y extends Comparable<Y>> Predicate between(
Expression<? extends Y> v, Expression<? extends Y> x,
Expression<? extends Y> y) {
return new Expressions.Between<Y>(v, x, y);
}
public <Y extends Comparable<Y>> Predicate between(
Expression<? extends Y> v, Y x, Y y) {
return new Expressions.Between<Y>(v,x,y);
}
public <T> Coalesce<T> coalesce() {
return new Expressions.Coalesce();
}
public <Y> Expression<Y> coalesce(Expression<? extends Y> x,
Expression<? extends Y> y) {
return new Expressions.Coalesce<Y>().value(x).value(y);
}
public <Y> Expression<Y> coalesce(Expression<? extends Y> x, Y y) {
return new Expressions.Coalesce<Y>().value(x).value(y);
}
public Expression<String> concat(Expression<String> x,
Expression<String> y) {
return new Expressions.Concat(x, y);
}
public Expression<String> concat(Expression<String> x, String y) {
return new Expressions.Concat(x, y);
}
public Expression<String> concat(String x, Expression<String> y) {
return new Expressions.Concat(x, y);
}
public Predicate conjunction() {
return new PredicateImpl.And();
}
public Expression<Long> count(Expression<?> x) {
return new Expressions.Count(x);
}
public Expression<Long> countDistinct(Expression<?> x) {
return new Expressions.Count(x, true);
}
public CriteriaQuery create() {
return new CriteriaQueryImpl(_model);
}
public Expression<Date> currentDate() {
return new Expressions.CurrentDate();
}
public Expression<Time> currentTime() {
return new Expressions.CurrentTime();
}
public Expression<Timestamp> currentTimestamp() {
return new Expressions.CurrentTimestamp();
}
public Order desc(Expression<?> x) {
return new OrderImpl(x, false);
}
public <N extends Number> Expression<N> diff(Expression<? extends N> x,
Expression<? extends N> y) {
return new Expressions.Diff<N>(x, y);
}
public <N extends Number> Expression<N> diff(
Expression<? extends N> x, N y) {
return new Expressions.Diff<N>(x, y);
}
public <N extends Number> Expression<N> diff(N x,
Expression<? extends N> y) {
return new Expressions.Diff<N>(x, y);
}
public Predicate disjunction() {
return new PredicateImpl.Or();
}
public Predicate equal(Expression<?> x, Expression<?> y) {
return new Expressions.Equal(x, y);
}
public Predicate equal(Expression<?> x, Object y) {
return new Expressions.Equal(x, y);
}
public Predicate exists(Subquery<?> subquery) {
throw new AbstractMethodError();
}
public <T> Expression<T> function(String name, Class<T> type,
Expression<?>... args) {
throw new AbstractMethodError();
}
public Predicate ge(Expression<? extends Number> x,
Expression<? extends Number> y) {
return new Expressions.GreaterThanEqual(x,y);
}
public Predicate ge(Expression<? extends Number> x, Number y) {
return new Expressions.GreaterThanEqual(x,y);
}
public <Y extends Comparable<Y>> Predicate greaterThan(
Expression<? extends Y> x, Expression<? extends Y> y) {
return new Expressions.GreaterThan(x,y);
}
public <Y extends Comparable<Y>> Predicate greaterThan(
Expression<? extends Y> x, Y y) {
return new Expressions.GreaterThan(x, y);
}
public <Y extends Comparable<Y>> Predicate greaterThanOrEqualTo(
Expression<? extends Y> x, Expression<? extends Y> y) {
return new Expressions.GreaterThanEqual(x,y);
}
public <Y extends Comparable<Y>> Predicate greaterThanOrEqualTo(
Expression<? extends Y> x, Y y) {
return new Expressions.GreaterThanEqual(x,y);
}
public <X extends Comparable<X>> Expression<X> greatest(Expression<X> x) {
return new Expressions.Max<X>(x);
}
public Predicate gt(Expression<? extends Number> x,
Expression<? extends Number> y) {
return new Expressions.GreaterThan(x,y);
}
public Predicate gt(Expression<? extends Number> x, Number y) {
return new Expressions.GreaterThan(x,y);
}
public <T> In<T> in(Expression<? extends T> expression) {
return new Expressions.In<T>(expression);
}
public <C extends Collection<?>> Predicate isEmpty(
Expression<C> collection) {
return new Expressions.IsEmpty(collection);
}
public Predicate isFalse(Expression<Boolean> x) {
return new Expressions.Equal(x, false);
}
public <E, C extends Collection<E>> Predicate isMember(E e,
Expression<C> c) {
return new Expressions.IsMember<E>(e, c);
}
public <E, C extends Collection<E>> Predicate isMember(Expression<E> e,
Expression<C> c) {
return new Expressions.IsMember<E>(e.getJavaType(), e, c);
}
public <C extends Collection<?>> Predicate isNotEmpty(
Expression<C> collection) {
return isEmpty(collection).negate();
}
public <E, C extends Collection<E>> Predicate isNotMember(E e,
Expression<C> c) {
return isMember(e, c).negate();
}
public <E, C extends Collection<E>> Predicate isNotMember(
Expression<E> e, Expression<C> c) {
return isMember(e, c).negate();
}
public Predicate isTrue(Expression<Boolean> x) {
return new Expressions.Equal(x, false);
}
public <K, M extends Map<K, ?>> Expression<Set<K>> keys(M map) {
throw new AbstractMethodError();
}
public Predicate le(Expression<? extends Number> x,
Expression<? extends Number> y) {
return new Expressions.LessThanEqual(x,y);
}
public Predicate le(Expression<? extends Number> x, Number y) {
return new Expressions.LessThanEqual(x,y);
}
public <X extends Comparable<X>> Expression<X> least(Expression<X> x) {
return new Expressions.Min<X>(x);
}
public Expression<Integer> length(Expression<String> x) {
throw new AbstractMethodError();
}
public <Y extends Comparable<Y>> Predicate lessThan(
Expression<? extends Y> x, Expression<? extends Y> y) {
return new Expressions.LessThan(x,y);
}
public <Y extends Comparable<Y>> Predicate lessThan(
Expression<? extends Y> x, Y y) {
return new Expressions.LessThan(x,y);
}
public <Y extends Comparable<Y>> Predicate lessThanOrEqualTo(
Expression<? extends Y> x, Expression<? extends Y> y) {
return new Expressions.LessThanEqual(x,y);
}
public <Y extends Comparable<Y>> Predicate lessThanOrEqualTo(
Expression<? extends Y> x, Y y) {
return new Expressions.LessThanEqual(x,y);
}
public Predicate like(Expression<String> x, Expression<String> pattern) {
return new Expressions.Like(x,pattern);
}
public Predicate like(Expression<String> x, String pattern) {
return new Expressions.Like(x,pattern);
}
public Predicate like(Expression<String> x, Expression<String> pattern,
Expression<Character> escapeChar) {
return new Expressions.Like(x,pattern,escapeChar);
}
public Predicate like(Expression<String> x, Expression<String> pattern,
char escapeChar) {
return new Expressions.Like(x,pattern,escapeChar);
}
public Predicate like(Expression<String> x, String pattern,
Expression<Character> escapeChar) {
return new Expressions.Like(x,pattern,escapeChar);
}
public Predicate like(Expression<String> x, String pattern,
char escapeChar) {
return new Expressions.Like(x,pattern,escapeChar);
}
public <T> Expression<T> literal(T value) {
return new Expressions.Constant<T>(value);
}
public Expression<Integer> locate(Expression<String> x,
Expression<String> pattern) {
throw new AbstractMethodError();
}
public Expression<Integer> locate(Expression<String> x, String pattern) {
throw new AbstractMethodError();
}
public Expression<Integer> locate(Expression<String> x,
Expression<String> pattern, Expression<Integer> from) {
throw new AbstractMethodError();
}
public Expression<Integer> locate(Expression<String> x, String pattern,
int from) {
throw new AbstractMethodError();
}
public Expression<String> lower(Expression<String> x) {
throw new AbstractMethodError();
}
public Predicate lt(Expression<? extends Number> x,
Expression<? extends Number> y) {
return new Expressions.LessThan(x,y);
}
public Predicate lt(Expression<? extends Number> x, Number y) {
return new Expressions.LessThan(x,y);
}
public <N extends Number> Expression<N> max(Expression<N> x) {
return new Expressions.Max<N>(x);
}
public <N extends Number> Expression<N> min(Expression<N> x) {
return new Expressions.Min<N>(x);
}
public Expression<Integer> mod(Expression<Integer> x,
Expression<Integer> y) {
return new Expressions.Mod(x,y);
}
public Expression<Integer> mod(Expression<Integer> x, Integer y) {
return new Expressions.Mod(x,y);
}
public Expression<Integer> mod(Integer x, Expression<Integer> y) {
return new Expressions.Mod(x,y);
}
public <N extends Number> Expression<N> neg(Expression<N> x) {
throw new AbstractMethodError();
}
public Predicate not(Expression<Boolean> restriction) {
throw new AbstractMethodError();
}
public Predicate notEqual(Expression<?> x, Expression<?> y) {
return equal(x, y).negate();
}
public Predicate notEqual(Expression<?> x, Object y) {
return equal(x, y).negate();
}
public Predicate notLike(Expression<String> x, Expression<String> pattern) {
return like(x, pattern).negate();
}
public Predicate notLike(Expression<String> x, String pattern) {
return like(x, pattern).negate();
}
public Predicate notLike(Expression<String> x, Expression<String> pattern,
Expression<Character> escapeChar) {
return like(x, pattern, escapeChar).negate();
}
public Predicate notLike(Expression<String> x, Expression<String> pattern,
char escapeChar) {
return like(x, pattern, escapeChar).negate();
}
public Predicate notLike(Expression<String> x, String pattern,
Expression<Character> escapeChar) {
return like(x, pattern, escapeChar).negate();
}
public Predicate notLike(Expression<String> x, String pattern,
char escapeChar) {
return like(x, pattern, escapeChar).negate();
}
public <Y> Expression<Y> nullif(Expression<Y> x, Expression<?> y) {
throw new AbstractMethodError();
}
public <Y> Expression<Y> nullif(Expression<Y> x, Y y) {
throw new AbstractMethodError();
}
public Predicate or(Predicate... restrictions) {
return new PredicateImpl.Or();
}
public Predicate or(Expression<Boolean> x, Expression<Boolean> y) {
return new PredicateImpl.Or(x,y);
}
public <T> Parameter<T> parameter(Class<T> paramClass) {
return new ParameterImpl<T>(paramClass);
}
public <T> Parameter<T> parameter(Class<T> paramClass, String name) {
return new ParameterImpl<T>(paramClass).setName(name);
}
public <N extends Number> Expression<N> prod(Expression<? extends N> x,
Expression<? extends N> y) {
return new Expressions.Product<N>(x,y);
}
public <N extends Number> Expression<N> prod(Expression<? extends N> x,
N y) {
return new Expressions.Product<N>(x,y);
}
public <N extends Number> Expression<N> prod(N x,
Expression<? extends N> y) {
return new Expressions.Product<N>(x,y);
}
public Expression<Number> quot(Expression<? extends Number> x,
Expression<? extends Number> y) {
return new Expressions.Quotient<Number>(x,y);
}
public Expression<Number> quot(Expression<? extends Number> x, Number y) {
return new Expressions.Quotient<Number>(x,y);
}
public Expression<Number> quot(Number x, Expression<? extends Number> y) {
return new Expressions.Quotient<Number>(x,y);
}
public <Y> Selection<Y> select(Class<Y> result,
Selection<?>... selections) {
throw new AbstractMethodError();
}
public <R> Case<R> selectCase() {
throw new AbstractMethodError();
}
public <C, R> SimpleCase<C, R> selectCase(
Expression<? extends C> expression) {
throw new AbstractMethodError();
}
public <C extends Collection<?>> Expression<Integer> size(C collection) {
return new Expressions.Size(collection);
}
public <C extends Collection<?>> Expression<Integer> size(
Expression<C> collection) {
return new Expressions.Size(collection);
}
public <Y> Expression<Y> some(Subquery<Y> subquery) {
throw new AbstractMethodError();
}
public Expression<Double> sqrt(Expression<? extends Number> x) {
return new Expressions.Sqrt(x);
}
public Expression<String> substring(Expression<String> x,
Expression<Integer> from) {
return new Expressions.Substring(x);
}
public Expression<String> substring(Expression<String> x, int from) {
return new Expressions.Substring(x, from);
}
public Expression<String> substring(Expression<String> x,
Expression<Integer> from, Expression<Integer> len) {
return new Expressions.Substring(x, from, len);
}
public Expression<String> substring(Expression<String> x, int from,
int len) {
return new Expressions.Substring(x, from, len);
}
public <N extends Number> Expression<N> sum(Expression<N> x) {
return new Expressions.Sum<N>(x);
}
public <N extends Number> Expression<N> sum(Expression<? extends N> x,
Expression<? extends N> y) {
return new Expressions.Sum<N>(x,y);
}
public <N extends Number> Expression<N> sum(Expression<? extends N> x,
N y) {
return new Expressions.Sum<N>(x,y);
}
public <N extends Number> Expression<N> sum(N x,
Expression<? extends N> y) {
return new Expressions.Sum<N>(x,y);
}
public Expression<BigDecimal> toBigDecimal(
Expression<? extends Number> number) {
return new Expressions.Cast<BigDecimal>(number, BigDecimal.class);
}
public Expression<BigInteger> toBigInteger(
Expression<? extends Number> number) {
return new Expressions.Cast<BigInteger>(number, BigInteger.class);
}
public Expression<Double> toDouble(Expression<? extends Number> number) {
return new Expressions.Cast<Double>(number, Double.class);
}
public Expression<Float> toFloat(Expression<? extends Number> number) {
return new Expressions.Cast<Float>(number, Float.class);
}
public Expression<Integer> toInteger(Expression<? extends Number> number) {
return new Expressions.Cast<Integer>(number, Integer.class);
}
public Expression<Long> toLong(Expression<? extends Number> number) {
return new Expressions.Cast<Long>(number, Long.class);
}
public Expression<String> toString(Expression<Character> character) {
return new Expressions.Cast<String>(character, String.class);
}
public Expression<String> trim(Expression<String> x) {
return new Expressions.Trim(x);
}
public Expression<String> trim(Trimspec ts, Expression<String> x) {
return new Expressions.Trim(x, ts);
}
public Expression<String> trim(Expression<Character> t,
Expression<String> x) {
return new Expressions.Trim(x, t);
}
public Expression<String> trim(char t, Expression<String> x) {
return new Expressions.Trim(x, t);
}
public Expression<String> trim(Trimspec ts, Expression<Character> t,
Expression<String> x) {
return new Expressions.Trim(x, t, ts);
}
public Expression<String> trim(Trimspec ts, char t, Expression<String> x) {
return new Expressions.Trim(x, t, ts);
}
public Expression<String> upper(Expression<String> x) {
throw new AbstractMethodError();
}
public <V, M extends Map<?, V>> Expression<Collection<V>> values(M map) {
throw new AbstractMethodError();
}
}

View File

@ -0,0 +1,218 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.Parameter;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Subquery;
import javax.persistence.metamodel.Entity;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.QueryExpressions;
import org.apache.openjpa.kernel.exps.Value;
import org.apache.openjpa.meta.ClassMetaData;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
import org.apache.openjpa.persistence.meta.Types;
/**
* Criteria query implementation.
*
* @author Pinaki Poddar
*
*/
public class CriteriaQueryImpl implements CriteriaQuery {
private final MetamodelImpl _model;
private Set<Root<?>> _roots;
private PredicateImpl _where;
private List<Order> _orders;
private Set<Parameter<?>> _params;
private List<Selection<?>> _selections;
private List<Expression<?>> _groups;
private PredicateImpl _having;
private Boolean _distinct;
public CriteriaQueryImpl(MetamodelImpl model) {
this._model = model;
}
public CriteriaQuery distinct(boolean distinct) {
_distinct = distinct;
return this;
}
public List<Order> getOrderList() {
return _orders;
}
public Set<Parameter<?>> getParameters() {
return _params;
}
public List<Selection<?>> getSelectionList() {
return _selections;
}
public CriteriaQuery groupBy(Expression<?>... grouping) {
_groups = new ArrayList<Expression<?>>();
for (Expression<?> e : grouping)
_groups.add(e);
return this;
}
public CriteriaQuery having(Expression<Boolean> restriction) {
_having = new PredicateImpl().add(restriction);
return this;
}
public CriteriaQuery having(Predicate... restrictions) {
_having = new PredicateImpl();
for (Predicate p : restrictions)
_having.add(p);
return this;
}
public CriteriaQuery orderBy(Order... o) {
_orders = Arrays.asList(o);
return this;
}
public CriteriaQuery select(Selection<?>... selections) {
_selections = Arrays.asList(selections);
return this;
}
public CriteriaQuery where(Expression<Boolean> restriction) {
_where = new PredicateImpl().add(restriction);
return this;
}
public CriteriaQuery where(Predicate... restrictions) {
_where = new PredicateImpl();
for (Predicate p : restrictions)
_where.add(p);
return this;
}
public <X> Root<X> from(Entity<X> entity) {
Root<X> root = new RootImpl<X>(entity);
if (_roots == null) {
_roots = new LinkedHashSet<Root<?>>();
}
_roots.add(root);
return root;
}
public <X> Root<X> from(Class<X> entityClass) {
return from(_model.entity(entityClass));
}
public List<Expression<?>> getGroupList() {
return _groups;
}
public Predicate getGroupRestriction() {
return _having;
}
public PredicateImpl getRestriction() {
return _where;
}
public Set<Root<?>> getRoots() {
return _roots;
}
public Root<?> getRoot() {
if (_roots == null || _roots.isEmpty())
throw new IllegalStateException("no root is set");
return _roots.iterator().next();
}
public boolean isDistinct() {
return _distinct;
}
public <U> Subquery<U> subquery(Class<U> type) {
// TODO Auto-generated method stub
return null;
}
/**
* Populate kernel expressions.
*/
QueryExpressions getQueryExpressions(ExpressionFactory factory) {
QueryExpressions exps = new QueryExpressions();
if (_roots != null) {
exps.accessPath = new ClassMetaData[_roots.size()];
int i = 0;
for (Root<?> r : _roots)
exps.accessPath[i++] = ((Types.Managed<?>)r.getModel()).meta;
}
// exps.alias = null; // String
exps.ascending = new boolean[]{false};
exps.distinct = _distinct == null ? QueryExpressions.DISTINCT_AUTO :
_distinct ? QueryExpressions.DISTINCT_TRUE
: QueryExpressions.DISTINCT_FALSE;
// exps.fetchInnerPaths = null; // String[]
// exps.fetchPaths = null; // String[]
exps.filter = _where == null ? factory.emptyExpression()
: _where.toKernelExpression(factory, _model);
// exps.grouping = null; // Value[]
// exps.groupingClauses = null; // String[]
exps.having = _having == null ? factory.emptyExpression()
: _having.toKernelExpression(factory, _model);
// exps.operation = QueryOperations.OP_SELECT;
// exps.ordering = null; //Value[]
// exps.orderingAliases = null; // String[]
// exps.orderingClauses = null; // String[]
// exps.parameterTypes = null; // LinkedMap<>
// exps.projectionAliases = null; // String[]
// exps.projectionClauses = null; // String[]
exps.projections = toValues(factory, getSelectionList());
// exps.range = null; // Value[]
// exps.resultClass = null; // Class
return exps;
}
Value[] toValues(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()];
int i = 0;
for (Selection<?> s : sels) {
result[i++] = ((ExpressionImpl<?>)s).toValue(factory, _model);
}
return result;
}
}

View File

@ -0,0 +1,91 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import java.util.Collection;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.Value;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
/**
* Expression for Criteria query.
*
* @author Pinaki Poddar
*
* @param <X> the type of the value this expression represents.
*/
public abstract class ExpressionImpl<X> extends SelectionImpl<X>
implements Expression<X> {
Value toValue(ExpressionFactory factory, MetamodelImpl model) {
throw new AbstractMethodError(this.getClass().getName());
}
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
throw new AbstractMethodError(this.getClass().getName());
}
/**
* @param cls the type of the evaluated result of the expression
*/
public ExpressionImpl(Class<X> cls) {
super(cls);
}
public <Y> Expression<Y> as(Class<Y> type) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Predicate in(Object... values) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Predicate in(Expression<?>... values) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Predicate in(Collection<?> values) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Predicate in(Expression<?> values) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Predicate isNotNull() {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Predicate isNull() {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
}

View File

@ -0,0 +1,724 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.QueryBuilder;
import javax.persistence.criteria.QueryBuilder.Trimspec;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.Value;
import org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
public class Expressions {
static Value toValue(ExpressionImpl<?> e, ExpressionFactory factory,
MetamodelImpl model) {
Value v = e.toValue(factory, model);
v.setImplicitType(e.getJavaType());
v.setAlias(e.getAlias());
return v;
}
/**
* Unary Functional Expression applies a unary function on a input
* Expression.
*
* @param <X> the type of the resultant expression
*/
public static class UnaryFunctionalExpression<X>
extends ExpressionImpl<X> {
protected ExpressionImpl<?> e;
public UnaryFunctionalExpression(Class<X> t, Expression<?> e) {
super(t);
this.e = (ExpressionImpl<?>)e;
}
public UnaryFunctionalExpression(Expression<X> e) {
this(e.getJavaType(), e);
}
}
/**
* Binary Functional Expression applies a binary function on a pair of
* input Expression.
*
*
* @param <X> the type of the resultant expression
*/
public static class BinarayFunctionalExpression<X>
extends ExpressionImpl<X>{
protected ExpressionImpl<?> e1;
protected ExpressionImpl<?> e2;
public BinarayFunctionalExpression(Class<X> t, Expression<?> x,
Expression<?> y) {
super(t);
e1 = (ExpressionImpl<?>)x;
e2 = (ExpressionImpl<?>)y;
}
}
/**
* Binary Logical Expression applies a binary function on a pair of
* input Expression to generate a Predicate.
*
*/
public static class BinaryLogicalExpression extends PredicateImpl{
protected ExpressionImpl<?> e1;
protected ExpressionImpl<?> e2;
public BinaryLogicalExpression(Expression<?> x, Expression<?> y) {
super();
e1 = (ExpressionImpl<?>)x;
e2 = (ExpressionImpl<?>)y;
}
}
public static class Abs<X> extends UnaryFunctionalExpression<X> {
public Abs(Expression<X> x) {
super(x);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.abs(Expressions.toValue(e, factory, model));
}
}
public static class Count extends UnaryFunctionalExpression<Long> {
private boolean _distinct;
public Count(Expression<?> x) {
this(x, false);
}
public Count(Expression<?> x, boolean distinct) {
super(Long.class, x);
_distinct = distinct;
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
Value v = factory.count(Expressions.toValue(e, factory, model));
return _distinct ? factory.distinct(v) : v;
}
}
public static class Avg extends UnaryFunctionalExpression<Double> {
public Avg(Expression<?> x) {
super(Double.class, x);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.avg(Expressions.toValue(e, factory, model));
}
}
public static class Sqrt extends UnaryFunctionalExpression<Double> {
public Sqrt(Expression<? extends Number> x) {
super(Double.class, x);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.sqrt(Expressions.toValue(e, factory, model));
}
}
public static class Max<X> extends UnaryFunctionalExpression<X> {
public Max(Expression<X> x) {
super(x);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.max(Expressions.toValue(e, factory, model));
}
}
public static class Min<X> extends UnaryFunctionalExpression<X> {
public Min(Expression<X> x) {
super(x);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.min(Expressions.toValue(e, factory, model));
}
}
public static class Size extends UnaryFunctionalExpression<Integer> {
public Size(Expression<? extends Collection<?>> x) {
super(Integer.class, x);
}
public Size(Collection<?> x) {
this(new Constant<Collection<?>>(x));
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.size(Expressions.toValue(e, factory, model));
}
}
public static class Cast<B> extends UnaryFunctionalExpression<B> {
Class<B> b;
public Cast(Expression<?> x, Class<B> b) {
super(b, x);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.cast(Expressions.toValue(e, factory, model), b);
}
}
public static class Concat extends BinarayFunctionalExpression<String> {
public Concat(Expression<String> x, Expression<String> y) {
super(String.class, x, y);
}
public Concat(Expression<String> x, String y) {
this(x, new Constant<String>(y));
}
public Concat(String x, Expression<String> y) {
this(new Constant<String>(x), y);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.concat(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class Substring extends UnaryFunctionalExpression<String> {
private ExpressionImpl<Integer> from;
private ExpressionImpl<Integer> len;
public Substring(Expression<String> s, Expression<Integer> from,
Expression<Integer> len) {
super(s);
this.from = (ExpressionImpl<Integer>)from;
this.len = (ExpressionImpl<Integer>)len;
}
public Substring(Expression<String> s) {
this(s, (Expression<Integer>)null, (Expression<Integer>)null);
}
public Substring(Expression<String> s, Integer from) {
this(s, new Constant<Integer>(from), null);
}
public Substring(Expression<String> s, Integer from, Integer len) {
this(s, new Constant<Integer>(from), new Constant<Integer>(len));
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return JPQLExpressionBuilder.convertSubstringArguments(factory,
Expressions.toValue(e, factory, model),
from == null ? null : from.toValue(factory, model),
len == null ? null : len.toValue(factory, model));
}
}
public static class Trim extends BinarayFunctionalExpression<String> {
static Expression<Character> defaultTrim = new Constant<Character>
(Character.class, new Character(' '));
static Trimspec defaultSpec = Trimspec.BOTH;
Trimspec ts;
public Trim(Expression<String> x, Expression<Character> y,
Trimspec ts) {
super(String.class, x, y);
this.ts = ts;
}
public Trim(Expression<String> x, Expression<Character> y) {
this(x, y, defaultSpec);
}
public Trim(Expression<String> x) {
this(x, defaultTrim, defaultSpec);
}
public Trim(Expression<String> x, Character t) {
this(x, new Constant<Character>(Character.class, t), defaultSpec);
}
public Trim(Expression<String> x, Character t, Trimspec ts) {
this(x, new Constant<Character>(Character.class, t), ts);
}
public Trim(Expression<String> x, Trimspec ts) {
this(x, defaultTrim, ts);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
Boolean spec = null;
if (ts != null) {
switch (ts) {
case LEADING : spec = true; break;
case TRAILING : spec = false; break;
case BOTH : spec = null; break;
}
}
return factory.trim(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model), spec);
}
}
public static class Sum<N extends Number>
extends BinarayFunctionalExpression<N> {
public Sum(Expression<? extends Number> x,
Expression<? extends Number> y) {
super((Class<N>)x.getJavaType(), x, y);
}
public Sum(Expression<? extends Number> x) {
this(x, (Expression<? extends Number>)null);
}
public Sum(Expression<? extends Number> x, Number y) {
this(x, new Constant<Number>(Number.class, y));
}
public Sum(Number x, Expression<? extends Number> y) {
this(new Constant<Number>(Number.class, x), y);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return (e2 == null)
? factory.sum(Expressions.toValue(e1, factory, model))
: factory.add(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class Product<N extends Number>
extends BinarayFunctionalExpression<N> {
public Product(Expression<? extends Number> x,
Expression<? extends Number> y) {
super((Class<N>)x.getJavaType(), x, y);
}
public Product(Expression<? extends Number> x, Number y) {
this(x, new Constant<Number>(Number.class, y));
}
public Product(Number x, Expression<? extends Number> y) {
this(new Constant<Number>(Number.class, x), y);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.multiply(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class Diff<N extends Number>
extends BinarayFunctionalExpression<N> {
public Diff(Expression<? extends Number> x,
Expression<? extends Number> y) {
super((Class<N>)x.getJavaType(), x, y);
}
public Diff(Expression<? extends Number> x, Number y) {
this(x, new Constant<Number>(Number.class, y));
}
public Diff(Number x, Expression<? extends Number> y) {
this(new Constant<Number>(Number.class, x), y);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.subtract(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class Quotient<N extends Number>
extends BinarayFunctionalExpression<N> {
public Quotient(Expression<? extends Number> x,
Expression<? extends Number> y) {
super((Class<N>)x.getJavaType(), x, y);
}
public Quotient(Expression<? extends Number> x, Number y) {
this(x, new Constant<Number>(y));
}
public Quotient(Number x, Expression<? extends Number> y) {
this(new Constant<Number>(x), y);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.divide(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class Mod extends BinarayFunctionalExpression<Integer> {
public Mod(Expression<Integer> x, Expression<Integer> y) {
super(Integer.class, x,y);
}
public Mod(Expression<Integer> x, Integer y) {
this(x,new Constant<Integer>(Integer.class, y));
}
public Mod(Integer x, Expression<Integer> y) {
this(new Constant<Integer>(Integer.class, x),y);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.mod(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class CurrentDate extends ExpressionImpl<java.sql.Date> {
public CurrentDate() {
super(java.sql.Date.class);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.getCurrentDate();
}
}
public static class CurrentTime extends ExpressionImpl<java.sql.Time> {
public CurrentTime() {
super(java.sql.Time.class);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.getCurrentTime();
}
}
public static class CurrentTimestamp
extends ExpressionImpl<java.sql.Timestamp> {
public CurrentTimestamp() {
super(java.sql.Timestamp.class);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.getCurrentTimestamp();
}
}
public static class Equal extends BinaryLogicalExpression {
public <X,Y> Equal(Expression<X> x, Expression<Y> y) {
super(x,y);
}
public <X> Equal(Expression<X> x, Object y) {
this(x, new Constant<Object>(Object.class, y));
}
@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.equal(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class GreaterThan extends BinaryLogicalExpression {
public <X,Y> GreaterThan(Expression<X> x, Expression<Y> y) {
super(x,y);
}
public <X> GreaterThan(Expression<X> x, Object y) {
this(x, new Constant<Object>(Object.class, y));
}
@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.greaterThan(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class GreaterThanEqual extends BinaryLogicalExpression {
public <X,Y> GreaterThanEqual(Expression<X> x, Expression<Y> y) {
super(x,y);
}
public <X> GreaterThanEqual(Expression<X> x, Object y) {
this(x, new Constant<Object>(Object.class, y));
}
@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.greaterThanEqual(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class LessThan extends BinaryLogicalExpression {
public <X,Y> LessThan(Expression<X> x, Expression<Y> y) {
super(x,y);
}
public <X> LessThan(Expression<X> x, Object y) {
this(x, new Constant<Object>(Object.class, y));
}
@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.lessThan(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class LessThanEqual extends BinaryLogicalExpression {
public <X,Y> LessThanEqual(Expression<X> x, Expression<Y> y) {
super(x,y);
}
public <X> LessThanEqual(Expression<X> x, Object y) {
this(x, new Constant<Object>(Object.class, y));
}
@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.lessThanEqual(
Expressions.toValue(e1, factory, model),
Expressions.toValue(e2, factory, model));
}
}
public static class Between<Y extends Comparable<Y>>
extends PredicateImpl.And {
public Between(Expression<? extends Y> v, Expression<? extends Y> x,
Expression<? extends Y> y) {
super(new GreaterThanEqual(v,x), new LessThanEqual(v,y));
}
public Between(Expression<? extends Y> v, Y x,
Y y) {
this(v, new Constant<Y>(x), new Constant<Y>(y));
}
}
public static class Constant<X> extends ExpressionImpl<X> {
public final Object arg;
public Constant(Class<X> t, X x) {
super(t);
this.arg = x;
}
public Constant(X x) {
this((Class<X>)x.getClass(),x);
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
return factory.newLiteral(arg, 1);
}
}
public static class IsEmpty extends PredicateImpl {
ExpressionImpl<?> collection;
public IsEmpty(Expression<?> collection) {
super();
this.collection = (ExpressionImpl<?>)collection;
}
@Override
public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.isEmpty(
Expressions.toValue(collection, factory, model));
}
}
public static class IsMember<E> extends PredicateImpl {
ExpressionImpl<E> element;
ExpressionImpl<?> collection;
public IsMember(Class<E> t, Expression<E> element,
Expression<?> collection) {
this.element = (ExpressionImpl<E>)element;
this.collection = (ExpressionImpl<?>)collection;
}
public IsMember(Class<E> t, E element, Expression<?> collection) {
this(t, new Constant<E>(element), collection);
}
public IsMember(E element, Expression<?> collection) {
this((Class<E>)element.getClass(), element, collection);
}
@Override
public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.contains(factory.getThis(),
Expressions.toValue(collection, factory, model));
}
}
public static class Like extends PredicateImpl {
ExpressionImpl<String> str;
ExpressionImpl<String> pattern;
ExpressionImpl<Character> escapeChar;
static ExpressionImpl<Character> defaultEscape =
new Constant<Character>(new Character(' '));
public Like(Expression<String> x, Expression<String> pattern,
Expression<Character> escapeChar) {
super();
this.str = (ExpressionImpl<String>)x;
this.pattern = (ExpressionImpl<String>)pattern;
this.escapeChar = (ExpressionImpl<Character>)escapeChar;
}
public Like(Expression<String> x, Expression<String> pat, char esc) {
this(x, pat, new Constant<Character>(Character.class, esc));
}
public Like(Expression<String> x, Expression<String> pattern) {
this(x, pattern, defaultEscape);
}
public Like(Expression<String> x, String pattern) {
this(x, new Constant<String>(pattern), defaultEscape);
}
public Like(Expression<String> x, String pat,
Expression<Character> esc) {
this(x, new Constant<String>(pat), esc);
}
public Like(Expression<String> x, String pat, Character esc) {
this(x, new Constant<String>(pat), new Constant<Character>(esc));
}
@Override
public org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
return factory.matches(
Expressions.toValue(str, factory, model),
Expressions.toValue(pattern, factory, model), "_", "%",
Expressions.toValue(escapeChar, factory, model).toString());
}
}
public static class Coalesce<T> extends ExpressionImpl<T>
implements QueryBuilder.Coalesce<T> {
private List<Expression<? extends T>> values =
new ArrayList<Expression<? extends T>>();
public Coalesce() {
super(null);
}
public Coalesce(Class<T> cls) {
super(cls);
}
public Coalesce<T> value(T value) {
return value(new Constant<T>(value));
}
public Coalesce<T> value(Expression<? extends T> value) {
values.add(value);
return this;
}
@Override
public org.apache.openjpa.kernel.exps.Value toValue(
ExpressionFactory factory, MetamodelImpl model) {
Value[] vs = new Value[values.size()];
int i = 0;
for (Expression<?> e : values)
vs[i++] = Expressions.toValue((ExpressionImpl<?>)e,
factory, model);
return factory.coalesceExpression(vs);
}
}
public static class In<T> extends PredicateImpl
implements QueryBuilder.In<T> {
private List<Expression<? extends T>> values =
new ArrayList<Expression<? extends T>>();
public In(Expression<?> e) {
super(null);
}
public Expression<T> getExpression() {
return null;
}
public In<T> value(T value) {
return value(new Constant<T>(value));
}
public In<T> value(Expression<? extends T> value) {
values.add(value);
return this;
}
}
}

View File

@ -0,0 +1,248 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import javax.persistence.criteria.CollectionJoin;
import javax.persistence.criteria.Fetch;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.ListJoin;
import javax.persistence.criteria.MapJoin;
import javax.persistence.criteria.SetJoin;
import javax.persistence.metamodel.AbstractCollection;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Collection;
import javax.persistence.metamodel.List;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Map;
import javax.persistence.metamodel.Set;
import org.apache.openjpa.persistence.criteria.PathImpl;
/**
* Represents a bound type, usually an entity that appears in the from clause,
* but may also be an embeddable belonging to an entity in the from clause.
* Serves as a factory for Joins of associations, embeddables and collections
* belonging to the type, and for Paths of attributes belonging to the type.
*
* @param <Z> the parent type of this receiver
* @param <X> the type represented by this receiver
*/
public class FromImpl<Z,X> extends PathImpl<X> implements From<Z,X> {
private java.util.Set<Join<X, ?>> _joins;
private java.util.Set<Fetch<X, ?>> _fetches;
/**
* Supply the non-null managed type.
*/
public FromImpl(ManagedType<X> type) {
super(type.getJavaType());
}
/**
* Return the joins that have been made from this type.
* @return joins made from this type
*/
public java.util.Set<Join<X, ?>> getJoins() {
throw new AbstractMethodError();
}
/**
* Join to the specified attribute using an inner join.
* @param attribute target of the join
* @return the resulting join
*/
public <Y> Join<X, Y> join(Attribute<? super X, Y> attribute) {
return join(attribute, JoinType.INNER);
}
/**
* Join to the specified attribute using the given join type.
* @param attribute target of the join
* @param jt join type
* @return the resulting join
*/
public <Y> Join<X, Y> join(Attribute<? super X, Y> attribute, JoinType jt) {
throw new AbstractMethodError();
}
/**
* Join to the specified Collection-valued attribute using an
* inner join.
* @param collection target of the join
* @return the resulting join
*/
public <Y> CollectionJoin<X, Y> join(Collection<? super X, Y> collection) {
return join(collection, JoinType.INNER);
}
/**
* Join to the specified Collection-valued attribute using the given
* join type.
* @param collection target of the join
* @return the resulting join
*/
public <Y> CollectionJoin<X, Y> join(Collection<? super X, Y> collection,
JoinType jt) {
throw new AbstractMethodError();
}
/**
* Join to the specified Set-valued attribute using an inner join.
* @param set target of the join
* @return the resulting join
*/
public <Y> SetJoin<X,Y> join(Set<? super X, Y> set) {
return join(set, JoinType.INNER);
}
/**
* Join to the specified Set-valued attribute using the given join type.
* @param set target of the join
* @return the resulting join
*/
public <Y> SetJoin<X,Y> join(Set<? super X, Y> set, JoinType jt) {
throw new AbstractMethodError();
}
/**
* Join to the specified List-valued attribute using an inner join.
* @param set target of the join
* @return the resulting join
*/
public <Y> ListJoin<X,Y> join(List<? super X, Y> list) {
return join(list, JoinType.INNER);
}
/**
* Join to the specified List-valued attribute using the given join type.
* @param set target of the join
* @return the resulting join
*/
public <Y> ListJoin<X,Y> join(List<? super X, Y> list, JoinType jt) {
throw new AbstractMethodError();
}
/**
* Join to the specified Map-valued attribute using an inner join.
* @param set target of the join
* @return the resulting join
*/
public <K,V> MapJoin<X,K,V> join(Map<? super X,K,V> map) {
return join(map, JoinType.INNER);
}
/**
* Join to the specified Map-valued attribute using the given join type.
* @param set target of the join
* @return the resulting join
*/
public <K,V> MapJoin<X,K,V> join(Map<? super X,K,V> map, JoinType jt) {
throw new AbstractMethodError();
}
// String based counterparts
public Join join(String attributeName) {
throw new AbstractMethodError();
}
public Join join(String attributeName, JoinType jt) {
throw new AbstractMethodError();
}
public CollectionJoin joinCollection(String attributeName) {
throw new AbstractMethodError();
}
public CollectionJoin joinCollection(String attributeName, JoinType jt) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public ListJoin joinList(String attributeName) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public ListJoin joinList(String attributeName, JoinType jt) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public MapJoin joinMap(String attributeName) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public MapJoin joinMap(String attributeName, JoinType jt) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public SetJoin joinSet(String attributeName) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public SetJoin joinSet(String attributeName, JoinType jt) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Fetch fetch(Attribute assoc) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Fetch fetch(AbstractCollection assoc) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Fetch fetch(String assocName) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Fetch fetch(Attribute assoc, JoinType jt) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Fetch fetch(AbstractCollection assoc, JoinType jt) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Fetch fetch(String assocName, JoinType jt) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public java.util.Set<Fetch<X, ?>> getFetches() {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
}

View File

@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Order;
/**
* Ordering clause of a criteria query.
*
* @author Pinaki Poddar
*
*/
public class OrderImpl implements Order {
private boolean _ascending;
private final Expression<?> e;
public OrderImpl(Expression<?> e, boolean asc) {
this.e = e;
_ascending = asc;
}
public OrderImpl(Expression<?> e) {
this(e, true);
}
public <T extends Comparable<T>> Expression<T> getExpression() {
return (Expression<T>)e;
}
public boolean isAscending() {
return _ascending;
}
public void reverse() {
_ascending = !_ascending;
}
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import javax.persistence.Parameter;
/**
* Parameter of a criteria query.
*
* @author Pinaki Poddar
*
* @param <T> the type of value held by this parameter.
*/
public class ParameterImpl<T> extends ExpressionImpl<T> implements Parameter<T>{
private String name;
private int position;
public ParameterImpl(Class<T> cls) {
super(cls);
}
public final String getName() {
return name;
}
public final ParameterImpl<T> setName(String name) {
this.name = name;
return this;
}
public final Integer getPosition() {
return position;
}
}

View File

@ -0,0 +1,114 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.metamodel.AbstractCollection;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.Map;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.kernel.exps.Value;
import org.apache.openjpa.persistence.meta.Members;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
/**
* Path from another (parent) path.
*
* @author ppoddar
*
* @param <X>
*/
public class PathImpl<X> extends ExpressionImpl<X> implements Path<X> {
private PathImpl<?> _parent;
private Members.Member<?,X> member;
/**
*
* @param cls
*/
protected PathImpl(Class<X> cls) {
super(cls);
}
public <Z> PathImpl(Members.Member<Z, X> member) {
super(member.getMemberJavaType());
this.member = member;
}
public <Z> PathImpl(PathImpl<Z> parent,
Members.Member<? super Z, X> member) {
super(member.getMemberJavaType());
_parent = parent;
this.member = member;
}
@Override
public Value toValue(ExpressionFactory factory, MetamodelImpl model) {
Value var = null;
if (_parent != null) {
org.apache.openjpa.kernel.exps.Path path =
(org.apache.openjpa.kernel.exps.Path)
_parent.toValue(factory, model);
path.get(member.fmd, false);
var = path;
} else {
var = factory.newPath();//getJavaType());
var.setMetaData(model.repos.getMetaData(getJavaType(), null, true));
}
return var;
}
public <Y> Path<Y> get(Attribute<? super X, Y> attr) {
return new PathImpl(this, (Members.Member<? super X, Y>)attr);
}
public Expression get(AbstractCollection collection) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Expression get(Map collection) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Path get(String attName) {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
//TODO: what does this return for a collection key, value? null?
public Bindable<X> getModel() {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
public Path<?> getParentPath() {
return _parent;
}
public Expression<Class<? extends X>> type() {
// TODO Auto-generated method stub
throw new AbstractMethodError();
}
}

View File

@ -0,0 +1,125 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import org.apache.openjpa.kernel.exps.ExpressionFactory;
import org.apache.openjpa.persistence.meta.MetamodelImpl;
public class PredicateImpl extends ExpressionImpl<Boolean>
implements Predicate {
List<Expression<Boolean>> _exps;
BooleanOperator _op;
boolean _negated = false;
protected PredicateImpl() {
super(Boolean.class);
}
protected PredicateImpl(BooleanOperator op) {
this();
_op = op;
}
protected PredicateImpl(BooleanOperator op, Predicate...restrictions) {
this(op);
for (Predicate p : restrictions)
add((PredicateImpl)p);
}
public PredicateImpl add(Expression<Boolean> s) {
if (_exps == null)
_exps = new ArrayList<Expression<Boolean>>();
_exps.add(s);
return this;
}
public List<Expression<Boolean>> getExpressions() {
return _exps;
}
public BooleanOperator getOperator() {
return _op;
}
public boolean isNegated() {
return _negated;
}
public PredicateImpl negate() {
PredicateImpl not = new PredicateImpl(_op);
not._negated = true;
not._exps = new ArrayList<Expression<Boolean>>(this._exps);
not._op = this._op;
return not;
}
@Override
org.apache.openjpa.kernel.exps.Expression toKernelExpression(
ExpressionFactory factory, MetamodelImpl model) {
if (_exps == null || _exps.isEmpty())
return factory.emptyExpression();
if (_exps.size() == 1)
return ((ExpressionImpl<?>)_exps.get(0))
.toKernelExpression(factory, model);
ExpressionImpl<?> e1 = (ExpressionImpl<?>)_exps.get(0);
ExpressionImpl<?> e2 = (ExpressionImpl<?>)_exps.get(1);
org.apache.openjpa.kernel.exps.Expression ke1 =
e1.toKernelExpression(factory, model);
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);
for (int i = 2; i < _exps.size(); i++) {
ExpressionImpl<?> e = (ExpressionImpl<?>)_exps.get(i);
result = factory.and(result,
e.toKernelExpression(factory, model));
}
return _negated ? factory.not(result) : result;
}
public static class And extends PredicateImpl {
public And(Expression<Boolean> x, Expression<Boolean> y) {
super(BooleanOperator.AND);
add(x).add(y);
}
public And(Predicate...restrictions) {
super(BooleanOperator.AND, restrictions);
}
}
public static class Or extends PredicateImpl {
public Or(Expression<Boolean> x, Expression<Boolean> y) {
super(BooleanOperator.OR);
add(x).add(y);
}
public Or(Predicate...restrictions) {
super(BooleanOperator.AND, restrictions);
}
}
}

View File

@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.Entity;
import org.apache.openjpa.persistence.criteria.FromImpl;
/**
* A path from itself.
*
* @author Pinaki Poddar
*
* @param <X>
*/
public class RootImpl<X> extends FromImpl<X,X> implements Root<X> {
private final Entity<X> _entity;
public RootImpl(Entity<X> type) {
super(type);
_entity = type;
}
public Entity<X> getModel() {
return _entity;
}
}

View File

@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.openjpa.persistence.criteria;
import javax.persistence.criteria.Selection;
import org.apache.openjpa.persistence.ResultItemImpl;
/**
* An item selected in the projection clause of Criteria query.
*
* @author Pinaki Poddar
*
* @param <X>
*/
public class SelectionImpl<X> extends ResultItemImpl<X>
implements Selection<X> {
public SelectionImpl(Class<X> cls) {
super(cls);
}
}

View File

@ -0,0 +1,17 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
org.apache.openjpa.persistence.criteria.CriteriaBuilder