Support for table names in SpEL expressions.
SpEL expressions in queries get processed in two steps: 1. First SpEL expressions outside parameters are detected and processed. This is done with a `StandardEvaluationContext` with the variables `tableName` and `qualifiedTableName` added. This step is introduced by this commit. 2. Parameters made up by SpEL expressions are processed as usual. Closes #1856 Original pull request #1863
This commit is contained in:
committed by
Mark Paluch
parent
4221840538
commit
f937738038
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* Base class for R2DBC and JDBC {@link QueryLookupStrategy} implementations.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 3.4
|
||||
*/
|
||||
public abstract class RelationalQueryLookupStrategy implements QueryLookupStrategy {
|
||||
|
||||
private final MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context;
|
||||
private final Dialect dialect;
|
||||
|
||||
protected RelationalQueryLookupStrategy(
|
||||
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> context,
|
||||
Dialect dialect) {
|
||||
|
||||
Assert.notNull(context, "RelationalMappingContext must not be null");
|
||||
Assert.notNull(dialect, "Dialect must not be null");
|
||||
|
||||
this.context = context;
|
||||
this.dialect = dialect;
|
||||
}
|
||||
|
||||
protected String evaluateTableExpressions(RepositoryMetadata repositoryMetadata, String queryString) {
|
||||
|
||||
return prepareQueryPreprocessor(repositoryMetadata).transform(queryString);
|
||||
}
|
||||
|
||||
private QueryPreprocessor prepareQueryPreprocessor(RepositoryMetadata repositoryMetadata) {
|
||||
|
||||
SqlIdentifier tableName = context.getPersistentEntity(repositoryMetadata.getDomainType()).getTableName();
|
||||
SqlIdentifier qualifiedTableName = context.getPersistentEntity(repositoryMetadata.getDomainType())
|
||||
.getQualifiedTableName();
|
||||
return new TableNameQueryPreprocessor(tableName, qualifiedTableName, dialect);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import org.springframework.data.relational.core.dialect.Dialect;
|
||||
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 {
|
||||
|
||||
private static final String EXPRESSION_PARAMETER = "$1#{";
|
||||
private static final String QUOTED_EXPRESSION_PARAMETER = "$1__HASH__{";
|
||||
|
||||
private static final Pattern EXPRESSION_PARAMETER_QUOTING = Pattern.compile("([:?])#\\{");
|
||||
private static final Pattern EXPRESSION_PARAMETER_UNQUOTING = Pattern.compile("([:?])__HASH__\\{");
|
||||
|
||||
private final SqlIdentifier tableName;
|
||||
private final SqlIdentifier qualifiedTableName;
|
||||
private final Dialect dialect;
|
||||
|
||||
public TableNameQueryPreprocessor(SqlIdentifier tableName, SqlIdentifier qualifiedTableName, Dialect dialect) {
|
||||
|
||||
Assert.notNull(tableName, "TableName must not be null");
|
||||
Assert.notNull(qualifiedTableName, "QualifiedTableName must not be null");
|
||||
Assert.notNull(dialect, "Dialect must not be null");
|
||||
|
||||
this.tableName = tableName;
|
||||
this.qualifiedTableName = qualifiedTableName;
|
||||
this.dialect = dialect;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String transform(String query) {
|
||||
|
||||
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
||||
evaluationContext.setVariable("tableName", tableName.toSql(dialect.getIdentifierProcessing()));
|
||||
evaluationContext.setVariable("qualifiedTableName", qualifiedTableName.toSql(dialect.getIdentifierProcessing()));
|
||||
|
||||
SpelExpressionParser parser = new SpelExpressionParser();
|
||||
|
||||
query = quoteExpressionsParameter(query);
|
||||
Expression expression = parser.parseExpression(query, ParserContext.TEMPLATE_EXPRESSION);
|
||||
|
||||
return unquoteParameterExpressions(expression.getValue(evaluationContext, String.class));
|
||||
}
|
||||
|
||||
private static String unquoteParameterExpressions(String result) {
|
||||
return EXPRESSION_PARAMETER_UNQUOTING.matcher(result).replaceAll(EXPRESSION_PARAMETER);
|
||||
}
|
||||
|
||||
private static String quoteExpressionsParameter(String query) {
|
||||
return EXPRESSION_PARAMETER_QUOTING.matcher(query).replaceAll(QUOTED_EXPRESSION_PARAMETER);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.support;
|
||||
|
||||
import org.assertj.core.api.SoftAssertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.relational.core.dialect.AnsiDialect;
|
||||
import org.springframework.data.relational.core.sql.SqlIdentifier;
|
||||
|
||||
/**
|
||||
* Tests for {@link TableNameQueryPreprocessor}.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
class TableNameQueryPreprocessorUnitTests {
|
||||
|
||||
@Test // GH-1856
|
||||
void transform() {
|
||||
|
||||
TableNameQueryPreprocessor preprocessor = new TableNameQueryPreprocessor(SqlIdentifier.quoted("some_table_name"), SqlIdentifier.quoted("qualified_table_name"), AnsiDialect.INSTANCE);
|
||||
SoftAssertions.assertSoftly(softly -> {
|
||||
|
||||
softly.assertThat(preprocessor.transform("someString")).isEqualTo("someString");
|
||||
softly.assertThat(preprocessor.transform("someString#{#tableName}restOfString"))
|
||||
.isEqualTo("someString\"some_table_name\"restOfString");
|
||||
softly.assertThat(preprocessor.transform("select from #{#tableName} where x = :#{#some other spel}"))
|
||||
.isEqualTo("select from \"some_table_name\" where x = :#{#some other spel}");
|
||||
softly.assertThat(preprocessor.transform("select from #{#qualifiedTableName}"))
|
||||
.isEqualTo("select from \"qualified_table_name\"");
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user