use of org.springframework.data.cassandra.core.query.Columns.Selector in project spring-data-cassandra by spring-projects.
the class QueryMapper method getMappedSelector.
private Selector getMappedSelector(Selector selector, CqlIdentifier cqlIdentifier) {
if (selector instanceof ColumnSelector) {
ColumnSelector columnSelector = (ColumnSelector) selector;
ColumnSelector mappedColumnSelector = ColumnSelector.from(cqlIdentifier);
return columnSelector.getAlias().map(mappedColumnSelector::as).orElse(mappedColumnSelector);
}
if (selector instanceof FunctionCall) {
FunctionCall functionCall = (FunctionCall) selector;
FunctionCall mappedFunctionCall = FunctionCall.from(functionCall.getExpression(), functionCall.getParameters().stream().map(obj -> {
if (obj instanceof Selector) {
return getMappedSelector((Selector) obj, cqlIdentifier);
}
return obj;
}).toArray());
return //
functionCall.getAlias().map(//
mappedFunctionCall::as).orElse(mappedFunctionCall);
}
throw new IllegalArgumentException(String.format("Selector [%s] not supported", selector));
}
use of org.springframework.data.cassandra.core.query.Columns.Selector 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.query.Columns.Selector in project spring-data-cassandra by spring-projects.
the class StatementFactory method select.
/**
* Create a {@literal SELECT} statement by mapping {@link Query} to {@link Select}.
*
* @param query must not be {@literal null}.
* @param persistentEntity must not be {@literal null}.
* @param tableName must not be {@literal null}.
* @return the select builder.
* @since 2.1
*/
public StatementBuilder<Select> select(Query query, CassandraPersistentEntity<?> persistentEntity, CqlIdentifier tableName) {
Assert.notNull(query, "Query must not be null");
Assert.notNull(persistentEntity, "CassandraPersistentEntity must not be null");
Assert.notNull(persistentEntity, "Table name must not be null");
Filter filter = getQueryMapper().getMappedObject(query, persistentEntity);
List<Selector> selectors = getQueryMapper().getMappedSelectors(query.getColumns(), persistentEntity);
return createSelect(query, persistentEntity, filter, selectors, tableName);
}
use of org.springframework.data.cassandra.core.query.Columns.Selector in project spring-data-cassandra by spring-projects.
the class StatementFactory method count.
/**
* Create a {@literal COUNT} statement by mapping {@link Query} to {@link Select}.
*
* @param query user-defined count {@link Query} to execute; must not be {@literal null}.
* @param entity {@link CassandraPersistentEntity entity} to count; must not be {@literal null}.
* @param tableName must not be {@literal null}.
* @return the select builder.
* @since 2.1
*/
public StatementBuilder<Select> count(Query query, CassandraPersistentEntity<?> entity, CqlIdentifier tableName) {
Filter filter = getQueryMapper().getMappedObject(query, entity);
List<Selector> selectors = Collections.singletonList(FunctionCall.from("COUNT", 1L));
return createSelect(query, entity, filter, selectors, tableName);
}
use of org.springframework.data.cassandra.core.query.Columns.Selector 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;
}
Aggregations