Search in sources :

Example 1 with Filter

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

the class ReactiveCassandraTemplateUnitTests method updateShouldApplyUpdateQueryWitLwt.

// DATACASS-575
@Test
void updateShouldApplyUpdateQueryWitLwt() {
    when(reactiveResultSet.rows()).thenReturn(Flux.just(row));
    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).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    verify(session).execute(statementCaptor.capture());
    assertThat(render(statementCaptor.getValue())).isEqualTo("UPDATE users SET firstname='Walter' WHERE id='heisenberg' IF firstname='Walter' AND lastname='White'");
}
Also used : VersionedUser(org.springframework.data.cassandra.domain.VersionedUser) User(org.springframework.data.cassandra.domain.User) Query(org.springframework.data.cassandra.core.query.Query) Filter(org.springframework.data.cassandra.core.query.Filter) Update(org.springframework.data.cassandra.core.query.Update) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 2 with Filter

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

the class CassandraTemplateUnitTests 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).execute(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 3 with Filter

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

the class StatementFactory method createSelect.

private StatementBuilder<Select> createSelect(Query query, CassandraPersistentEntity<?> entity, Filter filter, List<Selector> selectors, CqlIdentifier tableName) {
    Sort sort = Optional.of(query.getSort()).map(querySort -> getQueryMapper().getMappedSort(querySort, entity)).orElse(Sort.unsorted());
    StatementBuilder<Select> select = createSelectAndOrder(selectors, tableName, filter, sort);
    if (query.getLimit() > 0) {
        select.apply(it -> it.limit(Math.toIntExact(query.getLimit())));
    }
    if (query.isAllowFiltering()) {
        select.apply(Select::allowFiltering);
    }
    select.onBuild(statementBuilder -> query.getPagingState().ifPresent(statementBuilder::setPagingState));
    query.getQueryOptions().ifPresent(it -> select.transform(statement -> QueryOptionsUtil.addQueryOptions(statement, it)));
    return select;
}
Also used : 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) Select(com.datastax.oss.driver.api.querybuilder.select.Select) Sort(org.springframework.data.domain.Sort)

Example 4 with Filter

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

the class StatementFactory method select.

/**
 * Create a {@literal SELECT} statement by mapping {@link Query} to {@link Select}.
 *
 * @param query must not be {@literal null}.
 * @param persistentEntity must not be {@literal null}.
 * @param tableName must not be {@literal null}.
 * @return the select builder.
 * @since 2.1
 */
public StatementBuilder<Select> select(Query query, CassandraPersistentEntity<?> persistentEntity, CqlIdentifier tableName) {
    Assert.notNull(query, "Query must not be null");
    Assert.notNull(persistentEntity, "CassandraPersistentEntity must not be null");
    Assert.notNull(persistentEntity, "Table name must not be null");
    Filter filter = getQueryMapper().getMappedObject(query, persistentEntity);
    List<Selector> selectors = getQueryMapper().getMappedSelectors(query.getColumns(), persistentEntity);
    return createSelect(query, persistentEntity, 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 5 with Filter

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

the class QueryMapperUnitTests method shouldMapPropertyToColumnName.

// DATACASS-343
@Test
void shouldMapPropertyToColumnName() {
    Query query = Query.query(Criteria.where("firstName").is("bar"));
    Filter mappedObject = queryMapper.getMappedObject(query, persistentEntity);
    CriteriaDefinition mappedCriteriaDefinition = mappedObject.iterator().next();
    assertThat(mappedCriteriaDefinition.getColumnName()).isEqualTo(ColumnName.from(CqlIdentifier.fromCql("first_name")));
    assertThat(mappedCriteriaDefinition.getColumnName().toString()).isEqualTo("first_name");
}
Also used : 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)

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