Search in sources :

Example 1 with QueryOptions

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

the class QueryStatementCreator method select.

/**
 * Create a {@link Statement} from a {@link StringBasedQuery} and apply query options.
 *
 * @param stringBasedQuery must not be {@literal null}.
 * @param parameterAccessor must not be {@literal null}.
 * @return the {@link Statement}.
 */
SimpleStatement select(StringBasedQuery stringBasedQuery, CassandraParameterAccessor parameterAccessor, SpELExpressionEvaluator evaluator) {
    try {
        SimpleStatement boundQuery = stringBasedQuery.bindQuery(parameterAccessor, evaluator);
        Optional<QueryOptions> queryOptions = Optional.ofNullable(parameterAccessor.getQueryOptions());
        SimpleStatement queryToUse = boundQuery;
        if (queryOptions.isPresent()) {
            queryToUse = Optional.ofNullable(parameterAccessor.getQueryOptions()).map(it -> QueryOptionsUtil.addQueryOptions(boundQuery, it)).orElse(boundQuery);
        } else if (this.queryMethod.hasConsistencyLevel()) {
            queryToUse = queryToUse.setConsistencyLevel(this.queryMethod.getRequiredAnnotatedConsistencyLevel());
        }
        Idempotency idempotency = this.queryMethod.getIdempotency();
        if (idempotency != Idempotency.UNDEFINED) {
            queryToUse = queryToUse.setIdempotent(idempotency == Idempotency.IDEMPOTENT);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Created query [%s].", QueryExtractorDelegate.getCql(queryToUse)));
        }
        return queryToUse;
    } catch (RuntimeException cause) {
        throw QueryCreationException.create(this.queryMethod, cause);
    }
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) Idempotency(org.springframework.data.cassandra.repository.Query.Idempotency) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions)

Example 2 with QueryOptions

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

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

the class Query method pageRequest.

/**
 * Create a {@link Query} initialized with a {@link PageRequest} to fetch the first page of results or advance in
 * paging along with sorting. Reads (and overrides, if set) {@link Pageable#getPageSize() page size} into
 * {@link QueryOptions#getPageSize()} and sets {@code pagingState} and {@link Sort}.
 *
 * @param pageable must not be {@literal null}.
 * @return a new {@link Query} object containing the former settings with {@link PageRequest} applied.
 * @see CassandraPageRequest
 */
public Query pageRequest(Pageable pageable) {
    Assert.notNull(pageable, "Pageable must not be null");
    CassandraPageRequest.validatePageable(pageable);
    ByteBuffer pagingState = getPagingState().orElse(null);
    if (pageable instanceof CassandraPageRequest) {
        pagingState = ((CassandraPageRequest) pageable).getPagingState();
    }
    QueryOptions queryOptions = this.queryOptions.map(QueryOptions::mutate).orElse(QueryOptions.builder()).pageSize(pageable.getPageSize()).build();
    return new Query(this.criteriaDefinitions, this.columns, this.sort.and(pageable.getSort()), Optional.ofNullable(pagingState), Optional.of(queryOptions), this.limit, this.allowFiltering);
}
Also used : ByteBuffer(java.nio.ByteBuffer) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions)

Example 4 with QueryOptions

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

the class StatementFactoryUnitTests method deleteShouldApplyQueryOptions.

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

Example 5 with QueryOptions

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

the class ReactivePartTreeCassandraQueryUnitTests method shouldApplyQueryOptions.

// DATACASS-146
@Test
void shouldApplyQueryOptions() {
    QueryOptions queryOptions = QueryOptions.builder().pageSize(777).build();
    SimpleStatement statement = deriveQueryFromMethod(Repo.class, "findByFirstname", new Class[] { QueryOptions.class, String.class }, queryOptions, "Walter");
    assertThat(statement.getQuery()).isEqualTo("SELECT * FROM person WHERE firstname=?");
    assertThat(statement.getPageSize()).isEqualTo(777);
}
Also used : SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) Test(org.junit.jupiter.api.Test)

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