diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java index 9b8e42ce6e..8ea54979f8 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java @@ -527,10 +527,7 @@ public class QueryBuilderImpl implements QueryBuilder, Serializable { * {@inheritDoc} */ public ParameterExpression parameter(Class 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( this, paramClass ); } /** diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java index fd9641aee3..fb8c2048a0 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java @@ -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 extends SelectionImpl implements Expression * {@inheritDoc} */ public Expression as(Class type) { - // TODO-STEVE : implement - needs a cast expression - throw new UnsupportedOperationException( "Not yet implemented!" ); + return type.equals( getJavaType() ) + ? (Expression) this + : new CastFunction( queryBuilder(), type, this ); } /** diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java index 3f139f54f2..d553a20f75 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java @@ -53,6 +53,14 @@ public class ParameterExpressionImpl extends ExpressionImpl implements Par this.position = position; } + public ParameterExpressionImpl( + QueryBuilderImpl queryBuilder, + Class javaType) { + super( queryBuilder, javaType ); + this.name = null; + this.position = null; + } + public String getName() { return name; } diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java index 9bb982e641..2056d8b8da 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java @@ -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 { +public class AverageAggregrateFunction extends BasicFunctionExpression { public AverageAggregrateFunction( QueryBuilderImpl queryBuilder, Expression expression) { diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/BasicFunctionExpression.java similarity index 88% rename from entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java rename to entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/BasicFunctionExpression.java index 7d57c32f9a..b8a9b03c26 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/BasicFunctionExpression.java @@ -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 extends ExpressionImpl implements Expression { +public class BasicFunctionExpression extends ExpressionImpl implements Expression { private final String functionName; private final List> argumentExpressions; - public FunctionExpressionImpl( + public BasicFunctionExpression( QueryBuilderImpl queryBuilder, Class javaType, String functionName, @@ -46,7 +47,7 @@ public class FunctionExpressionImpl extends ExpressionImpl implements Expr this.argumentExpressions = argumentExpressions; } - public FunctionExpressionImpl( + public BasicFunctionExpression( QueryBuilderImpl queryBuilder, Class javaType, String functionName, diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/CastFunction.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/CastFunction.java new file mode 100644 index 0000000000..e860d5276f --- /dev/null +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/CastFunction.java @@ -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 CAST function. + * + * @param The cast result type. + * @param The type of the expression to be cast. + * + * @author Steve Ebersole + */ +public class CastFunction extends ExpressionImpl implements FunctionExpression { + public static final String CAST_NAME = "cast"; + + private final ExpressionImpl castSource; + + public CastFunction( + QueryBuilderImpl queryBuilder, + Class javaType, + ExpressionImpl castSource) { + super( queryBuilder, javaType ); + this.castSource = castSource; + } + + public String getFunctionName() { + return CAST_NAME; + } + + public ExpressionImpl getCastSource() { + return castSource; + } + +} diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/FunctionExpression.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/FunctionExpression.java new file mode 100644 index 0000000000..266965af5d --- /dev/null +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/FunctionExpression.java @@ -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 The type of the function result. + * + * @author Steve Ebersole + */ +public interface FunctionExpression extends Expression { + public String getFunctionName(); +} diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java index 026ca726d7..1c59bde53d 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java @@ -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 extends FunctionExpressionImpl { +public class SumAggregateFunction extends BasicFunctionExpression { public SumAggregateFunction( QueryBuilderImpl queryBuilder, Expression expression) { diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java index c9927cd523..047c1e2955 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java @@ -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() { diff --git a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java index 4f548a130f..08db43dd10 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java @@ -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 extends AbstractSimplePredicate implements QueryBuil QueryBuilderImpl queryBuilder, Expression 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 extends AbstractSimplePredicate implements QueryBuil Expression expression, Collection values) { super( queryBuilder ); - // TODO : implements - needs parameter expressions - throw new UnsupportedOperationException( "Not yet implemented!" ); + this.expression = expression; + // TODO : size this? + this.values = new ArrayList>(); + for ( T value : values ) { + this.values.add( new LiteralExpression( queryBuilder, value ) ); + } } + @SuppressWarnings("unchecked") public Expression getExpression() { - //noinspection unchecked return ( Expression ) expression; } @@ -126,8 +129,7 @@ public class InPredicate extends AbstractSimplePredicate implements QueryBuil } public InPredicate value(T value) { - // TODO : implements - needs parameter expressions - throw new UnsupportedOperationException( "Not yet implemented!" ); + return value( new LiteralExpression( queryBuilder(), value ) ); } public InPredicate value(Expression value) {