17
17
18
18
import io .r2dbc .spi .Row ;
19
19
import io .r2dbc .spi .RowMetadata ;
20
- import io .r2dbc .spi .Statement ;
21
20
22
21
import java .util .ArrayList ;
23
- import java .util .Collection ;
24
22
import java .util .Collections ;
25
23
import java .util .List ;
26
- import java .util .OptionalLong ;
27
- import java .util .Set ;
28
24
import java .util .function .BiFunction ;
29
25
import java .util .function .Function ;
30
26
31
27
import org .springframework .dao .InvalidDataAccessResourceUsageException ;
32
28
import org .springframework .data .convert .CustomConversions .StoreConversions ;
33
- import org .springframework .data .domain .Pageable ;
34
29
import org .springframework .data .domain .Sort ;
35
30
import org .springframework .data .domain .Sort .Order ;
36
31
import org .springframework .data .mapping .context .MappingContext ;
44
39
import org .springframework .data .r2dbc .function .convert .MappingR2dbcConverter ;
45
40
import org .springframework .data .r2dbc .function .convert .R2dbcConverter ;
46
41
import org .springframework .data .r2dbc .function .convert .R2dbcCustomConversions ;
47
- import org .springframework .data .r2dbc .function .query .ConditionBindings ;
42
+ import org .springframework .data .r2dbc .function .query .BoundCondition ;
48
43
import org .springframework .data .r2dbc .function .query .Criteria ;
49
44
import org .springframework .data .r2dbc .function .query .CriteriaMapper ;
50
- import org .springframework .data .r2dbc .support .StatementRenderUtil ;
51
45
import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
52
46
import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
53
47
import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
54
- import org .springframework .data .relational .core .sql .Expression ;
55
- import org .springframework .data .relational .core .sql .OrderByField ;
56
- import org .springframework .data .relational .core .sql .Select ;
57
- import org .springframework .data .relational .core .sql .SelectBuilder ;
58
- import org .springframework .data .relational .core .sql .StatementBuilder ;
59
48
import org .springframework .data .relational .core .sql .Table ;
60
49
import org .springframework .data .relational .core .sql .render .NamingStrategies ;
61
50
import org .springframework .data .relational .core .sql .render .RenderContext ;
@@ -100,14 +89,6 @@ private static R2dbcConverter createConverter(Dialect dialect) {
100
89
return new MappingR2dbcConverter (context , customConversions );
101
90
}
102
91
103
- public R2dbcConverter getConverter () {
104
- return converter ;
105
- }
106
-
107
- public MappingContext <RelationalPersistentEntity <?>, ? extends RelationalPersistentProperty > getMappingContext () {
108
- return mappingContext ;
109
- }
110
-
111
92
/**
112
93
* Creates a new {@link DefaultReactiveDataAccessStrategy} given {@link Dialect} and {@link R2dbcConverter}.
113
94
*
@@ -222,7 +203,7 @@ private SettableValue getArrayValue(SettableValue value, RelationalPersistentPro
222
203
* @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getMappedSort(java.lang.Class, org.springframework.data.domain.Sort)
223
204
*/
224
205
@ Override
225
- public Sort getMappedSort (Class <?> typeToRead , Sort sort ) {
206
+ public Sort getMappedSort (Sort sort , Class <?> typeToRead ) {
226
207
227
208
RelationalPersistentEntity <?> entity = getPersistentEntity (typeToRead );
228
209
if (entity == null ) {
@@ -245,105 +226,28 @@ public Sort getMappedSort(Class<?> typeToRead, Sort sort) {
245
226
return Sort .by (mappedOrder );
246
227
}
247
228
229
+ /*
230
+ * (non-Javadoc)
231
+ * @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getMappedCriteria(org.springframework.data.r2dbc.function.query.Criteria, org.springframework.data.relational.core.sql.Table)
232
+ */
248
233
@ Override
249
- public PreparedOperation <Select > select (String tableName , @ Nullable Class <?> entityType , Set <String > columns ,
250
- Sort sort , Pageable page , Criteria criteria ) {
251
-
252
- Table table = Table .create (tableName );
253
-
254
- Collection <? extends Expression > selectList ;
255
-
256
- if (columns .isEmpty ()) {
257
- selectList = Collections .singletonList (table .asterisk ());
258
- } else {
259
- selectList = table .columns (columns );
260
- }
261
-
262
- SelectBuilder .SelectFromAndJoin selectBuilder = StatementBuilder //
263
- .select (selectList ) //
264
- .from (table );//
265
-
266
- ConditionBindings conditionBindings ;
267
-
268
- if (criteria != null ) {
269
-
270
- BindMarkers bindMarkers = dialect .getBindMarkersFactory ().create ();
271
- conditionBindings = criteriaMapper .getMappedObject (bindMarkers , criteria , table ,
272
- entityType != null ? getRequiredPersistentEntity (entityType ) : null );
273
-
274
- selectBuilder .where (conditionBindings .getCondition ());
275
- } else {
276
- conditionBindings = null ;
277
- }
278
-
279
- selectBuilder .orderBy (createOrderByFields (table , sort ));
280
-
281
- OptionalLong limit = OptionalLong .empty ();
282
- OptionalLong offset = OptionalLong .empty ();
283
-
284
- if (page .isPaged ()) {
285
- limit = OptionalLong .of (page .getPageSize ());
286
- offset = OptionalLong .of (page .getOffset ());
287
- }
288
-
289
- Select select = selectBuilder .build ();
290
- String sql = StatementRenderUtil .render (select , limit , offset , this .dialect );
291
-
292
- if (conditionBindings == null ) {
293
-
294
- return new PreparedOperation <Select >() {
295
- @ Override
296
- public Select getSource () {
297
- return select ;
298
- }
299
-
300
- @ Override
301
- public Statement bind (Statement to ) {
302
- return to ;
303
- }
304
-
305
- @ Override
306
- public String toQuery () {
307
- return sql ;
308
- }
309
- };
310
- }
311
-
312
- return new PreparedOperation <Select >() {
313
- @ Override
314
- public Select getSource () {
315
- return select ;
316
- }
317
-
318
- @ Override
319
- public Statement bind (Statement to ) {
320
- conditionBindings .getBindings ().apply (to );
321
- return to ;
322
- }
323
-
324
- @ Override
325
- public String toQuery () {
326
- return sql ;
327
- }
328
- };
234
+ public BoundCondition getMappedCriteria (Criteria criteria , Table table ) {
235
+ return getMappedCriteria (criteria , table , null );
329
236
}
330
237
331
- private Collection <? extends OrderByField > createOrderByFields (Table table , Sort sortToUse ) {
332
-
333
- List <OrderByField > fields = new ArrayList <>();
334
-
335
- for (Sort .Order order : sortToUse ) {
238
+ /*
239
+ * (non-Javadoc)
240
+ * @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getMappedCriteria(org.springframework.data.r2dbc.function.query.Criteria, org.springframework.data.relational.core.sql.Table, java.lang.Class)
241
+ */
242
+ @ Override
243
+ public BoundCondition getMappedCriteria (Criteria criteria , Table table , @ Nullable Class <?> typeToRead ) {
336
244
337
- OrderByField orderByField = OrderByField . from ( table . column ( order . getProperty ()) );
245
+ BindMarkers bindMarkers = this . dialect . getBindMarkersFactory (). create ( );
338
246
339
- if (order .getDirection () != null ) {
340
- fields .add (order .isAscending () ? orderByField .asc () : orderByField .desc ());
341
- } else {
342
- fields .add (orderByField );
343
- }
344
- }
247
+ RelationalPersistentEntity <?> entity = typeToRead != null ? mappingContext .getRequiredPersistentEntity (typeToRead )
248
+ : null ;
345
249
346
- return fields ;
250
+ return criteriaMapper . getMappedObject ( bindMarkers , criteria , table , entity ) ;
347
251
}
348
252
349
253
/*
@@ -382,6 +286,18 @@ public BindMarkersFactory getBindMarkersFactory() {
382
286
return dialect .getBindMarkersFactory ();
383
287
}
384
288
289
+ /*
290
+ * (non-Javadoc)
291
+ * @see org.springframework.data.r2dbc.function.ReactiveDataAccessStrategy#getConverter()
292
+ */
293
+ public R2dbcConverter getConverter () {
294
+ return converter ;
295
+ }
296
+
297
+ public MappingContext <RelationalPersistentEntity <?>, ? extends RelationalPersistentProperty > getMappingContext () {
298
+ return mappingContext ;
299
+ }
300
+
385
301
private RelationalPersistentEntity <?> getRequiredPersistentEntity (Class <?> typeToRead ) {
386
302
return mappingContext .getRequiredPersistentEntity (typeToRead );
387
303
}
0 commit comments