diff --git a/querydsl-collections/src/main/java/com/querydsl/collections/CollQueryMixin.java b/querydsl-collections/src/main/java/com/querydsl/collections/CollQueryMixin.java index 2ccf7b0b1..eb2e65ec4 100644 --- a/querydsl-collections/src/main/java/com/querydsl/collections/CollQueryMixin.java +++ b/querydsl-collections/src/main/java/com/querydsl/collections/CollQueryMixin.java @@ -44,7 +44,7 @@ public class CollQueryMixin extends QueryMixin { } @Override - protected Predicate normalize(Predicate predicate, boolean where) { + protected Predicate convert(Predicate predicate, Role role) { predicate = (Predicate)ExpressionUtils.extract(predicate); if (predicate != null) { Context context = new Context(); diff --git a/querydsl-core/src/main/java/com/querydsl/core/support/DetachableMixin.java b/querydsl-core/src/main/java/com/querydsl/core/support/DetachableMixin.java index 5c4c833cb..857388001 100644 --- a/querydsl-core/src/main/java/com/querydsl/core/support/DetachableMixin.java +++ b/querydsl-core/src/main/java/com/querydsl/core/support/DetachableMixin.java @@ -107,7 +107,7 @@ public class DetachableMixin implements Detachable { QueryMetadata metadata = queryMixin.getMetadata().clone(); Expression[] copy = new Expression[projection.length]; for (int i = 0; i < copy.length; i++) { - Expression expr = queryMixin.convert(projection[i], false); + Expression expr = queryMixin.convert(projection[i], QueryMixin.Role.SELECT); copy[i] = nullAsTemplate(expr); } metadata.setProjection(Projections.tuple(copy)); @@ -116,7 +116,7 @@ public class DetachableMixin implements Detachable { private QueryMetadata projection(Expression projection) { QueryMetadata metadata = queryMixin.getMetadata().clone(); - Expression expr = queryMixin.convert(projection, false); + Expression expr = queryMixin.convert(projection, QueryMixin.Role.SELECT); expr = nullAsTemplate(expr); metadata.setProjection(expr); return metadata; diff --git a/querydsl-core/src/main/java/com/querydsl/core/support/QueryMixin.java b/querydsl-core/src/main/java/com/querydsl/core/support/QueryMixin.java index 16bd44fa0..e9442392c 100644 --- a/querydsl-core/src/main/java/com/querydsl/core/support/QueryMixin.java +++ b/querydsl-core/src/main/java/com/querydsl/core/support/QueryMixin.java @@ -34,6 +34,8 @@ import com.querydsl.core.types.FactoryExpressionUtils.FactoryExpressionAdapter; */ public class QueryMixin { + public enum Role { SELECT, FROM, WHERE, GROUP_BY, HAVING, ORDER_BY } + private final QueryMetadata metadata; private final boolean expandAnyPaths; @@ -98,7 +100,7 @@ public class QueryMixin { } public Expression setProjection(Expression e) { - e = convert(e, false); + e = convert(e, Role.SELECT); metadata.setProjection(e); return e; } @@ -106,7 +108,7 @@ public class QueryMixin { public T setProjection(Expression... o) { Expression[] copy = new Expression[o.length]; for (int i = 0; i < copy.length; i++) { - copy[i] = convert(o[i], false); + copy[i] = convert(o[i], Role.SELECT); } metadata.setProjection(Projections.tuple(copy)); return self; @@ -135,7 +137,7 @@ public class QueryMixin { } @SuppressWarnings("rawtypes") - public Expression convert(Expression expr, boolean forOrder) { + public Expression convert(Expression expr, Role role) { if (expandAnyPaths) { if (expr instanceof Path) { expr = (Expression)normalizePath((Path)expr); @@ -144,7 +146,7 @@ public class QueryMixin { } } if (expr instanceof ProjectionRole) { - return convert(((ProjectionRole) expr).getProjection(), forOrder); + return convert(((ProjectionRole) expr).getProjection(), role); } else if (expr instanceof FactoryExpression && !(expr instanceof FactoryExpressionAdapter)) { return FactoryExpressionUtils.wrap((FactoryExpression)expr); } else { @@ -152,6 +154,11 @@ public class QueryMixin { } } + protected Predicate convert(Predicate condition, Role role) { + return condition; + } + + public Expression createProjection(Expression[] args) { return Projections.tuple(args); } @@ -223,13 +230,13 @@ public class QueryMixin { } public final T having(Predicate e) { - metadata.addHaving(normalize(e, false)); + metadata.addHaving(convert(e, Role.HAVING)); return self; } public final T having(Predicate... o) { for (Predicate e : o) { - metadata.addHaving(normalize(e, false)); + metadata.addHaving(convert(e, Role.HAVING)); } return self; } @@ -328,19 +335,19 @@ public class QueryMixin { } public final T on(Predicate condition) { - metadata.addJoinCondition(normalize(condition, false)); + metadata.addJoinCondition(convert(condition, Role.FROM)); return self; } public final T on(Predicate... conditions) { for (Predicate condition : conditions) { - metadata.addJoinCondition(normalize(condition, false)); + metadata.addJoinCondition(convert(condition, Role.FROM)); } return self; } public final T orderBy(OrderSpecifier spec) { - Expression e = convert(spec.getTarget(), true); + Expression e = convert(spec.getTarget(), Role.ORDER_BY); if (!spec.getTarget().equals(e)) { metadata.addOrderBy(new OrderSpecifier(spec.getOrder(), e)); } else { @@ -404,21 +411,17 @@ public class QueryMixin { } public final T where(Predicate e) { - metadata.addWhere(normalize(e, true)); + metadata.addWhere(convert(e, Role.WHERE)); return self; } public final T where(Predicate... o) { for (Predicate e : o) { - metadata.addWhere(normalize(e, true)); + metadata.addWhere(convert(e, Role.WHERE)); } return self; } - protected Predicate normalize(Predicate condition, boolean where) { - return condition; - } - @Override public final boolean equals(Object o) { if (o == this) { diff --git a/querydsl-core/src/main/java/com/querydsl/core/types/expr/StringExpression.java b/querydsl-core/src/main/java/com/querydsl/core/types/expr/StringExpression.java index 6aaf00140..39c948266 100644 --- a/querydsl-core/src/main/java/com/querydsl/core/types/expr/StringExpression.java +++ b/querydsl-core/src/main/java/com/querydsl/core/types/expr/StringExpression.java @@ -396,7 +396,7 @@ public abstract class StringExpression extends ComparableExpression { /** * Return true if this String matches the given regular expression * - *

Some implementations such as Querydsl JPA will try to convert a regex expression into like + *

Some implementations such as Querydsl JPA will try to convert a regex expression into like * form and will throw an Exception when this fails

* * @param regex @@ -410,7 +410,7 @@ public abstract class StringExpression extends ComparableExpression { /** * Return true if this String matches the given regular expression * - *

Some implementations such as Querydsl JPA will try to convert a regex expression into like + *

Some implementations such as Querydsl JPA will try to convert a regex expression into like * form and will throw an Exception when this fails

* * @param regex diff --git a/querydsl-jdo/src/main/java/com/querydsl/jdo/JDOQueryMixin.java b/querydsl-jdo/src/main/java/com/querydsl/jdo/JDOQueryMixin.java index e3957343e..0163ed41f 100644 --- a/querydsl-jdo/src/main/java/com/querydsl/jdo/JDOQueryMixin.java +++ b/querydsl-jdo/src/main/java/com/querydsl/jdo/JDOQueryMixin.java @@ -44,14 +44,14 @@ public class JDOQueryMixin extends QueryMixin { } @Override - protected Predicate normalize(Predicate predicate, boolean where) { + protected Predicate convert(Predicate predicate, Role role) { predicate = (Predicate)ExpressionUtils.extract(predicate); if (predicate != null) { Context context = new Context(); Predicate transformed = (Predicate) predicate.accept(CollectionAnyVisitor.DEFAULT, context); for (int i = 0; i < context.paths.size(); i++) { Path path = context.paths.get(i); - addCondition(context, i, path, where); + addCondition(context, i, path, role); } return transformed; } else { @@ -60,11 +60,11 @@ public class JDOQueryMixin extends QueryMixin { } @SuppressWarnings("unchecked") - private void addCondition(Context context, int i, Path path, boolean where) { + private void addCondition(Context context, int i, Path path, Role role) { EntityPath alias = context.replacements.get(i); from(alias); Predicate condition = PredicateOperation.create(Ops.IN, alias, path.getMetadata().getParent()); - if (where) { + if (role == Role.WHERE) { super.where(condition); } else { super.having(condition); diff --git a/querydsl-jpa/src/main/java/com/querydsl/jpa/AbstractSQLQuery.java b/querydsl-jpa/src/main/java/com/querydsl/jpa/AbstractSQLQuery.java index acbfae344..5aa74543a 100644 --- a/querydsl-jpa/src/main/java/com/querydsl/jpa/AbstractSQLQuery.java +++ b/querydsl-jpa/src/main/java/com/querydsl/jpa/AbstractSQLQuery.java @@ -13,16 +13,16 @@ */ package com.querydsl.jpa; +import javax.persistence.Entity; + import com.querydsl.core.QueryMetadata; -import com.querydsl.sql.Configuration; -import com.querydsl.sql.ProjectableSQLQuery; import com.querydsl.core.support.QueryMixin; import com.querydsl.core.types.EntityPath; import com.querydsl.core.types.Expression; import com.querydsl.core.types.Operation; import com.querydsl.core.types.TemplateExpression; - -import javax.persistence.Entity; +import com.querydsl.sql.Configuration; +import com.querydsl.sql.ProjectableSQLQuery; /** * Abstract super class for SQLQuery implementation for JPA and Hibernate @@ -39,8 +39,8 @@ public abstract class AbstractSQLQuery> extends Pr } @Override - public Expression convert(Expression expr, boolean forOrder) { - return Conversions.convertForNativeQuery(super.convert(expr, forOrder)); + public Expression convert(Expression expr, Role role) { + return Conversions.convertForNativeQuery(super.convert(expr, role)); } } diff --git a/querydsl-jpa/src/main/java/com/querydsl/jpa/JPAQueryMixin.java b/querydsl-jpa/src/main/java/com/querydsl/jpa/JPAQueryMixin.java index f7def8baf..38483ced9 100644 --- a/querydsl-jpa/src/main/java/com/querydsl/jpa/JPAQueryMixin.java +++ b/querydsl-jpa/src/main/java/com/querydsl/jpa/JPAQueryMixin.java @@ -172,21 +172,21 @@ public class JPAQueryMixin extends QueryMixin { } @Override - public Expression convert(Expression expr, boolean forOrder) { + public Expression convert(Expression expr, Role role) { expr = (Expression) expr.accept(mapAccessVisitor, null); expr = (Expression) expr.accept(listAccessVisitor, null); - if (forOrder) { + if (role == Role.ORDER_BY) { if (expr instanceof Path) { expr = convertPathForOrder((Path)expr); } else { expr = (Expression)expr.accept(replaceVisitor, null); } } - return Conversions.convert(super.convert(expr, forOrder)); + return Conversions.convert(super.convert(expr, role)); } @Override - protected Predicate normalize(Predicate predicate, boolean where) { + protected Predicate convert(Predicate predicate, Role role) { if (predicate != null) { predicate = (Predicate) ExpressionUtils.extract(predicate); }