|
| 1 | +/* |
| 2 | + * Copyright 2013-2021 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 java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.BiFunction; |
| 21 | +import java.util.function.Function; |
| 22 | +import java.util.stream.Stream; |
| 23 | + |
| 24 | +import org.springframework.data.domain.Page; |
| 25 | +import org.springframework.data.domain.PageImpl; |
| 26 | +import org.springframework.data.domain.Pageable; |
| 27 | +import org.springframework.data.domain.Sort; |
| 28 | +import org.springframework.data.repository.query.FluentQuery.FetchableFluentQuery; |
| 29 | +import org.springframework.data.support.PageableExecutionUtils; |
| 30 | +import org.springframework.lang.Nullable; |
| 31 | + |
| 32 | +import com.querydsl.core.types.Predicate; |
| 33 | +import com.querydsl.jpa.JPQLQuery; |
| 34 | + |
| 35 | +/** |
| 36 | + * Immutable implementation of {@link FetchableFluentQuery} based on a Querydsl {@link Predicate}. All methods that |
| 37 | + * return a {@link FetchableFluentQuery} will return a new instance, not the original. |
| 38 | + * |
| 39 | + * @param <S> Domain type |
| 40 | + * @param <R> Result type |
| 41 | + * @author Greg Turnquist |
| 42 | + * @author Michael J. Simons |
| 43 | + * @since 2.6 |
| 44 | + */ |
| 45 | +public class FetchableFluentQueryByPredicate<S, R> extends FluentQuerySupport<R> implements FetchableFluentQuery<R> { |
| 46 | + |
| 47 | + private Predicate predicate; |
| 48 | + private Function<Sort, JPQLQuery<S>> finder; |
| 49 | + private BiFunction<Sort, Pageable, JPQLQuery<S>> pagedFinder; |
| 50 | + private Function<Predicate, Long> countOperation; |
| 51 | + private Function<Predicate, Boolean> existsOperation; |
| 52 | + |
| 53 | + public FetchableFluentQueryByPredicate(Predicate predicate, Class<R> resultType, Function<Sort, JPQLQuery<S>> finder, |
| 54 | + BiFunction<Sort, Pageable, JPQLQuery<S>> pagedFinder, Function<Predicate, Long> countOperation, |
| 55 | + Function<Predicate, Boolean> existsOperation) { |
| 56 | + this(predicate, resultType, Sort.unsorted(), null, finder, pagedFinder, countOperation, existsOperation); |
| 57 | + } |
| 58 | + |
| 59 | + private FetchableFluentQueryByPredicate(Predicate predicate, Class<R> resultType, Sort sort, |
| 60 | + @Nullable Collection<String> properties, Function<Sort, JPQLQuery<S>> finder, |
| 61 | + BiFunction<Sort, Pageable, JPQLQuery<S>> pagedFinder, Function<Predicate, Long> countOperation, |
| 62 | + Function<Predicate, Boolean> existsOperation) { |
| 63 | + |
| 64 | + super(resultType, sort, properties); |
| 65 | + this.predicate = predicate; |
| 66 | + this.finder = finder; |
| 67 | + this.pagedFinder = pagedFinder; |
| 68 | + this.countOperation = countOperation; |
| 69 | + this.existsOperation = existsOperation; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public FetchableFluentQuery<R> sortBy(Sort sort) { |
| 74 | + |
| 75 | + return new FetchableFluentQueryByPredicate<>(this.predicate, this.resultType, this.sort.and(sort), this.properties, |
| 76 | + this.finder, this.pagedFinder, this.countOperation, this.existsOperation); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public <NR> FetchableFluentQuery<NR> as(Class<NR> resultType) { |
| 81 | + |
| 82 | + return new FetchableFluentQueryByPredicate<>(this.predicate, resultType, this.sort, this.properties, this.finder, |
| 83 | + this.pagedFinder, this.countOperation, this.existsOperation); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public FetchableFluentQuery<R> project(Collection<String> properties) { |
| 88 | + |
| 89 | + return new FetchableFluentQueryByPredicate<>(this.predicate, this.resultType, this.sort, |
| 90 | + mergeProperties(properties), this.finder, this.pagedFinder, this.countOperation, this.existsOperation); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public R oneValue() { |
| 95 | + return firstValue(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public R firstValue() { |
| 100 | + |
| 101 | + List<R> all = all(); |
| 102 | + return all.isEmpty() ? null : all.get(0); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public List<R> all() { |
| 107 | + return (List<R>) this.finder.apply(this.sort).fetchResults().getResults(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Page<R> page(Pageable pageable) { |
| 112 | + return pageable.isUnpaged() ? new PageImpl<>(all()) : readPage(pageable); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public Stream<R> stream() { |
| 117 | + return all().stream(); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public long count() { |
| 122 | + return this.countOperation.apply(this.predicate); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public boolean exists() { |
| 127 | + return this.existsOperation.apply(this.predicate); |
| 128 | + } |
| 129 | + |
| 130 | + private Page<R> readPage(Pageable pageable) { |
| 131 | + |
| 132 | + JPQLQuery<S> query = this.pagedFinder.apply(this.sort, pageable); |
| 133 | + |
| 134 | + return (Page<R>) PageableExecutionUtils.getPage(query.fetchResults().getResults(), pageable, |
| 135 | + () -> this.countOperation.apply(this.predicate)); |
| 136 | + } |
| 137 | +} |
0 commit comments