Search in sources :

Example 21 with Filter

use of org.springframework.data.cassandra.core.query.Filter 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 22 with Filter

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

the class AsyncCassandraTemplateUnitTests method updateShouldApplyUpdateQueryWitLwt.

// DATACASS-575
@Test
void updateShouldApplyUpdateQueryWitLwt() {
    Filter ifCondition = Filter.from(where("firstname").is("Walter"), where("lastname").is("White"));
    Query query = Query.query(where("id").is("heisenberg")).queryOptions(UpdateOptions.builder().ifCondition(ifCondition).build());
    Update update = Update.update("firstname", "Walter");
    template.update(query, update, User.class);
    verify(session).executeAsync(statementCaptor.capture());
    assertThat(render(statementCaptor.getValue())).isEqualTo("UPDATE users SET firstname='Walter' WHERE id='heisenberg' IF firstname='Walter' AND lastname='White'");
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) Filter(org.springframework.data.cassandra.core.query.Filter) Update(org.springframework.data.cassandra.core.query.Update) Test(org.junit.jupiter.api.Test)

Example 23 with Filter

use of org.springframework.data.cassandra.core.query.Filter 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 24 with Filter

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

the class StatementFactory method count.

/**
 * Create a {@literal COUNT} statement by mapping {@link Query} to {@link Select}.
 *
 * @param query user-defined count {@link Query} to execute; must not be {@literal null}.
 * @param entity {@link CassandraPersistentEntity entity} to count; must not be {@literal null}.
 * @param tableName must not be {@literal null}.
 * @return the select builder.
 * @since 2.1
 */
public StatementBuilder<Select> count(Query query, CassandraPersistentEntity<?> entity, CqlIdentifier tableName) {
    Filter filter = getQueryMapper().getMappedObject(query, entity);
    List<Selector> selectors = Collections.singletonList(FunctionCall.from("COUNT", 1L));
    return createSelect(query, entity, filter, selectors, tableName);
}
Also used : Filter(org.springframework.data.cassandra.core.query.Filter) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Selector(org.springframework.data.cassandra.core.query.Columns.Selector)

Example 25 with Filter

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

the class StatementFactory method delete.

/**
 * Create a {@literal DELETE} statement by mapping {@link Query} to {@link Delete}.
 *
 * @param query must not be {@literal null}.
 * @param persistentEntity must not be {@literal null}.
 * @param tableName must not be {@literal null}.
 * @return the delete builder.
 * @see 2.1
 */
public StatementBuilder<Delete> delete(Query query, CassandraPersistentEntity<?> persistentEntity, CqlIdentifier tableName) {
    Assert.notNull(query, "Query must not be null");
    Assert.notNull(persistentEntity, "CassandraPersistentEntity must not be null");
    Assert.notNull(tableName, "Table name must not be null");
    Filter filter = getQueryMapper().getMappedObject(query, persistentEntity);
    List<CqlIdentifier> columnNames = getQueryMapper().getMappedColumnNames(query.getColumns(), persistentEntity);
    StatementBuilder<Delete> builder = delete(columnNames, tableName, filter);
    query.getQueryOptions().filter(DeleteOptions.class::isInstance).map(DeleteOptions.class::cast).map(DeleteOptions::getIfCondition).ifPresent(criteriaDefinitions -> applyDeleteIfCondition(builder, criteriaDefinitions));
    query.getQueryOptions().filter(WriteOptions.class::isInstance).map(WriteOptions.class::cast).ifPresent(writeOptions -> builder.apply(statement -> addWriteOptions(statement, writeOptions)));
    query.getQueryOptions().ifPresent(options -> builder.transform(statement -> QueryOptionsUtil.addQueryOptions(statement, options)));
    return builder;
}
Also used : Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) TermFactory(org.springframework.data.cassandra.core.cql.util.TermFactory) Relation(com.datastax.oss.driver.api.querybuilder.relation.Relation) Assignment(com.datastax.oss.driver.api.querybuilder.update.Assignment) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) SetAtIndexOp(org.springframework.data.cassandra.core.query.Update.SetAtIndexOp) SetOp(org.springframework.data.cassandra.core.query.Update.SetOp) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) EntityProjection(org.springframework.data.projection.EntityProjection) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) PersistentPropertyTranslator(org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator) Predicates(org.springframework.data.util.Predicates) Map(java.util.Map) RemoveOp(org.springframework.data.cassandra.core.query.Update.RemoveOp) Sort(org.springframework.data.domain.Sort) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Select(com.datastax.oss.driver.api.querybuilder.select.Select) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BindMarker(com.datastax.oss.driver.api.querybuilder.BindMarker) ClassUtils(org.springframework.util.ClassUtils) ColumnRelationBuilder(com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder) Collection(java.util.Collection) Set(java.util.Set) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate) Collectors(java.util.stream.Collectors) Selector(org.springframework.data.cassandra.core.query.Columns.Selector) List(java.util.List) UpdateMapper(org.springframework.data.cassandra.core.convert.UpdateMapper) PropertyDescriptor(java.beans.PropertyDescriptor) QueryMapper(org.springframework.data.cassandra.core.convert.QueryMapper) Optional(java.util.Optional) AddToOp(org.springframework.data.cassandra.core.query.Update.AddToOp) NonNull(org.springframework.lang.NonNull) AssignmentOp(org.springframework.data.cassandra.core.query.Update.AssignmentOp) IncrOp(org.springframework.data.cassandra.core.query.Update.IncrOp) SetAtKeyOp(org.springframework.data.cassandra.core.query.Update.SetAtKeyOp) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) AddToMapOp(org.springframework.data.cassandra.core.query.Update.AddToMapOp) UpdateStart(com.datastax.oss.driver.api.querybuilder.update.UpdateStart) OngoingAssignment(com.datastax.oss.driver.api.querybuilder.update.OngoingAssignment) Condition(com.datastax.oss.driver.api.querybuilder.condition.Condition) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Filter(org.springframework.data.cassandra.core.query.Filter) Function(java.util.function.Function) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) StatementBuilder(org.springframework.data.cassandra.core.cql.util.StatementBuilder) Nullable(org.springframework.lang.Nullable) Mode(org.springframework.data.cassandra.core.query.Update.AddToOp.Mode) ConditionBuilder(com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder) EntityWriter(org.springframework.data.convert.EntityWriter) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Update(org.springframework.data.cassandra.core.query.Update) CassandraConverter(org.springframework.data.cassandra.core.convert.CassandraConverter) Query(org.springframework.data.cassandra.core.query.Query) QueryOptionsUtil(org.springframework.data.cassandra.core.cql.QueryOptionsUtil) Term(com.datastax.oss.driver.api.querybuilder.term.Term) Collections(java.util.Collections) Where(org.springframework.data.cassandra.core.convert.Where) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) Filter(org.springframework.data.cassandra.core.query.Filter) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Aggregations

Filter (org.springframework.data.cassandra.core.query.Filter)31 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)21 Query (org.springframework.data.cassandra.core.query.Query)21 Test (org.junit.jupiter.api.Test)20 ColumnSelector (org.springframework.data.cassandra.core.query.Columns.ColumnSelector)10 Selector (org.springframework.data.cassandra.core.query.Columns.Selector)10 Update (org.springframework.data.cassandra.core.query.Update)9 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)8 ArrayList (java.util.ArrayList)8 Collections (java.util.Collections)8 List (java.util.List)8 Optional (java.util.Optional)8 Set (java.util.Set)8 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)8 Collection (java.util.Collection)7 CassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity)7 Columns (org.springframework.data.cassandra.core.query.Columns)7 FunctionCall (org.springframework.data.cassandra.core.query.Columns.FunctionCall)7 Predicate (org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)7 Sort (org.springframework.data.domain.Sort)7