Search in sources :

Example 6 with CassandraPersistentEntity

use of org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity 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 7 with CassandraPersistentEntity

use of org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity 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 8 with CassandraPersistentEntity

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

CassandraPersistentEntity (org.springframework.data.cassandra.core.mapping.CassandraPersistentEntity)8 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)7 Collections (java.util.Collections)7 List (java.util.List)7 CassandraPersistentProperty (org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty)7 ArrayList (java.util.ArrayList)6 Optional (java.util.Optional)6 Set (java.util.Set)6 Nullable (org.springframework.lang.Nullable)6 Assert (org.springframework.util.Assert)6 QueryBuilder (com.datastax.oss.driver.api.querybuilder.QueryBuilder)5 Delete (com.datastax.oss.driver.api.querybuilder.delete.Delete)5 Insert (com.datastax.oss.driver.api.querybuilder.insert.Insert)5 RegularInsert (com.datastax.oss.driver.api.querybuilder.insert.RegularInsert)5 Select (com.datastax.oss.driver.api.querybuilder.select.Select)5 Function (java.util.function.Function)5 Collectors (java.util.stream.Collectors)5 CassandraConverter (org.springframework.data.cassandra.core.convert.CassandraConverter)5 StatementBuilder (org.springframework.data.cassandra.core.cql.util.StatementBuilder)5 Columns (org.springframework.data.cassandra.core.query.Columns)5