18
18
import java .sql .ResultSet ;
19
19
import java .sql .SQLException ;
20
20
import java .util .ArrayList ;
21
+ import java .util .Collection ;
21
22
import java .util .Iterator ;
22
23
import java .util .List ;
23
- import java .util .Map ;
24
24
import java .util .Optional ;
25
- import java .util .function .BiFunction ;
26
25
27
26
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
28
27
import org .springframework .data .relational .core .dialect .Dialect ;
29
28
import org .springframework .data .relational .core .mapping .AggregatePath ;
30
29
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
30
+ import org .springframework .data .relational .core .query .Criteria ;
31
31
import org .springframework .data .relational .core .query .CriteriaDefinition ;
32
32
import org .springframework .data .relational .core .query .Query ;
33
33
import org .springframework .data .relational .core .sql .Condition ;
36
36
import org .springframework .data .relational .core .sqlgeneration .SingleQuerySqlGenerator ;
37
37
import org .springframework .data .relational .core .sqlgeneration .SqlGenerator ;
38
38
import org .springframework .data .relational .domain .RowDocument ;
39
+ import org .springframework .data .util .Streamable ;
39
40
import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
40
41
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
41
42
import org .springframework .lang .Nullable ;
42
43
import org .springframework .util .Assert ;
43
44
44
45
/**
45
46
* Reads complete Aggregates from the database, by generating appropriate SQL using a {@link SingleQuerySqlGenerator}
46
- * through {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. Results are converterd into an
47
+ * through {@link org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate}. Results are converted into an
47
48
* intermediate {@link RowDocumentResultSetExtractor RowDocument} and mapped via
48
49
* {@link org.springframework.data.relational.core.conversion.RelationalConverter#read(Class, RowDocument)}.
49
50
*
55
56
class AggregateReader <T > {
56
57
57
58
private final RelationalPersistentEntity <T > aggregate ;
58
- private final org .springframework .data .relational .core .sqlgeneration .SqlGenerator sqlGenerator ;
59
+ private final Table table ;
60
+ private final SqlGenerator sqlGenerator ;
59
61
private final JdbcConverter converter ;
60
62
private final NamedParameterJdbcOperations jdbcTemplate ;
61
63
private final RowDocumentResultSetExtractor extractor ;
@@ -66,6 +68,7 @@ class AggregateReader<T> {
66
68
this .converter = converter ;
67
69
this .aggregate = aggregate ;
68
70
this .jdbcTemplate = jdbcTemplate ;
71
+ this .table = Table .create (aggregate .getQualifiedTableName ());
69
72
70
73
this .sqlGenerator = new CachingSqlGenerator (
71
74
new SingleQuerySqlGenerator (converter .getMappingContext (), aliasFactory , dialect , aggregate ));
@@ -74,62 +77,58 @@ class AggregateReader<T> {
74
77
createPathToColumnMapping (aliasFactory ));
75
78
}
76
79
77
- public List <T > findAll () {
78
- return jdbcTemplate .query (sqlGenerator .findAll (), this ::extractAll );
79
- }
80
-
81
80
@ Nullable
82
81
public T findById (Object id ) {
83
82
84
- id = converter . writeValue ( id , aggregate .getRequiredIdProperty ().getTypeInformation () );
83
+ Query query = Query . query ( Criteria . where ( aggregate .getRequiredIdProperty ().getName ()). is ( id )). limit ( 1 );
85
84
86
- return jdbcTemplate . query ( sqlGenerator . findById (), Map . of ( "id" , id ), this :: extractZeroOrOne );
85
+ return findOne ( query );
87
86
}
88
87
89
- public Iterable <T > findAllById (Iterable <?> ids ) {
88
+ @ Nullable
89
+ public T findOne (Query query ) {
90
90
91
- List <Object > convertedIds = new ArrayList <>();
92
- for (Object id : ids ) {
93
- convertedIds .add (converter .writeValue (id , aggregate .getRequiredIdProperty ().getTypeInformation ()));
94
- }
91
+ MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
92
+ Condition condition = createCondition (query , parameterSource );
95
93
96
- return jdbcTemplate .query (sqlGenerator .findAllById ( ), Map . of ( "ids" , convertedIds ), this ::extractAll );
94
+ return jdbcTemplate .query (sqlGenerator .findAll ( condition ), parameterSource , this ::extractZeroOrOne );
97
95
}
98
96
99
- public Iterable <T > findAllBy (Query query ) {
97
+ public List <T > findAll () {
98
+ return jdbcTemplate .query (sqlGenerator .findAll (), this ::extractAll );
99
+ }
100
100
101
- MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
102
- BiFunction <Table , RelationalPersistentEntity , Condition > condition = createConditionSource (query , parameterSource );
103
- return jdbcTemplate .query (sqlGenerator .findAllByCondition (condition ), parameterSource , this ::extractAll );
101
+ public List <T > findAllById (Iterable <?> ids ) {
102
+
103
+ Collection <?> identifiers = ids instanceof Collection <?> idl ? idl : Streamable .of (ids ).toList ();
104
+ Query query = Query .query (Criteria .where (aggregate .getRequiredIdProperty ().getName ()).in (identifiers )).limit (1 );
105
+
106
+ return findAll (query );
104
107
}
105
108
106
- public Optional <T > findOneByQuery (Query query ) {
107
-
108
- MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
109
- BiFunction <Table , RelationalPersistentEntity , Condition > condition = createConditionSource (query , parameterSource );
109
+ public List <T > findAll (Query query ) {
110
110
111
- return Optional .ofNullable (
112
- jdbcTemplate .query (sqlGenerator .findAllByCondition (condition ), parameterSource , this ::extractZeroOrOne ));
111
+ MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
112
+ Condition condition = createCondition (query , parameterSource );
113
+ return jdbcTemplate .query (sqlGenerator .findAll (condition ), parameterSource , this ::extractAll );
113
114
}
114
115
115
- private BiFunction <Table , RelationalPersistentEntity , Condition > createConditionSource (Query query , MapSqlParameterSource parameterSource ) {
116
+ @ Nullable
117
+ private Condition createCondition (Query query , MapSqlParameterSource parameterSource ) {
116
118
117
119
QueryMapper queryMapper = new QueryMapper (converter );
118
120
119
- BiFunction <Table , RelationalPersistentEntity , Condition > condition = (table , aggregate ) -> {
120
- Optional <CriteriaDefinition > criteria = query .getCriteria ();
121
- return criteria
122
- .map (criteriaDefinition -> queryMapper .getMappedObject (parameterSource , criteriaDefinition , table , aggregate ))
123
- .orElse (null );
124
- };
125
- return condition ;
121
+ Optional <CriteriaDefinition > criteria = query .getCriteria ();
122
+ return criteria
123
+ .map (criteriaDefinition -> queryMapper .getMappedObject (parameterSource , criteriaDefinition , table , aggregate ))
124
+ .orElse (null );
126
125
}
127
126
128
127
/**
129
128
* Extracts a list of aggregates from the given {@link ResultSet} by utilizing the
130
129
* {@link RowDocumentResultSetExtractor} and the {@link JdbcConverter}. When used as a method reference this conforms
131
130
* to the {@link org.springframework.jdbc.core.ResultSetExtractor} contract.
132
- *
131
+ *
133
132
* @param rs the {@link ResultSet} from which to extract the data. Must not be {(}@literal null}.
134
133
* @return a {@code List} of aggregates, fully converted.
135
134
* @throws SQLException
@@ -195,21 +194,15 @@ public String keyColumn(AggregatePath path) {
195
194
* @author Jens Schauder
196
195
* @since 3.2
197
196
*/
198
- static class CachingSqlGenerator implements org .springframework .data .relational .core .sqlgeneration .SqlGenerator {
199
-
200
- private final org .springframework .data .relational .core .sqlgeneration .SqlGenerator delegate ;
197
+ static class CachingSqlGenerator implements SqlGenerator {
201
198
199
+ private final SqlGenerator delegate ;
202
200
private final String findAll ;
203
- private final String findById ;
204
- private final String findAllById ;
205
201
206
202
public CachingSqlGenerator (SqlGenerator delegate ) {
207
203
208
204
this .delegate = delegate ;
209
-
210
- findAll = delegate .findAll ();
211
- findById = delegate .findById ();
212
- findAllById = delegate .findAllById ();
205
+ this .findAll = delegate .findAll ();
213
206
}
214
207
215
208
@ Override
@@ -218,18 +211,8 @@ public String findAll() {
218
211
}
219
212
220
213
@ Override
221
- public String findById () {
222
- return findById ;
223
- }
224
-
225
- @ Override
226
- public String findAllById () {
227
- return findAllById ;
228
- }
229
-
230
- @ Override
231
- public String findAllByCondition (BiFunction <Table , RelationalPersistentEntity , Condition > conditionSource ) {
232
- return delegate .findAllByCondition (conditionSource );
214
+ public String findAll (@ Nullable Condition condition ) {
215
+ return delegate .findAll (condition );
233
216
}
234
217
235
218
@ Override
0 commit comments