use of com.querydsl.core.JoinExpression in project querydsl by querydsl.
the class QueryMixinTest method getJoins_with_condition.
@Test
public void getJoins_with_condition() {
mixin.innerJoin(entity);
mixin.on(entity.version.isNull(), entity.version.isNotNull());
assertEquals(1, mixin.getMetadata().getJoins().size());
JoinExpression je = mixin.getMetadata().getJoins().get(0);
assertEquals(entity, je.getTarget());
assertEquals(Expressions.allOf(entity.version.isNull(), entity.version.isNotNull()), je.getCondition());
}
use of com.querydsl.core.JoinExpression in project querydsl by querydsl.
the class SQLSerializer method getIdentifierColumns.
/**
* Return a list of expressions that can be used to uniquely define the query sources
*
* @param joins
* @return identifier columns
*/
@SuppressWarnings("unchecked")
private List<Expression<?>> getIdentifierColumns(List<JoinExpression> joins, boolean alias) {
if (joins.size() == 1) {
JoinExpression join = joins.get(0);
if (join.getTarget() instanceof RelationalPath) {
return ((RelationalPath) join.getTarget()).getColumns();
} else {
return Collections.emptyList();
}
} else {
List<Expression<?>> rv = Lists.newArrayList();
int counter = 0;
for (JoinExpression join : joins) {
if (join.getTarget() instanceof RelationalPath) {
RelationalPath path = (RelationalPath) join.getTarget();
List<Expression<?>> columns;
if (path.getPrimaryKey() != null) {
columns = path.getPrimaryKey().getLocalColumns();
} else {
columns = path.getColumns();
}
if (alias) {
for (Expression<?> column : columns) {
rv.add(ExpressionUtils.as(column, "col" + (++counter)));
}
} else {
rv.addAll(columns);
}
} else {
// not able to provide a distinct list of columns
return Collections.emptyList();
}
}
return rv;
}
}
use of com.querydsl.core.JoinExpression in project querydsl by querydsl.
the class SQLSerializer method serializeForQuery.
void serializeForQuery(QueryMetadata metadata, boolean forCountRow) {
boolean oldInSubquery = inSubquery;
inSubquery = inSubquery || getLength() > 0;
boolean oldSkipParent = skipParent;
skipParent = false;
final Expression<?> select = metadata.getProjection();
final List<JoinExpression> joins = metadata.getJoins();
final Predicate where = metadata.getWhere();
final List<? extends Expression<?>> groupBy = metadata.getGroupBy();
final Predicate having = metadata.getHaving();
final List<OrderSpecifier<?>> orderBy = metadata.getOrderBy();
final Set<QueryFlag> flags = metadata.getFlags();
final boolean hasFlags = !flags.isEmpty();
String suffix = null;
List<? extends Expression<?>> sqlSelect;
if (select instanceof FactoryExpression) {
sqlSelect = ((FactoryExpression<?>) select).getArgs();
} else if (select != null) {
sqlSelect = ImmutableList.of(select);
} else {
sqlSelect = ImmutableList.of();
}
// with
if (hasFlags) {
List<Expression<?>> withFlags = Lists.newArrayList();
boolean recursive = false;
for (QueryFlag flag : flags) {
if (flag.getPosition() == Position.WITH) {
if (flag.getFlag() == SQLTemplates.RECURSIVE) {
recursive = true;
continue;
}
withFlags.add(flag.getFlag());
}
}
if (!withFlags.isEmpty()) {
if (recursive) {
append(templates.getWithRecursive());
} else {
append(templates.getWith());
}
handle(", ", withFlags);
append("\n");
}
}
// start
if (hasFlags) {
serialize(Position.START, flags);
}
// select
Stage oldStage = stage;
stage = Stage.SELECT;
if (forCountRow) {
append(templates.getSelect());
if (hasFlags) {
serialize(Position.AFTER_SELECT, flags);
}
if (!metadata.isDistinct()) {
append(templates.getCountStar());
if (!groupBy.isEmpty()) {
append(templates.getFrom());
append("(");
append(templates.getSelect());
append("1 as one ");
suffix = ") internal";
}
} else {
List<? extends Expression<?>> columns;
if (sqlSelect.isEmpty()) {
columns = getIdentifierColumns(joins, !templates.isCountDistinctMultipleColumns());
} else {
columns = sqlSelect;
}
if (!groupBy.isEmpty()) {
// select count(*) from (select distinct ...)
append(templates.getCountStar());
append(templates.getFrom());
append("(");
append(templates.getSelectDistinct());
handleSelect(COMMA, columns);
suffix = ") internal";
} else if (columns.size() == 1) {
append(templates.getDistinctCountStart());
handle(columns.get(0));
append(templates.getDistinctCountEnd());
} else if (templates.isCountDistinctMultipleColumns()) {
append(templates.getDistinctCountStart());
append("(").handleSelect(COMMA, columns).append(")");
append(templates.getDistinctCountEnd());
} else {
// select count(*) from (select distinct ...)
append(templates.getCountStar());
append(templates.getFrom());
append("(");
append(templates.getSelectDistinct());
handleSelect(COMMA, columns);
suffix = ") internal";
}
}
} else if (!sqlSelect.isEmpty()) {
if (!metadata.isDistinct()) {
append(templates.getSelect());
} else {
append(templates.getSelectDistinct());
}
if (hasFlags) {
serialize(Position.AFTER_SELECT, flags);
}
handleSelect(COMMA, sqlSelect);
}
if (hasFlags) {
serialize(Position.AFTER_PROJECTION, flags);
}
// from
stage = Stage.FROM;
serializeSources(joins);
// where
if (hasFlags) {
serialize(Position.BEFORE_FILTERS, flags);
}
if (where != null) {
stage = Stage.WHERE;
append(templates.getWhere()).handle(where);
}
if (hasFlags) {
serialize(Position.AFTER_FILTERS, flags);
}
// group by
if (hasFlags) {
serialize(Position.BEFORE_GROUP_BY, flags);
}
if (!groupBy.isEmpty()) {
stage = Stage.GROUP_BY;
append(templates.getGroupBy()).handle(COMMA, groupBy);
}
if (hasFlags) {
serialize(Position.AFTER_GROUP_BY, flags);
}
// having
if (hasFlags) {
serialize(Position.BEFORE_HAVING, flags);
}
if (having != null) {
stage = Stage.HAVING;
append(templates.getHaving()).handle(having);
}
if (hasFlags) {
serialize(Position.AFTER_HAVING, flags);
}
// order by
if (hasFlags) {
serialize(Position.BEFORE_ORDER, flags);
}
if (!orderBy.isEmpty() && !forCountRow) {
stage = Stage.ORDER_BY;
append(templates.getOrderBy());
handleOrderBy(orderBy);
}
if (hasFlags) {
serialize(Position.AFTER_ORDER, flags);
}
// modifiers
if (!forCountRow && metadata.getModifiers().isRestricting() && !joins.isEmpty()) {
stage = Stage.MODIFIERS;
templates.serializeModifiers(metadata, this);
}
if (suffix != null) {
append(suffix);
}
// reset stage
stage = oldStage;
skipParent = oldSkipParent;
inSubquery = oldInSubquery;
}
use of com.querydsl.core.JoinExpression in project querydsl by querydsl.
the class JPAQueryMixinTest method orderBy_nonRoot_twice.
@Test
public void orderBy_nonRoot_twice() {
QDepartment department = QDepartment.department;
QCompany departmentCompany = new QCompany("department_company");
QEmployee departmentCompanyCeo = new QEmployee("department_company_ceo");
mixin.from(department);
mixin.orderBy(department.company.ceo.firstName.asc(), department.company.ceo.lastName.asc());
QueryMetadata md = mixin.getMetadata();
assertEquals(Arrays.asList(new JoinExpression(JoinType.DEFAULT, department), new JoinExpression(JoinType.LEFTJOIN, department.company.as(departmentCompany)), new JoinExpression(JoinType.LEFTJOIN, departmentCompany.ceo.as(departmentCompanyCeo))), md.getJoins());
assertEquals(Arrays.asList(departmentCompanyCeo.firstName.asc(), departmentCompanyCeo.lastName.asc()), md.getOrderBy());
}
use of com.querydsl.core.JoinExpression in project querydsl by querydsl.
the class JPAQueryMixinTest method orderBy.
@Test
public void orderBy() {
QCat cat = QCat.cat;
QCat catMate = new QCat("cat_mate");
mixin.from(cat);
mixin.orderBy(cat.mate.name.asc());
QueryMetadata md = mixin.getMetadata();
assertEquals(Arrays.asList(new JoinExpression(JoinType.DEFAULT, cat), new JoinExpression(JoinType.LEFTJOIN, cat.mate.as(catMate))), md.getJoins());
assertEquals(Arrays.asList(catMate.name.asc()), md.getOrderBy());
}
Aggregations