Polishing.

Remove Preprocessor interface. Add property accessors to RelationalQueryLookupStrategy. Reuse property accessors instead of loosely coupled object access.

See #1856
Original pull request #1863
This commit is contained in:
Mark Paluch
2024-09-04 10:02:05 +02:00
parent f937738038
commit 72774135a7
6 changed files with 47 additions and 84 deletions

View File

@@ -1,29 +0,0 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.relational.repository.query;
public interface QueryPreprocessor {
QueryPreprocessor NOOP = new QueryPreprocessor() {
@Override
public String transform(String query) {
return query;
}
};
String transform(String query);
}

View File

@@ -20,8 +20,6 @@ import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.repository.query.QueryPreprocessor;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.query.QueryLookupStrategy;
import org.springframework.util.Assert;
@@ -48,17 +46,20 @@ public abstract class RelationalQueryLookupStrategy implements QueryLookupStrate
this.dialect = dialect;
}
protected String evaluateTableExpressions(RepositoryMetadata repositoryMetadata, String queryString) {
return prepareQueryPreprocessor(repositoryMetadata).transform(queryString);
public MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> getMappingContext() {
return context;
}
private QueryPreprocessor prepareQueryPreprocessor(RepositoryMetadata repositoryMetadata) {
public Dialect getDialect() {
return dialect;
}
SqlIdentifier tableName = context.getPersistentEntity(repositoryMetadata.getDomainType()).getTableName();
SqlIdentifier qualifiedTableName = context.getPersistentEntity(repositoryMetadata.getDomainType())
.getQualifiedTableName();
return new TableNameQueryPreprocessor(tableName, qualifiedTableName, dialect);
protected String evaluateTableExpressions(RepositoryMetadata repositoryMetadata, String queryString) {
TableNameQueryPreprocessor preprocessor = new TableNameQueryPreprocessor(
context.getRequiredPersistentEntity(repositoryMetadata.getDomainType()), dialect);
return preprocessor.transform(queryString);
}
}

View File

@@ -16,23 +16,23 @@
package org.springframework.data.relational.repository.support;
import java.util.regex.Pattern;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.repository.query.QueryPreprocessor;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.util.Assert;
import java.util.regex.Pattern;
/**
* Replaces SpEL expressions based on table names in query strings.
*
* @author Jens Schauder
*/
class TableNameQueryPreprocessor implements QueryPreprocessor {
class TableNameQueryPreprocessor {
private static final String EXPRESSION_PARAMETER = "$1#{";
private static final String QUOTED_EXPRESSION_PARAMETER = "$1__HASH__{";
@@ -44,7 +44,11 @@ class TableNameQueryPreprocessor implements QueryPreprocessor {
private final SqlIdentifier qualifiedTableName;
private final Dialect dialect;
public TableNameQueryPreprocessor(SqlIdentifier tableName, SqlIdentifier qualifiedTableName, Dialect dialect) {
public TableNameQueryPreprocessor(RelationalPersistentEntity<?> entity, Dialect dialect) {
this(entity.getTableName(), entity.getQualifiedTableName(), dialect);
}
TableNameQueryPreprocessor(SqlIdentifier tableName, SqlIdentifier qualifiedTableName, Dialect dialect) {
Assert.notNull(tableName, "TableName must not be null");
Assert.notNull(qualifiedTableName, "QualifiedTableName must not be null");
@@ -55,7 +59,6 @@ class TableNameQueryPreprocessor implements QueryPreprocessor {
this.dialect = dialect;
}
@Override
public String transform(String query) {
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();