com.mysema.query.support.QueryMixin Maven / Gradle / Ivy
/*
* Copyright 2011, Mysema Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mysema.query.support;
import javax.annotation.Nullable;
import com.mysema.query.DefaultQueryMetadata;
import com.mysema.query.JoinFlag;
import com.mysema.query.JoinType;
import com.mysema.query.QueryFlag;
import com.mysema.query.QueryMetadata;
import com.mysema.query.QueryModifiers;
import com.mysema.query.Tuple;
import com.mysema.query.types.CollectionExpression;
import com.mysema.query.types.Expression;
import com.mysema.query.types.ExpressionUtils;
import com.mysema.query.types.FactoryExpression;
import com.mysema.query.types.FactoryExpressionUtils;
import com.mysema.query.types.FactoryExpressionUtils.FactoryExpressionAdapter;
import com.mysema.query.types.MapExpression;
import com.mysema.query.types.OrderSpecifier;
import com.mysema.query.types.ParamExpression;
import com.mysema.query.types.Path;
import com.mysema.query.types.Predicate;
import com.mysema.query.types.ProjectionRole;
import com.mysema.query.types.QTuple;
import com.mysema.query.types.SubQueryExpression;
/**
* Mixin style Query implementation
*
* @author tiwe
*
* @param type of wrapped query
*/
public class QueryMixin {
private final QueryMetadata metadata;
private final boolean expandAnyPaths;
private ReplaceVisitor replaceVisitor;
private T self;
public QueryMixin() {
this.metadata = new DefaultQueryMetadata();
this.expandAnyPaths = true;
}
public QueryMixin(QueryMetadata metadata) {
this.metadata = metadata;
this.expandAnyPaths = true;
}
public QueryMixin(QueryMetadata metadata, boolean expandAnyPaths) {
this.metadata = metadata;
this.expandAnyPaths = expandAnyPaths;
}
public QueryMixin(T self) {
this(self, new DefaultQueryMetadata());
}
public QueryMixin(T self, QueryMetadata metadata) {
this.self = self;
this.metadata = metadata;
this.expandAnyPaths = true;
}
public QueryMixin(T self, QueryMetadata metadata, boolean expandAnyPaths) {
this.self = self;
this.metadata = metadata;
this.expandAnyPaths = expandAnyPaths;
}
public T addJoin(JoinType joinType, Expression> target) {
metadata.addJoin(joinType, target);
return self;
}
public T addFlag(QueryFlag queryFlag) {
metadata.addFlag(queryFlag);
return self;
}
public T addJoinFlag(JoinFlag flag) {
metadata.addJoinFlag(flag);
return self;
}
public T removeFlag(QueryFlag queryFlag) {
metadata.removeFlag(queryFlag);
return self;
}
public Expression addProjection(Expression e) {
e = convert(e, false);
metadata.addProjection(e);
return e;
}
public T addProjection(Expression>... o) {
for (Expression> e : o) {
metadata.addProjection(convert(e, false));
}
return self;
}
private > P assertRoot(P p) {
if (!p.getRoot().equals(p)) {
throw new IllegalArgumentException(p + " is not a root path");
}
return p;
}
private Path> normalizePath(Path> expr) {
Context context = new Context();
Path> replaced = (Path>)expr.accept(CollectionAnyVisitor.DEFAULT, context);
if (!replaced.equals(expr)) {
for (int i = 0; i < context.paths.size(); i++) {
Path path = context.paths.get(i).getMetadata().getParent();
Path replacement = context.replacements.get(i);
this.innerJoin(path, replacement);
}
return replaced;
} else {
return expr;
}
}
@SuppressWarnings("rawtypes")
public