Skip to content

Commit 8fa9e3e

Browse files
committed
Polishing.
Simplify type and interface arrangement. See #1601 Original pull request: #1617
1 parent 7b27d0e commit 8fa9e3e

File tree

4 files changed

+63
-45
lines changed

4 files changed

+63
-45
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/AggregateReader.java

+40-35
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
import org.springframework.data.relational.core.sqlgeneration.SqlGenerator;
3838
import org.springframework.data.relational.domain.RowDocument;
3939
import org.springframework.data.util.Streamable;
40+
import org.springframework.jdbc.core.ResultSetExtractor;
4041
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
4142
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
4243
import org.springframework.lang.Nullable;
43-
import org.springframework.util.Assert;
4444

4545
/**
4646
* Reads complete Aggregates from the database, by generating appropriate SQL using a {@link SingleQuerySqlGenerator}
@@ -53,13 +53,14 @@
5353
* @author Mark Paluch
5454
* @since 3.2
5555
*/
56-
class AggregateReader<T> {
56+
class AggregateReader<T> implements PathToColumnMapping {
5757

5858
private final RelationalPersistentEntity<T> aggregate;
5959
private final Table table;
6060
private final SqlGenerator sqlGenerator;
6161
private final JdbcConverter converter;
6262
private final NamedParameterJdbcOperations jdbcTemplate;
63+
private final AliasFactory aliasFactory;
6364
private final RowDocumentResultSetExtractor extractor;
6465

6566
AggregateReader(Dialect dialect, JdbcConverter converter, AliasFactory aliasFactory,
@@ -70,8 +71,25 @@ class AggregateReader<T> {
7071
this.jdbcTemplate = jdbcTemplate;
7172
this.table = Table.create(aggregate.getQualifiedTableName());
7273
this.sqlGenerator = new SingleQuerySqlGenerator(converter.getMappingContext(), aliasFactory, dialect, aggregate);
73-
this.extractor = new RowDocumentResultSetExtractor(converter.getMappingContext(),
74-
createPathToColumnMapping(aliasFactory));
74+
this.aliasFactory = aliasFactory;
75+
this.extractor = new RowDocumentResultSetExtractor(converter.getMappingContext(), this);
76+
}
77+
78+
@Override
79+
public String column(AggregatePath path) {
80+
81+
String alias = aliasFactory.getColumnAlias(path);
82+
83+
if (alias == null) {
84+
throw new IllegalStateException(String.format("Alias for '%s' must not be null", path));
85+
}
86+
87+
return alias;
88+
}
89+
90+
@Override
91+
public String keyColumn(AggregatePath path) {
92+
return aliasFactory.getKeyAlias(path);
7593
}
7694

7795
@Nullable
@@ -84,30 +102,34 @@ public T findById(Object id) {
84102

85103
@Nullable
86104
public T findOne(Query query) {
87-
88-
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
89-
Condition condition = createCondition(query, parameterSource);
90-
91-
return jdbcTemplate.query(sqlGenerator.findAll(condition), parameterSource, this::extractZeroOrOne);
92-
}
93-
94-
public List<T> findAll() {
95-
return jdbcTemplate.query(sqlGenerator.findAll(), this::extractAll);
105+
return doFind(query, this::extractZeroOrOne);
96106
}
97107

98108
public List<T> findAllById(Iterable<?> ids) {
99109

100110
Collection<?> identifiers = ids instanceof Collection<?> idl ? idl : Streamable.of(ids).toList();
101-
Query query = Query.query(Criteria.where(aggregate.getRequiredIdProperty().getName()).in(identifiers)).limit(1);
111+
Query query = Query.query(Criteria.where(aggregate.getRequiredIdProperty().getName()).in(identifiers));
102112

103113
return findAll(query);
104114
}
105115

116+
@SuppressWarnings("ConstantConditions")
117+
public List<T> findAll() {
118+
return jdbcTemplate.query(sqlGenerator.findAll(), this::extractAll);
119+
}
120+
106121
public List<T> findAll(Query query) {
122+
return doFind(query, this::extractAll);
123+
}
124+
125+
@SuppressWarnings("ConstantConditions")
126+
private <R> R doFind(Query query, ResultSetExtractor<R> extractor) {
107127

108128
MapSqlParameterSource parameterSource = new MapSqlParameterSource();
109129
Condition condition = createCondition(query, parameterSource);
110-
return jdbcTemplate.query(sqlGenerator.findAll(condition), parameterSource, this::extractAll);
130+
String sql = sqlGenerator.findAll(condition);
131+
132+
return jdbcTemplate.query(sql, parameterSource, extractor);
111133
}
112134

113135
@Nullable
@@ -128,7 +150,7 @@ private Condition createCondition(Query query, MapSqlParameterSource parameterSo
128150
*
129151
* @param rs the {@link ResultSet} from which to extract the data. Must not be {(}@literal null}.
130152
* @return a {@code List} of aggregates, fully converted.
131-
* @throws SQLException
153+
* @throws SQLException on underlying JDBC errors.
132154
*/
133155
private List<T> extractAll(ResultSet rs) throws SQLException {
134156

@@ -146,10 +168,10 @@ private List<T> extractAll(ResultSet rs) throws SQLException {
146168
* {@link RowDocumentResultSetExtractor} and the {@link JdbcConverter}. When used as a method reference this conforms
147169
* to the {@link org.springframework.jdbc.core.ResultSetExtractor} contract.
148170
*
149-
* @param @param rs the {@link ResultSet} from which to extract the data. Must not be {(}@literal null}.
171+
* @param rs the {@link ResultSet} from which to extract the data. Must not be {(}@literal null}.
150172
* @return The single instance when the conversion results in exactly one instance. If the {@literal ResultSet} is
151173
* empty, null is returned.
152-
* @throws SQLException
174+
* @throws SQLException on underlying JDBC errors.
153175
* @throws IncorrectResultSizeDataAccessException when the conversion yields more than one instance.
154176
*/
155177
@Nullable
@@ -167,21 +189,4 @@ private T extractZeroOrOne(ResultSet rs) throws SQLException {
167189
return null;
168190
}
169191

170-
private PathToColumnMapping createPathToColumnMapping(AliasFactory aliasFactory) {
171-
return new PathToColumnMapping() {
172-
@Override
173-
public String column(AggregatePath path) {
174-
175-
String alias = aliasFactory.getColumnAlias(path);
176-
Assert.notNull(alias, () -> "alias for >" + path + "< must not be null");
177-
return alias;
178-
}
179-
180-
@Override
181-
public String keyColumn(AggregatePath path) {
182-
return aliasFactory.getKeyAlias(path);
183-
}
184-
};
185-
}
186-
187192
}

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/RowDocumentResultSetExtractor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private class RowDocumentIterator implements Iterator<RowDocument> {
159159
*/
160160
private boolean hasNext;
161161

162-
RowDocumentIterator(RelationalPersistentEntity<?> entity, ResultSet resultSet) throws SQLException {
162+
RowDocumentIterator(RelationalPersistentEntity<?> entity, ResultSet resultSet) {
163163

164164
ResultSetAdapter adapter = ResultSetAdapter.INSTANCE;
165165

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SingleQueryDataAccessStrategy.java

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

1717
package org.springframework.data.jdbc.core.convert;
1818

19+
import java.util.List;
1920
import java.util.Optional;
2021

2122
import org.springframework.data.domain.Pageable;
@@ -56,22 +57,22 @@ public <T> T findById(Object id, Class<T> domainType) {
5657
}
5758

5859
@Override
59-
public <T> Iterable<T> findAll(Class<T> domainType) {
60+
public <T> List<T> findAll(Class<T> domainType) {
6061
return getReader(domainType).findAll();
6162
}
6263

6364
@Override
64-
public <T> Iterable<T> findAllById(Iterable<?> ids, Class<T> domainType) {
65+
public <T> List<T> findAllById(Iterable<?> ids, Class<T> domainType) {
6566
return getReader(domainType).findAllById(ids);
6667
}
6768

6869
@Override
69-
public <T> Iterable<T> findAll(Class<T> domainType, Sort sort) {
70+
public <T> List<T> findAll(Class<T> domainType, Sort sort) {
7071
throw new UnsupportedOperationException();
7172
}
7273

7374
@Override
74-
public <T> Iterable<T> findAll(Class<T> domainType, Pageable pageable) {
75+
public <T> List<T> findAll(Class<T> domainType, Pageable pageable) {
7576
throw new UnsupportedOperationException();
7677
}
7778

@@ -81,12 +82,12 @@ public <T> Optional<T> findOne(Query query, Class<T> domainType) {
8182
}
8283

8384
@Override
84-
public <T> Iterable<T> findAll(Query query, Class<T> domainType) {
85+
public <T> List<T> findAll(Query query, Class<T> domainType) {
8586
return getReader(domainType).findAll(query);
8687
}
8788

8889
@Override
89-
public <T> Iterable<T> findAll(Query query, Class<T> domainType, Pageable pageable) {
90+
public <T> List<T> findAll(Query query, Class<T> domainType, Pageable pageable) {
9091
throw new UnsupportedOperationException();
9192
}
9293

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/core/AbstractJdbcAggregateTemplateIntegrationTests.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@
2323
import static org.springframework.data.jdbc.testing.TestDatabaseFeatures.Feature.*;
2424

2525
import java.time.LocalDateTime;
26-
import java.util.*;
2726
import java.util.ArrayList;
27+
import java.util.Collections;
28+
import java.util.HashMap;
29+
import java.util.HashSet;
30+
import java.util.Iterator;
31+
import java.util.List;
32+
import java.util.Map;
33+
import java.util.Objects;
34+
import java.util.Optional;
35+
import java.util.Set;
2836
import java.util.function.Function;
2937
import java.util.stream.IntStream;
3038

@@ -49,6 +57,7 @@
4957
import org.springframework.data.jdbc.core.convert.JdbcConverter;
5058
import org.springframework.data.jdbc.testing.EnabledOnFeature;
5159
import org.springframework.data.jdbc.testing.IntegrationTest;
60+
import org.springframework.data.jdbc.testing.TestClass;
5261
import org.springframework.data.jdbc.testing.TestConfiguration;
5362
import org.springframework.data.jdbc.testing.TestDatabaseFeatures;
5463
import org.springframework.data.mapping.context.InvalidPersistentPropertyPath;
@@ -63,6 +72,7 @@
6372
import org.springframework.data.relational.core.query.Query;
6473
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
6574
import org.springframework.test.context.ActiveProfiles;
75+
import org.springframework.test.context.ContextConfiguration;
6676

6777
/**
6878
* Integration tests for {@link JdbcAggregateTemplate}.
@@ -1927,8 +1937,8 @@ static class WithInsertOnly {
19271937
static class Config {
19281938

19291939
@Bean
1930-
Class<?> testClass() {
1931-
return JdbcAggregateTemplateIntegrationTests.class;
1940+
TestClass testClass() {
1941+
return TestClass.of(JdbcAggregateTemplateIntegrationTests.class);
19321942
}
19331943

19341944
@Bean
@@ -1938,9 +1948,11 @@ JdbcAggregateOperations operations(ApplicationEventPublisher publisher, Relation
19381948
}
19391949
}
19401950

1951+
@ContextConfiguration(classes = Config.class)
19411952
static class JdbcAggregateTemplateIntegrationTests extends AbstractJdbcAggregateTemplateIntegrationTests {}
19421953

19431954
@ActiveProfiles(value = PROFILE_SINGLE_QUERY_LOADING)
1955+
@ContextConfiguration(classes = Config.class)
19441956
static class JdbcAggregateTemplateSingleQueryLoadingIntegrationTests
19451957
extends AbstractJdbcAggregateTemplateIntegrationTests {
19461958

0 commit comments

Comments
 (0)