HHH-17779 support for key-based pagination

minor code cleanup
This commit is contained in:
Gavin King 2024-02-26 17:15:37 +01:00
parent dbd15fd690
commit 8421d1c8d1
1 changed files with 19 additions and 10 deletions

View File

@ -35,8 +35,6 @@ import org.hibernate.query.sqm.tree.predicate.SqmWhereClause;
import org.hibernate.query.sqm.tree.select.SqmQuerySpec; import org.hibernate.query.sqm.tree.select.SqmQuerySpec;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement; import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.hibernate.sql.results.internal.TupleMetadata; import org.hibernate.sql.results.internal.TupleMetadata;
import org.hibernate.sql.results.spi.ListResultsConsumer;
import org.hibernate.sql.results.spi.ListResultsConsumer.UniqueSemantic;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -238,15 +236,26 @@ abstract class AbstractSqmSelectionQuery<R> extends AbstractSelectionQuery<R> {
keyed( keyDefinition, key ); keyed( keyDefinition, key );
} }
final SqmSelectStatement<KeyedResult<R>> sqm = keyed( keyDefinition, key );
final ConcreteSqmSelectQueryPlan<KeyedResult<R>> plan = final ConcreteSqmSelectQueryPlan<KeyedResult<R>> plan =
buildConcreteQueryPlan( sqm, null, null, getQueryOptions() ); buildConcreteQueryPlan( keyed( keyDefinition, key ),
final List<KeyedResult<R>> executed = null, null,
plan.executeQuery( this, new ListResultsConsumer<>(UniqueSemantic.NONE) ); getQueryOptions() );
final List<Comparable<?>> keyOfLastResult = final List<KeyedResult<R>> executed = plan.performList(this);
executed.isEmpty() ? key : executed.get( executed.size()-1 ).getKey(); final KeyedPage<R> nextPage =
return new KeyedResultList<>( executed.stream().map(KeyedResult::getResult).collect(toList()), KeyedPage.forKey(keyDefinition, page.next(),
keyedPage, KeyedPage.forKey( keyDefinition, page.next(), keyOfLastResult ) ); getKeyOfLastResult(executed, key));
return new KeyedResultList<>( collectResultList( executed ), keyedPage, nextPage );
}
private static <R> List<R> collectResultList(List<KeyedResult<R>> executed) {
return executed.stream()
.map(KeyedResult::getResult)
.collect(toList());
}
private static <R> List<Comparable<?>> getKeyOfLastResult(
List<KeyedResult<R>> executed, List<Comparable<?>> key) {
return executed.isEmpty() ? key : executed.get(executed.size() - 1).getKey();
} }
public abstract Class<R> getExpectedResultType(); public abstract Class<R> getExpectedResultType();