Search in sources :

Example 1 with ColumnName

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

the class QueryMapper method getMappedSelectors.

/**
 * Map {@link Columns} with a {@link CassandraPersistentEntity type hint} to {@link ColumnSelector}s.
 *
 * @param columns must not be {@literal null}.
 * @param entity must not be {@literal null}.
 * @return the mapped {@link Selector}s.
 */
public List<Selector> getMappedSelectors(Columns columns, CassandraPersistentEntity<?> entity) {
    Assert.notNull(columns, "Columns must not be null");
    Assert.notNull(entity, "CassandraPersistentEntity must not be null");
    if (columns.isEmpty()) {
        return Collections.emptyList();
    }
    List<Selector> selectors = new ArrayList<>();
    for (ColumnName column : columns) {
        Field field = createPropertyField(entity, column);
        columns.getSelector(column).ifPresent(selector -> {
            List<CqlIdentifier> mappedColumnNames = getCqlIdentifier(column, field);
            for (CqlIdentifier mappedColumnName : mappedColumnNames) {
                selectors.add(getMappedSelector(selector, mappedColumnName));
            }
        });
    }
    if (columns.isEmpty()) {
        addColumns(entity, selectors);
    }
    return selectors;
}
Also used : ColumnName(org.springframework.data.cassandra.core.query.ColumnName) ArrayList(java.util.ArrayList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) Selector(org.springframework.data.cassandra.core.query.Columns.Selector)

Example 2 with ColumnName

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

the class QueryMapper method getMappedSort.

public Sort getMappedSort(Sort sort, CassandraPersistentEntity<?> entity) {
    Assert.notNull(sort, "Sort must not be null");
    Assert.notNull(entity, "CassandraPersistentEntity must not be null");
    if (!sort.iterator().hasNext()) {
        return sort;
    }
    List<Order> mappedOrders = new ArrayList<>();
    for (Order order : sort) {
        ColumnName columnName = ColumnName.from(order.getProperty());
        Field field = createPropertyField(entity, columnName);
        List<CqlIdentifier> mappedColumnNames = getCqlIdentifier(columnName, field);
        if (mappedColumnNames.isEmpty()) {
            mappedOrders.add(order);
        } else {
            for (CqlIdentifier mappedColumnName : mappedColumnNames) {
                mappedOrders.add(new Order(order.getDirection(), mappedColumnName.toString()));
            }
        }
    }
    return Sort.by(mappedOrders);
}
Also used : Order(org.springframework.data.domain.Sort.Order) ColumnName(org.springframework.data.cassandra.core.query.ColumnName) ArrayList(java.util.ArrayList) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 3 with ColumnName

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

the class QueryMapper method getMappedColumnNames.

/**
 * Map {@link Columns} with a {@link CassandraPersistentEntity type hint} to column names for included columns.
 * Function call selectors or other {@link org.springframework.data.cassandra.core.query.Columns.Selector} types are
 * not included.
 *
 * @param columns must not be {@literal null}.
 * @param entity must not be {@literal null}.
 * @return the mapped column names.
 */
public List<CqlIdentifier> getMappedColumnNames(Columns columns, CassandraPersistentEntity<?> entity) {
    Assert.notNull(columns, "Columns must not be null");
    Assert.notNull(entity, "CassandraPersistentEntity must not be null");
    if (columns.isEmpty()) {
        return Collections.emptyList();
    }
    List<CqlIdentifier> columnNames = new ArrayList<>();
    Set<PersistentProperty<?>> seen = new HashSet<>();
    for (ColumnName column : columns) {
        Field field = createPropertyField(entity, column);
        field.getProperty().ifPresent(seen::add);
        columns.getSelector(column).filter(selector -> selector instanceof ColumnSelector).ifPresent(columnSelector -> columnNames.addAll(getCqlIdentifier(column, field)));
    }
    if (columns.isEmpty()) {
        entity.doWithProperties((PropertyHandler<CassandraPersistentProperty>) property -> {
            if (property.isCompositePrimaryKey()) {
                return;
            }
            if (seen.add(property)) {
                columnNames.add(property.getRequiredColumnName());
            }
        });
    }
    return columnNames;
}
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) ColumnName(org.springframework.data.cassandra.core.query.ColumnName) ColumnSelector(org.springframework.data.cassandra.core.query.Columns.ColumnSelector) ArrayList(java.util.ArrayList) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) PersistentProperty(org.springframework.data.mapping.PersistentProperty) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) HashSet(java.util.HashSet)

Aggregations

CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 ArrayList (java.util.ArrayList)3 ColumnName (org.springframework.data.cassandra.core.query.ColumnName)3 ColumnSelector (org.springframework.data.cassandra.core.query.Columns.ColumnSelector)2 Selector (org.springframework.data.cassandra.core.query.Columns.Selector)2 Order (org.springframework.data.domain.Sort.Order)2 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 BasicCassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity)1 CassandraMappingContext (org.springframework.data.cassandra.core.mapping.CassandraMappingContext)1 CassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity)1 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)1 EmbeddedEntityOperations (org.springframework.data.cassandra.core.mapping.EmbeddedEntityOperations)1 Columns (org.springframework.data.cassandra.core.query.Columns)1 FunctionCall (org.springframework.data.cassandra.core.query.Columns.FunctionCall)1 Criteria (org.springframework.data.cassandra.core.query.Criteria)1 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)1