Search in sources :

Example 16 with JoinExpression

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());
}
Also used : JoinExpression(com.querydsl.core.JoinExpression) Test(org.junit.Test)

Example 17 with JoinExpression

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;
    }
}
Also used : JoinExpression(com.querydsl.core.JoinExpression) JoinExpression(com.querydsl.core.JoinExpression)

Example 18 with JoinExpression

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;
}
Also used : QueryFlag(com.querydsl.core.QueryFlag) JoinExpression(com.querydsl.core.JoinExpression) JoinExpression(com.querydsl.core.JoinExpression)

Example 19 with JoinExpression

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());
}
Also used : QCompany(com.querydsl.jpa.domain.QCompany) QueryMetadata(com.querydsl.core.QueryMetadata) JoinExpression(com.querydsl.core.JoinExpression) QEmployee(com.querydsl.jpa.domain.QEmployee) QDepartment(com.querydsl.jpa.domain.QDepartment) Test(org.junit.Test)

Example 20 with JoinExpression

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());
}
Also used : QCat(com.querydsl.jpa.domain.QCat) QueryMetadata(com.querydsl.core.QueryMetadata) JoinExpression(com.querydsl.core.JoinExpression) Test(org.junit.Test)

Aggregations

JoinExpression (com.querydsl.core.JoinExpression)24 Test (org.junit.Test)15 QueryMetadata (com.querydsl.core.QueryMetadata)14 QCat (com.querydsl.jpa.domain.QCat)8 QBookVersion (com.querydsl.jpa.domain4.QBookVersion)2 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 JoinType (com.querydsl.core.JoinType)1 QueryFlag (com.querydsl.core.QueryFlag)1 Context (com.querydsl.core.support.Context)1 QCompany (com.querydsl.jpa.domain.QCompany)1 QDepartment (com.querydsl.jpa.domain.QDepartment)1 QEmployee (com.querydsl.jpa.domain.QEmployee)1 QBookMark (com.querydsl.jpa.domain4.QBookMark)1 Map (java.util.Map)1