Skip to content

Commit

Permalink
Fix @SqlTableAlias alias name apply to column alias
Browse files Browse the repository at this point in the history
(cherry picked from commit 4f097f6)
  • Loading branch information
mhyeon-lee committed Jan 24, 2021
1 parent a28c9b1 commit e2c60ca
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import lombok.Value;
import lombok.With;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;

/**
* @author Myeonghyeon Lee
*/
Expand All @@ -57,6 +59,7 @@ public class Board {
@Builder.Default
private List<Post> posts = new ArrayList<>();

@SqlTableAlias("b_audit")
@Column("board_id")
private Audit audit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import lombok.Builder;
import lombok.Value;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;
import com.navercorp.spring.data.jdbc.plus.sql.guide.board.Board.Label;
import com.navercorp.spring.data.jdbc.plus.sql.guide.board.Board.Post;

Expand All @@ -44,6 +45,7 @@ public class PostDto {
@Column
Post post;

@SqlTableAlias("p_labels")
@MappedCollection(idColumn = "board_id")
Set<Label> labels;
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class BoardSql extends SqlGeneratorSupport {
LEFT OUTER JOIN n_config AS post_configMap
ON post_configMap.post_id = post.id
LEFT OUTER JOIN n_label AS labels
ON post.board_id = labels.board_id
LEFT OUTER JOIN n_label AS p_labels
ON post.board_id = p_labels.board_id
WHERE post.id = :postId
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import lombok.Value;
import lombok.With;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;

/**
* @author Myeonghyeon Lee
*/
Expand All @@ -56,6 +58,7 @@ public class Board {
@Builder.Default
private List<Post> posts = new ArrayList<>();

@SqlTableAlias("b_audit")
@Column("board_id")
private Audit audit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import lombok.Builder;
import lombok.Value;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;

/**
* @author Myeonghyeon Lee
*/
Expand All @@ -41,6 +43,7 @@ public class PostDto {
@Column
Board.Post post;

@SqlTableAlias("p_labels")
@MappedCollection(idColumn = "board_id")
Set<Board.Label> labels;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class BoardSql : SqlGeneratorSupport() {
LEFT OUTER JOIN n_config AS post_configMap
ON post_configMap.post_id = post.id
LEFT OUTER JOIN n_label AS labels
ON post.board_id = labels.board_id
LEFT OUTER JOIN n_label AS p_labels
ON post.board_id = p_labels.board_id
WHERE post.id = :postId
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.navercorp.spring.data.jdbc.plus.sql.guide.board

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Column
import org.springframework.data.relational.core.mapping.Embedded
Expand All @@ -41,6 +42,7 @@ data class Board(
@MappedCollection(idColumn = "board_id", keyColumn = "board_index")
val posts: List<Post>,

@SqlTableAlias("b_audit")
@Column("board_id")
val audit: Audit?,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.navercorp.spring.data.jdbc.plus.sql.guide.board

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Column
import org.springframework.data.relational.core.mapping.MappedCollection
Expand All @@ -34,6 +35,7 @@ data class PostDto(
@Column
val post: Post,

@SqlTableAlias("p_labels")
@MappedCollection(idColumn = "board_id")
val labels: Set<Label>
)
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class BoardSql : SqlGeneratorSupport() {
LEFT OUTER JOIN n_config AS post_configMap
ON post_configMap.post_id = post.id
LEFT OUTER JOIN n_label AS labels
ON post.board_id = labels.board_id
LEFT OUTER JOIN n_label AS p_labels
ON post.board_id = p_labels.board_id
WHERE post.id = :postId
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,14 @@ private Identifier getRelationEntityIdentifier(
return Identifier.of(relationPath.getReverseColumnName(), id, Object.class);
}

private String getIdColumnAlias(PersistentPropertyPathExtension relationPath) {
return relationPath.extendBy(relationPath.getLeafEntity().getRequiredIdProperty())
.getColumnAlias().getReference(identifierProcessing);
protected String getIdColumnAlias(PersistentPropertyPathExtension relationPath) {
return PropertyPathUtils.getColumnAlias(
relationPath.extendBy(relationPath.getLeafEntity().getRequiredIdProperty())
).getReference(identifierProcessing);
}

private String getQualifierColumnAlias(PersistentPropertyPathExtension relationPath) {
return relationPath.getTableAlias().getReference(identifierProcessing)
protected String getQualifierColumnAlias(PersistentPropertyPathExtension relationPath) {
return PropertyPathUtils.getTableAlias(relationPath).getReference(identifierProcessing)
+ "_" + relationPath.getQualifierColumn().getReference(identifierProcessing);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ class JdbcBackReferencePropertyValueProvider implements PropertyValueProvider<Re

@Override
public <T> T getPropertyValue(RelationalPersistentProperty property) {
return (T)resultSet.getObject(basePath.extendBy(property)
.getReverseColumnNameAlias().getReference(identifierProcessing));
PersistentPropertyPathExtension path = basePath.extendBy(property);

return (T)resultSet.getObject(PropertyPathUtils.getReverseColumnAlias(path)
.getReference(identifierProcessing));
}

public JdbcBackReferencePropertyValueProvider extendBy(RelationalPersistentProperty property) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public boolean hasProperty(RelationalPersistentProperty property) {
}

private String getColumnName(RelationalPersistentProperty property) {
return basePath.extendBy(property).getColumnAlias().getReference(identifierProcessing);
PersistentPropertyPathExtension path = basePath.extendBy(property);
return PropertyPathUtils.getColumnAlias(path).getReference(identifierProcessing);
}

public JdbcPropertyValueProvider extendBy(RelationalPersistentProperty property) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.navercorp.spring.data.jdbc.plus.sql.convert;

import javax.annotation.Nullable;

import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.util.Assert;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;

public class PropertyPathUtils {
static SqlIdentifier getColumnAlias(PersistentPropertyPathExtension path) {
return getColumnAlias(path, path.getColumnName());
}

static SqlIdentifier getColumnAlias(PersistentPropertyPathExtension path, SqlIdentifier columnName) {
PersistentPropertyPathExtension tableOwner = getTableOwningAncestor(path);
if (tableOwner.getLength() == 0) {
return columnName;
} else {
SqlIdentifier tableAlias = getTableAliasFromTableOwner(tableOwner);
return tableAlias == null ? columnName
: columnName.transform(name -> tableAlias.getReference(IdentifierProcessing.NONE) + "_" + name);
}
}

static SqlIdentifier getReverseColumnAlias(PersistentPropertyPathExtension path) {
return getReverseColumnAlias(path, path.getReverseColumnName());
}

static SqlIdentifier getReverseColumnAlias(
PersistentPropertyPathExtension path, SqlIdentifier reverseColumnName
) {
SqlIdentifier tableAlias = getTableAlias(path);
return tableAlias == null ? reverseColumnName
: reverseColumnName.transform(name -> tableAlias.getReference(IdentifierProcessing.NONE) + "_" + name);
}

// Refer from PersistentPropertyPathExtension#getTableAlias
@Nullable
public static SqlIdentifier getTableAlias(PersistentPropertyPathExtension path) {
PersistentPropertyPathExtension tableOwner = getTableOwningAncestor(path);
return getTableAliasFromTableOwner(tableOwner);
}

@Nullable
private static SqlIdentifier getTableAliasFromTableOwner(PersistentPropertyPathExtension tableOwner) {
if (tableOwner.getLength() > 0) { // path != null
return assembleTableAlias(tableOwner);
}

// path == null : root
SqlTableAlias sqlTableAlias = tableOwner.getLeafEntity().findAnnotation(SqlTableAlias.class);
if (sqlTableAlias != null) {
return SqlIdentifier.quoted(sqlTableAlias.value());
}

return null;
}

private static PersistentPropertyPathExtension getTableOwningAncestor(PersistentPropertyPathExtension path) {
return path.isEntity() && !path.isEmbedded() ? path : getTableOwningAncestor(path.getParentPath());
}

private static SqlIdentifier assembleTableAlias(PersistentPropertyPathExtension path) {

Assert.state(path != null, "Path is null");

PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = path.getRequiredPersistentPropertyPath();
RelationalPersistentProperty leafProperty = propertyPath.getRequiredLeafProperty();

String prefix;
if (path.isEmbedded()) {
prefix = leafProperty.getEmbeddedPrefix();
} else {
SqlTableAlias sqlTableAlias = leafProperty.findPropertyOrOwnerAnnotation(SqlTableAlias.class);
prefix = sqlTableAlias != null
? sqlTableAlias.value()
: leafProperty.getName();
}

if (path.getLength() == 1) {
Assert.notNull(prefix, "Prefix mus not be null.");
return SqlIdentifier.quoted(prefix);
}

PersistentPropertyPathExtension parentPath = path.getParentPath();
SqlIdentifier sqlIdentifier = assembleTableAlias(parentPath);

return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix))
: sqlIdentifier.transform(name -> name + "_" + prefix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,11 @@
*/
package com.navercorp.spring.data.jdbc.plus.sql.convert;

import javax.annotation.Nullable;

import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.Column;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.Table;
import org.springframework.util.Assert;

import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;

/**
* Utility to get from path to SQL DSL elements.
Expand Down Expand Up @@ -67,66 +60,22 @@ public Table getTable() {

@Override
public Table getTable(PersistentPropertyPathExtension path) {
SqlIdentifier tableAlias = this.getTableAlias(path);
SqlIdentifier tableAlias = PropertyPathUtils.getTableAlias(path);
Table table = Table.create(path.getTableName());
return tableAlias == null ? table : table.as(tableAlias);
}

@Override
public Column getColumn(PersistentPropertyPathExtension path) {
return getTable(path).column(path.getColumnName()).as(path.getColumnAlias());
SqlIdentifier columnName = path.getColumnName();
SqlIdentifier columnAlias = PropertyPathUtils.getColumnAlias(path, columnName);
return getTable(path).column(columnName).as(columnAlias);
}

@Override
public Column getReverseColumn(PersistentPropertyPathExtension path) {
return getTable(path).column(path.getReverseColumnName()).as(path.getReverseColumnNameAlias());
}

// Refer from PersistentPropertyPathExtension#getTableAlias
@Nullable
private SqlIdentifier getTableAlias(PersistentPropertyPathExtension path) {
PersistentPropertyPathExtension tableOwner = getTableOwningAncestor(path);
if (tableOwner.getLength() > 0) { // path != null
return this.assembleTableAlias(tableOwner);
}

// path == null : root
SqlTableAlias sqlTableAlias = tableOwner.getLeafEntity().findAnnotation(SqlTableAlias.class);
if (sqlTableAlias != null) {
return this.table.as(sqlTableAlias.value()).getReferenceName();
}

return null;
}

private PersistentPropertyPathExtension getTableOwningAncestor(PersistentPropertyPathExtension path) {
return path.isEntity() && !path.isEmbedded() ? path : this.getTableOwningAncestor(path.getParentPath());
}

private SqlIdentifier assembleTableAlias(PersistentPropertyPathExtension path) {

Assert.state(path != null, "Path is null");

PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = path.getRequiredPersistentPropertyPath();
RelationalPersistentProperty leafProperty = propertyPath.getRequiredLeafProperty();

String prefix;
if (path.isEmbedded()) {
prefix = leafProperty.getEmbeddedPrefix();
} else {
SqlTableAlias sqlTableAlias = leafProperty.findPropertyOrOwnerAnnotation(SqlTableAlias.class);
prefix = sqlTableAlias != null ? sqlTableAlias.value() : leafProperty.getName();
}

if (path.getLength() == 1) {
Assert.notNull(prefix, "Prefix mus not be null.");
return SqlIdentifier.quoted(prefix);
}

PersistentPropertyPathExtension parentPath = path.getParentPath();
SqlIdentifier sqlIdentifier = this.assembleTableAlias(parentPath);

return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix))
: sqlIdentifier.transform(name -> name + "_" + prefix);
SqlIdentifier reverseColumnName = path.getReverseColumnName();
SqlIdentifier reverseColumnAlias = PropertyPathUtils.getReverseColumnAlias(path, reverseColumnName);
return getTable(path).column(reverseColumnName).as(reverseColumnAlias);
}
}
Loading

0 comments on commit e2c60ca

Please sign in to comment.