Search in sources :

Example 1 with Operator

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

the class JavaTemplatesTest method mappings.

@Test
public void mappings() {
    Templates templates = new JavaTemplates();
    int matched = 0;
    for (Operator operator : Ops.values()) {
        ++matched;
        assertNotNull(templates.getTemplate(operator));
    }
    assertTrue(matched > 0);
}
Also used : Operator(com.querydsl.core.types.Operator) JavaTemplates(com.querydsl.core.types.JavaTemplates) Templates(com.querydsl.core.types.Templates) JavaTemplates(com.querydsl.core.types.JavaTemplates) Test(org.junit.Test)

Example 2 with Operator

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

the class NaryLogicalFilterProcessor method process.

@Override
public Predicate process(NaryLogicalFilter filter) throws RepositoryException {
    Predicate predicate = null;
    Operator operator = (filter instanceof AndFilter) ? Ops.AND : Ops.OR;
    for (ObjectFilter subfilter : filter.getConditions()) {
        Predicate right = context.process(subfilter);
        if (right != null) {
            predicate = predicate != null ? ExpressionUtils.predicate(operator, predicate, right) : right;
        }
    }
    return predicate;
}
Also used : Operator(com.querydsl.core.types.Operator) AndFilter(com.evolveum.midpoint.prism.query.AndFilter) ObjectFilter(com.evolveum.midpoint.prism.query.ObjectFilter) Predicate(com.querydsl.core.types.Predicate)

Example 3 with Operator

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

the class GenericQueryDslFilter method buildFilter.

/**
 * build the Filter for the base QueryDSL class and the passed in filter list
 *
 * @param filters name:test, age>10
 */
public static <T> BooleanBuilder buildFilter(EntityPathBase basePath, List<SearchCriteria> filters) {
    BooleanBuilder booleanBuilder = new BooleanBuilder();
    if (filters != null && !filters.isEmpty()) {
        filters.stream().forEach(filter -> {
            Path p = buildPathFromFilterColumn(basePath, filter.getKey());
            if (p != null) {
                Object value = filter.getValue();
                Operator op = operators.get(filter.getOperation());
                if (validateOperatorPath(op, p, filter.getKey(), value)) {
                    if (Ops.LIKE_IC.equals(op) && value instanceof String && value != null && !((String) value).endsWith("%")) {
                        value = ((String) value) + "%";
                    }
                    if (value == null || (value instanceof String && ((String) value).equalsIgnoreCase(NULL_FILTER))) {
                        op = Ops.IS_NULL;
                        value = null;
                    } else if (value instanceof String && ((String) value).equalsIgnoreCase(NOT_NULL_FILTER)) {
                        op = Ops.IS_NOT_NULL;
                        value = null;
                    }
                    if (value != null) {
                        Object convertedValue = getValueForQuery(p, filter.getKey(), op, value);
                        if (convertedValue != null && convertedValue instanceof Collection) {
                            op = Ops.IN;
                        }
                        if (convertedValue != null) {
                            Expression e = null;
                            if (convertedValue instanceof Comparable) {
                                e = Expressions.asComparable((Comparable) convertedValue);
                            } else {
                                e = Expressions.constant(convertedValue);
                            }
                            // reset the operator if looking for UUID
                            if (convertedValue instanceof UUID || convertedValue instanceof Enum) {
                                op = Ops.EQ;
                            }
                            if (filter.isOrFilter()) {
                                booleanBuilder.or(Expressions.predicate(op, p, e));
                            } else {
                                booleanBuilder.and(Expressions.predicate(op, p, e));
                            }
                        }
                    } else {
                        if (filter.isOrFilter()) {
                            booleanBuilder.or(Expressions.predicate(op, p));
                        } else {
                            booleanBuilder.and(Expressions.predicate(op, p));
                        }
                    }
                }
            }
        });
    }
    return booleanBuilder;
}
Also used : Path(com.querydsl.core.types.Path) StringPath(com.querydsl.core.types.dsl.StringPath) ComparablePath(com.querydsl.core.types.dsl.ComparablePath) EnumPath(com.querydsl.core.types.dsl.EnumPath) NumberPath(com.querydsl.core.types.dsl.NumberPath) Operator(com.querydsl.core.types.Operator) EnumExpression(com.querydsl.core.types.dsl.EnumExpression) Expression(com.querydsl.core.types.Expression) BooleanBuilder(com.querydsl.core.BooleanBuilder) ImmutableCollection(com.google.common.collect.ImmutableCollection) Collection(java.util.Collection) UUID(java.util.UUID)

Example 4 with Operator

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

the class GenericQueryDslFilter method parseFilterString.

/**
 * convert a passed in filter string to a list of criteria objects
 *
 * @param filterString a filter, <column><operator><value> Example: jobinstance.name==jobName,jobExcutionId>=200.  that will search for all jobs named 'jobName' and jobExecutionId >= 200
 * @return a list of criteria objects
 */
public static List<SearchCriteria> parseFilterString(String filterString) {
    List<SearchCriteria> searchCriterias = new ArrayList<>();
    if (StringUtils.isNotBlank(filterString)) {
        // first match and split on , for various filters
        String[] filterConditions = filterString.split(",(?=(?:[^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)");
        List<String> filterConditionsList = Arrays.asList(filterConditions);
        // Pattern used to match the <column><operator><value>
        String validOperatorsRegEx = operators.keySet().stream().map(key -> key).collect(Collectors.joining("|"));
        Pattern columnOperatorValuePattern = Pattern.compile("(.*)(" + validOperatorsRegEx + ")(.*)");
        filterConditionsList.stream().forEach(filter -> {
            Matcher matcher = columnOperatorValuePattern.matcher(filter);
            while (matcher.find()) {
                String field = matcher.group(1);
                String operator = matcher.group(2);
                String value = matcher.group(3);
                searchCriterias.add(new SearchCriteria(field, operator, value));
            }
        });
    }
    return searchCriterias;
}
Also used : EnumOperation(com.querydsl.core.types.dsl.EnumOperation) EnumTemplate(com.querydsl.core.types.dsl.EnumTemplate) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) ImmutableCollection(com.google.common.collect.ImmutableCollection) BooleanBuilder(com.querydsl.core.BooleanBuilder) Ops(com.querydsl.core.types.Ops) Path(com.querydsl.core.types.Path) ConvertUtilsBean(org.apache.commons.beanutils.ConvertUtilsBean) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Matcher(java.util.regex.Matcher) StringPath(com.querydsl.core.types.dsl.StringPath) Operator(com.querydsl.core.types.Operator) Formatters(com.thinkbiganalytics.Formatters) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Visitor(com.querydsl.core.types.Visitor) Logger(org.slf4j.Logger) TemplateExpressionImpl(com.querydsl.core.types.TemplateExpressionImpl) EnumExpression(com.querydsl.core.types.dsl.EnumExpression) ImmutableMap(com.google.common.collect.ImmutableMap) ExpressionUtils(com.querydsl.core.types.ExpressionUtils) Collection(java.util.Collection) DateTime(org.joda.time.DateTime) UUID(java.util.UUID) ComparablePath(com.querydsl.core.types.dsl.ComparablePath) BeanUtilsBean(org.apache.commons.beanutils.BeanUtilsBean) Expression(com.querydsl.core.types.Expression) Collectors(java.util.stream.Collectors) EnumPath(com.querydsl.core.types.dsl.EnumPath) OperationImpl(com.querydsl.core.types.OperationImpl) List(java.util.List) Expressions(com.querydsl.core.types.dsl.Expressions) PathImpl(com.querydsl.core.types.PathImpl) EntityPathBase(com.querydsl.core.types.dsl.EntityPathBase) NumberPath(com.querydsl.core.types.dsl.NumberPath) Pattern(java.util.regex.Pattern) SearchCriteria(com.thinkbiganalytics.metadata.api.SearchCriteria) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) SearchCriteria(com.thinkbiganalytics.metadata.api.SearchCriteria)

Example 5 with Operator

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

the class DefaultQueryEngine method project.

private List<?> project(QueryMetadata metadata, List<Expression<?>> sources, List<?> list) {
    Expression<?> projection = metadata.getProjection();
    Operator aggregator = null;
    if (projection instanceof Operation && Ops.aggOps.contains(((Operation) projection).getOperator())) {
        Operation<?> aggregation = (Operation<?>) projection;
        aggregator = aggregation.getOperator();
        projection = aggregation.getArg(0);
    }
    Evaluator projectionEvaluator = evaluatorFactory.create(metadata, sources, projection);
    EvaluatorFunction<Object, Object> transformer = new EvaluatorFunction(projectionEvaluator);
    List target = list.stream().map(transformer).collect(Collectors.toList());
    if (aggregator != null) {
        return Collections.singletonList(CollQueryFunctions.aggregate(target, projection, aggregator));
    } else {
        return target;
    }
}
Also used : Operator(com.querydsl.core.types.Operator) ArrayList(java.util.ArrayList) List(java.util.List) Operation(com.querydsl.core.types.Operation) Evaluator(com.querydsl.codegen.utils.Evaluator)

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