Search in sources :

Example 1 with SimpleExpression

use of com.querydsl.core.types.dsl.SimpleExpression in project spring-data-commons by spring-projects.

the class QuerydslDefaultBinding method bind.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.web.querydsl.QueryDslPredicateBuilder#buildPredicate(org.springframework.data.mapping.PropertyPath, java.lang.Object)
	 */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public Optional<Predicate> bind(Path<?> path, Collection<? extends Object> value) {
    Assert.notNull(path, "Path must not be null!");
    Assert.notNull(value, "Value must not be null!");
    if (value.isEmpty()) {
        return Optional.empty();
    }
    if (path instanceof CollectionPathBase) {
        BooleanBuilder builder = new BooleanBuilder();
        for (Object element : value) {
            builder.and(((CollectionPathBase) path).contains(element));
        }
        return Optional.of(builder.getValue());
    }
    if (path instanceof SimpleExpression) {
        if (value.size() > 1) {
            return Optional.of(((SimpleExpression) path).in(value));
        }
        return Optional.of(((SimpleExpression) path).eq(value.iterator().next()));
    }
    throw new IllegalArgumentException(String.format("Cannot create predicate for path '%s' with type '%s'.", path, path.getMetadata().getPathType()));
}
Also used : BooleanBuilder(com.querydsl.core.BooleanBuilder) CollectionPathBase(com.querydsl.core.types.dsl.CollectionPathBase) SimpleExpression(com.querydsl.core.types.dsl.SimpleExpression)

Aggregations

BooleanBuilder (com.querydsl.core.BooleanBuilder)1 CollectionPathBase (com.querydsl.core.types.dsl.CollectionPathBase)1 SimpleExpression (com.querydsl.core.types.dsl.SimpleExpression)1