Search in sources :

Example 26 with Filter

use of org.springframework.data.cassandra.core.query.Filter 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)

Example 27 with Filter

use of org.springframework.data.cassandra.core.query.Filter 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;
}
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) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) Filter(org.springframework.data.cassandra.core.query.Filter) Update(org.springframework.data.cassandra.core.query.Update)

Example 28 with Filter

use of org.springframework.data.cassandra.core.query.Filter 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;
}
Also used : Delete(com.datastax.oss.driver.api.querybuilder.delete.Delete) 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) WriteOptions(org.springframework.data.cassandra.core.cql.WriteOptions) Where(org.springframework.data.cassandra.core.convert.Where)

Example 29 with Filter

use of org.springframework.data.cassandra.core.query.Filter in project spring-data-cassandra by spring-projects.

the class StatementFactory method update.

/**
 * Create an {@literal UPDATE} statement by mapping {@code objectToUpdate} to {@link Update} considering
 * {@link UpdateOptions}.
 *
 * @param objectToUpdate must not be {@literal null}.
 * @param options must not be {@literal null}.
 * @param entity must not be {@literal null}.
 * @param tableName must not be {@literal null}.
 * @return the update builder.
 */
StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> update(Object objectToUpdate, WriteOptions options, CassandraPersistentEntity<?> entity, CqlIdentifier tableName) {
    Assert.notNull(tableName, "TableName must not be null");
    Assert.notNull(objectToUpdate, "Object to builder must not be null");
    Assert.notNull(options, "WriteOptions must not be null");
    Assert.notNull(entity, "CassandraPersistentEntity must not be null");
    Where where = new Where();
    cassandraConverter.write(objectToUpdate, where, entity);
    Map<CqlIdentifier, Object> object = new LinkedHashMap<>();
    cassandraConverter.write(objectToUpdate, object, entity);
    where.forEach((cqlIdentifier, o) -> object.remove(cqlIdentifier));
    StatementBuilder<com.datastax.oss.driver.api.querybuilder.update.Update> builder = StatementBuilder.of(QueryBuilder.update(tableName).set().where()).bind((statement, factory) -> ((UpdateWithAssignments) statement).set(toAssignments(object, factory)).where(toRelations(where, factory))).apply(update -> addWriteOptions(update, options));
    Optional.of(options).filter(UpdateOptions.class::isInstance).map(UpdateOptions.class::cast).map(UpdateOptions::getIfCondition).ifPresent(criteriaDefinitions -> applyUpdateIfCondition(builder, criteriaDefinitions));
    builder.transform(statement -> QueryOptionsUtil.addQueryOptions(statement, options));
    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) Update(org.springframework.data.cassandra.core.query.Update) Where(org.springframework.data.cassandra.core.convert.Where) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with Filter

use of org.springframework.data.cassandra.core.query.Filter in project spring-data-cassandra by spring-projects.

the class QueryMapper method getMappedObject.

/**
 * Map a {@link Filter} with a {@link CassandraPersistentEntity type hint}. Filter mapping translates property names
 * to column names and maps {@link Predicate} values to simple Cassandra values.
 *
 * @param filter must not be {@literal null}.
 * @param entity must not be {@literal null}.
 * @return the mapped {@link Filter}.
 */
public Filter getMappedObject(Filter filter, CassandraPersistentEntity<?> entity) {
    Assert.notNull(filter, "Filter must not be null");
    Assert.notNull(entity, "Entity must not be null");
    List<CriteriaDefinition> result = new ArrayList<>();
    for (CriteriaDefinition criteriaDefinition : filter) {
        Field field = createPropertyField(entity, criteriaDefinition.getColumnName());
        field.getProperty().filter(CassandraPersistentProperty::isCompositePrimaryKey).ifPresent(it -> {
            throw new IllegalArgumentException("Cannot use composite primary key directly. Reference a property of the composite primary key");
        });
        field.getProperty().filter(it -> it.getOrdinal() != null).ifPresent(it -> {
            throw new IllegalArgumentException(String.format("Cannot reference tuple value elements, property [%s]", field.getMappedKey()));
        });
        Predicate predicate = criteriaDefinition.getPredicate();
        Object value = predicate.getValue();
        ColumnType typeDescriptor = getColumnType(field, value, ColumnTypeTransformer.of(field, predicate.getOperator()));
        Object mappedValue = value != null ? getConverter().convertToColumnType(value, typeDescriptor) : null;
        Predicate mappedPredicate = new Predicate(predicate.getOperator(), mappedValue);
        result.add(Criteria.of(field.getMappedKey(), mappedPredicate));
    }
    return Filter.from(result);
}
Also used : Order(org.springframework.data.domain.Sort.Order) PropertyReferenceException(org.springframework.data.mapping.PropertyReferenceException) CassandraMappingContext(org.springframework.data.cassandra.core.mapping.CassandraMappingContext) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MappingContext(org.springframework.data.mapping.context.MappingContext) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Filter(org.springframework.data.cassandra.core.query.Filter) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) ArrayList(java.util.ArrayList) PropertyHandler(org.springframework.data.mapping.PropertyHandler) HashSet(java.util.HashSet) FunctionCall(org.springframework.data.cassandra.core.query.Columns.FunctionCall) Sort(org.springframework.data.domain.Sort) Nullable(org.springframework.lang.Nullable) EmbeddedEntityOperations(org.springframework.data.cassandra.core.mapping.EmbeddedEntityOperations) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) PersistentProperty(org.springframework.data.mapping.PersistentProperty) BasicCassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity) Criteria(org.springframework.data.cassandra.core.query.Criteria) Set(java.util.Set) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate) ColumnName(org.springframework.data.cassandra.core.query.ColumnName) Selector(org.springframework.data.cassandra.core.query.Columns.Selector) PersistentPropertyPath(org.springframework.data.mapping.PersistentPropertyPath) List(java.util.List) Optional(java.util.Optional) PropertyPath(org.springframework.data.mapping.PropertyPath) Collections(java.util.Collections) Columns(org.springframework.data.cassandra.core.query.Columns) Assert(org.springframework.util.Assert) ArrayList(java.util.ArrayList) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)

Aggregations

Filter (org.springframework.data.cassandra.core.query.Filter)31 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)21 Query (org.springframework.data.cassandra.core.query.Query)21 Test (org.junit.jupiter.api.Test)20 ColumnSelector (org.springframework.data.cassandra.core.query.Columns.ColumnSelector)10 Selector (org.springframework.data.cassandra.core.query.Columns.Selector)10 Update (org.springframework.data.cassandra.core.query.Update)9 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)8 ArrayList (java.util.ArrayList)8 Collections (java.util.Collections)8 List (java.util.List)8 Optional (java.util.Optional)8 Set (java.util.Set)8 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)8 Collection (java.util.Collection)7 CassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity)7 Columns (org.springframework.data.cassandra.core.query.Columns)7 FunctionCall (org.springframework.data.cassandra.core.query.Columns.FunctionCall)7 Predicate (org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)7 Sort (org.springframework.data.domain.Sort)7