use of org.springframework.data.cassandra.core.cql.util.StatementBuilder in project spring-data-cassandra by spring-projects.
the class StatementFactory method createSelect.
private StatementBuilder<Select> createSelect(Query query, CassandraPersistentEntity<?> entity, Filter filter, List<Selector> selectors, CqlIdentifier tableName) {
Sort sort = Optional.of(query.getSort()).map(querySort -> getQueryMapper().getMappedSort(querySort, entity)).orElse(Sort.unsorted());
StatementBuilder<Select> select = createSelectAndOrder(selectors, tableName, filter, sort);
if (query.getLimit() > 0) {
select.apply(it -> it.limit(Math.toIntExact(query.getLimit())));
}
if (query.isAllowFiltering()) {
select.apply(Select::allowFiltering);
}
select.onBuild(statementBuilder -> query.getPagingState().ifPresent(statementBuilder::setPagingState));
query.getQueryOptions().ifPresent(it -> select.transform(statement -> QueryOptionsUtil.addQueryOptions(statement, it)));
return select;
}
use of org.springframework.data.cassandra.core.cql.util.StatementBuilder in project spring-data-cassandra by spring-projects.
the class StatementFactory method delete.
/**
* Create a {@literal DELETE} statement by mapping {@link Query} to {@link Delete}.
*
* @param query must not be {@literal null}.
* @param persistentEntity must not be {@literal null}.
* @param tableName must not be {@literal null}.
* @return the delete builder.
* @see 2.1
*/
public StatementBuilder<Delete> delete(Query query, CassandraPersistentEntity<?> persistentEntity, CqlIdentifier tableName) {
Assert.notNull(query, "Query must not be null");
Assert.notNull(persistentEntity, "CassandraPersistentEntity must not be null");
Assert.notNull(tableName, "Table name must not be null");
Filter filter = getQueryMapper().getMappedObject(query, persistentEntity);
List<CqlIdentifier> columnNames = getQueryMapper().getMappedColumnNames(query.getColumns(), persistentEntity);
StatementBuilder<Delete> builder = delete(columnNames, tableName, filter);
query.getQueryOptions().filter(DeleteOptions.class::isInstance).map(DeleteOptions.class::cast).map(DeleteOptions::getIfCondition).ifPresent(criteriaDefinitions -> applyDeleteIfCondition(builder, criteriaDefinitions));
query.getQueryOptions().filter(WriteOptions.class::isInstance).map(WriteOptions.class::cast).ifPresent(writeOptions -> builder.apply(statement -> addWriteOptions(statement, writeOptions)));
query.getQueryOptions().ifPresent(options -> builder.transform(statement -> QueryOptionsUtil.addQueryOptions(statement, options)));
return builder;
}
use of org.springframework.data.cassandra.core.cql.util.StatementBuilder in project spring-data-cassandra by spring-projects.
the class StatementFactory method createSelectAndOrder.
private static StatementBuilder<Select> createSelectAndOrder(List<Selector> selectors, CqlIdentifier from, Filter filter, Sort sort) {
Select select;
if (selectors.isEmpty()) {
select = QueryBuilder.selectFrom(from).all();
} else {
List<com.datastax.oss.driver.api.querybuilder.select.Selector> mappedSelectors = new ArrayList<>(selectors.size());
for (Selector selector : selectors) {
com.datastax.oss.driver.api.querybuilder.select.Selector orElseGet = selector.getAlias().map(it -> getSelection(selector).as(it)).orElseGet(() -> getSelection(selector));
mappedSelectors.add(orElseGet);
}
select = QueryBuilder.selectFrom(from).selectors(mappedSelectors);
}
StatementBuilder<Select> builder = StatementBuilder.of(select);
builder.bind((statement, factory) -> {
return statement.where(getRelations(filter, factory));
});
if (sort.isSorted()) {
builder.apply((statement) -> {
Select statementToUse = statement;
for (Sort.Order order : sort) {
statementToUse = statementToUse.orderBy(order.getProperty(), order.isAscending() ? ClusteringOrder.ASC : ClusteringOrder.DESC);
}
return statementToUse;
});
}
return builder;
}
use of org.springframework.data.cassandra.core.cql.util.StatementBuilder in project spring-data-cassandra by spring-projects.
the class StatementFactory method update.
/**
* Create an {@literal UPDATE} statement by mapping {@link Query} to {@link Update}.
*
* @param query must not be {@literal null}.
* @param update must not be {@literal null}.
* @param persistentEntity must not be {@literal null}.
* @param tableName must not be {@literal null}.
* @return the update builder.
* @since 2.1
*/
StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> update(Query query, Update update, CassandraPersistentEntity<?> persistentEntity, CqlIdentifier tableName) {
Assert.notNull(query, "Query must not be null");
Assert.notNull(update, "Update must not be null");
Assert.notNull(persistentEntity, "CassandraPersistentEntity must not be null");
Assert.notNull(tableName, "Table name must not be null");
Filter filter = getQueryMapper().getMappedObject(query, persistentEntity);
Update mappedUpdate = getUpdateMapper().getMappedObject(update, persistentEntity);
StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> builder = update(tableName, mappedUpdate, filter);
query.getQueryOptions().filter(UpdateOptions.class::isInstance).map(UpdateOptions.class::cast).map(UpdateOptions::getIfCondition).ifPresent(criteriaDefinitions -> applyUpdateIfCondition(builder, criteriaDefinitions));
query.getQueryOptions().filter(WriteOptions.class::isInstance).map(WriteOptions.class::cast).ifPresent(writeOptions -> builder.apply(statement -> addWriteOptions(statement, writeOptions)));
query.getQueryOptions().ifPresent(options -> builder.transform(statementBuilder -> QueryOptionsUtil.addQueryOptions(statementBuilder, options)));
return builder;
}
use of org.springframework.data.cassandra.core.cql.util.StatementBuilder 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;
}
Aggregations