Author: steve.ebersole(a)jboss.com
Date: 2009-08-06 10:18:54 -0400 (Thu, 06 Aug 2009)
New Revision: 17243
Added:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/BasicFunctionExpression.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/CastFunction.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/FunctionExpression.java
Removed:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java
Log:
EJB-447 : Implement JPA 2.0 criteria apis (support for casts, completed IN-predicate
support & parameter creation)
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/QueryBuilderImpl.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -527,10 +527,7 @@
* {@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 );
}
/**
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ExpressionImpl.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -26,9 +26,10 @@
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 @@
* {@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 );
}
/**
Deleted:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -1,66 +0,0 @@
-/*
- * 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.List;
-import java.util.Arrays;
-import javax.persistence.criteria.Expression;
-
-import org.hibernate.ejb.criteria.QueryBuilderImpl;
-
-/**
- * TODO : javadoc
- *
- * @author Steve Ebersole
- */
-public class FunctionExpressionImpl<X> extends ExpressionImpl<X> implements
Expression<X> {
- private final String functionName;
- private final List<Expression<?>> argumentExpressions;
-
- public FunctionExpressionImpl(
- QueryBuilderImpl queryBuilder,
- Class<X> javaType,
- String functionName,
- List<Expression<?>> argumentExpressions) {
- super( queryBuilder, javaType );
- this.functionName = functionName;
- this.argumentExpressions = argumentExpressions;
- }
-
- public FunctionExpressionImpl(
- QueryBuilderImpl queryBuilder,
- Class<X> javaType,
- String functionName,
- Expression<?>... argumentExpressions) {
- super( queryBuilder, javaType );
- this.functionName = functionName;
- this.argumentExpressions = Arrays.asList( argumentExpressions );
- }
-
- public String getFunctionName() {
- return functionName;
- }
-
- public List<Expression<?>> getArgumentExpressions() {
- return argumentExpressions;
- }
-}
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/ParameterExpressionImpl.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -53,6 +53,14 @@
this.position = position;
}
+ public ParameterExpressionImpl(
+ QueryBuilderImpl queryBuilder,
+ Class<T> javaType) {
+ super( queryBuilder, javaType );
+ this.name = null;
+ this.position = null;
+ }
+
public String getName() {
return name;
}
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/AverageAggregrateFunction.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -23,7 +23,6 @@
import javax.persistence.criteria.Expression;
-import org.hibernate.ejb.criteria.expression.FunctionExpressionImpl;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
@@ -34,7 +33,7 @@
*
* @author Steve Ebersole
*/
-public class AverageAggregrateFunction extends FunctionExpressionImpl<Double> {
+public class AverageAggregrateFunction extends BasicFunctionExpression<Double> {
public AverageAggregrateFunction(
QueryBuilderImpl queryBuilder,
Expression<? extends Number> expression) {
Copied:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/BasicFunctionExpression.java
(from rev 17227,
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/FunctionExpressionImpl.java)
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/BasicFunctionExpression.java
(rev 0)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/BasicFunctionExpression.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -0,0 +1,67 @@
+/*
+ * 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.expression.*;
+import java.util.List;
+import java.util.Arrays;
+import javax.persistence.criteria.Expression;
+
+import org.hibernate.ejb.criteria.QueryBuilderImpl;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class BasicFunctionExpression<X> extends ExpressionImpl<X> implements
Expression<X> {
+ private final String functionName;
+ private final List<Expression<?>> argumentExpressions;
+
+ public BasicFunctionExpression(
+ QueryBuilderImpl queryBuilder,
+ Class<X> javaType,
+ String functionName,
+ List<Expression<?>> argumentExpressions) {
+ super( queryBuilder, javaType );
+ this.functionName = functionName;
+ this.argumentExpressions = argumentExpressions;
+ }
+
+ public BasicFunctionExpression(
+ QueryBuilderImpl queryBuilder,
+ Class<X> javaType,
+ String functionName,
+ Expression<?>... argumentExpressions) {
+ super( queryBuilder, javaType );
+ this.functionName = functionName;
+ this.argumentExpressions = Arrays.asList( argumentExpressions );
+ }
+
+ public String getFunctionName() {
+ return functionName;
+ }
+
+ public List<Expression<?>> getArgumentExpressions() {
+ return argumentExpressions;
+ }
+}
Added:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/CastFunction.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/CastFunction.java
(rev 0)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/CastFunction.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -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;
+ }
+
+}
Added:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/FunctionExpression.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/FunctionExpression.java
(rev 0)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/FunctionExpression.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -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();
+}
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/expression/function/SumAggregateFunction.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -23,7 +23,6 @@
import javax.persistence.criteria.Expression;
-import org.hibernate.ejb.criteria.expression.FunctionExpressionImpl;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
/**
@@ -34,7 +33,7 @@
*
* @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) {
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/ComparisonPredicate.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -24,9 +24,10 @@
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 @@
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() {
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java 2009-08-06
13:04:10 UTC (rev 17242)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/criteria/predicate/InPredicate.java 2009-08-06
14:18:54 UTC (rev 17243)
@@ -28,6 +28,7 @@
import javax.persistence.criteria.Expression;
import org.hibernate.ejb.criteria.QueryBuilderImpl;
+import org.hibernate.ejb.criteria.expression.LiteralExpression;
/**
* TODO : javadoc
@@ -91,9 +92,7 @@
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 @@
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 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) {