Search in sources :

Example 11 with CriteriaDefinition

use of org.springframework.data.cassandra.core.query.CriteriaDefinition in project spring-data-cassandra by spring-projects.

the class QueryMapperUnitTests method shouldMapCollectionApplyingUdtValueCollectionConversion.

// DATACASS-343
@Test
void shouldMapCollectionApplyingUdtValueCollectionConversion() {
    Query query = Query.query(Criteria.where("address").in(new Address("21 Jump-Street")));
    Filter mappedObject = queryMapper.getMappedObject(query, persistentEntity);
    CriteriaDefinition mappedCriteriaDefinition = mappedObject.iterator().next();
    CriteriaDefinition.Predicate predicate = mappedCriteriaDefinition.getPredicate();
    assertThat(predicate.getOperator()).isEqualTo(Operators.IN);
    assertThat(predicate.getValue()).isInstanceOf(Collection.class);
    assertThat((List<UdtValue>) predicate.getValue()).extracting(UdtValue::getFormattedContents).contains("{street:'21 Jump-Street'}");
}
Also used : UdtValue(com.datastax.oss.driver.api.core.data.UdtValue) Query(org.springframework.data.cassandra.core.query.Query) Filter(org.springframework.data.cassandra.core.query.Filter) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Test(org.junit.jupiter.api.Test)

Example 12 with CriteriaDefinition

use of org.springframework.data.cassandra.core.query.CriteriaDefinition in project spring-data-cassandra by spring-projects.

the class QueryMapperUnitTests method shouldMapEnumToNumberIn.

// DATACASS-343
@Test
void shouldMapEnumToNumberIn() {
    Query query = Query.query(Criteria.where("number").in(State.Inactive));
    Filter mappedObject = queryMapper.getMappedObject(query, persistentEntity);
    CriteriaDefinition mappedCriteriaDefinition = mappedObject.iterator().next();
    assertThat(mappedCriteriaDefinition.getPredicate().getValue()).isInstanceOf(Collection.class).isEqualTo(Collections.singletonList(1));
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) Filter(org.springframework.data.cassandra.core.query.Filter) Collection(java.util.Collection) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Test(org.junit.jupiter.api.Test)

Example 13 with CriteriaDefinition

use of org.springframework.data.cassandra.core.query.CriteriaDefinition in project spring-data-cassandra by spring-projects.

the class CassandraQueryCreator method create.

/* (non-Javadoc)
	 * @see org.springframework.data.repository.query.parser.AbstractQueryCreator#create(org.springframework.data.repository.query.parser.Part, java.util.Iterator)
	 */
@Override
protected Filter create(Part part, Iterator<Object> iterator) {
    PersistentPropertyPath<CassandraPersistentProperty> path = getMappingContext().getPersistentPropertyPath(part.getProperty());
    CassandraPersistentProperty property = path.getLeafProperty();
    Assert.state(property != null && path.toDotPath() != null, "Leaf property must not be null");
    Object filterOrCriteria = from(part, property, Criteria.where(path.toDotPath()), (PotentiallyConvertingIterator) iterator);
    if (filterOrCriteria instanceof CriteriaDefinition) {
        return Filter.from((CriteriaDefinition) filterOrCriteria);
    }
    return (Filter) filterOrCriteria;
}
Also used : Filter(org.springframework.data.cassandra.core.query.Filter) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition)

Example 14 with CriteriaDefinition

use of org.springframework.data.cassandra.core.query.CriteriaDefinition 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)

Example 15 with CriteriaDefinition

use of org.springframework.data.cassandra.core.query.CriteriaDefinition in project spring-data-cassandra by spring-projects.

the class StatementFactory method toClause.

private static Relation toClause(CriteriaDefinition criteriaDefinition, TermFactory factory) {
    CqlIdentifier columnName = criteriaDefinition.getColumnName().getCqlIdentifier().orElseGet(() -> CqlIdentifier.fromInternal(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())));
    ColumnRelationBuilder<Relation> column = Relation.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));
        case LIKE:
            return column.like(factory.create(value));
        case IS_NOT_NULL:
            return column.isNotNull();
        case CONTAINS:
            Assert.state(value != null, () -> String.format("CONTAINS value for column %s is null", columnName));
            return column.contains(factory.create(value));
        case CONTAINS_KEY:
            Assert.state(value != null, () -> String.format("CONTAINS KEY value for column %s is null", columnName));
            return column.containsKey(factory.create(value));
    }
    throw new IllegalArgumentException(String.format("Criteria %s %s %s not supported", columnName, predicate.getOperator(), value));
}
Also used : Relation(com.datastax.oss.driver.api.querybuilder.relation.Relation) BindMarker(com.datastax.oss.driver.api.querybuilder.BindMarker) Term(com.datastax.oss.driver.api.querybuilder.term.Term) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)

Aggregations

CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)16 Filter (org.springframework.data.cassandra.core.query.Filter)14 Test (org.junit.jupiter.api.Test)12 Query (org.springframework.data.cassandra.core.query.Query)12 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)4 Predicate (org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 BindMarker (com.datastax.oss.driver.api.querybuilder.BindMarker)2 Term (com.datastax.oss.driver.api.querybuilder.term.Term)2 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)2 Condition (com.datastax.oss.driver.api.querybuilder.condition.Condition)1 Relation (com.datastax.oss.driver.api.querybuilder.relation.Relation)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 BasicCassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity)1