Skip to content

Commit a8044cb

Browse files
committed
rowmappers extraction
1 parent 180b7ed commit a8044cb

11 files changed

+319
-176
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.springframework.data.jdbc.repository.query;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
6+
import org.springframework.jdbc.core.RowMapper;
7+
import org.springframework.lang.Nullable;
8+
import org.springframework.util.Assert;
9+
10+
/**
11+
* Abstract {@link RowMapper} that delegates the actual mapping logic to a {@link AbstractDelegatingRowMapper#delegate delegate}
12+
*
13+
* @author Mikhail Polivakha
14+
*/
15+
public abstract class AbstractDelegatingRowMapper<T> implements RowMapper<T> {
16+
17+
private final RowMapper<T> delegate;
18+
19+
protected AbstractDelegatingRowMapper(RowMapper<T> delegate) {
20+
Assert.notNull(delegate, "Delegating RowMapper cannot be null");
21+
22+
this.delegate = delegate;
23+
}
24+
25+
@Override
26+
public T mapRow(ResultSet rs, int rowNum) throws SQLException {
27+
T intermediate = delegate.mapRow(rs, rowNum);
28+
return postProcessMapping(intermediate);
29+
}
30+
31+
/**
32+
* The post-processing callback for implementations.
33+
*
34+
* @return the mapped entity after applying post-processing logic
35+
*/
36+
protected T postProcessMapping(@Nullable T object) {
37+
return object;
38+
}
39+
}

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

-61
Original file line numberDiff line numberDiff line change
@@ -148,65 +148,4 @@ private <T> JdbcQueryExecution<Stream<T>> streamQuery(RowMapper<T> rowMapper) {
148148
private <T> JdbcQueryExecution<T> createSingleReadingQueryExecution(ResultSetExtractor<T> resultSetExtractor) {
149149
return (query, parameters) -> operations.query(query, parameters, resultSetExtractor);
150150
}
151-
152-
/**
153-
* Factory to create a {@link RowMapper} for a given class.
154-
*
155-
* @since 2.3
156-
*/
157-
public interface RowMapperFactory {
158-
159-
/**
160-
* Create a {@link RowMapper} based on the expected return type passed in as an argument.
161-
*
162-
* @param result must not be {@code null}.
163-
* @return a {@code RowMapper} producing instances of {@code result}.
164-
*/
165-
RowMapper<Object> create(Class<?> result);
166-
167-
/**
168-
* Obtain a {@code RowMapper} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
169-
*
170-
* @param reference must not be {@code null}.
171-
* @since 3.4
172-
*/
173-
default RowMapper<Object> getRowMapper(String reference) {
174-
throw new UnsupportedOperationException("getRowMapper is not supported");
175-
}
176-
177-
/**
178-
* Obtain a {@code ResultSetExtractor} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
179-
*
180-
* @param reference must not be {@code null}.
181-
* @since 3.4
182-
*/
183-
default ResultSetExtractor<Object> getResultSetExtractor(String reference) {
184-
throw new UnsupportedOperationException("getResultSetExtractor is not supported");
185-
}
186-
}
187-
188-
/**
189-
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterwards into {@code Object}.
190-
*
191-
* @param <T>
192-
* @since 2.3
193-
*/
194-
protected static class ConvertingRowMapper<T> implements RowMapper<Object> {
195-
196-
private final RowMapper<T> delegate;
197-
private final Converter<Object, Object> converter;
198-
199-
public ConvertingRowMapper(RowMapper<T> delegate, Converter<Object, Object> converter) {
200-
this.delegate = delegate;
201-
this.converter = converter;
202-
}
203-
204-
@Override
205-
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
206-
207-
T object = delegate.mapRow(rs, rowNum);
208-
209-
return object == null ? null : converter.convert(object);
210-
}
211-
}
212151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.springframework.data.jdbc.repository.query;
2+
3+
import java.sql.ResultSet;
4+
5+
import org.springframework.context.ApplicationEventPublisher;
6+
import org.springframework.data.mapping.callback.EntityCallbacks;
7+
import org.springframework.data.relational.core.mapping.event.AfterConvertCallback;
8+
import org.springframework.data.relational.core.mapping.event.AfterConvertEvent;
9+
import org.springframework.jdbc.core.RowMapper;
10+
import org.springframework.lang.Nullable;
11+
12+
/**
13+
* Delegating {@link RowMapper} implementation that applies post-processing logic
14+
* after the {@link RowMapper#mapRow(ResultSet, int)}. In particular, it emits the
15+
* {@link AfterConvertEvent} event and invokes the {@link AfterConvertCallback} callbacks.
16+
*
17+
* @author Mark Paluch
18+
* @author Mikhail Polivakha
19+
*/
20+
public class CallbackCapableRowMapper<T> extends AbstractDelegatingRowMapper<T> {
21+
22+
private final ApplicationEventPublisher publisher;
23+
private final @Nullable EntityCallbacks callbacks;
24+
25+
public CallbackCapableRowMapper(RowMapper<T> delegate, ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks) {
26+
super(delegate);
27+
this.publisher = publisher;
28+
this.callbacks = callbacks;
29+
}
30+
31+
@Override
32+
public T postProcessMapping(@Nullable T object) {
33+
if (object != null) {
34+
35+
publisher.publishEvent(new AfterConvertEvent<>(object));
36+
37+
if (callbacks != null) {
38+
return callbacks.callback(AfterConvertCallback.class, object);
39+
}
40+
41+
}
42+
return object;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.springframework.data.jdbc.repository.query;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.springframework.jdbc.core.RowMapper;
5+
import org.springframework.lang.Nullable;
6+
7+
/**
8+
* Delegating {@link RowMapper} that reads a row into {@code T} and converts it afterwards into {@code Object}.
9+
*
10+
* @author Mark Paluch
11+
* @author Mikhail Polivakha
12+
*
13+
* @since 2.3
14+
*/
15+
public class ConvertingRowMapper extends AbstractDelegatingRowMapper<Object> {
16+
17+
private final Converter<Object, Object> converter;
18+
19+
public ConvertingRowMapper(RowMapper<Object> delegate, Converter<Object, Object> converter) {
20+
super(delegate);
21+
this.converter = converter;
22+
}
23+
24+
@Override
25+
public Object postProcessMapping(@Nullable Object object) {
26+
return object != null ? converter.convert(object) : null;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.springframework.data.jdbc.repository.query;
2+
3+
import org.springframework.context.ApplicationEventPublisher;
4+
import org.springframework.data.jdbc.core.convert.EntityRowMapper;
5+
import org.springframework.data.jdbc.core.convert.JdbcConverter;
6+
import org.springframework.data.jdbc.repository.QueryMappingConfiguration;
7+
import org.springframework.data.mapping.callback.EntityCallbacks;
8+
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
9+
import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;
10+
import org.springframework.jdbc.core.ResultSetExtractor;
11+
import org.springframework.jdbc.core.RowMapper;
12+
import org.springframework.jdbc.core.SingleColumnRowMapper;
13+
14+
/**
15+
* Default implementation of {@link RowMapperFactory}. Honors the custom mappings defined
16+
* in {@link QueryMappingConfiguration}.
17+
* <p>
18+
* This implementation is not capable of loading the {@link RowMapper} or {@link ResultSetExtractor}
19+
* by reference via corresponding methods from {@link RowMapperFactory}.
20+
*
21+
* @implNote Public APIs of this class are thread-safe.
22+
* @author Mikhail Polivakha
23+
*/
24+
public class DefaultRowMapperFactory implements RowMapperFactory {
25+
26+
private final RelationalMappingContext context;
27+
private final JdbcConverter converter;
28+
private final QueryMappingConfiguration queryMappingConfiguration;
29+
private final EntityCallbacks entityCallbacks;
30+
private final ApplicationEventPublisher publisher;
31+
32+
public DefaultRowMapperFactory(
33+
RelationalMappingContext context,
34+
JdbcConverter converter,
35+
QueryMappingConfiguration queryMappingConfiguration,
36+
EntityCallbacks entityCallbacks,
37+
ApplicationEventPublisher publisher
38+
) {
39+
this.context = context;
40+
this.converter = converter;
41+
this.queryMappingConfiguration = queryMappingConfiguration;
42+
this.entityCallbacks = entityCallbacks;
43+
this.publisher = publisher;
44+
}
45+
46+
@Override
47+
@SuppressWarnings("unchecked")
48+
public RowMapper<Object> getRowMapper(Class<?> returnedObjectType) {
49+
50+
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);
51+
52+
if (persistentEntity == null) {
53+
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType,
54+
converter.getConversionService());
55+
}
56+
57+
return (RowMapper<Object>) determineDefaultMapper(returnedObjectType);
58+
}
59+
60+
private RowMapper<?> determineDefaultMapper(Class<?> returnedObjectType) {
61+
62+
RowMapper<?> configuredQueryMapper = queryMappingConfiguration.getRowMapper(returnedObjectType);
63+
64+
if (configuredQueryMapper != null) {
65+
return configuredQueryMapper;
66+
}
67+
68+
EntityRowMapper<?> defaultEntityRowMapper = new EntityRowMapper<>( //
69+
context.getRequiredPersistentEntity(returnedObjectType), //
70+
converter //
71+
);
72+
73+
return new CallbackCapableRowMapper<>(defaultEntityRowMapper, publisher, entityCallbacks);
74+
}
75+
}

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

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.function.Function;
2525
import java.util.function.LongSupplier;
2626
import java.util.function.Supplier;
27-
import java.util.stream.Stream;
2827

2928
import org.springframework.core.convert.converter.Converter;
3029
import org.springframework.data.domain.Pageable;
@@ -298,11 +297,11 @@ public CachedRowMapperFactory(PartTree tree, RowMapperFactory rowMapperFactory,
298297
this.rowMapperFunction = processor -> {
299298

300299
if (tree.isCountProjection() || tree.isExistsProjection()) {
301-
return rowMapperFactory.create(resolveTypeToRead(processor));
300+
return rowMapperFactory.getRowMapper(resolveTypeToRead(processor));
302301
}
303302
Converter<Object, Object> resultProcessingConverter = new ResultProcessingConverter(processor,
304303
converter.getMappingContext(), converter.getEntityInstantiators());
305-
return new ConvertingRowMapper<>(rowMapperFactory.create(processor.getReturnedType().getDomainType()),
304+
return new ConvertingRowMapper(rowMapperFactory.getRowMapper(processor.getReturnedType().getDomainType()),
306305
resultProcessingConverter);
307306
};
308307

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.springframework.data.jdbc.repository.query;
2+
3+
import org.springframework.jdbc.core.ResultSetExtractor;
4+
import org.springframework.jdbc.core.RowMapper;
5+
6+
/**
7+
* Factory to create a {@link RowMapper} for a given class.
8+
*
9+
* @author Jens Schauder
10+
* @author Mikhail Polivakha
11+
*
12+
* @since 2.3
13+
*/
14+
public interface RowMapperFactory {
15+
16+
/**
17+
* Obtain a {@link RowMapper} based on the expected return type passed in as an argument.
18+
*
19+
* @param result must not be {@code null}.
20+
* @return a {@code RowMapper} producing instances of {@code result}.
21+
*/
22+
RowMapper<Object> getRowMapper(Class<?> result);
23+
24+
/**
25+
* Obtain a {@link RowMapper} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
26+
*
27+
* @param reference must not be {@code null}.
28+
* @since 3.4
29+
*/
30+
default RowMapper<Object> getRowMapper(String reference) {
31+
throw new UnsupportedOperationException("getRowMapper by reference is not supported");
32+
}
33+
34+
/**
35+
* Obtain a {@code ResultSetExtractor} from some other source, typically a {@link org.springframework.beans.factory.BeanFactory}.
36+
*
37+
* @param reference must not be {@code null}.
38+
* @since 3.4
39+
*/
40+
default ResultSetExtractor<Object> getResultSetExtractor(String reference) {
41+
throw new UnsupportedOperationException("getResultSetExtractor by reference is not supported");
42+
}
43+
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public StringBasedJdbcQuery(String query, JdbcQueryMethod queryMethod, NamedPara
179179
}
180180

181181
this.cachedRowMapperFactory = new CachedRowMapperFactory(
182-
() -> rowMapperFactory.create(queryMethod.getResultProcessor().getReturnedType().getReturnedType()));
182+
() -> rowMapperFactory.getRowMapper(queryMethod.getResultProcessor().getReturnedType().getReturnedType()));
183183
this.cachedResultSetExtractorFactory = new CachedResultSetExtractorFactory(
184184
this.cachedRowMapperFactory::getRowMapper);
185185

@@ -376,11 +376,11 @@ RowMapper<Object> determineRowMapper(ResultProcessor resultProcessor, boolean ha
376376

377377
if (hasDynamicProjection) {
378378

379-
RowMapper<Object> rowMapperToUse = rowMapperFactory.create(resultProcessor.getReturnedType().getDomainType());
379+
RowMapper<Object> rowMapperToUse = rowMapperFactory.getRowMapper(resultProcessor.getReturnedType().getDomainType());
380380

381381
ResultProcessingConverter converter = new ResultProcessingConverter(resultProcessor,
382382
this.converter.getMappingContext(), this.converter.getEntityInstantiators());
383-
return new ConvertingRowMapper<>(rowMapperToUse, converter);
383+
return new ConvertingRowMapper(rowMapperToUse, converter);
384384
}
385385

386386
return cachedRowMapperFactory.getRowMapper();

0 commit comments

Comments
 (0)