EJB-447 : Implement JPA 2.0 criteria apis (miscellaneous)

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17252 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-08-07 18:38:07 +00:00
parent 3175437151
commit a83a455fdd
9 changed files with 595 additions and 58 deletions

View File

@ -42,9 +42,15 @@ import javax.persistence.Tuple;
import org.hibernate.ejb.EntityManagerFactoryImpl;
import org.hibernate.ejb.criteria.expression.BinaryArithmeticOperation;
import org.hibernate.ejb.criteria.expression.CoalesceExpression;
import org.hibernate.ejb.criteria.expression.CompoundSelectionImpl;
import org.hibernate.ejb.criteria.expression.ConcatExpression;
import org.hibernate.ejb.criteria.expression.ExpressionImpl;
import org.hibernate.ejb.criteria.expression.ParameterExpressionImpl;
import org.hibernate.ejb.criteria.expression.LiteralExpression;
import org.hibernate.ejb.criteria.expression.NullifExpression;
import org.hibernate.ejb.criteria.expression.SearchedCaseExpression;
import org.hibernate.ejb.criteria.expression.SimpleCaseExpression;
import org.hibernate.ejb.criteria.expression.SubqueryComparisonModifierExpression;
import org.hibernate.ejb.criteria.expression.UnaryArithmeticOperation;
import org.hibernate.ejb.criteria.expression.function.AbsFunction;
@ -68,6 +74,7 @@ import org.hibernate.ejb.criteria.predicate.CompoundPredicate;
import org.hibernate.ejb.criteria.predicate.ComparisonPredicate;
import org.hibernate.ejb.criteria.predicate.InPredicate;
import org.hibernate.ejb.criteria.predicate.BetweenPredicate;
import org.hibernate.ejb.criteria.predicate.ExistsPredicate;
import org.hibernate.ejb.criteria.predicate.LikePredicate;
import static org.hibernate.ejb.criteria.predicate.ComparisonPredicate.ComparisonOperator;
@ -1023,7 +1030,7 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public Predicate exists(Subquery<?> subquery) {
return null;
return new ExistsPredicate( this, subquery );
}
/**
@ -1063,88 +1070,140 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
}
// miscellaneous expressions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/**
* {@inheritDoc}
*/
public <Y> Expression<Y> coalesce(Expression<? extends Y> exp1, Expression<? extends Y> exp2) {
return coalesce( (Class<Y>)null, exp1, exp2 );
}
public <Y> Expression<Y> coalesce(Class<Y> type, Expression<? extends Y> exp1, Expression<? extends Y> exp2) {
return new CoalesceExpression<Y>( this, type ).value( exp1 ).value( exp2 );
}
/**
* {@inheritDoc}
*/
public <Y> Expression<Y> coalesce(Expression<? extends Y> exp1, Y exp2) {
return coalesce( (Class<Y>)null, exp1, exp2 );
}
public <Y> Expression<Y> coalesce(Class<Y> type, Expression<? extends Y> exp1, Y exp2) {
return new CoalesceExpression<Y>( this, type ).value( exp1 ).value( exp2 );
}
/**
* {@inheritDoc}
*/
public <T> Coalesce<T> coalesce() {
return coalesce( (Class<T>)null );
}
public <T> Coalesce<T> coalesce(Class<T> type) {
return new CoalesceExpression<T>( this, type );
}
/**
* {@inheritDoc}
*/
public Expression<String> concat(Expression<String> string1, Expression<String> string2) {
return new ConcatExpression( this, string1, string2 );
}
/**
* {@inheritDoc}
*/
public Expression<String> concat(Expression<String> string1, String string2) {
return new ConcatExpression( this, string1, string2 );
}
/**
* {@inheritDoc}
*/
public Expression<String> concat(String string1, Expression<String> string2) {
return new ConcatExpression( this, string1, string2 );
}
/**
* {@inheritDoc}
*/
public <Y> Expression<Y> nullif(Expression<Y> exp1, Expression<?> exp2) {
return nullif( (Class<Y>)null, exp1, exp2 );
}
public <Y> Expression<Y> nullif(Class<Y> type, Expression<Y> exp1, Expression<?> exp2) {
return new NullifExpression<Y>( this, type, exp1, exp2 );
}
public <Y> Expression<Y> nullif(Expression<Y> exp1, Y exp2) {
return nullif( (Class<Y>)null, exp1, exp2 );
}
public <Y> Expression<Y> nullif(Class<Y> type, Expression<Y> exp1, Y exp2) {
return new NullifExpression<Y>( this, type, exp1, exp2 );
}
public <C, R> SimpleCase<C, R> selectCase(Expression<? extends C> expression) {
return selectCase( (Class<R>)null, expression );
}
public <C, R> SimpleCase<C, R> selectCase(Class<R> type, Expression<? extends C> expression) {
return new SimpleCaseExpression<C, R>( this, type, expression );
}
public <R> Case<R> selectCase() {
return selectCase( (Class<R>)null );
}
public <R> Case<R> selectCase(Class<R> type) {
return new SearchedCaseExpression<R>( this, type );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public <C extends Collection<?>> Predicate isEmpty(Expression<C> cExpression) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <C extends Collection<?>> Predicate isNotEmpty(Expression<C> cExpression) {
return null;
public <C extends Collection<?>> Predicate isNotEmpty(Expression<C> colelctionExpression) {
return isEmpty( colelctionExpression ).negate();
}
public <C extends Collection<?>> Expression<Integer> size(C c) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <C extends Collection<?>> Expression<Integer> size(Expression<C> cExpression) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <E, C extends Collection<E>> Predicate isMember(E e, Expression<C> cExpression) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <E, C extends Collection<E>> Predicate isNotMember(E e, Expression<C> cExpression) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <E, C extends Collection<E>> Predicate isMember(Expression<E> eExpression, Expression<C> cExpression) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <E, C extends Collection<E>> Predicate isNotMember(Expression<E> eExpression, Expression<C> cExpression) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <V, M extends Map<?, V>> Expression<Collection<V>> values(M map) {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
public <K, M extends Map<K, ?>> Expression<Set<K>> keys(M m) {
return null;
}
public Expression<String> concat(Expression<String> stringExpression, Expression<String> stringExpression1) {
return null;
}
public Expression<String> concat(Expression<String> stringExpression, String s) {
return null;
}
public Expression<String> concat(String s, Expression<String> stringExpression) {
return null;
}
public <Y> Expression<Y> coalesce(Expression<? extends Y> expression, Expression<? extends Y> expression1) {
return null;
}
public <Y> Expression<Y> coalesce(Expression<? extends Y> expression, Y y) {
return null;
}
public <Y> Expression<Y> nullif(Expression<Y> yExpression, Expression<?> expression) {
return null;
}
public <Y> Expression<Y> nullif(Expression<Y> yExpression, Y y) {
return null;
}
public <T> Coalesce<T> coalesce() {
return null;
}
public <C, R> SimpleCase<C, R> selectCase(Expression<? extends C> expression) {
return null;
}
public <R> Case<R> selectCase() {
return null;
throw new UnsupportedOperationException( "Not yet implemented!" );
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.expression;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.QueryBuilder.Coalesce;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Models an ANSI SQL <tt>COALESCE</tt> expression. <tt>COALESCE</tt> is a specialized <tt>CASE</tt> statement.
*
* @author Steve Ebersole
*/
public class CoalesceExpression<T> extends ExpressionImpl<T> implements Coalesce<T> {
private final List<Expression<? extends T>> expressions;
private Class<T> javaType;
public CoalesceExpression(QueryBuilderImpl queryBuilder) {
this( queryBuilder, null );
}
public CoalesceExpression(
QueryBuilderImpl queryBuilder,
Class<T> javaType) {
super( queryBuilder, javaType );
this.javaType = javaType;
this.expressions = new ArrayList<Expression<? extends T>>();
}
@Override
public Class<T> getJavaType() {
return javaType;
}
public Coalesce<T> value(T value) {
return value( new LiteralExpression<T>( queryBuilder(), value ) );
}
public Coalesce<T> value(Expression<? extends T> value) {
expressions.add( value );
if ( javaType == null ) {
javaType = (Class<T>) value.getJavaType();
}
return this;
}
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.expression;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class ConcatExpression extends ExpressionImpl<String> {
private Expression<String> string1;
private Expression<String> string2;
public ConcatExpression(
QueryBuilderImpl queryBuilder,
Expression<String> expression1,
Expression<String> expression2) {
super( queryBuilder, String.class );
this.string1 = expression1;
this.string2 = expression2;
}
public ConcatExpression(
QueryBuilderImpl queryBuilder,
Expression<String> string1,
String string2) {
this( queryBuilder, string1, wrap(queryBuilder, string2) );
}
private static Expression<String> wrap(QueryBuilderImpl queryBuilder, String string) {
return new LiteralExpression<String>( queryBuilder, string );
}
public ConcatExpression(
QueryBuilderImpl queryBuilder,
String string1,
Expression<String> string2) {
this( queryBuilder, wrap(queryBuilder, string1), string2 );
}
public Expression<String> getString1() {
return string1;
}
public Expression<String> getString2() {
return string2;
}
}

View File

@ -31,11 +31,16 @@ import org.hibernate.ejb.criteria.QueryBuilderImpl;
public class LiteralExpression<T> extends ExpressionImpl<T> {
private final T literal;
public LiteralExpression(
QueryBuilderImpl queryBuilder,
T literal) {
//noinspection unchecked
super( queryBuilder, ( Class<T> ) literal.getClass() );
public LiteralExpression(QueryBuilderImpl queryBuilder, T literal) {
this( queryBuilder, (Class<T>) determineClass( literal ), literal );
}
private static Class determineClass(Object literal) {
return literal == null ? null : literal.getClass();
}
public LiteralExpression(QueryBuilderImpl queryBuilder, Class<T> type, T literal) {
super( queryBuilder, type );
this.literal = literal;
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.expression;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Models an ANSI SQL <tt>NULLIF</tt> expression. <tt>NULLIF</tt> is a specialized <tt>CASE</tt> statement.
*
* @author Steve Ebersole
*/
public class NullifExpression<T> extends ExpressionImpl<T> {
private final Expression<? extends T> primaryExpression;
private final Expression<?> secondaryExpression;
public NullifExpression(
QueryBuilderImpl queryBuilder,
Class<T> javaType,
Expression<? extends T> primaryExpression,
Expression<?> secondaryExpression) {
super( queryBuilder, (Class<T>)determineType(javaType, primaryExpression) );
this.primaryExpression = primaryExpression;
this.secondaryExpression = secondaryExpression;
}
public NullifExpression(
QueryBuilderImpl queryBuilder,
Class<T> javaType,
Expression<? extends T> primaryExpression,
Object secondaryExpression) {
super( queryBuilder, (Class<T>)determineType(javaType, primaryExpression) );
this.primaryExpression = primaryExpression;
this.secondaryExpression = new LiteralExpression( queryBuilder, secondaryExpression );
}
private static Class determineType(Class javaType, Expression primaryExpression) {
return javaType != null ? javaType : primaryExpression.getJavaType();
}
public Expression<? extends T> getPrimaryExpression() {
return primaryExpression;
}
public Expression<?> getSecondaryExpression() {
return secondaryExpression;
}
}

View File

@ -0,0 +1,108 @@
/*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.expression;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.QueryBuilder.Case;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class SearchedCaseExpression<R> extends ExpressionImpl<R> implements Case<R> {
private Class<R> javaType; // overrides the javaType kept on tuple-impl so that we can adjust it
private List<WhenClause> whenClauses = new ArrayList<WhenClause>();
private Expression<? extends R> otherwiseResult;
public class WhenClause {
private final Expression<Boolean> condition;
private final Expression<? extends R> result;
public WhenClause(Expression<Boolean> condition, Expression<? extends R> result) {
this.condition = condition;
this.result = result;
}
public Expression<Boolean> getCondition() {
return condition;
}
public Expression<? extends R> getResult() {
return result;
}
}
public SearchedCaseExpression(
QueryBuilderImpl queryBuilder,
Class<R> javaType) {
super(queryBuilder, javaType);
this.javaType = javaType;
}
public Case<R> when(Expression<Boolean> condition, R result) {
return when( condition, buildLiteral(result) );
}
private LiteralExpression<R> buildLiteral(R result) {
final Class<R> type = result != null
? (Class<R>) result.getClass()
: (Class<R>) getJavaType();
return new LiteralExpression<R>( queryBuilder(), type, result );
}
public Case<R> when(Expression<Boolean> condition, Expression<? extends R> result) {
WhenClause whenClause = new WhenClause( condition, result );
whenClauses.add( whenClause );
adjustJavaType( result );
return this;
}
private void adjustJavaType(Expression<? extends R> exp) {
if ( javaType == null ) {
javaType = (Class<R>) exp.getJavaType();
}
}
public Expression<R> otherwise(R result) {
return otherwise( buildLiteral(result) );
}
public Expression<R> otherwise(Expression<? extends R> result) {
this.otherwiseResult = result;
adjustJavaType( result );
return this;
}
public Expression<? extends R> getOtherwiseResult() {
return otherwiseResult;
}
public List<WhenClause> getWhenClauses() {
return whenClauses;
}
}

View File

@ -0,0 +1,114 @@
/*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.expression;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.QueryBuilder.SimpleCase;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class SimpleCaseExpression<C,R> extends ExpressionImpl<R> implements SimpleCase<C,R> {
private Class<R> javaType;
private final Expression<? extends C> expression;
private List<WhenClause> whenClauses = new ArrayList<WhenClause>();
private Expression<? extends R> otherwiseResult;
public class WhenClause {
private final C condition;
private final Expression<? extends R> result;
public WhenClause(C condition, Expression<? extends R> result) {
this.condition = condition;
this.result = result;
}
public C getCondition() {
return condition;
}
public Expression<? extends R> getResult() {
return result;
}
}
public SimpleCaseExpression(
QueryBuilderImpl queryBuilder,
Class<R> javaType,
Expression<? extends C> expression) {
super(queryBuilder, javaType);
this.javaType = javaType;
this.expression = expression;
}
public Expression<C> getExpression() {
return (Expression<C>) expression;
}
public SimpleCase<C, R> when(C condition, R result) {
return when( condition, buildLiteral(result) );
}
private LiteralExpression<R> buildLiteral(R result) {
final Class<R> type = result != null
? (Class<R>) result.getClass()
: (Class<R>) getJavaType();
return new LiteralExpression<R>( queryBuilder(), type, result );
}
public SimpleCase<C, R> when(C condition, Expression<? extends R> result) {
WhenClause whenClause = new WhenClause( condition, result );
whenClauses.add( whenClause );
adjustJavaType( result );
return this;
}
private void adjustJavaType(Expression<? extends R> exp) {
if ( javaType == null ) {
javaType = (Class<R>) exp.getJavaType();
}
}
public Expression<R> otherwise(R result) {
return otherwise( buildLiteral(result) );
}
public Expression<R> otherwise(Expression<? extends R> result) {
this.otherwiseResult = result;
adjustJavaType( result );
return this;
}
public Expression<? extends R> getOtherwiseResult() {
return otherwiseResult;
}
public List<WhenClause> getWhenClauses() {
return whenClauses;
}
}

View File

@ -43,7 +43,7 @@ public class AbstractSimplePredicate extends AbstractPredicateImpl {
return BooleanOperator.AND;
}
public List<Expression<Boolean>> getExpressions() {
public final List<Expression<Boolean>> getExpressions() {
return NO_EXPRESSIONS;
}

View File

@ -0,0 +1,44 @@
/*
* Copyright (c) 2009, Red Hat Middleware LLC or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.predicate;
import javax.persistence.criteria.Subquery;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
* Models an <tt>EXISTS(<subquery>)</tt> predicate
*
* @author Steve Ebersole
*/
public class ExistsPredicate extends AbstractSimplePredicate {
private final Subquery<?> subquery;
public ExistsPredicate(QueryBuilderImpl queryBuilder, Subquery<?> subquery) {
super(queryBuilder);
this.subquery = subquery;
}
public Subquery<?> getSubquery() {
return subquery;
}
}