21
21
import java .util .Iterator ;
22
22
import java .util .List ;
23
23
import java .util .Map ;
24
+ import java .util .Optional ;
25
+ import java .util .function .BiFunction ;
24
26
25
27
import org .springframework .dao .IncorrectResultSizeDataAccessException ;
26
28
import org .springframework .data .relational .core .dialect .Dialect ;
27
29
import org .springframework .data .relational .core .mapping .AggregatePath ;
28
30
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
31
+ import org .springframework .data .relational .core .query .CriteriaDefinition ;
32
+ import org .springframework .data .relational .core .query .Query ;
33
+ import org .springframework .data .relational .core .sql .Condition ;
34
+ import org .springframework .data .relational .core .sql .Table ;
29
35
import org .springframework .data .relational .core .sqlgeneration .AliasFactory ;
30
36
import org .springframework .data .relational .core .sqlgeneration .SingleQuerySqlGenerator ;
31
37
import org .springframework .data .relational .core .sqlgeneration .SqlGenerator ;
32
38
import org .springframework .data .relational .domain .RowDocument ;
39
+ import org .springframework .jdbc .core .namedparam .MapSqlParameterSource ;
33
40
import org .springframework .jdbc .core .namedparam .NamedParameterJdbcOperations ;
34
41
import org .springframework .lang .Nullable ;
35
42
import org .springframework .util .Assert ;
@@ -89,6 +96,35 @@ public Iterable<T> findAllById(Iterable<?> ids) {
89
96
return jdbcTemplate .query (sqlGenerator .findAllById (), Map .of ("ids" , convertedIds ), this ::extractAll );
90
97
}
91
98
99
+ public Iterable <T > findAllBy (Query query ) {
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 );
104
+ }
105
+
106
+ public Optional <T > findOneByQuery (Query query ) {
107
+
108
+ MapSqlParameterSource parameterSource = new MapSqlParameterSource ();
109
+ BiFunction <Table , RelationalPersistentEntity , Condition > condition = createConditionSource (query , parameterSource );
110
+
111
+ return Optional .ofNullable (
112
+ jdbcTemplate .query (sqlGenerator .findAllByCondition (condition ), parameterSource , this ::extractZeroOrOne ));
113
+ }
114
+
115
+ private BiFunction <Table , RelationalPersistentEntity , Condition > createConditionSource (Query query , MapSqlParameterSource parameterSource ) {
116
+
117
+ QueryMapper queryMapper = new QueryMapper (converter );
118
+
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 ;
126
+ }
127
+
92
128
/**
93
129
* Extracts a list of aggregates from the given {@link ResultSet} by utilizing the
94
130
* {@link RowDocumentResultSetExtractor} and the {@link JdbcConverter}. When used as a method reference this conforms
@@ -115,7 +151,8 @@ private List<T> extractAll(ResultSet rs) throws SQLException {
115
151
* to the {@link org.springframework.jdbc.core.ResultSetExtractor} contract.
116
152
*
117
153
* @param @param rs the {@link ResultSet} from which to extract the data. Must not be {(}@literal null}.
118
- * @return The single instance when the conversion results in exactly one instance. If the {@literal ResultSet} is empty, null is returned.
154
+ * @return The single instance when the conversion results in exactly one instance. If the {@literal ResultSet} is
155
+ * empty, null is returned.
119
156
* @throws SQLException
120
157
* @throws IncorrectResultSizeDataAccessException when the conversion yields more than one instance.
121
158
*/
@@ -190,9 +227,15 @@ public String findAllById() {
190
227
return findAllById ;
191
228
}
192
229
230
+ @ Override
231
+ public String findAllByCondition (BiFunction <Table , RelationalPersistentEntity , Condition > conditionSource ) {
232
+ return delegate .findAllByCondition (conditionSource );
233
+ }
234
+
193
235
@ Override
194
236
public AliasFactory getAliasFactory () {
195
237
return delegate .getAliasFactory ();
196
238
}
239
+
197
240
}
198
241
}
0 commit comments