Search in sources :

Example 11 with JoinExpression

use of com.querydsl.core.JoinExpression in project querydsl by querydsl.

the class JPAQueryMixinTest method orderBy_reuse.

@Test
public void orderBy_reuse() {
    QCat cat = QCat.cat;
    QCat mate = new QCat("mate");
    mixin.from(cat);
    mixin.leftJoin(cat.mate, mate);
    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(mate))), md.getJoins());
    assertEquals(Arrays.asList(mate.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)

Example 12 with JoinExpression

use of com.querydsl.core.JoinExpression in project querydsl by querydsl.

the class JPAQueryMixinTest method orderBy_groupBy.

@Test
public void orderBy_groupBy() {
    QCat cat = QCat.cat;
    mixin.from(cat);
    mixin.groupBy(cat.mate.name);
    mixin.orderBy(cat.mate.name.asc());
    QueryMetadata md = mixin.getMetadata();
    assertEquals(Arrays.asList(new JoinExpression(JoinType.DEFAULT, cat)), md.getJoins());
    assertEquals(Arrays.asList(cat.mate.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)

Example 13 with JoinExpression

use of com.querydsl.core.JoinExpression in project querydsl by querydsl.

the class SQLSerializer method serializeSources.

private void serializeSources(List<JoinExpression> joins) {
    if (joins.isEmpty()) {
        String dummyTable = templates.getDummyTable();
        if (!Strings.isNullOrEmpty(dummyTable)) {
            append(templates.getFrom());
            append(dummyTable);
        }
    } else {
        append(templates.getFrom());
        for (int i = 0; i < joins.size(); i++) {
            final JoinExpression je = joins.get(i);
            if (je.getFlags().isEmpty()) {
                if (i > 0) {
                    append(templates.getJoinSymbol(je.getType()));
                }
                handleJoinTarget(je);
                if (je.getCondition() != null) {
                    append(templates.getOn()).handle(je.getCondition());
                }
            } else {
                serialize(JoinFlag.Position.START, je.getFlags());
                if (!serialize(JoinFlag.Position.OVERRIDE, je.getFlags()) && i > 0) {
                    append(templates.getJoinSymbol(je.getType()));
                }
                serialize(JoinFlag.Position.BEFORE_TARGET, je.getFlags());
                handleJoinTarget(je);
                serialize(JoinFlag.Position.BEFORE_CONDITION, je.getFlags());
                if (je.getCondition() != null) {
                    append(templates.getOn()).handle(je.getCondition());
                }
                serialize(JoinFlag.Position.END, je.getFlags());
            }
        }
    }
}
Also used : JoinExpression(com.querydsl.core.JoinExpression)

Example 14 with JoinExpression

use of com.querydsl.core.JoinExpression in project querydsl by querydsl.

the class DefaultEvaluatorFactory method createEvaluator.

/**
     * Create an Evaluator for the given sources and the given optional filter
     *
     * @param metadata query metadata
     * @param joins joins
     * @param filter where condition
     * @return evaluator
     */
public Evaluator<List<Object[]>> createEvaluator(QueryMetadata metadata, List<JoinExpression> joins, @Nullable Predicate filter) {
    List<String> sourceNames = new ArrayList<String>();
    List<Type> sourceTypes = new ArrayList<Type>();
    List<Class<?>> sourceClasses = new ArrayList<Class<?>>();
    StringBuilder vars = new StringBuilder();
    CollQuerySerializer ser = new CollQuerySerializer(templates);
    ser.append("java.util.List<Object[]> rv = new java.util.ArrayList<Object[]>();\n");
    List<String> anyJoinMatchers = new ArrayList<String>();
    // creating context
    for (JoinExpression join : joins) {
        Expression<?> target = join.getTarget();
        String typeName = com.mysema.codegen.support.ClassUtils.getName(target.getType());
        if (vars.length() > 0) {
            vars.append(",");
        }
        switch(join.getType()) {
            case DEFAULT:
                ser.append("for (" + typeName + " " + target + " : " + target + "_) {\n");
                vars.append(target);
                sourceNames.add(target + "_");
                sourceTypes.add(new SimpleType(Types.ITERABLE, new ClassType(TypeCategory.SIMPLE, target.getType())));
                sourceClasses.add(Iterable.class);
                break;
            case INNERJOIN:
            case LEFTJOIN:
                Operation<?> alias = (Operation<?>) join.getTarget();
                boolean colAnyJoin = join.getCondition() != null && join.getCondition().toString().equals("any");
                boolean leftJoin = join.getType() == JoinType.LEFTJOIN;
                String matcher = null;
                if (colAnyJoin) {
                    matcher = alias.getArg(1).toString() + "_matched";
                    ser.append("boolean " + matcher + " = false;\n");
                    anyJoinMatchers.add(matcher);
                }
                ser.append("for (" + typeName + " " + alias.getArg(1) + " : ");
                if (leftJoin) {
                    ser.append(CollQueryFunctions.class.getName() + ".leftJoin(");
                }
                if (colAnyJoin) {
                    Context context = new Context();
                    Expression<?> replacement = alias.getArg(0).accept(collectionAnyVisitor, context);
                    ser.handle(replacement);
                } else {
                    ser.handle(alias.getArg(0));
                }
                if (alias.getArg(0).getType().equals(Map.class)) {
                    ser.append(".values()");
                }
                if (leftJoin) {
                    ser.append(")");
                }
                ser.append(") {\n");
                if (matcher != null) {
                    ser.append("if (!" + matcher + ") {\n");
                }
                vars.append(alias.getArg(1));
                break;
            default:
                throw new IllegalArgumentException("Illegal join expression " + join);
        }
    }
    // filter
    if (filter != null) {
        ser.append("try {\n");
        ser.append("if (");
        ser.handle(filter).append(") {\n");
        for (String matcher : anyJoinMatchers) {
            ser.append("    " + matcher + " = true;\n");
        }
        ser.append("    rv.add(new Object[]{" + vars + "});\n");
        ser.append("}\n");
        ser.append("} catch (NullPointerException npe) { }\n");
    } else {
        ser.append("rv.add(new Object[]{" + vars + "});\n");
    }
    // closing context
    int amount = joins.size() + anyJoinMatchers.size();
    for (int i = 0; i < amount; i++) {
        ser.append("}\n");
    }
    ser.append("return rv;");
    Map<Object, String> constantToLabel = ser.getConstantToLabel();
    Map<String, Object> constants = getConstants(metadata, constantToLabel);
    ClassType projectionType = new ClassType(TypeCategory.LIST, List.class, Types.OBJECTS);
    return factory.createEvaluator(ser.toString(), projectionType, sourceNames.toArray(new String[sourceNames.size()]), sourceTypes.toArray(new Type[sourceTypes.size()]), sourceClasses.toArray(new Class<?>[sourceClasses.size()]), constants);
}
Also used : Context(com.querydsl.core.support.Context) ArrayList(java.util.ArrayList) JoinType(com.querydsl.core.JoinType) JoinExpression(com.querydsl.core.JoinExpression)

Example 15 with JoinExpression

use of com.querydsl.core.JoinExpression in project querydsl by querydsl.

the class OrderedQueryMetadata method getJoins.

@Override
public List<JoinExpression> getJoins() {
    if (joins == null) {
        joins = Lists.newArrayList();
        int separator = 0;
        for (JoinExpression j : super.getJoins()) {
            if (j.getType() == JoinType.DEFAULT) {
                joins.add(separator++, j);
            } else {
                joins.add(j);
            }
        }
    }
    return joins;
}
Also used : JoinExpression(com.querydsl.core.JoinExpression)

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