2020import java .sql .ResultSet ;
2121import java .util .ArrayList ;
2222import java .util .Collection ;
23- import java .util .LinkedHashMap ;
2423import java .util .List ;
25- import java .util .Map ;
2624import java .util .function .Function ;
27- import java .util .function .IntFunction ;
2825import java .util .function .LongSupplier ;
2926import java .util .function .Supplier ;
3027
28+ import org .jspecify .annotations .NonNull ;
3129import org .jspecify .annotations .Nullable ;
3230import org .springframework .core .convert .converter .Converter ;
33- import org .springframework .data .domain .KeysetScrollPosition ;
3431import org .springframework .data .domain .Limit ;
35- import org .springframework .data .domain .OffsetScrollPosition ;
3632import org .springframework .data .domain .Pageable ;
3733import org .springframework .data .domain .ScrollPosition ;
3834import org .springframework .data .domain .Slice ;
4137import org .springframework .data .domain .Window ;
4238import org .springframework .data .jdbc .core .JdbcAggregateOperations ;
4339import org .springframework .data .jdbc .core .convert .JdbcConverter ;
44- import org .springframework .data .mapping . PersistentPropertyAccessor ;
40+ import org .springframework .data .jdbc . repository . support . ScrollDelegate ;
4541import org .springframework .data .relational .core .conversion .RelationalConverter ;
4642import org .springframework .data .relational .core .dialect .Dialect ;
4743import org .springframework .data .relational .core .mapping .RelationalMappingContext ;
4844import org .springframework .data .relational .core .mapping .RelationalPersistentEntity ;
49- import org .springframework .data .relational .core .mapping .RelationalPersistentProperty ;
5045import org .springframework .data .relational .repository .query .RelationalEntityMetadata ;
5146import org .springframework .data .relational .repository .query .RelationalParameterAccessor ;
5247import org .springframework .data .relational .repository .query .RelationalParametersParameterAccessor ;
@@ -169,7 +164,7 @@ private Sort getDynamicSort(RelationalParameterAccessor accessor) {
169164
170165 @ Override
171166 @ Nullable
172- public Object execute (Object [] values ) {
167+ public Object execute (@ Nullable Object [] values ) {
173168
174169 RelationalParametersParameterAccessor accessor = new RelationalParametersParameterAccessor (getQueryMethod (),
175170 values );
@@ -205,20 +200,20 @@ private JdbcQueryExecution<?> getQueryExecution(ResultProcessor processor,
205200
206201 if (getQueryMethod ().isScrollQuery ()) {
207202 // noinspection unchecked
208- return new ScrollQueryExecution <>((JdbcQueryExecution <Collection <Object >>) queryExecution ,
209- accessor .getScrollPosition (), this . tree . getMaxResults (), tree .getSort (), tree .getResultLimit (),
203+ return new ScrollQueryExecution <>((JdbcQueryExecution <@ NonNull Collection <Object >>) queryExecution ,
204+ accessor .getScrollPosition (), tree .getSort (), tree .getResultLimit (),
210205 getQueryMethod ().getEntityInformation ().getTableEntity ());
211206 }
212207
213208 if (getQueryMethod ().isSliceQuery ()) {
214209 // noinspection unchecked
215- return new SliceQueryExecution <>((JdbcQueryExecution <Collection <Object >>) queryExecution , accessor .getPageable ());
210+ return new SliceQueryExecution <>((JdbcQueryExecution <@ NonNull Collection <Object >>) queryExecution , accessor .getPageable ());
216211 }
217212
218213 if (getQueryMethod ().isPageQuery ()) {
219214
220215 // noinspection unchecked
221- return new PageQueryExecution <>((JdbcQueryExecution <Collection <Object >>) queryExecution , accessor .getPageable (),
216+ return new PageQueryExecution <>((JdbcQueryExecution <@ NonNull Collection <Object >>) queryExecution , accessor .getPageable (),
222217 () -> {
223218
224219 RelationalEntityMetadata <?> entityMetadata = getQueryMethod ().getEntityInformation ();
@@ -243,12 +238,8 @@ private JdbcQueryExecution<?> getQueryExecution(ResultProcessor processor,
243238 }
244239
245240 ParametrizedQuery createQuery (RelationalParametersParameterAccessor accessor , ReturnedType returnedType ) {
241+ JdbcQueryCreator queryCreator = new JdbcQueryCreator (tree , converter , dialect , getQueryMethod (), accessor , returnedType );
246242
247- RelationalEntityMetadata <?> entityMetadata = getQueryMethod ().getEntityInformation ();
248-
249- JdbcQueryCreator queryCreator = new JdbcQueryCreator (context , tree , converter , dialect , entityMetadata , accessor ,
250- getQueryMethod ().isSliceQuery (), returnedType , this .getQueryMethod ().lookupLockAnnotation (),
251- getQueryMethod ().isScrollQuery ());
252243 return queryCreator .createQuery (getDynamicSort (accessor ));
253244 }
254245
@@ -284,16 +275,14 @@ private JdbcQueryExecution<?> getJdbcQueryExecution(@Nullable ResultSetExtractor
284275 static class ScrollQueryExecution <T > implements JdbcQueryExecution <Window <T >> {
285276 private final JdbcQueryExecution <? extends Collection <T >> delegate ;
286277 private final @ Nullable ScrollPosition position ;
287- private final @ Nullable Integer maxResults ;
288278 private final Sort sort ;
289279 private final Limit limit ;
290280 private final RelationalPersistentEntity <?> tableEntity ;
291281
292282 ScrollQueryExecution (JdbcQueryExecution <? extends Collection <T >> delegate , @ Nullable ScrollPosition position ,
293- @ Nullable Integer maxResults , Sort sort , Limit limit , RelationalPersistentEntity <?> tableEntity ) {
283+ Sort sort , Limit limit , RelationalPersistentEntity <?> tableEntity ) {
294284 this .delegate = delegate ;
295285 this .position = position ;
296- this .maxResults = maxResults ;
297286 this .sort = sort ;
298287 this .limit = limit ;
299288 this .tableEntity = tableEntity ;
@@ -303,65 +292,7 @@ static class ScrollQueryExecution<T> implements JdbcQueryExecution<Window<T>> {
303292 public @ Nullable Window <T > execute (String query , SqlParameterSource parameter ) {
304293 Collection <T > result = delegate .execute (query , parameter );
305294
306- List <T > resultList = result instanceof List ? (List <T >) result : new ArrayList <>(result );
307- IntFunction <? extends ScrollPosition > positionFunction = null ;
308- if (position instanceof OffsetScrollPosition )
309- positionFunction = ((OffsetScrollPosition ) position ).positionFunction ();
310-
311- if (position instanceof KeysetScrollPosition ) {
312- Map <String , Object > keys = ((KeysetScrollPosition ) position ).getKeys ();
313- List <String > orders = new ArrayList <>(keys .keySet ());
314-
315- if (orders .isEmpty ())
316- orders = sort .get ().map (Sort .Order ::getProperty ).toList ();
317-
318- List <RelationalPersistentProperty > properties = new ArrayList <>();
319- for (String propertyName : orders ) {
320- RelationalPersistentProperty prop = tableEntity .getPersistentProperty (propertyName );
321- if (prop == null )
322- continue ;
323-
324- properties .add (prop );
325- }
326-
327- final Map <String , Object > resultKeys = extractKeys (resultList , properties );
328- positionFunction = (ignoredI ) -> ScrollPosition .of (resultKeys , ((KeysetScrollPosition ) position ).getDirection ());
329- }
330-
331- if (positionFunction == null )
332- throw new UnsupportedOperationException ("Not supported scroll type." );
333-
334- boolean hasNext ;
335- if (maxResults != null )
336- hasNext = resultList .size () >= maxResults ;
337- else if (limit .isLimited ())
338- hasNext = resultList .size () >= limit .max ();
339- else
340- hasNext = !resultList .isEmpty ();
341-
342- return Window .from (resultList , positionFunction , hasNext );
343- }
344-
345- private Map <String , Object > extractKeys (List <T > resultList , List <RelationalPersistentProperty > properties ) {
346- if (resultList .isEmpty ())
347- return Map .of ();
348-
349- Map <String , Object > result = new LinkedHashMap <>();
350-
351- T last = resultList .get (resultList .size () - 1 );
352- PersistentPropertyAccessor <T > accessor = tableEntity .getPropertyAccessor (last );
353-
354- for (RelationalPersistentProperty property : properties ) {
355- String propertyName = property .getName ();
356- Object propertyValue = accessor .getProperty (property );
357-
358- if (propertyValue == null )
359- continue ;
360-
361- result .put (propertyName , propertyValue );
362- }
363-
364- return result ;
295+ return ScrollDelegate .scroll (result , position , limit , sort , tableEntity );
365296 }
366297 }
367298
0 commit comments