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

@@ -35,7 +35,6 @@ import org.springframework.data.jdbc.core.convert.JdbcConverter;
import org.springframework.data.jdbc.core.mapping.JdbcValue;
import org.springframework.data.jdbc.support.JdbcUtil;
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
import org.springframework.data.relational.repository.query.QueryPreprocessor;
import org.springframework.data.relational.repository.query.RelationalParameterAccessor;
import org.springframework.data.relational.repository.query.RelationalParametersParameterAccessor;
import org.springframework.data.repository.query.Parameter;
@@ -113,33 +112,34 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery {
public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
RowMapperFactory rowMapperFactory, JdbcConverter converter,
QueryMethodEvaluationContextProvider evaluationContextProvider) {
this(queryMethod, operations, rowMapperFactory, converter, evaluationContextProvider, QueryPreprocessor.NOOP.transform(queryMethod.getRequiredQuery()));
this(queryMethod.getRequiredQuery(), queryMethod, operations, rowMapperFactory, converter,
evaluationContextProvider);
}
/**
* Creates a new {@link StringBasedJdbcQuery} for the given {@link JdbcQueryMethod}, {@link RelationalMappingContext}
* and {@link RowMapperFactory}.
*
* @param query must not be {@literal null} or empty.
* @param queryMethod must not be {@literal null}.
* @param operations must not be {@literal null}.
* @param rowMapperFactory must not be {@literal null}.
* @param converter must not be {@literal null}.
* @param evaluationContextProvider must not be {@literal null}.
* @param query
* @since 3.4
*/
public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
RowMapperFactory rowMapperFactory, JdbcConverter converter,
QueryMethodEvaluationContextProvider evaluationContextProvider, String query) {
public StringBasedJdbcQuery(String query, JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
RowMapperFactory rowMapperFactory, JdbcConverter converter,
QueryMethodEvaluationContextProvider evaluationContextProvider) {
super(queryMethod, operations);
Assert.hasText(query, "Query must not be null or empty");
Assert.notNull(rowMapperFactory, "RowMapperFactory must not be null");
this.converter = converter;
this.rowMapperFactory = rowMapperFactory;
if (queryMethod.isSliceQuery()) {
throw new UnsupportedOperationException(
"Slice queries are not supported using string-based queries; Offending method: " + queryMethod);

View File

@@ -66,10 +66,9 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
private static final Log LOG = LogFactory.getLog(JdbcQueryLookupStrategy.class);
private final ApplicationEventPublisher publisher;
private final @Nullable EntityCallbacks callbacks;
private final RelationalMappingContext context;
private final @Nullable EntityCallbacks callbacks;
private final JdbcConverter converter;
private final Dialect dialect;
private final QueryMappingConfiguration queryMappingConfiguration;
private final NamedParameterJdbcOperations operations;
@Nullable private final BeanFactory beanfactory;
@@ -83,24 +82,25 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
super(context, dialect);
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
Assert.notNull(context, "RelationalMappingContext must not be null");
Assert.notNull(converter, "JdbcConverter must not be null");
Assert.notNull(dialect, "Dialect must not be null");
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
Assert.notNull(evaluationContextProvider, "QueryMethodEvaluationContextProvier must not be null");
this.context = context;
this.publisher = publisher;
this.callbacks = callbacks;
this.context = context;
this.converter = converter;
this.dialect = dialect;
this.queryMappingConfiguration = queryMappingConfiguration;
this.operations = operations;
this.beanfactory = beanfactory;
this.evaluationContextProvider = evaluationContextProvider;
}
public RelationalMappingContext getMappingContext() {
return context;
}
/**
* {@link QueryLookupStrategy} to create a query from the method name.
*
@@ -124,7 +124,7 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
JdbcQueryMethod queryMethod = getJdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries);
return new PartTreeJdbcQuery(getContext(), queryMethod, getDialect(), getConverter(), getOperations(),
return new PartTreeJdbcQuery(getMappingContext(), queryMethod, getDialect(), getConverter(), getOperations(),
this::createMapper);
}
}
@@ -161,8 +161,8 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
String queryString = evaluateTableExpressions(repositoryMetadata, queryMethod.getRequiredQuery());
StringBasedJdbcQuery query = new StringBasedJdbcQuery(queryMethod, getOperations(), this::createMapper,
getConverter(), evaluationContextProvider, queryString);
StringBasedJdbcQuery query = new StringBasedJdbcQuery(queryString, queryMethod, getOperations(),
this::createMapper, getConverter(), evaluationContextProvider);
query.setBeanFactory(getBeanFactory());
return query;
}
@@ -224,7 +224,7 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
*/
JdbcQueryMethod getJdbcQueryMethod(Method method, RepositoryMetadata repositoryMetadata,
ProjectionFactory projectionFactory, NamedQueries namedQueries) {
return new JdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries, context);
return new JdbcQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries, getMappingContext());
}
/**
@@ -277,18 +277,10 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
}
}
RelationalMappingContext getContext() {
return context;
}
JdbcConverter getConverter() {
return converter;
}
Dialect getDialect() {
return dialect;
}
NamedParameterJdbcOperations getOperations() {
return operations;
}
@@ -301,7 +293,7 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
@SuppressWarnings("unchecked")
RowMapper<Object> createMapper(Class<?> returnedObjectType) {
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);
RelationalPersistentEntity<?> persistentEntity = getMappingContext().getPersistentEntity(returnedObjectType);
if (persistentEntity == null) {
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType,
@@ -319,7 +311,7 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
return configuredQueryMapper;
EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( //
context.getRequiredPersistentEntity(returnedObjectType), //
getMappingContext().getRequiredPersistentEntity(returnedObjectType), //
converter //
);