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));
}
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;
}
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"));
}
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));
}
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;
}
Aggregations