Search in sources :

Example 1 with Condition

use of com.datastax.oss.driver.api.querybuilder.condition.Condition in project spring-data-cassandra by spring-projects.

the class StatementFactory method toCondition.

private static Condition toCondition(CriteriaDefinition criteriaDefinition, TermFactory factory) {
    String columnName = criteriaDefinition.getColumnName().toCql();
    Predicate predicate = criteriaDefinition.getPredicate();
    CriteriaDefinition.Operators predicateOperator = CriteriaDefinition.Operators.from(predicate.getOperator().toString()).orElseThrow(() -> new IllegalArgumentException(String.format("Unknown operator [%s]", predicate.getOperator())));
    ConditionBuilder<Condition> column = Condition.column(columnName);
    Object value = predicate.getValue();
    switch(predicateOperator) {
        case EQ:
            return column.isEqualTo(factory.create(value));
        case NE:
            return column.isNotEqualTo(factory.create(value));
        case GT:
            return column.isGreaterThan(factory.create(value));
        case GTE:
            return column.isGreaterThanOrEqualTo(factory.create(value));
        case LT:
            return column.isLessThan(factory.create(value));
        case LTE:
            return column.isLessThanOrEqualTo(factory.create(value));
        case IN:
            if (isCollectionLike(value)) {
                if (factory.canBindCollection()) {
                    Term term = factory.create(value);
                    return term instanceof BindMarker ? column.in((BindMarker) term) : column.in(term);
                }
                return column.in(toLiterals(value));
            }
            return column.in(factory.create(value));
    }
    throw new IllegalArgumentException(String.format("Criteria %s %s %s not supported for IF Conditions", columnName, predicate.getOperator(), value));
}
Also used : Condition(com.datastax.oss.driver.api.querybuilder.condition.Condition) BindMarker(com.datastax.oss.driver.api.querybuilder.BindMarker) Term(com.datastax.oss.driver.api.querybuilder.term.Term) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)

Aggregations

BindMarker (com.datastax.oss.driver.api.querybuilder.BindMarker)1 Condition (com.datastax.oss.driver.api.querybuilder.condition.Condition)1 Term (com.datastax.oss.driver.api.querybuilder.term.Term)1 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)1 Predicate (org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)1