Search in sources :

Example 11 with WriteOptions

use of org.springframework.data.cassandra.core.cql.WriteOptions in project spring-data-cassandra by spring-projects.

the class ReactiveCassandraBatchTemplateIntegrationTests method shouldUpdateCollectionOfEntitiesWithTtl.

// DATACASS-574
@Test
void shouldUpdateCollectionOfEntitiesWithTtl() {
    walter.setEmail("walter@white.com");
    int ttl = 30;
    WriteOptions options = WriteOptions.builder().ttl(ttl).build();
    ReactiveCassandraBatchOperations batchOperations = new ReactiveCassandraBatchTemplate(template, BatchType.LOGGED);
    Mono<ReactiveResultSet> resultSet = batchOperations.update(walter, options).execute().then(template.getReactiveCqlOperations().queryForResultSet("SELECT TTL(email), email FROM group;"));
    // 
    resultSet.flatMapMany(ReactiveResultSet::availableRows).collectList().as(// 
    StepVerifier::create).assertNext(rows -> {
        for (Row row : rows) {
            if (walter.getEmail().equals(row.getString(1))) {
                assertThat(row.getInt(0)).isBetween(1, ttl);
            } else {
                assertThat(row.getInt(0)).isZero();
            }
        }
    }).verifyComplete();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) DefaultBridgedReactiveSession(org.springframework.data.cassandra.core.cql.session.DefaultBridgedReactiveSession) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) GroupKey(org.springframework.data.cassandra.domain.GroupKey) SchemaTestUtils(org.springframework.data.cassandra.repository.support.SchemaTestUtils) Mono(reactor.core.publisher.Mono) Random(java.util.Random) AbstractKeyspaceCreatingIntegrationTests(org.springframework.data.cassandra.test.util.AbstractKeyspaceCreatingIntegrationTests) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) FlatGroup(org.springframework.data.cassandra.domain.FlatGroup) Group(org.springframework.data.cassandra.domain.Group) BatchType(com.datastax.oss.driver.api.core.cql.BatchType) ReactiveCqlTemplate(org.springframework.data.cassandra.core.cql.ReactiveCqlTemplate) Assertions(org.assertj.core.api.Assertions) Schedulers(reactor.core.scheduler.Schedulers) Collections(java.util.Collections) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) Row(com.datastax.oss.driver.api.core.cql.Row) MappingCassandraConverter(org.springframework.data.cassandra.core.convert.MappingCassandraConverter) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) ReactiveResultSet(org.springframework.data.cassandra.ReactiveResultSet) Row(com.datastax.oss.driver.api.core.cql.Row) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 12 with WriteOptions

use of org.springframework.data.cassandra.core.cql.WriteOptions in project spring-data-cassandra by spring-projects.

the class StatementFactoryUnitTests method shouldCreateSetInsertWithTimestamp.

// DATACASS-656
@Test
void shouldCreateSetInsertWithTimestamp() {
    WriteOptions options = WriteOptions.builder().timestamp(1234).build();
    Person person = new Person();
    person.id = "foo";
    StatementBuilder<RegularInsert> insert = statementFactory.insert(person, options);
    assertThat(insert.build(ParameterHandling.INLINE).getQuery()).isEqualTo("INSERT INTO person (id) VALUES ('foo') USING TIMESTAMP 1234");
}
Also used : WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Test(org.junit.jupiter.api.Test)

Example 13 with WriteOptions

use of org.springframework.data.cassandra.core.cql.WriteOptions in project spring-data-cassandra by spring-projects.

the class StatementFactoryUnitTests method shouldCreateSetInsertWithTtl.

// DATACASS-656
@Test
void shouldCreateSetInsertWithTtl() {
    WriteOptions options = WriteOptions.builder().ttl(Duration.ofMinutes(1)).build();
    Person person = new Person();
    person.id = "foo";
    StatementBuilder<RegularInsert> insert = statementFactory.insert(person, options);
    assertThat(insert.build(ParameterHandling.INLINE).getQuery()).isEqualTo("INSERT INTO person (id) VALUES ('foo') USING TTL 60");
}
Also used : WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Test(org.junit.jupiter.api.Test)

Example 14 with WriteOptions

use of org.springframework.data.cassandra.core.cql.WriteOptions in project spring-data-cassandra by spring-projects.

the class StatementFactoryUnitTests method shouldCreateSetUpdateWithTimestamp.

// DATACASS-656
@Test
void shouldCreateSetUpdateWithTimestamp() {
    WriteOptions options = WriteOptions.builder().timestamp(1234).build();
    Query query = Query.query(Criteria.where("foo").is("bar")).queryOptions(options);
    StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> update = statementFactory.update(query, Update.empty().set("firstName", "baz"), personEntity);
    assertThat(update.build(ParameterHandling.INLINE).getQuery()).isEqualTo("UPDATE person USING TIMESTAMP 1234 SET first_name='baz' WHERE foo='bar'");
}
Also used : WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) Query(org.springframework.data.cassandra.core.query.Query) Update(org.springframework.data.cassandra.core.query.Update) Test(org.junit.jupiter.api.Test)

Example 15 with WriteOptions

use of org.springframework.data.cassandra.core.cql.WriteOptions 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

WriteOptions (org.springframework.data.cassandra.core.cql.WriteOptions)18 Test (org.junit.jupiter.api.Test)14 Update (org.springframework.data.cassandra.core.query.Update)9 Collections (java.util.Collections)8 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)7 Row (com.datastax.oss.driver.api.core.cql.Row)6 Query (org.springframework.data.cassandra.core.query.Query)6 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)4 BindMarker (com.datastax.oss.driver.api.querybuilder.BindMarker)4 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)4 Condition (com.datastax.oss.driver.api.querybuilder.condition.Condition)4 ConditionBuilder (com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder)4 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)4 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)4 Insert (com.datastax.oss.driver.api.querybuilder.insert.Insert)4 ColumnRelationBuilder (com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder)4 Relation (com.datastax.oss.driver.api.querybuilder.relation.Relation)4 Select (com.datastax.oss.driver.api.querybuilder.select.Select)4 Term (com.datastax.oss.driver.api.querybuilder.term.Term)4