Search in sources :

Example 1 with ColumnSelector

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

use of org.springframework.data.cassandra.core.query.Columns.ColumnSelector 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 3 with ColumnSelector

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

ColumnSelector (org.springframework.data.cassandra.core.query.Columns.ColumnSelector)3 Selector (org.springframework.data.cassandra.core.query.Columns.Selector)3 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 ArrayList (java.util.ArrayList)2 ColumnName (org.springframework.data.cassandra.core.query.ColumnName)2 FunctionCall (org.springframework.data.cassandra.core.query.Columns.FunctionCall)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 Criteria (org.springframework.data.cassandra.core.query.Criteria)1 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)1 Predicate (org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)1