mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-13 21:01:01 +08:00
Merge pull request #1176 from querydsl/i1067
Combine QueryMixin.convert and normalize
This commit is contained in:
commit
7a05abd619
@ -44,7 +44,7 @@ public class CollQueryMixin<T> extends QueryMixin<T> {
|
||||
}
|
||||
|
||||
@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();
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -34,6 +34,8 @@ import com.querydsl.core.types.FactoryExpressionUtils.FactoryExpressionAdapter;
|
||||
*/
|
||||
public class QueryMixin<T> {
|
||||
|
||||
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<T> {
|
||||
}
|
||||
|
||||
public <E> Expression<E> setProjection(Expression<E> e) {
|
||||
e = convert(e, false);
|
||||
e = convert(e, Role.SELECT);
|
||||
metadata.setProjection(e);
|
||||
return e;
|
||||
}
|
||||
@ -106,7 +108,7 @@ public class QueryMixin<T> {
|
||||
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<T> {
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public <RT> Expression<RT> convert(Expression<RT> expr, boolean forOrder) {
|
||||
public <RT> Expression<RT> convert(Expression<RT> expr, Role role) {
|
||||
if (expandAnyPaths) {
|
||||
if (expr instanceof Path) {
|
||||
expr = (Expression)normalizePath((Path)expr);
|
||||
@ -144,7 +146,7 @@ public class QueryMixin<T> {
|
||||
}
|
||||
}
|
||||
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<RT>)expr);
|
||||
} else {
|
||||
@ -152,6 +154,11 @@ public class QueryMixin<T> {
|
||||
}
|
||||
}
|
||||
|
||||
protected Predicate convert(Predicate condition, Role role) {
|
||||
return condition;
|
||||
}
|
||||
|
||||
|
||||
public Expression<Tuple> createProjection(Expression<?>[] args) {
|
||||
return Projections.tuple(args);
|
||||
}
|
||||
@ -223,13 +230,13 @@ public class QueryMixin<T> {
|
||||
}
|
||||
|
||||
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<T> {
|
||||
}
|
||||
|
||||
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<T> {
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@ -396,7 +396,7 @@ public abstract class StringExpression extends ComparableExpression<String> {
|
||||
/**
|
||||
* Return true if this String matches the given regular expression
|
||||
*
|
||||
* <p>Some implementations such as Querydsl JPA will try to convert a regex expression into like
|
||||
* <p>Some implementations such as Querydsl JPA will try to convert a regex expression into like
|
||||
* form and will throw an Exception when this fails</p>
|
||||
*
|
||||
* @param regex
|
||||
@ -410,7 +410,7 @@ public abstract class StringExpression extends ComparableExpression<String> {
|
||||
/**
|
||||
* Return true if this String matches the given regular expression
|
||||
*
|
||||
* <p>Some implementations such as Querydsl JPA will try to convert a regex expression into like
|
||||
* <p>Some implementations such as Querydsl JPA will try to convert a regex expression into like
|
||||
* form and will throw an Exception when this fails</p>
|
||||
*
|
||||
* @param regex
|
||||
|
||||
@ -44,14 +44,14 @@ public class JDOQueryMixin<T> extends QueryMixin<T> {
|
||||
}
|
||||
|
||||
@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<T> extends QueryMixin<T> {
|
||||
}
|
||||
|
||||
@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);
|
||||
|
||||
@ -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<Q extends AbstractSQLQuery<Q>> extends Pr
|
||||
}
|
||||
|
||||
@Override
|
||||
public <RT> Expression<RT> convert(Expression<RT> expr, boolean forOrder) {
|
||||
return Conversions.convertForNativeQuery(super.convert(expr, forOrder));
|
||||
public <RT> Expression<RT> convert(Expression<RT> expr, Role role) {
|
||||
return Conversions.convertForNativeQuery(super.convert(expr, role));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -172,21 +172,21 @@ public class JPAQueryMixin<T> extends QueryMixin<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <RT> Expression<RT> convert(Expression<RT> expr, boolean forOrder) {
|
||||
public <RT> Expression<RT> convert(Expression<RT> expr, Role role) {
|
||||
expr = (Expression<RT>) expr.accept(mapAccessVisitor, null);
|
||||
expr = (Expression<RT>) expr.accept(listAccessVisitor, null);
|
||||
if (forOrder) {
|
||||
if (role == Role.ORDER_BY) {
|
||||
if (expr instanceof Path) {
|
||||
expr = convertPathForOrder((Path)expr);
|
||||
} else {
|
||||
expr = (Expression<RT>)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);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user