com.querydsl.sql.ProjectableSQLQuery Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
*
* 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.querydsl.sql;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jetbrains.annotations.Nullable;
import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.FetchableQuery;
import com.querydsl.core.JoinFlag;
import com.querydsl.core.NonUniqueResultException;
import com.querydsl.core.Query;
import com.querydsl.core.QueryFlag;
import com.querydsl.core.QueryFlag.Position;
import com.querydsl.core.support.FetchableSubQueryBase;
import com.querydsl.core.support.QueryMixin;
import com.querydsl.core.types.*;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.Wildcard;
/**
* {@code ProjectableSQLQuery} is the base type for SQL query implementations
*
* @param result type
* @param concrete subtype
*/
public abstract class ProjectableSQLQuery & Query> extends FetchableSubQueryBase
implements SQLCommonQuery, FetchableQuery {
private static final Path> defaultQueryAlias = ExpressionUtils.path(Object.class, "query");
protected final Configuration configuration;
@Nullable
protected Expression> union;
protected SubQueryExpression> firstUnionSubQuery;
protected boolean unionAll;
@SuppressWarnings("unchecked")
public ProjectableSQLQuery(QueryMixin queryMixin, Configuration configuration) {
super(queryMixin);
this.queryMixin.setSelf((Q) this);
this.configuration = configuration;
}
@Override
public R accept(Visitor v, @Nullable C context) {
if (union != null) {
return union.accept(v, context);
} else {
return super.accept(v, context);
}
}
/**
* Add the given String literal as a join flag to the last added join with the position
* BEFORE_TARGET
*
* @param flag join flag
* @return the current object
*/
@Override
public Q addJoinFlag(String flag) {
return addJoinFlag(flag, JoinFlag.Position.BEFORE_TARGET);
}
/**
* Add the given String literal as a join flag to the last added join
*
* @param flag join flag
* @param position position
* @return the current object
*/
@Override
@SuppressWarnings("unchecked")
public Q addJoinFlag(String flag, JoinFlag.Position position) {
queryMixin.addJoinFlag(new JoinFlag(flag, position));
return (Q) this;
}
/**
* Add the given prefix and expression as a general query flag
*
* @param position position of the flag
* @param prefix prefix for the flag
* @param expr expression of the flag
* @return the current object
*/
@Override
public Q addFlag(Position position, String prefix, Expression> expr) {
Expression> flag = Expressions.template(expr.getType(), prefix + "{0}", expr);
return queryMixin.addFlag(new QueryFlag(position, flag));
}
/**
* Add the given query flag
*
* @param flag query flag
* @return the current object
*/
public Q addFlag(QueryFlag flag) {
return queryMixin.addFlag(flag);
}
/**
* Add the given String literal as query flag
*
* @param position position
* @param flag query flag
* @return the current object
*/
@Override
public Q addFlag(Position position, String flag) {
return queryMixin.addFlag(new QueryFlag(position, flag));
}
/**
* Add the given Expression as a query flag
*
* @param position position
* @param flag query flag
* @return the current object
*/
@Override
public Q addFlag(Position position, Expression> flag) {
return queryMixin.addFlag(new QueryFlag(position, flag));
}
@Override
public long fetchCount() {
queryMixin.setProjection(Wildcard.countAsInt);
return ((Number) fetchOne()).longValue();
}
public Q from(Expression> arg) {
return queryMixin.from(arg);
}
@Override
public Q from(Expression>... args) {
return queryMixin.from(args);
}
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Q from(SubQueryExpression> subQuery, Path> alias) {
return queryMixin.from((Expression) ExpressionUtils.as((Expression) subQuery, alias));
}
@Override
public Q fullJoin(EntityPath> target) {
return queryMixin.fullJoin(target);
}
@Override
public Q fullJoin(EntityPath target, Path alias) {
return queryMixin.fullJoin(target, alias);
}
@Override
public Q fullJoin(RelationalFunctionCall target, Path alias) {
return queryMixin.fullJoin(target, alias);
}
@Override
public Q fullJoin(SubQueryExpression> target, Path> alias) {
return queryMixin.fullJoin(target, alias);
}
@Override
public Q fullJoin(ForeignKey key, RelationalPath entity) {
return queryMixin.fullJoin(entity).on(key.on(entity));
}
@Override
public Q innerJoin(EntityPath> target) {
return queryMixin.innerJoin(target);
}
@Override
public Q innerJoin(EntityPath target, Path alias) {
return queryMixin.innerJoin(target, alias);
}
@Override
public Q innerJoin(RelationalFunctionCall target, Path alias) {
return queryMixin.innerJoin(target, alias);
}
@Override
public Q innerJoin(SubQueryExpression> target, Path> alias) {
return queryMixin.innerJoin(target, alias);
}
@Override
public Q innerJoin(ForeignKey key, RelationalPath entity) {
return queryMixin.innerJoin(entity).on(key.on(entity));
}
@Override
public Q join(EntityPath> target) {
return queryMixin.join(target);
}
@Override
public Q join(EntityPath target, Path alias) {
return queryMixin.join(target, alias);
}
@Override
public Q join(RelationalFunctionCall target, Path alias) {
return queryMixin.join(target, alias);
}
@Override
public Q join(SubQueryExpression> target, Path> alias) {
return queryMixin.join(target, alias);
}
@Override
public Q join(ForeignKey key, RelationalPath entity) {
return queryMixin.join(entity).on(key.on(entity));
}
@Override
public Q leftJoin(EntityPath> target) {
return queryMixin.leftJoin(target);
}
@Override
public Q leftJoin(EntityPath target, Path alias) {
return queryMixin.leftJoin(target, alias);
}
@Override
public Q leftJoin(RelationalFunctionCall target, Path alias) {
return queryMixin.leftJoin(target, alias);
}
@Override
public Q leftJoin(SubQueryExpression> target, Path> alias) {
return queryMixin.leftJoin(target, alias);
}
@Override
public Q leftJoin(ForeignKey key, RelationalPath entity) {
return queryMixin.leftJoin(entity).on(key.on(entity));
}
@Override
public Q rightJoin(EntityPath> target) {
return queryMixin.rightJoin(target);
}
@Override
public Q rightJoin(EntityPath target, Path alias) {
return queryMixin.rightJoin(target, alias);
}
@Override
public Q rightJoin(RelationalFunctionCall target, Path alias) {
return queryMixin.rightJoin(target, alias);
}
@Override
public Q rightJoin(SubQueryExpression> target, Path> alias) {
return queryMixin.rightJoin(target, alias);
}
@Override
public Q rightJoin(ForeignKey key, RelationalPath entity) {
return queryMixin.rightJoin(entity).on(key.on(entity));
}
@SuppressWarnings("unchecked")
private