Search in sources :

Example 1 with Selector

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));
}
Also used : ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Selector(org.springframework.data.cassandra.core.query.Columns.Selector)

Example 2 with 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;
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) TermFactory(org.springframework.data.cassandra.core.cql.util.TermFactory) Relation(com.datastax.oss.driver.api.querybuilder.relation.Relation) Assignment(com.datastax.oss.driver.api.querybuilder.update.Assignment) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) SetAtIndexOp(org.springframework.data.cassandra.core.query.Update.SetAtIndexOp) SetOp(org.springframework.data.cassandra.core.query.Update.SetOp) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) EntityProjection(org.springframework.data.projection.EntityProjection) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) PersistentPropertyTranslator(org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator) Predicates(org.springframework.data.util.Predicates) Map(java.util.Map) RemoveOp(org.springframework.data.cassandra.core.query.Update.RemoveOp) Sort(org.springframework.data.domain.Sort) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Select(com.datastax.oss.driver.api.querybuilder.select.Select) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BindMarker(com.datastax.oss.driver.api.querybuilder.BindMarker) ClassUtils(org.springframework.util.ClassUtils) ColumnRelationBuilder(com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder) Collection(java.util.Collection) Set(java.util.Set) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate) Collectors(java.util.stream.Collectors) Selector(org.springframework.data.cassandra.core.query.Columns.Selector) List(java.util.List) UpdateMapper(org.springframework.data.cassandra.core.convert.UpdateMapper) PropertyDescriptor(java.beans.PropertyDescriptor) QueryMapper(org.springframework.data.cassandra.core.convert.QueryMapper) Optional(java.util.Optional) AddToOp(org.springframework.data.cassandra.core.query.Update.AddToOp) NonNull(org.springframework.lang.NonNull) AssignmentOp(org.springframework.data.cassandra.core.query.Update.AssignmentOp) IncrOp(org.springframework.data.cassandra.core.query.Update.IncrOp) SetAtKeyOp(org.springframework.data.cassandra.core.query.Update.SetAtKeyOp) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) AddToMapOp(org.springframework.data.cassandra.core.query.Update.AddToMapOp) UpdateStart(com.datastax.oss.driver.api.querybuilder.update.UpdateStart) OngoingAssignment(com.datastax.oss.driver.api.querybuilder.update.OngoingAssignment) Condition(com.datastax.oss.driver.api.querybuilder.condition.Condition) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Filter(org.springframework.data.cassandra.core.query.Filter) Function(java.util.function.Function) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) StatementBuilder(org.springframework.data.cassandra.core.cql.util.StatementBuilder) Nullable(org.springframework.lang.Nullable) Mode(org.springframework.data.cassandra.core.query.Update.AddToOp.Mode) ConditionBuilder(com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder) EntityWriter(org.springframework.data.convert.EntityWriter) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Update(org.springframework.data.cassandra.core.query.Update) CassandraConverter(org.springframework.data.cassandra.core.convert.CassandraConverter) Query(org.springframework.data.cassandra.core.query.Query) QueryOptionsUtil(org.springframework.data.cassandra.core.cql.QueryOptionsUtil) Term(com.datastax.oss.driver.api.querybuilder.term.Term) Collections(java.util.Collections) Where(org.springframework.data.cassandra.core.convert.Where) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) Select(com.datastax.oss.driver.api.querybuilder.select.Select) Sort(org.springframework.data.domain.Sort)

Example 3 with Selector

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);
}
Also used : Filter(org.springframework.data.cassandra.core.query.Filter) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Selector(org.springframework.data.cassandra.core.query.Columns.Selector)

Example 4 with Selector

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);
}
Also used : Filter(org.springframework.data.cassandra.core.query.Filter) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Selector(org.springframework.data.cassandra.core.query.Columns.Selector)

Example 5 with Selector

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;
}
Also used : QueryBuilder(com.datastax.oss.driver.api.querybuilder.QueryBuilder) QueryOptions(org.springframework.data.cassandra.core.cql.QueryOptions) TermFactory(org.springframework.data.cassandra.core.cql.util.TermFactory) Relation(com.datastax.oss.driver.api.querybuilder.relation.Relation) Assignment(com.datastax.oss.driver.api.querybuilder.update.Assignment) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Insert(com.datastax.oss.driver.api.querybuilder.insert.Insert) Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) SetAtIndexOp(org.springframework.data.cassandra.core.query.Update.SetAtIndexOp) SetOp(org.springframework.data.cassandra.core.query.Update.SetOp) UpdateWithAssignments(com.datastax.oss.driver.api.querybuilder.update.UpdateWithAssignments) EntityProjection(org.springframework.data.projection.EntityProjection) DeleteSelection(com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) PersistentPropertyTranslator(org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator) Predicates(org.springframework.data.util.Predicates) Map(java.util.Map) RemoveOp(org.springframework.data.cassandra.core.query.Update.RemoveOp) Sort(org.springframework.data.domain.Sort) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Select(com.datastax.oss.driver.api.querybuilder.select.Select) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BindMarker(com.datastax.oss.driver.api.querybuilder.BindMarker) ClassUtils(org.springframework.util.ClassUtils) ColumnRelationBuilder(com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder) Collection(java.util.Collection) Set(java.util.Set) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate) Collectors(java.util.stream.Collectors) Selector(org.springframework.data.cassandra.core.query.Columns.Selector) List(java.util.List) UpdateMapper(org.springframework.data.cassandra.core.convert.UpdateMapper) PropertyDescriptor(java.beans.PropertyDescriptor) QueryMapper(org.springframework.data.cassandra.core.convert.QueryMapper) Optional(java.util.Optional) AddToOp(org.springframework.data.cassandra.core.query.Update.AddToOp) NonNull(org.springframework.lang.NonNull) AssignmentOp(org.springframework.data.cassandra.core.query.Update.AssignmentOp) IncrOp(org.springframework.data.cassandra.core.query.Update.IncrOp) SetAtKeyOp(org.springframework.data.cassandra.core.query.Update.SetAtKeyOp) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) AddToMapOp(org.springframework.data.cassandra.core.query.Update.AddToMapOp) UpdateStart(com.datastax.oss.driver.api.querybuilder.update.UpdateStart) OngoingAssignment(com.datastax.oss.driver.api.querybuilder.update.OngoingAssignment) Condition(com.datastax.oss.driver.api.querybuilder.condition.Condition) ClusteringOrder(com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Filter(org.springframework.data.cassandra.core.query.Filter) Function(java.util.function.Function) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ProjectionInformation(org.springframework.data.projection.ProjectionInformation) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) StatementBuilder(org.springframework.data.cassandra.core.cql.util.StatementBuilder) Nullable(org.springframework.lang.Nullable) Mode(org.springframework.data.cassandra.core.query.Update.AddToOp.Mode) ConditionBuilder(com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder) EntityWriter(org.springframework.data.convert.EntityWriter) RegularInsert(com.datastax.oss.driver.api.querybuilder.insert.RegularInsert) Update(org.springframework.data.cassandra.core.query.Update) CassandraConverter(org.springframework.data.cassandra.core.convert.CassandraConverter) Query(org.springframework.data.cassandra.core.query.Query) QueryOptionsUtil(org.springframework.data.cassandra.core.cql.QueryOptionsUtil) Term(com.datastax.oss.driver.api.querybuilder.term.Term) Collections(java.util.Collections) Where(org.springframework.data.cassandra.core.convert.Where) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) ArrayList(java.util.ArrayList) Select(com.datastax.oss.driver.api.querybuilder.select.Select) Sort(org.springframework.data.domain.Sort) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Selector(org.springframework.data.cassandra.core.query.Columns.Selector)

Aggregations

ColumnSelector (org.springframework.data.cassandra.core.query.Columns.ColumnSelector)7 Selector (org.springframework.data.cassandra.core.query.Columns.Selector)7 Filter (org.springframework.data.cassandra.core.query.Filter)5 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)4 ArrayList (java.util.ArrayList)4 FunctionCall (org.springframework.data.cassandra.core.query.Columns.FunctionCall)4 Collections (java.util.Collections)3 List (java.util.List)3 Optional (java.util.Optional)3 Set (java.util.Set)3 ClusteringOrder (com.datastax.oss.driver.api.core.metadata.schema.ClusteringOrder)2 BindMarker (com.datastax.oss.driver.api.querybuilder.BindMarker)2 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)2 Condition (com.datastax.oss.driver.api.querybuilder.condition.Condition)2 ConditionBuilder (com.datastax.oss.driver.api.querybuilder.condition.ConditionBuilder)2 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)2 DeleteSelection (com.datastax.oss.driver.api.querybuilder.delete.DeleteSelection)2 Insert (com.datastax.oss.driver.api.querybuilder.insert.Insert)2 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)2 ColumnRelationBuilder (com.datastax.oss.driver.api.querybuilder.relation.ColumnRelationBuilder)2