|
| 1 | +/* |
| 2 | + * Copyright 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.relational.core.sql; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +/** |
| 21 | + * Represents an analytic function, also known as windowing function |
| 22 | + * |
| 23 | + * @author Jens Schauder |
| 24 | + * @since 2.7 |
| 25 | + */ |
| 26 | +public class AnalyticFunction extends AbstractSegment implements Expression { |
| 27 | + |
| 28 | + private final SimpleFunction function; |
| 29 | + private final Partition partition; |
| 30 | + private final OrderBy orderBy; |
| 31 | + |
| 32 | + public static AnalyticFunction create(String function, Expression... arguments) { |
| 33 | + |
| 34 | + return new AnalyticFunction(SimpleFunction.create(function, Arrays.asList(arguments)), new Partition(), |
| 35 | + new OrderBy()); |
| 36 | + } |
| 37 | + |
| 38 | + private AnalyticFunction(SimpleFunction function, Partition partition, OrderBy orderBy) { |
| 39 | + |
| 40 | + super(function, partition, orderBy); |
| 41 | + |
| 42 | + this.function = function; |
| 43 | + this.partition = partition; |
| 44 | + this.orderBy = orderBy; |
| 45 | + } |
| 46 | + |
| 47 | + public AnalyticFunction partitionBy(Expression... partitionBy) { |
| 48 | + |
| 49 | + return new AnalyticFunction(function, new Partition(partitionBy), orderBy); |
| 50 | + } |
| 51 | + |
| 52 | + public AnalyticFunction orderBy(OrderByField... orderBy) { |
| 53 | + return new AnalyticFunction(function, partition, new OrderBy(orderBy)); |
| 54 | + } |
| 55 | + |
| 56 | + public AnalyticFunction orderBy(Expression... orderByExpression) { |
| 57 | + |
| 58 | + final OrderByField[] orderByFields = Arrays.stream(orderByExpression) // |
| 59 | + .map(OrderByField::from) // |
| 60 | + .toArray(OrderByField[]::new); |
| 61 | + |
| 62 | + return new AnalyticFunction(function, partition, new OrderBy(orderByFields)); |
| 63 | + } |
| 64 | + |
| 65 | + public AliasedAnalyticFunction as(String alias) { |
| 66 | + return new AliasedAnalyticFunction(this, SqlIdentifier.unquoted(alias)); |
| 67 | + } |
| 68 | + |
| 69 | + public AliasedAnalyticFunction as(SqlIdentifier alias) { |
| 70 | + return new AliasedAnalyticFunction(this, alias); |
| 71 | + } |
| 72 | + |
| 73 | + public static class Partition extends SegmentList<Expression> { |
| 74 | + Partition(Expression... expressions) { |
| 75 | + super(expressions); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private static class AliasedAnalyticFunction extends AnalyticFunction implements Aliased { |
| 80 | + |
| 81 | + private final SqlIdentifier alias; |
| 82 | + |
| 83 | + AliasedAnalyticFunction(AnalyticFunction analyticFunction, SqlIdentifier alias) { |
| 84 | + |
| 85 | + super(analyticFunction.function, analyticFunction.partition, analyticFunction.orderBy); |
| 86 | + this.alias = alias; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public SqlIdentifier getAlias() { |
| 91 | + return alias; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments