|
15 | 15 | */
|
16 | 16 | package org.mybatis.dynamic.sql.select.aggregate;
|
17 | 17 |
|
| 18 | +import java.util.function.Function; |
| 19 | + |
18 | 20 | import org.mybatis.dynamic.sql.BindableColumn;
|
19 | 21 | import org.mybatis.dynamic.sql.VisitableCondition;
|
20 | 22 | import org.mybatis.dynamic.sql.render.RenderingContext;
|
|
24 | 26 | import org.mybatis.dynamic.sql.where.render.DefaultConditionVisitor;
|
25 | 27 |
|
26 | 28 | public class Sum<T> extends AbstractUniTypeFunction<T, Sum<T>> {
|
27 |
| - private final VisitableCondition<T> condition; |
| 29 | + private final Function<RenderingContext, FragmentAndParameters> renderer; |
28 | 30 |
|
29 | 31 | private Sum(BindableColumn<T> column) {
|
30 |
| - this(column, null); |
| 32 | + super(column); |
| 33 | + renderer = rc -> column.render(rc).mapFragment(Sum::applyAggregate); |
31 | 34 | }
|
32 | 35 |
|
33 | 36 | private Sum(BindableColumn<T> column, VisitableCondition<T> condition) {
|
34 | 37 | super(column);
|
35 |
| - this.condition = condition; |
36 |
| - } |
| 38 | + renderer = rc -> { |
| 39 | + Validator.assertTrue(condition.shouldRender(), "ERROR.37", "sum"); //$NON-NLS-1$ //$NON-NLS-2$ |
37 | 40 |
|
38 |
| - @Override |
39 |
| - public FragmentAndParameters render(RenderingContext renderingContext) { |
40 |
| - if (condition == null) { |
41 |
| - return renderWithoutCondition(renderingContext); |
42 |
| - } else { |
43 |
| - return renderWithCondition(renderingContext); |
44 |
| - } |
45 |
| - } |
| 41 | + DefaultConditionVisitor<T> visitor = new DefaultConditionVisitor.Builder<T>() |
| 42 | + .withColumn(column) |
| 43 | + .withRenderingContext(rc) |
| 44 | + .build(); |
46 | 45 |
|
47 |
| - private FragmentAndParameters renderWithoutCondition(RenderingContext renderingContext) { |
48 |
| - return column.render(renderingContext).mapFragment(this::applyAggregate); |
| 46 | + return condition.accept(visitor).mapFragment(Sum::applyAggregate); |
| 47 | + }; |
49 | 48 | }
|
50 | 49 |
|
51 |
| - private FragmentAndParameters renderWithCondition(RenderingContext renderingContext) { |
52 |
| - Validator.assertTrue(condition.shouldRender(), "ERROR.37", "sum"); //$NON-NLS-1$ //$NON-NLS-2$ |
53 |
| - |
54 |
| - DefaultConditionVisitor<T> visitor = new DefaultConditionVisitor.Builder<T>() |
55 |
| - .withColumn(column) |
56 |
| - .withRenderingContext(renderingContext) |
57 |
| - .build(); |
| 50 | + private Sum(BindableColumn<T> column, Function<RenderingContext, FragmentAndParameters> renderer) { |
| 51 | + super(column); |
| 52 | + this.renderer = renderer; |
| 53 | + } |
58 | 54 |
|
59 |
| - return condition.accept(visitor).mapFragment(this::applyAggregate); |
| 55 | + @Override |
| 56 | + public FragmentAndParameters render(RenderingContext renderingContext) { |
| 57 | + return renderer.apply(renderingContext); |
60 | 58 | }
|
61 | 59 |
|
62 |
| - private String applyAggregate(String s) { |
| 60 | + private static String applyAggregate(String s) { |
63 | 61 | return "sum(" + s + ")"; //$NON-NLS-1$ //$NON-NLS-2$
|
64 | 62 | }
|
65 | 63 |
|
66 | 64 | @Override
|
67 | 65 | protected Sum<T> copy() {
|
68 |
| - return new Sum<>(column, condition); |
| 66 | + return new Sum<>(column, renderer); |
69 | 67 | }
|
70 | 68 |
|
71 | 69 | public static <T> Sum<T> of(BindableColumn<T> column) {
|
|
0 commit comments