15
15
*/
16
16
package org .springframework .data .jdbc .repository .support ;
17
17
18
+ import java .util .ArrayList ;
19
+ import java .util .Collection ;
18
20
import java .util .Collections ;
19
21
import java .util .List ;
22
+ import java .util .function .Function ;
20
23
import java .util .function .UnaryOperator ;
21
- import java .util .stream .Collectors ;
22
24
import java .util .stream .Stream ;
23
25
import java .util .stream .StreamSupport ;
24
26
25
27
import org .springframework .data .domain .Example ;
28
+ import org .springframework .data .domain .OffsetScrollPosition ;
26
29
import org .springframework .data .domain .Page ;
27
30
import org .springframework .data .domain .Pageable ;
31
+ import org .springframework .data .domain .ScrollPosition ;
28
32
import org .springframework .data .domain .Sort ;
33
+ import org .springframework .data .domain .Window ;
29
34
import org .springframework .data .jdbc .core .JdbcAggregateOperations ;
30
35
import org .springframework .data .relational .core .query .Query ;
31
36
import org .springframework .data .relational .repository .query .RelationalExampleMapper ;
37
+ import org .springframework .util .Assert ;
32
38
33
39
/**
34
40
* {@link org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery} using {@link Example}.
35
41
*
36
42
* @author Diego Krupitza
43
+ * @author Mark Paluch
37
44
* @since 3.0
38
45
*/
39
46
class FetchableFluentQueryByExample <S , R > extends FluentQuerySupport <S , R > {
@@ -43,13 +50,13 @@ class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> {
43
50
44
51
FetchableFluentQueryByExample (Example <S > example , Class <R > resultType , RelationalExampleMapper exampleMapper ,
45
52
JdbcAggregateOperations entityOperations ) {
46
- this (example , Sort .unsorted (), resultType , Collections .emptyList (), exampleMapper , entityOperations );
53
+ this (example , Sort .unsorted (), 0 , resultType , Collections .emptyList (), exampleMapper , entityOperations );
47
54
}
48
55
49
- FetchableFluentQueryByExample (Example <S > example , Sort sort , Class < R > resultType , List < String > fieldsToInclude ,
50
- RelationalExampleMapper exampleMapper , JdbcAggregateOperations entityOperations ) {
56
+ FetchableFluentQueryByExample (Example <S > example , Sort sort , int limit , Class < R > resultType ,
57
+ List < String > fieldsToInclude , RelationalExampleMapper exampleMapper , JdbcAggregateOperations entityOperations ) {
51
58
52
- super (example , sort , resultType , fieldsToInclude );
59
+ super (example , sort , limit , resultType , fieldsToInclude );
53
60
54
61
this .exampleMapper = exampleMapper ;
55
62
this .entityOperations = entityOperations ;
@@ -71,10 +78,40 @@ public R firstValue() {
71
78
72
79
@ Override
73
80
public List <R > all () {
81
+ return findAll (createQuery ().sort (getSort ()));
82
+ }
74
83
75
- return StreamSupport
76
- .stream (this .entityOperations .findAll (createQuery ().sort (getSort ()), getExampleType ()).spliterator (), false )
77
- .map (item -> this .getConversionFunction ().apply (item )).collect (Collectors .toList ());
84
+ private List <R > findAll (Query query ) {
85
+
86
+ Function <Object , R > conversionFunction = this .getConversionFunction ();
87
+ Iterable <S > raw = this .entityOperations .findAll (query , getExampleType ());
88
+
89
+ List <R > result = new ArrayList <>(raw instanceof Collections ? ((Collection <?>) raw ).size () : 16 );
90
+
91
+ for (S s : raw ) {
92
+ result .add (conversionFunction .apply (s ));
93
+ }
94
+
95
+ return result ;
96
+ }
97
+
98
+ @ Override
99
+ public Window <R > scroll (ScrollPosition scrollPosition ) {
100
+
101
+ Assert .notNull (scrollPosition , "ScrollPosition must not be null" );
102
+
103
+ if (scrollPosition instanceof OffsetScrollPosition osp ) {
104
+
105
+ Query query = createQuery ().sort (getSort ()).offset (osp .getOffset ());
106
+
107
+ if (getLimit () > 0 ) {
108
+ query = query .limit (getLimit ());
109
+ }
110
+
111
+ return ScrollDelegate .scroll (query , this ::findAll , osp );
112
+ }
113
+
114
+ return super .scroll (scrollPosition );
78
115
}
79
116
80
117
@ Override
@@ -114,16 +151,18 @@ private Query createQuery(UnaryOperator<Query> queryCustomizer) {
114
151
query = query .columns (getFieldsToInclude ().toArray (new String [0 ]));
115
152
}
116
153
154
+ query = query .limit (getLimit ());
155
+
117
156
query = queryCustomizer .apply (query );
118
157
119
158
return query ;
120
159
}
121
160
122
161
@ Override
123
- protected <R > FluentQuerySupport <S , R > create (Example <S > example , Sort sort , Class <R > resultType ,
162
+ protected <R > FluentQuerySupport <S , R > create (Example <S > example , Sort sort , int limit , Class <R > resultType ,
124
163
List <String > fieldsToInclude ) {
125
164
126
- return new FetchableFluentQueryByExample <>(example , sort , resultType , fieldsToInclude , this .exampleMapper ,
165
+ return new FetchableFluentQueryByExample <>(example , sort , limit , resultType , fieldsToInclude , this .exampleMapper ,
127
166
this .entityOperations );
128
167
}
129
168
}
0 commit comments