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