Search in sources :

Example 1 with Columns

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

the class ReactiveCassandraTemplate method doSelect.

<T> Flux<T> doSelect(Query query, Class<?> entityClass, CqlIdentifier tableName, Class<T> returnType) {
    CassandraPersistentEntity<?> persistentEntity = getRequiredPersistentEntity(entityClass);
    EntityProjection<T, ?> projection = entityOperations.introspectProjection(returnType, entityClass);
    Columns columns = getStatementFactory().computeColumnsForProjection(projection, query.getColumns(), persistentEntity, returnType);
    Query queryToUse = query.columns(columns);
    StatementBuilder<Select> select = getStatementFactory().select(queryToUse, persistentEntity, tableName);
    Function<Row, T> mapper = getMapper(projection, tableName);
    return doQuery(select.build(), (row, rowNum) -> mapper.apply(row));
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) Select(com.datastax.oss.driver.api.querybuilder.select.Select) Columns(org.springframework.data.cassandra.core.query.Columns) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 2 with Columns

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

the class StatementFactory method computeColumnsForProjection.

/**
 * Compute the {@link Columns} to include type if the {@code returnType} is a {@literal DTO projection} or a
 * {@literal closed interface projection}.
 *
 * @param columns must not be {@literal null}.
 * @param domainType must not be {@literal null}.
 * @param returnType must not be {@literal null}.
 * @return {@link Columns} with columns to be included.
 * @since 2.2
 */
Columns computeColumnsForProjection(EntityProjection<?, ?> projection, Columns columns, CassandraPersistentEntity<?> domainType, Class<?> returnType) {
    if (!columns.isEmpty() || ClassUtils.isAssignable(domainType.getType(), returnType)) {
        return columns;
    }
    if (projection.getMappedType().getType().isInterface()) {
        projection.forEach(propertyPath -> columns.include(propertyPath.getPropertyPath().getSegment()));
    } else {
        // DTO projections use merged metadata between domain type and result type
        PersistentPropertyTranslator translator = PersistentPropertyTranslator.create(domainType, Predicates.negate(CassandraPersistentProperty::hasExplicitColumnName));
        CassandraPersistentEntity<?> persistentEntity = getQueryMapper().getConverter().getMappingContext().getRequiredPersistentEntity(projection.getMappedType());
        for (CassandraPersistentProperty property : persistentEntity) {
            columns.include(translator.translate(property).getColumnName());
        }
    }
    Columns projectedColumns = Columns.empty();
    if (returnType.isInterface()) {
        ProjectionInformation projectionInformation = cassandraConverter.getProjectionFactory().getProjectionInformation(returnType);
        if (projectionInformation.isClosed()) {
            for (PropertyDescriptor inputProperty : projectionInformation.getInputProperties()) {
                projectedColumns = projectedColumns.include(inputProperty.getName());
            }
        }
    } else {
        for (PersistentProperty<?> property : domainType) {
            projectedColumns = projectedColumns.include(property.getName());
        }
    }
    return projectedColumns;
}
Also used : ProjectionInformation(org.springframework.data.projection.ProjectionInformation) PropertyDescriptor(java.beans.PropertyDescriptor) Columns(org.springframework.data.cassandra.core.query.Columns) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) PersistentPropertyTranslator(org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator)

Example 3 with Columns

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

the class QueryMapperUnitTests method shouldMapColumnWithCompositePrimaryKeyClass.

// DATACASS-343
@Test
void shouldMapColumnWithCompositePrimaryKeyClass() {
    Columns columnNames = Columns.from("key.firstname");
    List<CqlIdentifier> mappedObject = queryMapper.getMappedColumnNames(columnNames, mappingContext.getRequiredPersistentEntity(TypeWithKeyClass.class));
    assertThat(mappedObject).contains(CqlIdentifier.fromCql("first_name"));
}
Also used : Columns(org.springframework.data.cassandra.core.query.Columns) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) TypeWithKeyClass(org.springframework.data.cassandra.domain.TypeWithKeyClass) Test(org.junit.jupiter.api.Test)

Example 4 with Columns

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

the class CassandraTemplate method doSelect.

<T> List<T> doSelect(Query query, Class<?> entityClass, CqlIdentifier tableName, Class<T> returnType) {
    CassandraPersistentEntity<?> entity = getRequiredPersistentEntity(entityClass);
    EntityProjection<T, ?> projection = entityOperations.introspectProjection(returnType, entityClass);
    Columns columns = getStatementFactory().computeColumnsForProjection(projection, query.getColumns(), entity, returnType);
    Query queryToUse = query.columns(columns);
    StatementBuilder<Select> select = getStatementFactory().select(queryToUse, entity, tableName);
    Function<Row, T> mapper = getMapper(projection, tableName);
    return doQuery(select.build(), (row, rowNum) -> mapper.apply(row));
}
Also used : Query(org.springframework.data.cassandra.core.query.Query) Select(com.datastax.oss.driver.api.querybuilder.select.Select) Columns(org.springframework.data.cassandra.core.query.Columns) Row(com.datastax.oss.driver.api.core.cql.Row)

Example 5 with Columns

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

Columns (org.springframework.data.cassandra.core.query.Columns)5 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)2 Row (com.datastax.oss.driver.api.core.cql.Row)2 Select (com.datastax.oss.driver.api.querybuilder.select.Select)2 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)2 Query (org.springframework.data.cassandra.core.query.Query)2 PropertyDescriptor (java.beans.PropertyDescriptor)1 ArrayList (java.util.ArrayList)1 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 Test (org.junit.jupiter.api.Test)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 EmbeddedEntityOperations (org.springframework.data.cassandra.core.mapping.EmbeddedEntityOperations)1 PersistentPropertyTranslator (org.springframework.data.cassandra.core.mapping.PersistentPropertyTranslator)1 ColumnName (org.springframework.data.cassandra.core.query.ColumnName)1