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);
}
}
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);
}
}
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);
}
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);
}
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);
}
Aggregations