Search in sources :

Example 6 with CassandraPersistentProperty

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;
}
Also used : ArrayList(java.util.ArrayList) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier)

Example 7 with CassandraPersistentProperty

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);
}
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) ArrayList(java.util.ArrayList) CriteriaDefinition(org.springframework.data.cassandra.core.query.CriteriaDefinition) Predicate(org.springframework.data.cassandra.core.query.CriteriaDefinition.Predicate)

Example 8 with CassandraPersistentProperty

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

Example 9 with CassandraPersistentProperty

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;
}
Also used : ArrayList(java.util.ArrayList) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) CreateIndexSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateIndexSpecification) Indexed(org.springframework.data.cassandra.core.mapping.Indexed)

Example 10 with CassandraPersistentProperty

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;
}
Also used : CreateTableSpecification(org.springframework.data.cassandra.core.cql.keyspace.CreateTableSpecification) CassandraPersistentEntity(org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity) DataType(com.datastax.oss.driver.api.core.type.DataType) CassandraPersistentProperty(org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty) MappingException(org.springframework.data.mapping.MappingException)

Aggregations

CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)11 ArrayList (java.util.ArrayList)4 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)3 CassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity)3 EmbeddedEntityOperations (org.springframework.data.cassandra.core.mapping.EmbeddedEntityOperations)3 Columns (org.springframework.data.cassandra.core.query.Columns)3 CriteriaDefinition (org.springframework.data.cassandra.core.query.CriteriaDefinition)3 Filter (org.springframework.data.cassandra.core.query.Filter)3 Nullable (org.springframework.lang.Nullable)3 DataType (com.datastax.oss.driver.api.core.type.DataType)2 Collections (java.util.Collections)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Optional (java.util.Optional)2 Set (java.util.Set)2 CreateUserTypeSpecification (org.springframework.data.cassandra.core.cql.keyspace.CreateUserTypeSpecification)2 BasicCassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.BasicCassandraPersistentEntity)2 CassandraMappingContext (org.springframework.data.cassandra.core.mapping.CassandraMappingContext)2 ColumnName (org.springframework.data.cassandra.core.query.ColumnName)2 ColumnSelector (org.springframework.data.cassandra.core.query.Columns.ColumnSelector)2