EJB-447 : Implement JPA 2.0 criteria apis (support for casts, completed IN-predicate support & parameter creation)

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17243 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2009-08-06 14:18:54 +00:00
parent aceeb48968
commit eb0396b1c6
10 changed files with 125 additions and 27 deletions

View File

@ -527,10 +527,7 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable {
* {@inheritDoc}
*/
public <T> ParameterExpression<T> parameter(Class<T> paramClass) {
// TODO : AFAICT there is really no way to implement this one correctly.
// the problem is the need to report getPosition...
throw new UnsupportedOperationException( "Note yet implemented!" );
// return new ParameterExpressionImpl( this, paramClass, ??? );
return new ParameterExpressionImpl<T>( this, paramClass );
}
/**

View File

@ -26,9 +26,10 @@ import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
import org.hibernate.ejb.criteria.expression.function.CastFunction;
/**
* TODO : javadoc
* Models an expression in the criteria query language.
*
* @author Steve Ebersole
*/
@ -41,8 +42,9 @@ public class ExpressionImpl<T> extends SelectionImpl<T> implements Expression<T>
* {@inheritDoc}
*/
public <X> Expression<X> as(Class<X> type) {
// TODO-STEVE : implement - needs a cast expression
throw new UnsupportedOperationException( "Not yet implemented!" );
return type.equals( getJavaType() )
? (Expression<X>) this
: new CastFunction<X, T>( queryBuilder(), type, this );
}
/**

View File

@ -53,6 +53,14 @@ public class ParameterExpressionImpl<T> extends ExpressionImpl<T> implements Par
this.position = position;
}
public ParameterExpressionImpl(
QueryBuilderImpl queryBuilder,
Class<T> javaType) {
super( queryBuilder, javaType );
this.name = null;
this.position = null;
}
public String getName() {
return name;
}

View File

@ -23,7 +23,6 @@ package org.hibernate.ejb.criteria.expression.function;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.expression.FunctionExpressionImpl;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
@ -34,7 +33,7 @@ import org.hibernate.ejb.criteria.QueryBuilderImpl;
*
* @author Steve Ebersole
*/
public class AverageAggregrateFunction extends FunctionExpressionImpl<Double> {
public class AverageAggregrateFunction extends BasicFunctionExpression<Double> {
public AverageAggregrateFunction(
QueryBuilderImpl queryBuilder,
Expression<? extends Number> expression) {

View File

@ -19,8 +19,9 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.ejb.criteria.expression;
package org.hibernate.ejb.criteria.expression.function;
import org.hibernate.ejb.criteria.expression.*;
import java.util.List;
import java.util.Arrays;
import javax.persistence.criteria.Expression;
@ -32,11 +33,11 @@ import org.hibernate.ejb.criteria.QueryBuilderImpl;
*
* @author Steve Ebersole
*/
public class FunctionExpressionImpl<X> extends ExpressionImpl<X> implements Expression<X> {
public class BasicFunctionExpression<X> extends ExpressionImpl<X> implements Expression<X> {
private final String functionName;
private final List<Expression<?>> argumentExpressions;
public FunctionExpressionImpl(
public BasicFunctionExpression(
QueryBuilderImpl queryBuilder,
Class<X> javaType,
String functionName,
@ -46,7 +47,7 @@ public class FunctionExpressionImpl<X> extends ExpressionImpl<X> implements Expr
this.argumentExpressions = argumentExpressions;
}
public FunctionExpressionImpl(
public BasicFunctionExpression(
QueryBuilderImpl queryBuilder,
Class<X> javaType,
String functionName,

View File

@ -0,0 +1,56 @@
/*
* 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.function;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
import org.hibernate.ejb.criteria.expression.ExpressionImpl;
/**
* Models a <tt>CAST</tt> function.
*
* @param <T> The cast result type.
* @param <Y> The type of the expression to be cast.
*
* @author Steve Ebersole
*/
public class CastFunction<T,Y> extends ExpressionImpl<T> implements FunctionExpression<T> {
public static final String CAST_NAME = "cast";
private final ExpressionImpl<Y> castSource;
public CastFunction(
QueryBuilderImpl queryBuilder,
Class<T> javaType,
ExpressionImpl<Y> castSource) {
super( queryBuilder, javaType );
this.castSource = castSource;
}
public String getFunctionName() {
return CAST_NAME;
}
public ExpressionImpl<Y> getCastSource() {
return castSource;
}
}

View File

@ -0,0 +1,35 @@
/*
* 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.function;
import javax.persistence.criteria.Expression;
/**
* Models an expression resolved as a SQL function call.
*
* @param <T> The type of the function result.
*
* @author Steve Ebersole
*/
public interface FunctionExpression<T> extends Expression<T> {
public String getFunctionName();
}

View File

@ -23,7 +23,6 @@ package org.hibernate.ejb.criteria.expression.function;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.expression.FunctionExpressionImpl;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
@ -34,7 +33,7 @@ import org.hibernate.ejb.criteria.QueryBuilderImpl;
*
* @author Steve Ebersole
*/
public class SumAggregateFunction<N extends Number> extends FunctionExpressionImpl<N> {
public class SumAggregateFunction<N extends Number> extends BasicFunctionExpression<N> {
public SumAggregateFunction(
QueryBuilderImpl queryBuilder,
Expression<N> expression) {

View File

@ -24,9 +24,10 @@ package org.hibernate.ejb.criteria.predicate;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
import org.hibernate.ejb.criteria.expression.LiteralExpression;
/**
* TODO : javadoc
* Models a basic relational comparison predicate.
*
* @author Steve Ebersole
*/
@ -54,9 +55,7 @@ public class ComparisonPredicate extends AbstractSimplePredicate {
super( queryBuilder );
this.comparisonOperator = comparisonOperator;
this.leftHandSide = leftHandSide;
// TODO : generate a parameter expression once those are ready...
this.rightHandSide = null;
throw new UnsupportedOperationException( "Not yet implemented!" );
this.rightHandSide = new LiteralExpression( queryBuilder, rightHandSide );
}
public ComparisonOperator getComparisonOperator() {

View File

@ -28,6 +28,7 @@ import java.util.Collection;
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
import org.hibernate.ejb.criteria.expression.LiteralExpression;
/**
* TODO : javadoc
@ -91,9 +92,7 @@ public class InPredicate<T> extends AbstractSimplePredicate implements QueryBuil
QueryBuilderImpl queryBuilder,
Expression<? extends T> expression,
T... values) {
super( queryBuilder );
// TODO : implements - needs parameter expressions
throw new UnsupportedOperationException( "Not yet implemented!" );
this( queryBuilder, expression, Arrays.asList( values ) );
}
/**
@ -108,12 +107,16 @@ public class InPredicate<T> extends AbstractSimplePredicate implements QueryBuil
Expression<? extends T> expression,
Collection<T> values) {
super( queryBuilder );
// TODO : implements - needs parameter expressions
throw new UnsupportedOperationException( "Not yet implemented!" );
this.expression = expression;
// TODO : size this?
this.values = new ArrayList<Expression<? extends T>>();
for ( T value : values ) {
this.values.add( new LiteralExpression<T>( queryBuilder, value ) );
}
}
@SuppressWarnings("unchecked")
public Expression<T> getExpression() {
//noinspection unchecked
return ( Expression<T> ) expression;
}
@ -126,8 +129,7 @@ public class InPredicate<T> extends AbstractSimplePredicate implements QueryBuil
}
public InPredicate<T> value(T value) {
// TODO : implements - needs parameter expressions
throw new UnsupportedOperationException( "Not yet implemented!" );
return value( new LiteralExpression<T>( queryBuilder(), value ) );
}
public InPredicate<T> value(Expression<? extends T> value) {