37
37
import org .springframework .data .relational .core .sqlgeneration .SqlGenerator ;
38
38
import org .springframework .data .relational .domain .RowDocument ;
39
39
import org .springframework .data .util .Streamable ;
40
+ import org .springframework .jdbc .core .ResultSetExtractor ;
40
41
import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
41
42
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
42
43
import org .springframework .lang .Nullable ;
43
- import org .springframework .util .Assert ;
44
44
45
45
/**
46
46
* Reads complete Aggregates from the database, by generating appropriate SQL using a {@link SingleQuerySqlGenerator}
53
53
* @author Mark Paluch
54
54
* @since 3.2
55
55
*/
56
- class AggregateReader <T > {
56
+ class AggregateReader <T > implements PathToColumnMapping {
57
57
58
58
private final RelationalPersistentEntity <T > aggregate ;
59
59
private final Table table ;
60
60
private final SqlGenerator sqlGenerator ;
61
61
private final JdbcConverter converter ;
62
62
private final NamedParameterJdbcOperations jdbcTemplate ;
63
+ private final AliasFactory aliasFactory ;
63
64
private final RowDocumentResultSetExtractor extractor ;
64
65
65
66
AggregateReader (Dialect dialect , JdbcConverter converter , AliasFactory aliasFactory ,
@@ -70,8 +71,25 @@ class AggregateReader<T> {
70
71
this .jdbcTemplate = jdbcTemplate ;
71
72
this .table = Table .create (aggregate .getQualifiedTableName ());
72
73
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 );
75
93
}
76
94
77
95
@ Nullable
@@ -84,30 +102,34 @@ public T findById(Object id) {
84
102
85
103
@ Nullable
86
104
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 );
96
106
}
97
107
98
108
public List <T > findAllById (Iterable <?> ids ) {
99
109
100
110
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 ));
102
112
103
113
return findAll (query );
104
114
}
105
115
116
+ @ SuppressWarnings ("ConstantConditions" )
117
+ public List <T > findAll () {
118
+ return jdbcTemplate .query (sqlGenerator .findAll (), this ::extractAll );
119
+ }
120
+
106
121
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 ) {
107
127
108
128
MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
109
129
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 );
111
133
}
112
134
113
135
@ Nullable
@@ -128,7 +150,7 @@ private Condition createCondition(Query query, MapSqlParameterSource parameterSo
128
150
*
129
151
* @param rs the {@link ResultSet} from which to extract the data. Must not be {(}@literal null}.
130
152
* @return a {@code List} of aggregates, fully converted.
131
- * @throws SQLException
153
+ * @throws SQLException on underlying JDBC errors.
132
154
*/
133
155
private List <T > extractAll (ResultSet rs ) throws SQLException {
134
156
@@ -146,10 +168,10 @@ private List<T> extractAll(ResultSet rs) throws SQLException {
146
168
* {@link RowDocumentResultSetExtractor} and the {@link JdbcConverter}. When used as a method reference this conforms
147
169
* to the {@link org.springframework.jdbc.core.ResultSetExtractor} contract.
148
170
*
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}.
150
172
* @return The single instance when the conversion results in exactly one instance. If the {@literal ResultSet} is
151
173
* empty, null is returned.
152
- * @throws SQLException
174
+ * @throws SQLException on underlying JDBC errors.
153
175
* @throws IncorrectResultSizeDataAccessException when the conversion yields more than one instance.
154
176
*/
155
177
@ Nullable
@@ -167,21 +189,4 @@ private T extractZeroOrOne(ResultSet rs) throws SQLException {
167
189
return null ;
168
190
}
169
191
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
-
187
192
}
0 commit comments