Search in sources :

Example 1 with PersistentPropertyPathExtension

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

the class JdbcAggregateChangeExecutionContext method getParentId.

private Object getParentId(DbAction.WithDependingOn<?> action) {
    PersistentPropertyPathExtension path = new PersistentPropertyPathExtension(context, action.getPropertyPath());
    PersistentPropertyPathExtension idPath = path.getIdDefiningParentPath();
    DbAction.WithEntity<?> idOwningAction = getIdOwningAction(action, idPath);
    return getPotentialGeneratedIdFrom(idOwningAction);
}
Also used : DbAction(org.springframework.data.relational.core.conversion.DbAction) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension)

Example 2 with PersistentPropertyPathExtension

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

the class JdbcQueryCreator method selectBuilder.

private SelectBuilder.SelectJoin selectBuilder(Table table) {
    List<Expression> columnExpressions = new ArrayList<>();
    RelationalPersistentEntity<?> entity = entityMetadata.getTableEntity();
    SqlContext sqlContext = new SqlContext(entity);
    List<Join> joinTables = new ArrayList<>();
    for (PersistentPropertyPath<RelationalPersistentProperty> path : context.findPersistentPropertyPaths(entity.getType(), p -> true)) {
        PersistentPropertyPathExtension extPath = new PersistentPropertyPathExtension(context, path);
        if (returnedType.needsCustomConstruction()) {
            if (!returnedType.getInputProperties().contains(extPath.getRequiredPersistentPropertyPath().getBaseProperty().getName())) {
                continue;
            }
        }
        // add a join if necessary
        Join join = getJoin(sqlContext, extPath);
        if (join != null) {
            joinTables.add(join);
        }
        Column column = getColumn(sqlContext, extPath);
        if (column != null) {
            columnExpressions.add(column);
        }
    }
    SelectBuilder.SelectAndFrom selectBuilder = StatementBuilder.select(columnExpressions);
    SelectBuilder.SelectJoin baseSelect = selectBuilder.from(table);
    for (Join join : joinTables) {
        baseSelect = baseSelect.leftOuterJoin(join.joinTable).on(join.joinColumn).equals(join.parentId);
    }
    return baseSelect;
}
Also used : ArrayList(java.util.ArrayList) Expression(org.springframework.data.relational.core.sql.Expression) Column(org.springframework.data.relational.core.sql.Column) PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty) SelectBuilder(org.springframework.data.relational.core.sql.SelectBuilder)

Example 3 with PersistentPropertyPathExtension

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

the class SqlGenerator method selectBuilder.

private SelectBuilder.SelectWhere selectBuilder(Collection<SqlIdentifier> keyColumns) {
    Table table = getTable();
    List<Expression> columnExpressions = new ArrayList<>();
    List<Join> joinTables = new ArrayList<>();
    for (PersistentPropertyPath<RelationalPersistentProperty> path : mappingContext.findPersistentPropertyPaths(entity.getType(), p -> true)) {
        PersistentPropertyPathExtension extPath = new PersistentPropertyPathExtension(mappingContext, path);
        // add a join if necessary
        Join join = getJoin(extPath);
        if (join != null) {
            joinTables.add(join);
        }
        Column column = getColumn(extPath);
        if (column != null) {
            columnExpressions.add(column);
        }
    }
    for (SqlIdentifier keyColumn : keyColumns) {
        columnExpressions.add(table.column(keyColumn).as(keyColumn));
    }
    SelectBuilder.SelectAndFrom selectBuilder = StatementBuilder.select(columnExpressions);
    SelectBuilder.SelectJoin baseSelect = selectBuilder.from(table);
    for (Join join : joinTables) {
        baseSelect = baseSelect.leftOuterJoin(join.joinTable).on(join.joinColumn).equals(join.parentId);
    }
    return (SelectBuilder.SelectWhere) baseSelect;
}
Also used : PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) RelationalPersistentProperty(org.springframework.data.relational.core.mapping.RelationalPersistentProperty)

Example 4 with PersistentPropertyPathExtension

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

the class SqlGenerator method getJoin.

@Nullable
Join getJoin(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 : PersistentPropertyPathExtension(org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension) Nullable(org.springframework.lang.Nullable)

Example 5 with PersistentPropertyPathExtension

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

the class SqlGenerator method createDeleteAllSql.

/**
 * Create a {@code DELETE} query and optionally filter by {@link PersistentPropertyPath}.
 *
 * @param path can be {@literal null}.
 * @return the statement as a {@link String}. Guaranteed to be not {@literal null}.
 */
String createDeleteAllSql(@Nullable PersistentPropertyPath<RelationalPersistentProperty> path) {
    Table table = getTable();
    DeleteBuilder.DeleteWhere deleteAll = Delete.builder().from(table);
    if (path == null) {
        return render(deleteAll.build());
    }
    return createDeleteByPathAndCriteria(new PersistentPropertyPathExtension(mappingContext, path), Column::isNotNull);
}
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