|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.jpa.repository.query; |
| 17 | + |
| 18 | +import jakarta.persistence.criteria.CriteriaBuilder; |
| 19 | +import jakarta.persistence.criteria.CriteriaQuery; |
| 20 | +import jakarta.persistence.criteria.Predicate; |
| 21 | +import jakarta.persistence.criteria.Root; |
| 22 | + |
| 23 | +import java.util.Collection; |
| 24 | +import java.util.LinkedHashSet; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.springframework.data.domain.KeysetScrollPosition; |
| 28 | +import org.springframework.data.domain.Sort; |
| 29 | +import org.springframework.data.jpa.repository.support.JpaEntityInformation; |
| 30 | +import org.springframework.data.repository.query.ReturnedType; |
| 31 | +import org.springframework.data.repository.query.parser.PartTree; |
| 32 | +import org.springframework.lang.Nullable; |
| 33 | + |
| 34 | +/** |
| 35 | + * Extension to {@link JpaQueryCreator} to create queries considering {@link KeysetScrollPosition keyset scrolling}. |
| 36 | + * |
| 37 | + * @author Mark Paluch |
| 38 | + * @since 3.1 |
| 39 | + */ |
| 40 | +class JpaKeysetScrollQueryCreator extends JpaQueryCreator { |
| 41 | + |
| 42 | + private final JpaEntityInformation<?, ?> entityInformation; |
| 43 | + private final KeysetScrollPosition scrollPosition; |
| 44 | + |
| 45 | + public JpaKeysetScrollQueryCreator(PartTree tree, ReturnedType type, CriteriaBuilder builder, |
| 46 | + ParameterMetadataProvider provider, JpaEntityInformation<?, ?> entityInformation, |
| 47 | + KeysetScrollPosition scrollPosition) { |
| 48 | + super(tree, type, builder, provider); |
| 49 | + this.entityInformation = entityInformation; |
| 50 | + this.scrollPosition = scrollPosition; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected CriteriaQuery<?> complete(@Nullable Predicate predicate, Sort sort, CriteriaQuery<?> query, |
| 55 | + CriteriaBuilder builder, Root<?> root) { |
| 56 | + |
| 57 | + KeysetScrollSpecification<Object> keysetSpec = new KeysetScrollSpecification<>(scrollPosition, sort, |
| 58 | + entityInformation); |
| 59 | + Predicate keysetPredicate = keysetSpec.createPredicate(root, builder); |
| 60 | + |
| 61 | + CriteriaQuery<?> queryToUse = super.complete(predicate, keysetSpec.sort(), query, builder, root); |
| 62 | + |
| 63 | + if (keysetPredicate != null) { |
| 64 | + if (queryToUse.getRestriction() != null) { |
| 65 | + return queryToUse.where(builder.and(queryToUse.getRestriction(), keysetPredicate)); |
| 66 | + } |
| 67 | + return queryToUse.where(keysetPredicate); |
| 68 | + } |
| 69 | + |
| 70 | + return queryToUse; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + Collection<String> getRequiredSelection(Sort sort, ReturnedType returnedType) { |
| 75 | + |
| 76 | + Sort sortToUse = KeysetScrollSpecification.createSort(scrollPosition, sort, entityInformation); |
| 77 | + |
| 78 | + Set<String> selection = new LinkedHashSet<>(returnedType.getInputProperties()); |
| 79 | + sortToUse.forEach(it -> selection.add(it.getProperty())); |
| 80 | + |
| 81 | + return selection; |
| 82 | + } |
| 83 | +} |
0 commit comments