use of org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty in project spring-data-cassandra by spring-projects.
the class QueryMapper method getCqlIdentifier.
private List<CqlIdentifier> getCqlIdentifier(ColumnName column, Field field) {
List<CqlIdentifier> identifiers = new ArrayList<>(1);
try {
if (field.getProperty().isPresent()) {
CassandraPersistentProperty property = field.getProperty().get();
if (property.isCompositePrimaryKey()) {
BasicCassandraPersistentEntity<?> primaryKeyEntity = mappingContext.getRequiredPersistentEntity(property);
primaryKeyEntity.forEach(it -> {
identifiers.add(it.getRequiredColumnName());
});
} else {
identifiers.add(property.getRequiredColumnName());
}
} else if (column.getColumnName().isPresent()) {
identifiers.add(CqlIdentifier.fromCql(column.getColumnName().get()));
} else {
column.getCqlIdentifier().ifPresent(identifiers::add);
}
} catch (IllegalStateException cause) {
throw new IllegalArgumentException(cause.getMessage(), cause);
}
return identifiers;
}
use of org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty 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);
}
use of org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty 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;
}
use of org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty in project spring-data-cassandra by spring-projects.
the class SchemaFactory method getCreateIndexSpecificationsFor.
/**
* Returns {@link CreateIndexSpecification index specifications} derived from {@link CassandraPersistentEntity} using
* {@link CqlIdentifier table name}.
*
* @param entity must not be {@literal null}.
* @param tableName must not be {@literal null}.
* @return
* @since 2.0
*/
public List<CreateIndexSpecification> getCreateIndexSpecificationsFor(CassandraPersistentEntity<?> entity, CqlIdentifier tableName) {
Assert.notNull(entity, "CassandraPersistentEntity must not be null");
Assert.notNull(tableName, "Table name must not be null");
List<CreateIndexSpecification> indexes = new ArrayList<>();
for (CassandraPersistentProperty property : entity) {
if (property.isCompositePrimaryKey()) {
indexes.addAll(getCreateIndexSpecificationsFor(mappingContext.getRequiredPersistentEntity(property)));
}
if (property.isEmbedded()) {
if (property.isAnnotationPresent(Indexed.class)) {
Indexed indexed = property.findAnnotation(Indexed.class);
for (CassandraPersistentProperty embeddedProperty : embeddedEntityOperations.getEntity(property)) {
indexes.add(IndexSpecificationFactory.createIndexSpecification(indexed, embeddedProperty));
}
} else {
indexes.addAll(getCreateIndexSpecificationsFor(embeddedEntityOperations.getEntity(property)));
}
} else {
indexes.addAll(IndexSpecificationFactory.createIndexSpecifications(property));
}
}
indexes.forEach(it -> it.tableName(entity.getTableName()));
return indexes;
}
use of org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty in project spring-data-cassandra by spring-projects.
the class SchemaFactory method getCreateTableSpecificationFor.
/**
* Returns a {@link CreateTableSpecification} for the given entity using {@link CqlIdentifier table name}, including
* all mapping information.
*
* @param entity must not be {@literal null}.
* @param tableName must not be {@literal null}.
* @return
* @since 2.2
*/
public CreateTableSpecification getCreateTableSpecificationFor(CassandraPersistentEntity<?> entity, CqlIdentifier tableName) {
Assert.notNull(tableName, "Table name must not be null");
Assert.notNull(entity, "CassandraPersistentEntity must not be null");
CreateTableSpecification specification = createTable(tableName);
for (CassandraPersistentProperty property : entity) {
if (property.isCompositePrimaryKey()) {
CassandraPersistentEntity<?> primaryKeyEntity = mappingContext.getRequiredPersistentEntity(property.getRawType());
for (CassandraPersistentProperty primaryKeyProperty : primaryKeyEntity) {
DataType dataType = getDataType(primaryKeyProperty);
if (primaryKeyProperty.isPartitionKeyColumn()) {
specification.partitionKeyColumn(primaryKeyProperty.getRequiredColumnName(), dataType);
} else {
// cluster column
specification.clusteredKeyColumn(primaryKeyProperty.getRequiredColumnName(), dataType, primaryKeyProperty.getPrimaryKeyOrdering());
}
}
} else if (property.isEmbedded()) {
CassandraPersistentEntity<?> embeddedEntity = embeddedEntityOperations.getEntity(property);
for (CassandraPersistentProperty embeddedProperty : embeddedEntity) {
DataType dataType = getDataType(embeddedProperty);
specification.column(embeddedProperty.getRequiredColumnName(), dataType);
}
} else {
DataType type = UserTypeUtil.potentiallyFreeze(getDataType(property));
if (property.isIdProperty() || property.isPartitionKeyColumn()) {
specification.partitionKeyColumn(property.getRequiredColumnName(), type);
} else if (property.isClusterKeyColumn()) {
specification.clusteredKeyColumn(property.getRequiredColumnName(), type, property.getPrimaryKeyOrdering());
} else if (property.isStaticColumn()) {
specification.staticColumn(property.getRequiredColumnName(), type);
} else {
specification.column(property.getRequiredColumnName(), type);
}
}
}
if (specification.getPartitionKeyColumns().isEmpty()) {
throw new MappingException(String.format("No partition key columns found in entity [%s]", entity.getType()));
}
return specification;
}
Aggregations