Skip to content

Commit

Permalink
Introduce AggregatePathUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
schauder committed Jun 13, 2023
1 parent d4ac9ed commit cb890c5
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 419 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.data.relational.core.conversion.DbActionExecutionResult;
import org.springframework.data.relational.core.conversion.IdValueSource;
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
Expand Down Expand Up @@ -188,7 +189,7 @@ private Identifier getParentKeys(DbAction.WithDependingOn<?> action, JdbcConvert

private Object getParentId(DbAction.WithDependingOn<?> action) {

DbAction.WithEntity<?> idOwningAction = getIdOwningAction(action, context.getAggregatePath(action.getPropertyPath()).getIdDefiningParentPath());
DbAction.WithEntity<?> idOwningAction = getIdOwningAction(action, AggregatePathUtil.getIdDefiningParentPath(context.getAggregatePath(action.getPropertyPath())));

return getPotentialGeneratedIdFrom(idOwningAction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.springframework.data.relational.core.conversion.BasicRelationalConverter;
import org.springframework.data.relational.core.conversion.RelationalConverter;
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
Expand Down Expand Up @@ -456,8 +457,8 @@ private Object readOrLoadProperty(@Nullable Object id, RelationalPersistentPrope
private Iterable<Object> resolveRelation(@Nullable Object id, RelationalPersistentProperty property) {

Identifier identifier = id == null //
? this.identifier.withPart(rootPath.getQualifierColumn(), key, Object.class) //
: Identifier.of(rootPath.append(property).getReverseColumnName(), id, Object.class);
? this.identifier.withPart(AggregatePathUtil.getQualifierColumn(rootPath), key, Object.class) //
: Identifier.of(AggregatePathUtil.getReverseColumnName(rootPath.append(property)), id, Object.class);

PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = path.append(property)
.getRequiredPersistentPropertyPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.conversion.IdValueSource;
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
Expand Down Expand Up @@ -399,7 +399,7 @@ private EntityRowMapper<?> getEntityRowMapper(AggregatePath path, Identifier ide

private RowMapper<?> getMapEntityRowMapper(AggregatePath path, Identifier identifier) {

SqlIdentifier keyColumn = path.getQualifierColumn();
SqlIdentifier keyColumn = AggregatePathUtil.getQualifierColumn(path);
Assert.notNull(keyColumn, () -> "KeyColumn must not be null for " + path);

return new MapEntityRowMapper<>(path, converter, identifier, keyColumn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.springframework.data.mapping.model.PropertyValueProvider;
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;

/**
Expand Down Expand Up @@ -45,7 +46,7 @@ class JdbcBackReferencePropertyValueProvider implements PropertyValueProvider<Re

@Override
public <T> T getPropertyValue(RelationalPersistentProperty property) {
return (T) resultSet.getObject(basePath.append(property).getReverseColumnNameAlias().getReference());
return (T) resultSet.getObject(AggregatePathUtil.getReverseColumnNameAlias(basePath.append(property)).getReference());
}

public JdbcBackReferencePropertyValueProvider extendBy(RelationalPersistentProperty property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.jdbc.core.convert;

import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -56,9 +57,9 @@ public static JdbcIdentifierBuilder forBackReferences(JdbcConverter converter, A
@Nullable Object value) {

Identifier identifier = Identifier.of( //
path.getReverseColumnName(), //
AggregatePathUtil.getReverseColumnName(path), //
value, //
converter.getColumnType(path.getIdDefiningParentPath().getRequiredIdProperty()) //
converter.getColumnType(AggregatePathUtil.getIdDefiningParentPath(path).getRequiredIdProperty()) //
);

return new JdbcIdentifierBuilder(identifier);
Expand All @@ -77,7 +78,6 @@ public JdbcIdentifierBuilder withQualifier(PersistentPropertyPathExtension path,
return withQualifier(path.getAggregatePath(), value);
}


/**
* Adds a qualifier to the identifier to build. A qualifier is a map key or a list index.
*
Expand All @@ -90,7 +90,8 @@ public JdbcIdentifierBuilder withQualifier(AggregatePath path, Object value) {
Assert.notNull(path, "Path must not be null");
Assert.notNull(value, "Value must not be null");

identifier = identifier.withPart(path.getQualifierColumn(), value, path.getQualifierColumnType());
identifier = identifier.withPart(AggregatePathUtil.getQualifierColumn(path), value,
AggregatePathUtil.getQualifierColumnType(path));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.springframework.data.mapping.model.PropertyValueProvider;
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;

/**
Expand Down Expand Up @@ -57,7 +58,7 @@ public boolean hasProperty(RelationalPersistentProperty property) {
}

private String getColumnName(RelationalPersistentProperty property) {
return basePath.append(property).getColumnAlias().getReference();
return AggregatePathUtil.getColumnAlias(basePath.append(property)).getReference();
}

public JdbcPropertyValueProvider extendBy(RelationalPersistentProperty property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.data.jdbc.core.convert;

import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.SqlIdentifier;
Expand Down Expand Up @@ -55,16 +55,17 @@ Table getTable() {

Table getTable(AggregatePath path) {

SqlIdentifier tableAlias = path.getTableAlias();
Table table = Table.create(path.getQualifiedTableName());
SqlIdentifier tableAlias = AggregatePathUtil.getTableAlias(path);
Table table = Table.create(AggregatePathUtil.getQualifiedTableName(path));
return tableAlias == null ? table : table.as(tableAlias);
}

Column getColumn(AggregatePath path) {
return getTable(path).column(path.getColumnName()).as(path.getColumnAlias());
return getTable(path).column(AggregatePathUtil.getColumnName(path)).as(AggregatePathUtil.getColumnAlias(path));
}

Column getReverseColumn(AggregatePath path) {
return getTable(path).column(path.getReverseColumnName()).as(path.getReverseColumnNameAlias());
return getTable(path).column(AggregatePathUtil.getReverseColumnName(path))
.as(AggregatePathUtil.getReverseColumnNameAlias(path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
Expand Down Expand Up @@ -127,16 +127,16 @@ private Condition getSubselectCondition(AggregatePath path, Function<Column, Con

AggregatePath parentPath = path.getParentPath();

if (!parentPath.hasIdProperty()) {
if (!AggregatePathUtil.hasIdProperty(parentPath)) {
if (parentPath.getLength() > 1) {
return getSubselectCondition(parentPath, rootCondition, filterColumn);
}
return rootCondition.apply(filterColumn);
}

Table subSelectTable = Table.create(parentPath.getQualifiedTableName());
Column idColumn = subSelectTable.column(parentPath.getIdColumnName());
Column selectFilterColumn = subSelectTable.column(parentPath.getEffectiveIdColumnName());
Table subSelectTable = Table.create(AggregatePathUtil.getQualifiedTableName(parentPath));
Column idColumn = subSelectTable.column(AggregatePathUtil.getIdColumnName(parentPath));
Column selectFilterColumn = subSelectTable.column(AggregatePathUtil.getEffectiveIdColumnName(parentPath));

Condition innerCondition;

Expand Down Expand Up @@ -219,7 +219,7 @@ String getFindAllByProperty(Identifier parentIdentifier,

AggregatePath path = mappingContext.getAggregatePath(propertyPath);

return getFindAllByProperty(parentIdentifier, path.getQualifierColumn(), path.isOrdered());
return getFindAllByProperty(parentIdentifier, AggregatePathUtil.getQualifierColumn(path), path.isOrdered());
}

/**
Expand Down Expand Up @@ -561,7 +561,7 @@ Column getColumn(AggregatePath path) {

if (path.isQualified() //
|| path.isCollectionLike() //
|| path.hasIdProperty() //
|| AggregatePathUtil.hasIdProperty(path) //
) {
return null;
}
Expand All @@ -581,13 +581,13 @@ Join getJoin(AggregatePath path) {

Table currentTable = sqlContext.getTable(path);

AggregatePath idDefiningParentPath = path.getIdDefiningParentPath();
AggregatePath idDefiningParentPath = AggregatePathUtil.getIdDefiningParentPath(path);
Table parentTable = sqlContext.getTable(idDefiningParentPath);

return new Join( //
currentTable, //
currentTable.column(path.getReverseColumnName()), //
parentTable.column(idDefiningParentPath.getIdColumnName()) //
currentTable.column(AggregatePathUtil.getReverseColumnName(path)), //
parentTable.column(AggregatePathUtil.getIdColumnName(idDefiningParentPath)) //
);
}

Expand Down Expand Up @@ -710,13 +710,13 @@ private DeleteBuilder.DeleteWhereAndOr createBaseDeleteByIdIn(Table table) {

private String createDeleteByPathAndCriteria(AggregatePath path, Function<Column, Condition> rootCondition) {

Table table = Table.create(path.getQualifiedTableName());
Table table = Table.create(AggregatePathUtil.getQualifiedTableName(path));

DeleteBuilder.DeleteWhere builder = Delete.builder() //
.from(table);
Delete delete;

Column filterColumn = table.column(path.getReverseColumnName());
Column filterColumn = table.column(AggregatePathUtil.getReverseColumnName(path));

if (path.getLength() == 1) {

Expand Down Expand Up @@ -926,7 +926,7 @@ private SelectBuilder.SelectJoin getExistsSelect() {
for (PersistentPropertyPath<RelationalPersistentProperty> path : mappingContext
.findPersistentPropertyPaths(entity.getType(), p -> true)) {

AggregatePath aggregatePath = mappingContext.getAggregatePath( path);
AggregatePath aggregatePath = mappingContext.getAggregatePath(path);

// add a join if necessary
Join join = getJoin(aggregatePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.dialect.RenderContextFactory;
import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
Expand Down Expand Up @@ -296,7 +297,7 @@ private Column getColumn(SqlContext sqlContext, AggregatePath path) {

if (path.isQualified() //
|| path.isCollectionLike() //
|| path.hasIdProperty() //
|| AggregatePathUtil.hasIdProperty(path) //
) {
return null;
}
Expand All @@ -316,13 +317,13 @@ Join getJoin(SqlContext sqlContext, AggregatePath path) {

Table currentTable = sqlContext.getTable(path);

AggregatePath idDefiningParentPath = path.getIdDefiningParentPath();
AggregatePath idDefiningParentPath = AggregatePathUtil.getIdDefiningParentPath(path);
Table parentTable = sqlContext.getTable(idDefiningParentPath);

return new Join( //
currentTable, //
currentTable.column(path.getReverseColumnName()), //
parentTable.column(idDefiningParentPath.getIdColumnName()) //
currentTable.column(AggregatePathUtil.getReverseColumnName(path)), //
parentTable.column(AggregatePathUtil.getIdColumnName(idDefiningParentPath)) //
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.jdbc.repository.query;

import org.springframework.data.relational.core.mapping.AggregatePath;
import org.springframework.data.relational.core.mapping.AggregatePathUtil;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.Column;
Expand Down Expand Up @@ -56,16 +57,16 @@ Table getTable() {

Table getTable(AggregatePath path) {

SqlIdentifier tableAlias = path.getTableAlias();
Table table = Table.create(path.getQualifiedTableName());
SqlIdentifier tableAlias = AggregatePathUtil.getTableAlias(path);
Table table = Table.create(AggregatePathUtil.getQualifiedTableName(path));
return tableAlias == null ? table : table.as(tableAlias);
}

Column getColumn(AggregatePath path) {
return getTable(path).column(path.getColumnName()).as(path.getColumnAlias());
return getTable(path).column(AggregatePathUtil.getColumnName(path)).as(AggregatePathUtil.getColumnAlias(path));
}

Column getReverseColumn(AggregatePath path) {
return getTable(path).column(path.getReverseColumnName()).as(path.getReverseColumnNameAlias());
return getTable(path).column(AggregatePathUtil.getReverseColumnName(path)).as(AggregatePathUtil.getReverseColumnNameAlias(path));
}
}
Loading

0 comments on commit cb890c5

Please sign in to comment.