Search in sources :

Example 11 with QueryOptions

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

the class CassandraTemplateIntegrationTests method selectByQueryWithNonExistingKeyspaceShouldThrowThatKeyspaceDoesNotExists.

// DATACASS-767
@Test
void selectByQueryWithNonExistingKeyspaceShouldThrowThatKeyspaceDoesNotExists() {
    assumeThat(cassandraVersion.isGreaterThanOrEqualTo(CASSANDRA_4)).isTrue();
    QueryOptions queryOptions = QueryOptions.builder().keyspace(CqlIdentifier.fromCql("non_existing")).build();
    User user = new User("heisenberg", "Walter", "White");
    template.insert(user);
    Query query = Query.query(where("id").is("heisenberg")).queryOptions(queryOptions);
    assertThatThrownBy(() -> assertThat(template.select(query, User.class)).isEmpty()).isInstanceOf(CassandraInvalidQueryException.class).hasMessageContaining("Keyspace 'non_existing' does not exist");
}
Also used : User(org.springframework.data.cassandra.domain.User) Query(org.springframework.data.cassandra.core.query.Query) CassandraInvalidQueryException(org.springframework.data.cassandra.CassandraInvalidQueryException) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) Test(org.junit.jupiter.api.Test)

Example 12 with QueryOptions

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

the class StatementFactoryUnitTests method selectShouldApplyQueryOptions.

// DATACASS-708
@Test
void selectShouldApplyQueryOptions() {
    QueryOptions queryOptions = // 
    QueryOptions.builder().executionProfile(// 
    "foo").serialConsistencyLevel(// 
    DefaultConsistencyLevel.QUORUM).build();
    StatementBuilder<Select> select = statementFactory.select(Query.empty().queryOptions(queryOptions), converter.getMappingContext().getRequiredPersistentEntity(Group.class));
    SimpleStatement statement = select.build();
    assertThat(statement.getExecutionProfileName()).isEqualTo("foo");
    assertThat(statement.getSerialConsistencyLevel()).isEqualTo(DefaultConsistencyLevel.QUORUM);
}
Also used : Group(org.springframework.data.cassandra.domain.Group) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Select(com.datastax.oss.driver.api.querybuilder.select.Select) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) Test(org.junit.jupiter.api.Test)

Example 13 with QueryOptions

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

the class CompositeKeyCrudIntegrationTests method test.

@Test
void test() {
    operations.insert(correlationEntity1);
    operations.insert(correlationEntity2);
    Select select = QueryBuilder.selectFrom("identity_correlations").all().where(Relation.column("type").isEqualTo(QueryBuilder.literal("a")), Relation.column("value").isEqualTo(QueryBuilder.literal("b")));
    List<CorrelationEntity> correlationEntities = operations.select(select.build(), CorrelationEntity.class);
    assertThat(correlationEntities).hasSize(2);
    QueryOptions queryOptions = QueryOptions.builder().consistencyLevel(DefaultConsistencyLevel.ONE).build();
    operations.delete(correlationEntity1, queryOptions);
    operations.delete(correlationEntity2, queryOptions);
    correlationEntities = operations.select(select.build(), CorrelationEntity.class);
    assertThat(correlationEntities).isEmpty();
}
Also used : CorrelationEntity(org.springframework.data.cassandra.repository.forcequote.compositeprimarykey.entity.CorrelationEntity) Select(com.datastax.oss.driver.api.querybuilder.select.Select) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) Test(org.junit.jupiter.api.Test)

Example 14 with QueryOptions

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

the class StatementFactory method delete.

/**
 * Create an {@literal DELETE} statement by mapping {@code entity} to {@link Delete DELETE … WHERE} considering
 * {@link WriteOptions}.
 *
 * @param entity must not be {@literal null}.
 * @param options must not be {@literal null}.
 * @param entityWriter must not be {@literal null}.
 * @param tableName must not be {@literal null}.
 * @return the delete builder.
 */
StatementBuilder<Delete> delete(Object entity, QueryOptions options, EntityWriter<Object, Object> entityWriter, CqlIdentifier tableName) {
    Assert.notNull(tableName, "TableName must not be null");
    Assert.notNull(entity, "Object to builder must not be null");
    Assert.notNull(entityWriter, "EntityWriter must not be null");
    Where where = new Where();
    entityWriter.write(entity, where);
    StatementBuilder<Delete> builder = StatementBuilder.of(QueryBuilder.deleteFrom(tableName).where()).bind((statement, factory) -> statement.where(toRelations(where, factory)));
    Optional.of(options).filter(WriteOptions.class::isInstance).map(WriteOptions.class::cast).ifPresent(it -> builder.apply(statement -> addWriteOptions(statement, it)));
    Optional.of(options).filter(DeleteOptions.class::isInstance).map(DeleteOptions.class::cast).map(DeleteOptions::getIfCondition).ifPresent(criteriaDefinitions -> applyDeleteIfCondition(builder, criteriaDefinitions));
    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) Where(org.springframework.data.cassandra.core.convert.Where)

Aggregations

QueryOptions (org.springframework.data.cassandra.core.cql.QueryOptions)14 Test (org.junit.jupiter.api.Test)10 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)7 Query (org.springframework.data.cassandra.core.query.Query)4 Select (com.datastax.oss.driver.api.querybuilder.select.Select)3 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)2 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)1 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)1 BindMarker (com.datastax.oss.driver.api.querybuilder.BindMarker)1 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)1 Condition (com.datastax.oss.driver.api.querybuilder.condition.Condition)1 ConditionBuilder (com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder)1 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)1 Insert (com.datastax.oss.driver.api.querybuilder.insert.Insert)1 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)1 ColumnRelationBuilder (com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder)1 Relation (com.datastax.oss.driver.api.querybuilder.relation.Relation)1 Term (com.datastax.oss.driver.api.querybuilder.term.Term)1 Assignment (com.datastax.oss.driver.api.querybuilder.update.Assignment)1 OngoingAssignment (com.datastax.oss.driver.api.querybuilder.update.OngoingAssignment)1