|
| 1 | +/* |
| 2 | + * Copyright 2024 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 static org.springframework.data.jpa.repository.query.JpaQueryParsingToken.*; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +/** |
| 24 | + * Utility class encapsulating common query transformations. |
| 25 | + * |
| 26 | + * @author Mark Paluch |
| 27 | + * @since 3.2.5 |
| 28 | + */ |
| 29 | +class QueryTransformers { |
| 30 | + |
| 31 | + /** |
| 32 | + * Filter a token list from a {@code SELECT} clause to be used within a count query. That is, filter any {@code AS …} |
| 33 | + * aliases. |
| 34 | + * |
| 35 | + * @param selection the input selection. |
| 36 | + * @return filtered selection to be used with count queries. |
| 37 | + */ |
| 38 | + static List<JpaQueryParsingToken> filterCountSelection(List<JpaQueryParsingToken> selection) { |
| 39 | + |
| 40 | + List<JpaQueryParsingToken> target = new ArrayList<>(selection.size()); |
| 41 | + boolean skipNext = false; |
| 42 | + |
| 43 | + for (JpaQueryParsingToken token : selection) { |
| 44 | + |
| 45 | + if (skipNext) { |
| 46 | + skipNext = false; |
| 47 | + continue; |
| 48 | + } |
| 49 | + |
| 50 | + if (token.isA(TOKEN_AS)) { |
| 51 | + skipNext = true; |
| 52 | + continue; |
| 53 | + } |
| 54 | + target.add(token); |
| 55 | + } |
| 56 | + |
| 57 | + return target; |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments