|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.relational.core.mapping; |
| 17 | + |
| 18 | +import java.util.Optional; |
| 19 | + |
| 20 | +import org.springframework.data.mapping.model.BasicPersistentEntity; |
| 21 | +import org.springframework.data.relational.core.sql.SqlIdentifier; |
| 22 | +import org.springframework.data.util.Lazy; |
| 23 | +import org.springframework.data.util.TypeInformation; |
| 24 | +import org.springframework.expression.Expression; |
| 25 | +import org.springframework.expression.ParserContext; |
| 26 | +import org.springframework.expression.common.LiteralExpression; |
| 27 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 28 | +import org.springframework.lang.Nullable; |
| 29 | +import org.springframework.util.StringUtils; |
| 30 | + |
| 31 | +/** |
| 32 | + * SQL-specific {@link RelationalPersistentEntity} implementation that adds SQL-specific meta-data such as the table and |
| 33 | + * schema name. |
| 34 | + * |
| 35 | + * @author Jens Schauder |
| 36 | + * @author Greg Turnquist |
| 37 | + * @author Bastian Wilhelm |
| 38 | + * @author Mikhail Polivakha |
| 39 | + * @author Kurt Niemi |
| 40 | + */ |
| 41 | +class BasicRelationalPersistentEntity<T> extends BasicPersistentEntity<T, RelationalPersistentProperty> |
| 42 | + implements RelationalPersistentEntity<T> { |
| 43 | + |
| 44 | + private static final SpelExpressionParser PARSER = new SpelExpressionParser(); |
| 45 | + |
| 46 | + private final NamingStrategy namingStrategy; |
| 47 | + |
| 48 | + private final Lazy<SqlIdentifier> tableName; |
| 49 | + private final @Nullable Expression tableNameExpression; |
| 50 | + |
| 51 | + private final Lazy<Optional<SqlIdentifier>> schemaName; |
| 52 | + private final ExpressionEvaluator expressionEvaluator; |
| 53 | + private boolean forceQuote = true; |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new {@link BasicRelationalPersistentEntity} for the given {@link TypeInformation}. |
| 57 | + * |
| 58 | + * @param information must not be {@literal null}. |
| 59 | + */ |
| 60 | + BasicRelationalPersistentEntity(TypeInformation<T> information, NamingStrategy namingStrategy, |
| 61 | + ExpressionEvaluator expressionEvaluator) { |
| 62 | + |
| 63 | + super(information); |
| 64 | + |
| 65 | + this.namingStrategy = namingStrategy; |
| 66 | + this.expressionEvaluator = expressionEvaluator; |
| 67 | + |
| 68 | + Lazy<Optional<SqlIdentifier>> defaultSchema = Lazy.of(() -> { |
| 69 | + if (StringUtils.hasText(namingStrategy.getSchema())) { |
| 70 | + return Optional.of(createDerivedSqlIdentifier(namingStrategy.getSchema())); |
| 71 | + } |
| 72 | + return Optional.empty(); |
| 73 | + }); |
| 74 | + |
| 75 | + if (isAnnotationPresent(Table.class)) { |
| 76 | + |
| 77 | + Table table = getRequiredAnnotation(Table.class); |
| 78 | + |
| 79 | + // TODO: support expressions for schema |
| 80 | + this.tableName = StringUtils.hasText(table.value()) ? Lazy.of(() -> createSqlIdentifier(table.value())) |
| 81 | + : Lazy.of(() -> createDerivedSqlIdentifier(namingStrategy.getTableName(getType()))); |
| 82 | + this.tableNameExpression = detectExpression(table.value()); |
| 83 | + |
| 84 | + this.schemaName = StringUtils.hasText(table.schema()) |
| 85 | + ? Lazy.of(() -> Optional.of(createSqlIdentifier(table.schema()))) |
| 86 | + : defaultSchema; |
| 87 | + |
| 88 | + } else { |
| 89 | + |
| 90 | + this.tableName = Lazy.of(() -> createDerivedSqlIdentifier(namingStrategy.getTableName(getType()))); |
| 91 | + this.tableNameExpression = null; |
| 92 | + this.schemaName = defaultSchema; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns a SpEL {@link Expression} if the given {@link String} is actually an expression that does not evaluate to a |
| 98 | + * {@link LiteralExpression} (indicating that no subsequent evaluation is necessary). |
| 99 | + * |
| 100 | + * @param potentialExpression can be {@literal null} |
| 101 | + * @return can be {@literal null}. |
| 102 | + */ |
| 103 | + @Nullable |
| 104 | + private static Expression detectExpression(@Nullable String potentialExpression) { |
| 105 | + |
| 106 | + if (!StringUtils.hasText(potentialExpression)) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + Expression expression = PARSER.parseExpression(potentialExpression, ParserContext.TEMPLATE_EXPRESSION); |
| 111 | + return expression instanceof LiteralExpression ? null : expression; |
| 112 | + } |
| 113 | + |
| 114 | + private SqlIdentifier createSqlIdentifier(String name) { |
| 115 | + return isForceQuote() ? SqlIdentifier.quoted(name) : SqlIdentifier.unquoted(name); |
| 116 | + } |
| 117 | + |
| 118 | + private SqlIdentifier createDerivedSqlIdentifier(String name) { |
| 119 | + return new DerivedSqlIdentifier(name, isForceQuote()); |
| 120 | + } |
| 121 | + |
| 122 | + public boolean isForceQuote() { |
| 123 | + return forceQuote; |
| 124 | + } |
| 125 | + |
| 126 | + public void setForceQuote(boolean forceQuote) { |
| 127 | + this.forceQuote = forceQuote; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public SqlIdentifier getTableName() { |
| 132 | + |
| 133 | + if (tableNameExpression == null) { |
| 134 | + return tableName.get(); |
| 135 | + } |
| 136 | + |
| 137 | + return createSqlIdentifier(expressionEvaluator.evaluate(tableNameExpression)); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public SqlIdentifier getQualifiedTableName() { |
| 142 | + |
| 143 | + SqlIdentifier schema = schemaName.get().orElse(null); |
| 144 | + |
| 145 | + if (schema == null) { |
| 146 | + return getTableName(); |
| 147 | + } |
| 148 | + |
| 149 | + return SqlIdentifier.from(schema, getTableName()); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public SqlIdentifier getIdColumn() { |
| 154 | + return getRequiredIdProperty().getColumnName(); |
| 155 | + } |
| 156 | + |
| 157 | + @Override |
| 158 | + public String toString() { |
| 159 | + return String.format("BasicRelationalPersistentEntity<%s>", getType()); |
| 160 | + } |
| 161 | +} |
0 commit comments