Search in sources :

Example 16 with CriteriaDefinition

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

the class QueryMapper method getMappedObject.

/**
 * Map a {@link Filter} with a {@link CassandraPersistentEntity type hint}. Filter mapping translates property names
 * to column names and maps {@link Predicate} values to simple Cassandra values.
 *
 * @param filter must not be {@literal null}.
 * @param entity must not be {@literal null}.
 * @return the mapped {@link Filter}.
 */
public Filter getMappedObject(Filter filter, CassandraPersistentEntity<?> entity) {
    Assert.notNull(filter, "Filter must not be null");
    Assert.notNull(entity, "Entity must not be null");
    List<CriteriaDefinition> result = new ArrayList<>();
    for (CriteriaDefinition criteriaDefinition : filter) {
        Field field = createPropertyField(entity, criteriaDefinition.getColumnName());
        field.getProperty().filter(CassandraPersistentProperty::isCompositePrimaryKey).ifPresent(it -> {
            throw new IllegalArgumentException("Cannot use composite primary key directly. Reference a property of the composite primary key");
        });
        field.getProperty().filter(it -> it.getOrdinal() != null).ifPresent(it -> {
            throw new IllegalArgumentException(String.format("Cannot reference tuple value elements, property [%s]", field.getMappedKey()));
        });
        Predicate predicate = criteriaDefinition.getPredicate();
        Object value = predicate.getValue();
        ColumnType typeDescriptor = getColumnType(field, value, ColumnTypeTransformer.of(field, predicate.getOperator()));
        Object mappedValue = value != null ? getConverter().convertToColumnType(value, typeDescriptor) : null;
        Predicate mappedPredicate = new Predicate(predicate.getOperator(), mappedValue);
        result.add(Criteria.of(field.getMappedKey(), mappedPredicate));
    }
    return Filter.from(result);
}
Also used : Order(org.springframework.data.domain.Sort.Order) PropertyReferenceException(org.springframework.data.mapping.PropertyReferenceException) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MappingContext(org.springframework.data.mapping.context.MappingContext) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Filter(org.springframework.data.cassandra.core.query.Filter) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) HashSet(java.util.HashSet) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) Sort(org.springframework.data.domain.Sort) Nullable(org.springframework.lang.Nullable) EmbeddedEntityOperations(org.springframework.data.cassandra.core.mapping.EmbeddedEntityOperations) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BasicCassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity) Criteria(org.springframework.data.cassandra.core.query.Criteria) Set(java.util.Set) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate) ColumnName(org.springframework.data.cassandra.core.query.ColumnName) Selector(org.springframework.data.cassandra.core.query.Columns.Selector) PersistentPropertyPath(org.springframework.data.mapping.PersistentPropertyPath) List(java.util.List) Optional(java.util.Optional) PropertyPath(org.springframework.data.mapping.PropertyPath) Collections(java.util.Collections) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) ArrayList(java.util.ArrayList) 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