Search in sources :

Example 1 with Query

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

the class ReactiveCassandraTemplate method doSelect.

<T> Flux<T> doSelect(Query query, Class<?> entityClass, CqlIdentifier tableName, Class<T> returnType) {
    CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entityClass);
    EntityProjection<T, ?> projection = entityOperations.introspectProjection(returnType, entityClass);
    Columns columns = getStatementFactory().computeColumnsForProjection(projection, query.getColumns(), persistentEntity, returnType);
    Query queryToUse = query.columns(columns);
    StatementBuilder<Select> select = getStatementFactory().select(queryToUse, persistentEntity, tableName);
    Function<Row, T> mapper = getMapper(projection, tableName);
    return doQuery(select.build(), (row, rowNum) -> mapper.apply(row));
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) Select(com.datastax.oss.driver.api.querybuilder.select.Select) Columns(org.springframework.data.cassandra.core.query.Columns) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 2 with Query

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

the class QueryStatementCreator method doWithQuery.

/**
 * A {@link Function} to {@link Query} derived from a {@link PartTree} and apply query options.
 *
 * @param parameterAccessor must not be {@literal null}.
 * @param tree must not be {@literal null}.
 * @param function callback function must not be {@literal null}.
 * @return the {@literal SELECT} {@link Statement}.
 */
<T> T doWithQuery(CassandraParameterAccessor parameterAccessor, PartTree tree, Function<Query, ? extends T> function) {
    CassandraQueryCreator queryCreator = new CassandraQueryCreator(tree, parameterAccessor, this.mappingContext);
    Query query = queryCreator.createQuery();
    try {
        if (tree.isLimiting()) {
            query = query.limit(tree.getMaxResults());
        }
        if (allowsFiltering()) {
            query = query.withAllowFiltering();
        }
        Optional<QueryOptions> queryOptions = Optional.ofNullable(parameterAccessor.getQueryOptions());
        if (queryOptions.isPresent()) {
            query = Optional.ofNullable(parameterAccessor.getQueryOptions()).map(query::queryOptions).orElse(query);
        } else if (this.queryMethod.hasConsistencyLevel()) {
            query = query.queryOptions(QueryOptions.builder().consistencyLevel(this.queryMethod.getRequiredAnnotatedConsistencyLevel()).build());
        }
        return function.apply(query);
    } catch (RuntimeException cause) {
        throw QueryCreationException.create(this.queryMethod, cause);
    }
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions)

Example 3 with Query

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

the class ReactiveCassandraTemplateIntegrationTests method deleteColumnsByQueryShouldRemoveColumn.

// DATACASS-343
@Test
void deleteColumnsByQueryShouldRemoveColumn() {
    User user = new User("heisenberg", "Walter", "White");
    template.insert(user).block();
    Query query = Query.query(Criteria.where("id").is("heisenberg")).columns(Columns.from("lastname"));
    assertThat(template.delete(query, User.class).block()).isTrue();
    User loaded = template.selectOneById(user.getId(), User.class).block();
    assertThat(loaded.getFirstname()).isEqualTo("Walter");
    assertThat(loaded.getLastname()).isNull();
}
Also used : User(org.springframework.data.cassandra.domain.User) Query(org.springframework.data.cassandra.core.query.Query) Test(org.junit.jupiter.api.Test)

Example 4 with Query

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

the class ReactiveCassandraTemplateIntegrationTests method updateShouldUpdateEntityByQuery.

// DATACASS-343
@Test
void updateShouldUpdateEntityByQuery() {
    User user = new User("heisenberg", "Walter", "White");
    template.insert(user).block();
    Query query = Query.query(Criteria.where("id").is("heisenberg"));
    boolean result = template.update(query, Update.empty().set("firstname", "Walter Hartwell"), User.class).block();
    assertThat(result).isTrue();
    assertThat(template.selectOneById(user.getId(), User.class).block().getFirstname()).isEqualTo("Walter Hartwell");
}
Also used : User(org.springframework.data.cassandra.domain.User) Query(org.springframework.data.cassandra.core.query.Query) Test(org.junit.jupiter.api.Test)

Example 5 with Query

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

the class ReactiveCassandraTemplateUnitTests method deleteShouldRemoveByQueryWithLwt.

// DATACASS-575
@Test
void deleteShouldRemoveByQueryWithLwt() {
    when(reactiveResultSet.rows()).thenReturn(Flux.just(row));
    DeleteOptions options = DeleteOptions.builder().ifCondition(where("firstname").is("Walter")).build();
    Query query = Query.query(where("id").is("heisenberg")).queryOptions(options);
    // 
    template.delete(query, User.class).as(// 
    StepVerifier::create).expectNextCount(// 
    1).verifyComplete();
    verify(session).execute(statementCaptor.capture());
    assertThat(render(statementCaptor.getValue())).isEqualTo("DELETE FROM users WHERE id='heisenberg' IF firstname='Walter'");
}
Also used : VersionedUser(org.springframework.data.cassandra.domain.VersionedUser) User(org.springframework.data.cassandra.domain.User) Query(org.springframework.data.cassandra.core.query.Query) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

Query (org.springframework.data.cassandra.core.query.Query)72 Test (org.junit.jupiter.api.Test)65 User (org.springframework.data.cassandra.domain.User)22 Filter (org.springframework.data.cassandra.core.query.Filter)18 Update (org.springframework.data.cassandra.core.query.Update)17 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)15 Select (com.datastax.oss.driver.api.querybuilder.select.Select)13 UserToken (org.springframework.data.cassandra.domain.UserToken)8 Group (org.springframework.data.cassandra.domain.Group)7 QueryOptions (org.springframework.data.cassandra.core.cql.QueryOptions)6 Columns (org.springframework.data.cassandra.core.query.Columns)6 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)5 WriteOptions (org.springframework.data.cassandra.core.cql.WriteOptions)5 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)4 Collection (java.util.Collection)4 LinkedHashMap (java.util.LinkedHashMap)4 StepVerifier (reactor.test.StepVerifier)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)3 Uuids (com.datastax.oss.driver.api.core.uuid.Uuids)3