View Javadoc
1   /*
2    *    Copyright 2016-2025 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.mybatis.dynamic.sql.select.aggregate;
17  
18  import java.util.function.Function;
19  
20  import org.mybatis.dynamic.sql.BasicColumn;
21  import org.mybatis.dynamic.sql.BindableColumn;
22  import org.mybatis.dynamic.sql.RenderableCondition;
23  import org.mybatis.dynamic.sql.render.RenderingContext;
24  import org.mybatis.dynamic.sql.select.function.AbstractUniTypeFunction;
25  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
26  import org.mybatis.dynamic.sql.util.Validator;
27  import org.mybatis.dynamic.sql.where.render.ColumnAndConditionRenderer;
28  
29  public class Sum<T> extends AbstractUniTypeFunction<T, Sum<T>> {
30      private final Function<RenderingContext, FragmentAndParameters> renderer;
31  
32      private Sum(BasicColumn column) {
33          super(column);
34          renderer = rc -> column.render(rc).mapFragment(this::applyAggregate);
35      }
36  
37      private Sum(BindableColumn<T> column, RenderableCondition<T> condition) {
38          super(column);
39          renderer = rc -> {
40              Validator.assertTrue(condition.shouldRender(rc), "ERROR.37", "sum"); //$NON-NLS-1$ //$NON-NLS-2$
41  
42              return new ColumnAndConditionRenderer.Builder<T>()
43                      .withColumn(column)
44                      .withCondition(condition)
45                      .withRenderingContext(rc)
46                      .build()
47                      .render()
48                      .mapFragment(this::applyAggregate);
49          };
50      }
51  
52      private Sum(BasicColumn column, Function<RenderingContext, FragmentAndParameters> renderer) {
53          super(column);
54          this.renderer = renderer;
55      }
56  
57      @Override
58      public FragmentAndParameters render(RenderingContext renderingContext) {
59          return renderer.apply(renderingContext);
60      }
61  
62      private String applyAggregate(String s) {
63          return "sum(" + s + ")"; //$NON-NLS-1$ //$NON-NLS-2$
64      }
65  
66      @Override
67      protected Sum<T> copy() {
68          return new Sum<>(column, renderer);
69      }
70  
71      public static <T> Sum<T> of(BindableColumn<T> column) {
72          return new Sum<>(column);
73      }
74  
75      public static Sum<Object> of(BasicColumn column) {
76          return new Sum<>(column);
77      }
78  
79      public static <T> Sum<T> of(BindableColumn<T> column, RenderableCondition<T> condition) {
80          return new Sum<>(column, condition);
81      }
82  }