Skip to content

Commit 9d4efca

Browse files
serezakorotaevmp911de
authored andcommitted
Allow @Table and @Column name expressions to return SqlIdentifier.
Signed-off-by: Sergey Korotaev <[email protected]> Closes #1524 Original pull request: #2077
1 parent c7762f4 commit 9d4efca

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentEntity.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
* @author Bastian Wilhelm
3838
* @author Mikhail Polivakha
3939
* @author Kurt Niemi
40+
* @author Sergey Korotaev
4041
*/
4142
class BasicRelationalPersistentEntity<T> extends BasicPersistentEntity<T, RelationalPersistentProperty>
4243
implements RelationalPersistentEntity<T> {
@@ -47,7 +48,7 @@ class BasicRelationalPersistentEntity<T> extends BasicPersistentEntity<T, Relati
4748
private final @Nullable Expression tableNameExpression;
4849
private final Lazy<Optional<SqlIdentifier>> schemaName;
4950
private final @Nullable Expression schemaNameExpression;
50-
private final ExpressionEvaluator expressionEvaluator;
51+
private final SqlIdentifierExpressionEvaluator sqlIdentifierExpressionEvaluator;
5152
private boolean forceQuote = true;
5253

5354
/**
@@ -56,11 +57,11 @@ class BasicRelationalPersistentEntity<T> extends BasicPersistentEntity<T, Relati
5657
* @param information must not be {@literal null}.
5758
*/
5859
BasicRelationalPersistentEntity(TypeInformation<T> information, NamingStrategy namingStrategy,
59-
ExpressionEvaluator expressionEvaluator) {
60+
SqlIdentifierExpressionEvaluator sqlIdentifierExpressionEvaluator) {
6061

6162
super(information);
6263

63-
this.expressionEvaluator = expressionEvaluator;
64+
this.sqlIdentifierExpressionEvaluator = sqlIdentifierExpressionEvaluator;
6465

6566
Lazy<Optional<SqlIdentifier>> defaultSchema = Lazy.of(() -> StringUtils.hasText(namingStrategy.getSchema())
6667
? Optional.of(createDerivedSqlIdentifier(namingStrategy.getSchema()))
@@ -129,15 +130,15 @@ public SqlIdentifier getTableName() {
129130
return tableName.get();
130131
}
131132

132-
return createSqlIdentifier(expressionEvaluator.evaluate(tableNameExpression));
133+
return sqlIdentifierExpressionEvaluator.evaluate(tableNameExpression, isForceQuote());
133134
}
134135

135136
@Override
136137
public SqlIdentifier getQualifiedTableName() {
137138

138139
SqlIdentifier schema;
139140
if (schemaNameExpression != null) {
140-
schema = createSqlIdentifier(expressionEvaluator.evaluate(schemaNameExpression));
141+
schema = sqlIdentifierExpressionEvaluator.evaluate(schemaNameExpression, isForceQuote());
141142
} else {
142143
schema = schemaName.get().orElse(null);
143144
}
@@ -147,7 +148,7 @@ public SqlIdentifier getQualifiedTableName() {
147148
}
148149

149150
if (schemaNameExpression != null) {
150-
schema = createSqlIdentifier(expressionEvaluator.evaluate(schemaNameExpression));
151+
schema = sqlIdentifierExpressionEvaluator.evaluate(schemaNameExpression, isForceQuote());
151152
}
152153

153154
return SqlIdentifier.from(schema, getTableName());

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/BasicRelationalPersistentProperty.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* @author Florian Lüdiger
4444
* @author Bastian Wilhelm
4545
* @author Kurt Niemi
46+
* @author Sergey Korotaev
4647
*/
4748
public class BasicRelationalPersistentProperty extends AnnotationBasedPersistentProperty<RelationalPersistentProperty>
4849
implements RelationalPersistentProperty {
@@ -61,7 +62,9 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
6162

6263
private final NamingStrategy namingStrategy;
6364
private boolean forceQuote = true;
64-
private ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(EvaluationContextProvider.DEFAULT);
65+
66+
private SqlIdentifierExpressionEvaluator sqlIdentifierExpressionEvaluator =
67+
new SqlIdentifierExpressionEvaluator(EvaluationContextProvider.DEFAULT);
6568

6669
/**
6770
* Creates a new {@link BasicRelationalPersistentProperty}.
@@ -73,7 +76,7 @@ public class BasicRelationalPersistentProperty extends AnnotationBasedPersistent
7376
* @since 2.0
7477
*/
7578
public BasicRelationalPersistentProperty(Property property, PersistentEntity<?, RelationalPersistentProperty> owner,
76-
SimpleTypeHolder simpleTypeHolder, NamingStrategy namingStrategy) {
79+
SimpleTypeHolder simpleTypeHolder, NamingStrategy namingStrategy) {
7780

7881
super(property, owner, simpleTypeHolder);
7982
this.namingStrategy = namingStrategy;
@@ -136,8 +139,8 @@ public BasicRelationalPersistentProperty(Property property, PersistentEntity<?,
136139
this.collectionKeyColumnName = collectionKeyColumnName;
137140
}
138141

139-
void setExpressionEvaluator(ExpressionEvaluator expressionEvaluator) {
140-
this.expressionEvaluator = expressionEvaluator;
142+
void setSqlIdentifierExpressionEvaluator(SqlIdentifierExpressionEvaluator sqlIdentifierExpressionEvaluator) {
143+
this.sqlIdentifierExpressionEvaluator = sqlIdentifierExpressionEvaluator;
141144
}
142145

143146
/**
@@ -191,7 +194,7 @@ public SqlIdentifier getColumnName() {
191194
return columnName.get();
192195
}
193196

194-
return createSqlIdentifier(expressionEvaluator.evaluate(columnNameExpression));
197+
return sqlIdentifierExpressionEvaluator.evaluate(columnNameExpression, isForceQuote());
195198
}
196199

197200
@Override
@@ -222,7 +225,7 @@ public SqlIdentifier getKeyColumn() {
222225
return collectionKeyColumnName.get();
223226
}
224227

225-
return createSqlIdentifier(expressionEvaluator.evaluate(collectionKeyColumnNameExpression));
228+
return sqlIdentifierExpressionEvaluator.evaluate(collectionKeyColumnNameExpression, isForceQuote());
226229
}
227230

228231
@Override

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/RelationalMappingContext.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public class RelationalMappingContext
4848

4949
private boolean forceQuote = true;
5050

51-
private final ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(EvaluationContextProvider.DEFAULT);
51+
private final SqlIdentifierExpressionEvaluator sqlIdentifierExpressionEvaluator =
52+
new SqlIdentifierExpressionEvaluator(EvaluationContextProvider.DEFAULT);
5253
private boolean singleQueryLoadingEnabled = false;
5354

5455
/**
@@ -99,7 +100,7 @@ public void setForceQuote(boolean forceQuote) {
99100
* @since 3.2
100101
*/
101102
public void setSqlIdentifierSanitizer(SqlIdentifierSanitizer sanitizer) {
102-
this.expressionEvaluator.setSanitizer(sanitizer);
103+
this.sqlIdentifierExpressionEvaluator.setSanitizer(sanitizer);
103104
}
104105

105106
public NamingStrategy getNamingStrategy() {
@@ -108,7 +109,7 @@ public NamingStrategy getNamingStrategy() {
108109

109110
@Override
110111
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
111-
this.expressionEvaluator.setProvider(new ExtensionAwareEvaluationContextProvider(applicationContext));
112+
this.sqlIdentifierExpressionEvaluator.setProvider(new ExtensionAwareEvaluationContextProvider(applicationContext));
112113
}
113114

114115
@Nullable
@@ -130,7 +131,7 @@ public RelationalPersistentEntity<?> getPersistentEntity(RelationalPersistentPro
130131
protected <T> RelationalPersistentEntity<T> createPersistentEntity(TypeInformation<T> typeInformation) {
131132

132133
BasicRelationalPersistentEntity<T> entity = new BasicRelationalPersistentEntity<>(typeInformation,
133-
this.namingStrategy, this.expressionEvaluator);
134+
this.namingStrategy, this.sqlIdentifierExpressionEvaluator);
134135
entity.setForceQuote(isForceQuote());
135136

136137
return entity;
@@ -171,7 +172,7 @@ public void setSingleQueryLoadingEnabled(boolean singleQueryLoadingEnabled) {
171172
protected void applyDefaults(BasicRelationalPersistentProperty persistentProperty) {
172173

173174
persistentProperty.setForceQuote(isForceQuote());
174-
persistentProperty.setExpressionEvaluator(this.expressionEvaluator);
175+
persistentProperty.setSqlIdentifierExpressionEvaluator(this.sqlIdentifierExpressionEvaluator);
175176
}
176177

177178
/**

spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/ExpressionEvaluator.java renamed to spring-data-relational/src/main/java/org/springframework/data/relational/core/mapping/SqlIdentifierExpressionEvaluator.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.springframework.data.relational.core.mapping;
22

3+
import org.springframework.data.relational.core.sql.SqlIdentifier;
34
import org.springframework.data.spel.EvaluationContextProvider;
45
import org.springframework.expression.EvaluationException;
56
import org.springframework.expression.Expression;
@@ -13,25 +14,31 @@
1314
* {@link #setSanitizer(SqlIdentifierSanitizer)} method.
1415
*
1516
* @author Kurt Niemi
17+
* @author Sergey Korotaev
1618
* @see SqlIdentifierSanitizer
1719
* @since 3.2
1820
*/
19-
class ExpressionEvaluator {
21+
class SqlIdentifierExpressionEvaluator {
2022

2123
private EvaluationContextProvider provider;
2224

2325
private SqlIdentifierSanitizer sanitizer = SqlIdentifierSanitizer.words();
2426

25-
public ExpressionEvaluator(EvaluationContextProvider provider) {
27+
public SqlIdentifierExpressionEvaluator(EvaluationContextProvider provider) {
2628
this.provider = provider;
2729
}
2830

29-
public String evaluate(Expression expression) throws EvaluationException {
31+
public SqlIdentifier evaluate(Expression expression, boolean isForceQuote) throws EvaluationException {
3032

3133
Assert.notNull(expression, "Expression must not be null.");
3234

33-
String result = expression.getValue(provider.getEvaluationContext(null), String.class);
34-
return sanitizer.sanitize(result);
35+
Object value = expression.getValue(provider.getEvaluationContext(null), Object.class);
36+
if (value instanceof SqlIdentifier sqlIdentifier) {
37+
return sqlIdentifier;
38+
}
39+
40+
String sanitizedResult = sanitizer.sanitize((String) value);
41+
return isForceQuote ? SqlIdentifier.quoted(sanitizedResult) : SqlIdentifier.unquoted(sanitizedResult);
3542
}
3643

3744
public void setSanitizer(SqlIdentifierSanitizer sanitizer) {

0 commit comments

Comments
 (0)