Skip to content

Commit b74762b

Browse files
author
mhyeon-lee
committed
Add @SqlTableAlias for generate columns table alias
(cherry picked from commit 21a428b)
1 parent 836351d commit b74762b

File tree

4 files changed

+252
-50
lines changed

4 files changed

+252
-50
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Spring JDBC Plus
3+
*
4+
* Copyright 2020-2021 NAVER Corp.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.navercorp.spring.data.jdbc.plus.sql.annotation;
20+
21+
import java.lang.annotation.ElementType;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
/**
27+
* The annotation Sql table alias
28+
*
29+
* @author Myeonghyeon Lee
30+
*/
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
33+
public @interface SqlTableAlias {
34+
String value();
35+
}

spring-data-jdbc-plus-sql/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/convert/SqlContext.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
*/
1616
package com.navercorp.spring.data.jdbc.plus.sql.convert;
1717

18+
import javax.annotation.Nullable;
19+
20+
import org.springframework.data.mapping.PersistentPropertyPath;
1821
import org.springframework.data.relational.core.mapping.PersistentPropertyPathExtension;
1922
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
23+
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
2024
import org.springframework.data.relational.core.sql.Column;
2125
import org.springframework.data.relational.core.sql.SqlIdentifier;
2226
import org.springframework.data.relational.core.sql.Table;
27+
import org.springframework.util.Assert;
28+
29+
import com.navercorp.spring.data.jdbc.plus.sql.annotation.SqlTableAlias;
2330

2431
/**
2532
* Utility to get from path to SQL DSL elements.
@@ -60,8 +67,7 @@ public Table getTable() {
6067

6168
@Override
6269
public Table getTable(PersistentPropertyPathExtension path) {
63-
64-
SqlIdentifier tableAlias = path.getTableAlias();
70+
SqlIdentifier tableAlias = this.getTableAlias(path);
6571
Table table = Table.create(path.getTableName());
6672
return tableAlias == null ? table : table.as(tableAlias);
6773
}
@@ -75,4 +81,52 @@ public Column getColumn(PersistentPropertyPathExtension path) {
7581
public Column getReverseColumn(PersistentPropertyPathExtension path) {
7682
return getTable(path).column(path.getReverseColumnName()).as(path.getReverseColumnNameAlias());
7783
}
84+
85+
// Refer from PersistentPropertyPathExtension#getTableAlias
86+
@Nullable
87+
private SqlIdentifier getTableAlias(PersistentPropertyPathExtension path) {
88+
PersistentPropertyPathExtension tableOwner = getTableOwningAncestor(path);
89+
if (tableOwner.getLength() > 0) { // path != null
90+
return this.assembleTableAlias(tableOwner);
91+
}
92+
93+
// path == null : root
94+
SqlTableAlias sqlTableAlias = tableOwner.getLeafEntity().findAnnotation(SqlTableAlias.class);
95+
if (sqlTableAlias != null) {
96+
return this.table.as(sqlTableAlias.value()).getReferenceName();
97+
}
98+
99+
return null;
100+
}
101+
102+
private PersistentPropertyPathExtension getTableOwningAncestor(PersistentPropertyPathExtension path) {
103+
return path.isEntity() && !path.isEmbedded() ? path : this.getTableOwningAncestor(path.getParentPath());
104+
}
105+
106+
private SqlIdentifier assembleTableAlias(PersistentPropertyPathExtension path) {
107+
108+
Assert.state(path != null, "Path is null");
109+
110+
PersistentPropertyPath<? extends RelationalPersistentProperty> propertyPath = path.getRequiredPersistentPropertyPath();
111+
RelationalPersistentProperty leafProperty = propertyPath.getRequiredLeafProperty();
112+
113+
String prefix;
114+
if (path.isEmbedded()) {
115+
prefix = leafProperty.getEmbeddedPrefix();
116+
} else {
117+
SqlTableAlias sqlTableAlias = leafProperty.findPropertyOrOwnerAnnotation(SqlTableAlias.class);
118+
prefix = sqlTableAlias != null ? sqlTableAlias.value() : leafProperty.getName();
119+
}
120+
121+
if (path.getLength() == 1) {
122+
Assert.notNull(prefix, "Prefix mus not be null.");
123+
return SqlIdentifier.quoted(prefix);
124+
}
125+
126+
PersistentPropertyPathExtension parentPath = path.getParentPath();
127+
SqlIdentifier sqlIdentifier = this.assembleTableAlias(parentPath);
128+
129+
return parentPath.isEmbedded() ? sqlIdentifier.transform(name -> name.concat(prefix))
130+
: sqlIdentifier.transform(name -> name + "_" + prefix);
131+
}
78132
}

spring-data-jdbc-plus-sql/src/main/java/com/navercorp/spring/data/jdbc/plus/sql/convert/SqlGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ String selectColumns() {
577577
new PersistentPropertyPathExtension(mappingContext, path);
578578
Column column = getColumn(extPath);
579579
if (column != null) {
580-
columnExpressions.add(column);
580+
columnExpressions.add(getColumnExpression(extPath, column));
581581
}
582582
}
583583

@@ -610,10 +610,10 @@ String selectFrom() {
610610
if (join != null) {
611611
joinTables.add(join);
612612
}
613-
613+
614614
Column column = getColumn(extPath);
615615
if (column != null) {
616-
columnExpressions.add(column);
616+
columnExpressions.add(getColumnExpression(extPath, column));
617617
}
618618
}
619619

0 commit comments

Comments
 (0)