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