Search in sources :

Example 6 with Operator

use of com.querydsl.core.types.Operator in project querydsl by querydsl.

the class SerializerBase method visitOperation.

protected void visitOperation(Class<?> type, Operator operator, final List<? extends Expression<?>> args) {
    final Template template = templates.getTemplate(operator);
    if (template != null) {
        final int precedence = templates.getPrecedence(operator);
        boolean first = true;
        for (final Template.Element element : template.getElements()) {
            final Object rv = element.convert(args);
            if (rv instanceof Expression) {
                final Expression<?> expr = (Expression<?>) rv;
                if (precedence > -1 && expr instanceof Operation) {
                    Operator op = ((Operation<?>) expr).getOperator();
                    int opPrecedence = templates.getPrecedence(op);
                    if (precedence < opPrecedence) {
                        append("(").handle(expr).append(")");
                    } else if (!first && precedence == opPrecedence && !SAME_PRECEDENCE.contains(op)) {
                        append("(").handle(expr).append(")");
                    } else {
                        handle(expr);
                    }
                } else {
                    handle(expr);
                }
                first = false;
            } else if (element.isString()) {
                append(rv.toString());
            } else {
                visitConstant(rv);
            }
        }
    } else if (strict) {
        throw new IllegalArgumentException(String.format("No pattern found for %s. Make sure to register any custom functions with %s.", operator, templates.getClass()));
    } else {
        append(operator.toString());
        append("(");
        handle(", ", args);
        append(")");
    }
}
Also used : Operator(com.querydsl.core.types.Operator) ParamExpression(com.querydsl.core.types.ParamExpression) TemplateExpression(com.querydsl.core.types.TemplateExpression) Expression(com.querydsl.core.types.Expression) FactoryExpression(com.querydsl.core.types.FactoryExpression) Operation(com.querydsl.core.types.Operation) Template(com.querydsl.core.types.Template)

Example 7 with Operator

use of com.querydsl.core.types.Operator in project querydsl by querydsl.

the class CoverageTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws IllegalArgumentException, IllegalAccessException {
    // make sure all Operators are covered in expression factory methods
    Set<Operator> usedOperators = new HashSet<Operator>();
    List<Expression<?>> exprs = new ArrayList<Expression<?>>();
    Entity entity = Alias.alias(Entity.class, "entity");
    // numeric
    exprs.addAll(projections.numeric($(entity.getNum()), $(entity.getNum()), 1, false));
    exprs.addAll(matchers.numeric($(entity.getNum()), $(entity.getNum()), 1));
    exprs.addAll(filters.numeric($(entity.getNum()), $(entity.getNum()), 1));
    exprs.addAll(projections.numericCasts($(entity.getNum()), $(entity.getNum()), 1));
    // string
    exprs.addAll(projections.string($(entity.getStr()), $(entity.getStr()), "abc"));
    exprs.addAll(matchers.string($(entity.getStr()), $(entity.getStr()), "abc"));
    exprs.addAll(filters.string($(entity.getStr()), $(entity.getStr()), "abc"));
    // date
    exprs.addAll(projections.date($(entity.getDate()), $(entity.getDate()), new java.sql.Date(0)));
    exprs.addAll(matchers.date($(entity.getDate()), $(entity.getDate()), new java.sql.Date(0)));
    exprs.addAll(filters.date($(entity.getDate()), $(entity.getDate()), new java.sql.Date(0)));
    // dateTime
    exprs.addAll(projections.dateTime($(entity.getDateTime()), $(entity.getDateTime()), new java.util.Date(0)));
    exprs.addAll(matchers.dateTime($(entity.getDateTime()), $(entity.getDateTime()), new java.util.Date(0)));
    exprs.addAll(filters.dateTime($(entity.getDateTime()), $(entity.getDateTime()), new java.util.Date(0)));
    // time
    exprs.addAll(projections.time($(entity.getTime()), $(entity.getTime()), new java.sql.Time(0)));
    exprs.addAll(matchers.time($(entity.getTime()), $(entity.getTime()), new java.sql.Time(0)));
    exprs.addAll(filters.time($(entity.getTime()), $(entity.getTime()), new java.sql.Time(0)));
    // boolean
    exprs.addAll(filters.booleanFilters($(entity.isBool()), $(entity.isBool())));
    // collection
    exprs.addAll(projections.list($(entity.getList()), $(entity.getList()), ""));
    exprs.addAll(filters.list($(entity.getList()), $(entity.getList()), ""));
    // array
    exprs.addAll(projections.array($(entity.getArray()), $(entity.getArray()), ""));
    exprs.addAll(filters.array($(entity.getArray()), $(entity.getArray()), ""));
    // map
    exprs.addAll(projections.map($(entity.getMap()), $(entity.getMap()), "", ""));
    exprs.addAll(filters.map($(entity.getMap()), $(entity.getMap()), "", ""));
    for (Expression<?> e : exprs) {
        if (e instanceof Operation) {
            Operation<?> op = (Operation<?>) e;
            if (op.getArg(0) instanceof Operation) {
                usedOperators.add(((Operation<?>) op.getArg(0)).getOperator());
            } else if (op.getArgs().size() > 1 && op.getArg(1) instanceof Operation) {
                usedOperators.add(((Operation<?>) op.getArg(1)).getOperator());
            }
            usedOperators.add(op.getOperator());
        }
    }
    // missing mappings
    usedOperators.addAll(Arrays.<Operator>asList(Ops.INSTANCE_OF, Ops.ALIAS, Ops.ARRAY_SIZE, Ops.MOD, Ops.STRING_CAST, // Ops.DELEGATE,
    Ops.WRAPPED, Ops.ORDER, Ops.XOR, Ops.XNOR, Ops.CASE_WHEN, Ops.CASE_ELSE, Ops.CASE_EQ_WHEN, Ops.CASE_EQ_ELSE, Ops.LIST, Ops.SET, Ops.SINGLETON, Ops.COALESCE, // TODO: add support
    Ops.ORDINAL, Ops.MATCHES_IC, // aggregation
    Ops.AggOps.AVG_AGG, Ops.AggOps.MAX_AGG, Ops.AggOps.MIN_AGG, Ops.AggOps.SUM_AGG, Ops.AggOps.COUNT_AGG, Ops.AggOps.COUNT_ALL_AGG, Ops.EXISTS));
    List<Operator> notContained = new ArrayList<Operator>();
    for (Field field : Ops.class.getFields()) {
        if (Operator.class.isAssignableFrom(field.getType())) {
            Operator val = (Operator) field.get(null);
            if (!usedOperators.contains(val)) {
                System.err.println(field.getName() + " was not contained");
                notContained.add(val);
            }
        }
    }
    assertTrue(notContained.size() + " errors in processing, see log for details", notContained.isEmpty());
}
Also used : Operator(com.querydsl.core.types.Operator) Operation(com.querydsl.core.types.Operation) Field(java.lang.reflect.Field) Expression(com.querydsl.core.types.Expression) Test(org.junit.Test)

Example 8 with Operator

use of com.querydsl.core.types.Operator in project querydsl by querydsl.

the class LuceneSerializer method toQuery.

private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
    Operator op = operation.getOperator();
    if (op == Ops.OR) {
        return toTwoHandSidedQuery(operation, Occur.SHOULD, metadata);
    } else if (op == Ops.AND) {
        return toTwoHandSidedQuery(operation, Occur.MUST, metadata);
    } else if (op == Ops.NOT) {
        BooleanQuery bq = new BooleanQuery();
        bq.add(new BooleanClause(toQuery(operation.getArg(0), metadata), Occur.MUST_NOT));
        bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
        return bq;
    } else if (op == Ops.LIKE) {
        return like(operation, metadata);
    } else if (op == Ops.LIKE_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.EQ) {
        return eq(operation, metadata, false);
    } else if (op == Ops.EQ_IGNORE_CASE) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.NE) {
        return ne(operation, metadata, false);
    } else if (op == Ops.STARTS_WITH) {
        return startsWith(metadata, operation, false);
    } else if (op == Ops.STARTS_WITH_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.ENDS_WITH) {
        return endsWith(operation, metadata, false);
    } else if (op == Ops.ENDS_WITH_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.STRING_CONTAINS) {
        return stringContains(operation, metadata, false);
    } else if (op == Ops.STRING_CONTAINS_IC) {
        throw new IgnoreCaseUnsupportedException();
    } else if (op == Ops.BETWEEN) {
        return between(operation, metadata);
    } else if (op == Ops.IN) {
        return in(operation, metadata, false);
    } else if (op == Ops.NOT_IN) {
        return notIn(operation, metadata, false);
    } else if (op == Ops.LT) {
        return lt(operation, metadata);
    } else if (op == Ops.GT) {
        return gt(operation, metadata);
    } else if (op == Ops.LOE) {
        return le(operation, metadata);
    } else if (op == Ops.GOE) {
        return ge(operation, metadata);
    } else if (op == LuceneOps.LUCENE_QUERY) {
        @SuppressWarnings("unchecked") Constant<Query> // this is the expected type
        expectedConstant = (Constant<Query>) operation.getArg(0);
        return expectedConstant.getConstant();
    }
    throw new UnsupportedOperationException("Illegal operation " + operation);
}
Also used : Operator(com.querydsl.core.types.Operator) BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Constant(com.querydsl.core.types.Constant) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

Operator (com.querydsl.core.types.Operator)8 Expression (com.querydsl.core.types.Expression)4 Operation (com.querydsl.core.types.Operation)3 ImmutableCollection (com.google.common.collect.ImmutableCollection)2 BooleanBuilder (com.querydsl.core.BooleanBuilder)2 Path (com.querydsl.core.types.Path)2 ComparablePath (com.querydsl.core.types.dsl.ComparablePath)2 EnumExpression (com.querydsl.core.types.dsl.EnumExpression)2 EnumPath (com.querydsl.core.types.dsl.EnumPath)2 NumberPath (com.querydsl.core.types.dsl.NumberPath)2 StringPath (com.querydsl.core.types.dsl.StringPath)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 UUID (java.util.UUID)2 AndFilter (com.evolveum.midpoint.prism.query.AndFilter)1 ObjectFilter (com.evolveum.midpoint.prism.query.ObjectFilter)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Evaluator (com.querydsl.codegen.utils.Evaluator)1