Skip to content

Commit 7277413

Browse files
committed
Polishing.
Remove Preprocessor interface. Add property accessors to RelationalQueryLookupStrategy. Reuse property accessors instead of loosely coupled object access. See #1856 Original pull request #1863
1 parent f937738 commit 7277413

File tree

6 files changed

+46
-83
lines changed

6 files changed

+46
-83
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQuery.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.data.jdbc.core.mapping.JdbcValue;
3636
import org.springframework.data.jdbc.support.JdbcUtil;
3737
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
38-
import org.springframework.data.relational.repository.query.QueryPreprocessor;
3938
import org.springframework.data.relational.repository.query.RelationalParameterAccessor;
4039
import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor;
4140
import org.springframework.data.repository.query.Parameter;
@@ -113,33 +112,34 @@ public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOpera
113112
public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
114113
RowMapperFactory rowMapperFactory, JdbcConverter converter,
115114
QueryMethodEvaluationContextProvider evaluationContextProvider) {
116-
this(queryMethod, operations, rowMapperFactory, converter, evaluationContextProvider, QueryPreprocessor.NOOP.transform(queryMethod.getRequiredQuery()));
115+
this(queryMethod.getRequiredQuery(), queryMethod, operations, rowMapperFactory, converter,
116+
evaluationContextProvider);
117117
}
118118

119119
/**
120120
* Creates a new {@link StringBasedJdbcQuery} for the given {@link JdbcQueryMethod}, {@link RelationalMappingContext}
121121
* and {@link RowMapperFactory}.
122122
*
123+
* @param query must not be {@literal null} or empty.
123124
* @param queryMethod must not be {@literal null}.
124125
* @param operations must not be {@literal null}.
125126
* @param rowMapperFactory must not be {@literal null}.
126127
* @param converter must not be {@literal null}.
127128
* @param evaluationContextProvider must not be {@literal null}.
128-
* @param query
129129
* @since 3.4
130130
*/
131-
public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
132-
RowMapperFactory rowMapperFactory, JdbcConverter converter,
133-
QueryMethodEvaluationContextProvider evaluationContextProvider, String query) {
131+
public StringBasedJdbcQuery(String query, JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
132+
RowMapperFactory rowMapperFactory, JdbcConverter converter,
133+
QueryMethodEvaluationContextProvider evaluationContextProvider) {
134134

135135
super(queryMethod, operations);
136136

137+
Assert.hasText(query, "Query must not be null or empty");
137138
Assert.notNull(rowMapperFactory, "RowMapperFactory must not be null");
138139

139140
this.converter = converter;
140141
this.rowMapperFactory = rowMapperFactory;
141142

142-
143143
if (queryMethod.isSliceQuery()) {
144144
throw new UnsupportedOperationException(
145145
"Slice queries are not supported using string-based queries; Offending method: " + queryMethod);

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java

+12-20
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
6666
private static final Log LOG = LogFactory.getLog(JdbcQueryLookupStrategy.class);
6767

6868
private final ApplicationEventPublisher publisher;
69-
private final @Nullable EntityCallbacks callbacks;
7069
private final RelationalMappingContext context;
70+
private final @Nullable EntityCallbacks callbacks;
7171
private final JdbcConverter converter;
72-
private final Dialect dialect;
7372
private final QueryMappingConfiguration queryMappingConfiguration;
7473
private final NamedParameterJdbcOperations operations;
7574
@Nullable private final BeanFactory beanfactory;
@@ -83,24 +82,25 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
8382
super(context, dialect);
8483

8584
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
86-
Assert.notNull(context, "RelationalMappingContext must not be null");
8785
Assert.notNull(converter, "JdbcConverter must not be null");
88-
Assert.notNull(dialect, "Dialect must not be null");
8986
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
9087
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
9188
Assert.notNull(evaluationContextProvider, "QueryMethodEvaluationContextProvier must not be null");
9289

90+
this.context = context;
9391
this.publisher = publisher;
9492
this.callbacks = callbacks;
95-
this.context = context;
9693
this.converter = converter;
97-
this.dialect = dialect;
9894
this.queryMappingConfiguration = queryMappingConfiguration;
9995
this.operations = operations;
10096
this.beanfactory = beanfactory;
10197
this.evaluationContextProvider = evaluationContextProvider;
10298
}
10399

100+
public RelationalMappingContext getMappingContext() {
101+
return context;
102+
}
103+
104104
/**
105105
* {@link QueryLookupStrategy} to create a query from the method name.
106106
*
@@ -124,7 +124,7 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
124124

125125
JdbcQueryMethod queryMethod = getJdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries);
126126

127-
return new PartTreeJdbcQuery(getContext(), queryMethod, getDialect(), getConverter(), getOperations(),
127+
return new PartTreeJdbcQuery(getMappingContext(), queryMethod, getDialect(), getConverter(), getOperations(),
128128
this::createMapper);
129129
}
130130
}
@@ -161,8 +161,8 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
161161

162162
String queryString = evaluateTableExpressions(repositoryMetadata, queryMethod.getRequiredQuery());
163163

164-
StringBasedJdbcQuery query = new StringBasedJdbcQuery(queryMethod, getOperations(), this::createMapper,
165-
getConverter(), evaluationContextProvider, queryString);
164+
StringBasedJdbcQuery query = new StringBasedJdbcQuery(queryString, queryMethod, getOperations(),
165+
this::createMapper, getConverter(), evaluationContextProvider);
166166
query.setBeanFactory(getBeanFactory());
167167
return query;
168168
}
@@ -224,7 +224,7 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
224224
*/
225225
JdbcQueryMethod getJdbcQueryMethod(Method method, RepositoryMetadata repositoryMetadata,
226226
ProjectionFactory projectionFactory, NamedQueries namedQueries) {
227-
return new JdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries, context);
227+
return new JdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries, getMappingContext());
228228
}
229229

230230
/**
@@ -277,18 +277,10 @@ public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPubl
277277
}
278278
}
279279

280-
RelationalMappingContext getContext() {
281-
return context;
282-
}
283-
284280
JdbcConverter getConverter() {
285281
return converter;
286282
}
287283

288-
Dialect getDialect() {
289-
return dialect;
290-
}
291-
292284
NamedParameterJdbcOperations getOperations() {
293285
return operations;
294286
}
@@ -301,7 +293,7 @@ BeanFactory getBeanFactory() {
301293
@SuppressWarnings("unchecked")
302294
RowMapper<Object> createMapper(Class<?> returnedObjectType) {
303295

304-
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);
296+
RelationalPersistentEntity<?> persistentEntity = getMappingContext().getPersistentEntity(returnedObjectType);
305297

306298
if (persistentEntity == null) {
307299
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType,
@@ -319,7 +311,7 @@ private RowMapper<?> determineDefaultMapper(Class<?> returnedObjectType) {
319311
return configuredQueryMapper;
320312

321313
EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( //
322-
context.getRequiredPersistentEntity(returnedObjectType), //
314+
getMappingContext().getRequiredPersistentEntity(returnedObjectType), //
323315
converter //
324316
);
325317

spring-data-r2dbc/src/main/java/org/springframework/data/r2dbc/repository/support/R2dbcRepositoryFactory.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ protected Object getTargetRepository(RepositoryInformation information) {
112112
RelationalEntityInformation<?, ?> entityInformation = getEntityInformation(information.getDomainType(),
113113
information);
114114

115-
return getTargetRepositoryViaReflection(information, entityInformation,
116-
operations, this.converter);
115+
return getTargetRepositoryViaReflection(information, entityInformation, operations, this.converter);
117116
}
118117

119118
@Override
@@ -138,7 +137,7 @@ private <T, ID> RelationalEntityInformation<T, ID> getEntityInformation(Class<T>
138137
}
139138

140139
/**
141-
* {@link QueryLookupStrategy} to create R2DBC queries..
140+
* {@link QueryLookupStrategy} to create R2DBC queries.
142141
*
143142
* @author Mark Paluch
144143
* @author Jens Schauder
@@ -167,21 +166,18 @@ private static class R2dbcQueryLookupStrategy extends RelationalQueryLookupStrat
167166
public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata, ProjectionFactory factory,
168167
NamedQueries namedQueries) {
169168

170-
MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> mappingContext = this.converter.getMappingContext();
171-
172-
R2dbcQueryMethod queryMethod = new R2dbcQueryMethod(method, metadata, factory,
173-
mappingContext);
169+
R2dbcQueryMethod queryMethod = new R2dbcQueryMethod(method, metadata, factory, getMappingContext());
174170
String namedQueryName = queryMethod.getNamedQueryName();
175171

176172
if (namedQueries.hasQuery(namedQueryName) || queryMethod.hasAnnotatedQuery()) {
177173

178-
String query = namedQueries.hasQuery(namedQueryName) ? namedQueries.getQuery(namedQueryName) : queryMethod.getRequiredAnnotatedQuery();
179-
query = evaluateTableExpressions(metadata, query);
174+
String query = namedQueries.hasQuery(namedQueryName) ? namedQueries.getQuery(namedQueryName)
175+
: queryMethod.getRequiredAnnotatedQuery();
176+
query = evaluateTableExpressions(metadata, query);
180177

181178
return new StringBasedR2dbcQuery(query, queryMethod, this.entityOperations, this.converter,
182-
this.dataAccessStrategy,
183-
parser, this.evaluationContextProvider);
184-
179+
this.dataAccessStrategy, parser, this.evaluationContextProvider);
180+
185181
} else {
186182
return new PartTreeR2dbcQuery(queryMethod, this.entityOperations, this.converter, this.dataAccessStrategy);
187183
}

spring-data-relational/src/main/java/org/springframework/data/relational/repository/query/QueryPreprocessor.java

-29
This file was deleted.

spring-data-relational/src/main/java/org/springframework/data/relational/repository/support/RelationalQueryLookupStrategy.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import org.springframework.data.relational.core.dialect.Dialect;
2121
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
2222
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
23-
import org.springframework.data.relational.core.sql.SqlIdentifier;
24-
import org.springframework.data.relational.repository.query.QueryPreprocessor;
2523
import org.springframework.data.repository.core.RepositoryMetadata;
2624
import org.springframework.data.repository.query.QueryLookupStrategy;
2725
import org.springframework.util.Assert;
@@ -48,17 +46,20 @@ protected RelationalQueryLookupStrategy(
4846
this.dialect = dialect;
4947
}
5048

51-
protected String evaluateTableExpressions(RepositoryMetadata repositoryMetadata, String queryString) {
49+
public MappingContext<? extends RelationalPersistentEntity<?>, ? extends RelationalPersistentProperty> getMappingContext() {
50+
return context;
51+
}
5252

53-
return prepareQueryPreprocessor(repositoryMetadata).transform(queryString);
53+
public Dialect getDialect() {
54+
return dialect;
5455
}
5556

56-
private QueryPreprocessor prepareQueryPreprocessor(RepositoryMetadata repositoryMetadata) {
57+
protected String evaluateTableExpressions(RepositoryMetadata repositoryMetadata, String queryString) {
58+
59+
TableNameQueryPreprocessor preprocessor = new TableNameQueryPreprocessor(
60+
context.getRequiredPersistentEntity(repositoryMetadata.getDomainType()), dialect);
5761

58-
SqlIdentifier tableName = context.getPersistentEntity(repositoryMetadata.getDomainType()).getTableName();
59-
SqlIdentifier qualifiedTableName = context.getPersistentEntity(repositoryMetadata.getDomainType())
60-
.getQualifiedTableName();
61-
return new TableNameQueryPreprocessor(tableName, qualifiedTableName, dialect);
62+
return preprocessor.transform(queryString);
6263
}
6364

6465
}

spring-data-relational/src/main/java/org/springframework/data/relational/repository/support/TableNameQueryPreprocessor.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616

1717
package org.springframework.data.relational.repository.support;
1818

19+
import java.util.regex.Pattern;
20+
1921
import org.springframework.data.relational.core.dialect.Dialect;
22+
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
2023
import org.springframework.data.relational.core.sql.SqlIdentifier;
21-
import org.springframework.data.relational.repository.query.QueryPreprocessor;
2224
import org.springframework.expression.Expression;
2325
import org.springframework.expression.ParserContext;
2426
import org.springframework.expression.spel.standard.SpelExpressionParser;
2527
import org.springframework.expression.spel.support.StandardEvaluationContext;
2628
import org.springframework.util.Assert;
2729

28-
import java.util.regex.Pattern;
29-
3030
/**
3131
* Replaces SpEL expressions based on table names in query strings.
3232
*
3333
* @author Jens Schauder
3434
*/
35-
class TableNameQueryPreprocessor implements QueryPreprocessor {
35+
class TableNameQueryPreprocessor {
3636

3737
private static final String EXPRESSION_PARAMETER = "$1#{";
3838
private static final String QUOTED_EXPRESSION_PARAMETER = "$1__HASH__{";
@@ -44,7 +44,11 @@ class TableNameQueryPreprocessor implements QueryPreprocessor {
4444
private final SqlIdentifier qualifiedTableName;
4545
private final Dialect dialect;
4646

47-
public TableNameQueryPreprocessor(SqlIdentifier tableName, SqlIdentifier qualifiedTableName, Dialect dialect) {
47+
public TableNameQueryPreprocessor(RelationalPersistentEntity<?> entity, Dialect dialect) {
48+
this(entity.getTableName(), entity.getQualifiedTableName(), dialect);
49+
}
50+
51+
TableNameQueryPreprocessor(SqlIdentifier tableName, SqlIdentifier qualifiedTableName, Dialect dialect) {
4852

4953
Assert.notNull(tableName, "TableName must not be null");
5054
Assert.notNull(qualifiedTableName, "QualifiedTableName must not be null");
@@ -55,7 +59,6 @@ public TableNameQueryPreprocessor(SqlIdentifier tableName, SqlIdentifier qualifi
5559
this.dialect = dialect;
5660
}
5761

58-
@Override
5962
public String transform(String query) {
6063

6164
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();

0 commit comments

Comments
 (0)