Search in sources :

Example 1 with Context

use of com.querydsl.core.support.Context in project querydsl by querydsl.

the class JPAQueryMixin method convert.

@Override
protected Predicate convert(Predicate predicate, Role role) {
    if (predicate != null) {
        predicate = (Predicate) ExpressionUtils.extract(predicate);
    }
    if (predicate != null) {
        predicate = (Predicate) predicate.accept(mapAccessVisitor, null);
        predicate = (Predicate) predicate.accept(listAccessVisitor, null);
    }
    if (predicate != null) {
        // transform any usage
        predicate = (Predicate) predicate.accept(collectionAnyVisitor, new Context());
        return predicate;
    } else {
        return null;
    }
}
Also used : Context(com.querydsl.core.support.Context)

Example 2 with Context

use of com.querydsl.core.support.Context in project querydsl by querydsl.

the class CollQueryMixin method convert.

@Override
protected Predicate convert(Predicate predicate, Role role) {
    predicate = (Predicate) ExpressionUtils.extract(predicate);
    if (predicate != null) {
        Context context = new Context();
        Predicate transformed = (Predicate) predicate.accept(collectionAnyVisitor, context);
        for (int i = 0; i < context.paths.size(); i++) {
            leftJoin((Path) context.paths.get(i).getMetadata().getParent(), (Path) context.replacements.get(i));
            on(ANY);
        }
        return transformed;
    } else {
        return predicate;
    }
}
Also used : Context(com.querydsl.core.support.Context) Predicate(com.querydsl.core.types.Predicate)

Example 3 with Context

use of com.querydsl.core.support.Context 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 4 with Context

use of com.querydsl.core.support.Context in project querydsl by querydsl.

the class JPACollectionAnyVisitorTest method serialize.

private String serialize(Expression<?> expression) {
    Expression<?> transformed = expression.accept(new JPACollectionAnyVisitor(), new Context());
    JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT, null);
    serializer.handle(transformed);
    return serializer.toString();
}
Also used : Context(com.querydsl.core.support.Context)

Example 5 with Context

use of com.querydsl.core.support.Context in project querydsl by querydsl.

the class JDOQueryMixin method convert.

@Override
protected Predicate convert(Predicate predicate, Role role) {
    predicate = (Predicate) ExpressionUtils.extract(predicate);
    if (predicate != null) {
        Context context = new Context();
        Predicate transformed = (Predicate) predicate.accept(collectionAnyVisitor, context);
        for (int i = 0; i < context.paths.size(); i++) {
            Path<?> path = context.paths.get(i);
            addCondition(context, i, path, role);
        }
        return transformed;
    } else {
        return null;
    }
}
Also used : Context(com.querydsl.core.support.Context)

Aggregations

Context (com.querydsl.core.support.Context)5 JoinExpression (com.querydsl.core.JoinExpression)1 JoinType (com.querydsl.core.JoinType)1 Predicate (com.querydsl.core.types.Predicate)1 ArrayList (java.util.ArrayList)1