Search in sources :

Example 6 with PersistentPropertyPathExtension

use of org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension in project spring-data-jdbc by spring-projects.

the class JdbcQueryCreator method getJoin.

@Nullable
Join getJoin(SqlContext sqlContext, PersistentPropertyPathExtension path) {
    if (!path.isEntity() || path.isEmbedded() || path.isMultiValued()) {
        return null;
    }
    Table currentTable = sqlContext.getTable(path);
    PersistentPropertyPathExtension idDefiningParentPath = path.getIdDefiningParentPath();
    Table parentTable = sqlContext.getTable(idDefiningParentPath);
    return new // 
    Join(// 
    currentTable, // 
    currentTable.column(path.getReverseColumnName()), // 
    parentTable.column(idDefiningParentPath.getIdColumnName()));
}
Also used : Table(org.springframework.data.relational.core.sql.Table) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) Nullable(org.springframework.lang.Nullable)

Example 7 with PersistentPropertyPathExtension

use of org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension in project spring-data-jdbc by spring-projects.

the class JdbcQueryCreator method validate.

/**
 * Validate parameters for the derived query. Specifically checking that the query method defines scalar parameters
 * and collection parameters where required and that invalid parameter declarations are rejected.
 *
 * @param tree the tree structure defining the predicate of the query.
 * @param parameters parameters for the predicate.
 */
static void validate(PartTree tree, Parameters<?, ?> parameters, MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context) {
    RelationalQueryCreator.validate(tree, parameters);
    for (PartTree.OrPart parts : tree) {
        for (Part part : parts) {
            PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = context.getPersistentPropertyPath(part.getProperty());
            PersistentPropertyPathExtension path = new PersistentPropertyPathExtension(context, propertyPath);
            for (PersistentPropertyPathExtension pathToValidate = path; path.getLength() > 0; path = path.getParentPath()) {
                validateProperty(pathToValidate);
            }
        }
    }
}
Also used : Part(org.springframework.data.repository.query.parser.Part) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) PartTree(org.springframework.data.repository.query.parser.PartTree)

Example 8 with PersistentPropertyPathExtension

use of org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension in project spring-data-jdbc by spring-projects.

the class JdbcAggregateChangeExecutionContext method getParentKeys.

private Identifier getParentKeys(DbAction.WithDependingOn<?> action, JdbcConverter converter) {
    Object id = getParentId(action);
    JdbcIdentifierBuilder identifier = // 
    JdbcIdentifierBuilder.forBackReferences(converter, new PersistentPropertyPathExtension(context, action.getPropertyPath()), id);
    for (Map.Entry<PersistentPropertyPath<RelationalPersistentProperty>, Object> qualifier : action.getQualifiers().entrySet()) {
        identifier = identifier.withQualifier(new PersistentPropertyPathExtension(context, qualifier.getKey()), qualifier.getValue());
    }
    return identifier.build();
}
Also used : JdbcIdentifierBuilder(org.springframework.data.jdbc.core.convert.JdbcIdentifierBuilder) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) PersistentPropertyPath(org.springframework.data.mapping.PersistentPropertyPath) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with PersistentPropertyPathExtension

use of org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension in project spring-data-jdbc by spring-projects.

the class DefaultDataAccessStrategy method findAllByPath.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.jdbc.core.RelationResolver#findAllByPath(org.springframework.data.jdbc.support.Identifier, org.springframework.data.mapping.PersistentPropertyPath)
	 */
@Override
@SuppressWarnings("unchecked")
public Iterable<Object> findAllByPath(Identifier identifier, PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath) {
    Assert.notNull(identifier, "identifier must not be null.");
    Assert.notNull(propertyPath, "propertyPath must not be null.");
    PersistentPropertyPathExtension path = new PersistentPropertyPathExtension(context, propertyPath);
    Class<?> actualType = path.getActualType();
    String findAllByProperty = // 
    sql(actualType).getFindAllByProperty(identifier, path.getQualifierColumn(), path.isOrdered());
    RowMapper<?> rowMapper = path.isMap() ? this.getMapEntityRowMapper(path, identifier) : this.getEntityRowMapper(path, identifier);
    return operations.query(findAllByProperty, createParameterSource(identifier, getIdentifierProcessing()), (RowMapper<Object>) rowMapper);
}
Also used : PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)

Example 10 with PersistentPropertyPathExtension

use of org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension in project spring-data-jdbc by spring-projects.

the class SqlGenerator method getSubselectCondition.

/**
 * Construct a IN-condition based on a {@link Select Sub-Select} which selects the ids (or stand ins for ids) of the
 * given {@literal path} to those that reference the root entities specified by the {@literal rootCondition}.
 *
 * @param path specifies the table and id to select
 * @param rootCondition the condition on the root of the path determining what to select
 * @param filterColumn the column to apply the IN-condition to.
 * @return the IN condition
 */
private Condition getSubselectCondition(PersistentPropertyPathExtension path, Function<Column, Condition> rootCondition, Column filterColumn) {
    PersistentPropertyPathExtension parentPath = path.getParentPath();
    if (!parentPath.hasIdProperty()) {
        if (parentPath.getLength() > 1) {
            return getSubselectCondition(parentPath, rootCondition, filterColumn);
        }
        return rootCondition.apply(filterColumn);
    }
    Table subSelectTable = Table.create(parentPath.getTableName());
    Column idColumn = subSelectTable.column(parentPath.getIdColumnName());
    Column selectFilterColumn = subSelectTable.column(parentPath.getEffectiveIdColumnName());
    Condition innerCondition;
    if (parentPath.getLength() == 1) {
        // if the parent is the root of the path
        // apply the rootCondition
        innerCondition = rootCondition.apply(selectFilterColumn);
    } else {
        // otherwise we need another layer of subselect
        innerCondition = getSubselectCondition(parentPath, rootCondition, selectFilterColumn);
    }
    Select select = // 
    Select.builder().select(// 
    idColumn).from(// 
    subSelectTable).where(innerCondition).build();
    return filterColumn.in(select);
}
Also used : PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)

Aggregations

PersistentPropertyPathExtension (org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)16 Test (org.junit.jupiter.api.Test)6 RelationalPersistentProperty (org.springframework.data.relational.core.mapping.RelationalPersistentProperty)5 BasicRelationalPersistentProperty (org.springframework.data.relational.core.mapping.BasicRelationalPersistentProperty)3 UUID (java.util.UUID)2 SqlIdentifier (org.springframework.data.relational.core.sql.SqlIdentifier)2 Nullable (org.springframework.lang.Nullable)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 JdbcIdentifierBuilder (org.springframework.data.jdbc.core.convert.JdbcIdentifierBuilder)1 PersistentPropertyPath (org.springframework.data.mapping.PersistentPropertyPath)1 DbAction (org.springframework.data.relational.core.conversion.DbAction)1 Column (org.springframework.data.relational.core.sql.Column)1 Expression (org.springframework.data.relational.core.sql.Expression)1 SelectBuilder (org.springframework.data.relational.core.sql.SelectBuilder)1 Table (org.springframework.data.relational.core.sql.Table)1 Part (org.springframework.data.repository.query.parser.Part)1 PartTree (org.springframework.data.repository.query.parser.PartTree)1