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;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.mybatis.dynamic.sql.SqlBuilder.*;
20  
21  import java.sql.JDBCType;
22  import java.util.Optional;
23  
24  import org.junit.jupiter.api.Test;
25  import org.mybatis.dynamic.sql.SqlColumn;
26  import org.mybatis.dynamic.sql.SqlTable;
27  import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
28  import org.mybatis.dynamic.sql.render.RenderingContext;
29  import org.mybatis.dynamic.sql.render.RenderingStrategies;
30  import org.mybatis.dynamic.sql.select.render.HavingRenderer;
31  import org.mybatis.dynamic.sql.util.FragmentAndParameters;
32  
33  class HavingModelTest {
34      @Test
35      void testSimpleHaving() {
36          SqlTable table = SqlTable.of("foo");
37          SqlColumn<Integer> id = table.column("id", JDBCType.INTEGER);
38  
39          HavingModel model = having(id, isLessThan(4))
40                  .or(id, isGreaterThan(14))
41                  .build();
42  
43          Optional<FragmentAndParameters> havingClause = renderHavingModel(model);
44  
45          assertThat(havingClause).hasValueSatisfying(hc ->
46                  assertThat(hc.fragment()).isEqualTo("having id < :p1 or id > :p2")
47          );
48      }
49  
50      @Test
51      void testComplexHaving() {
52          SqlTable table = SqlTable.of("foo");
53          SqlColumn<Integer> id = table.column("id", JDBCType.INTEGER);
54  
55          HavingModel model = having(group(id, isLessThan(10), and(id, isGreaterThan(4))))
56                  .or(id, isGreaterThan(14))
57                  .build();
58  
59          Optional<FragmentAndParameters> havingClause = renderHavingModel(model);
60  
61          assertThat(havingClause).hasValueSatisfying(hc ->
62                  assertThat(hc.fragment()).isEqualTo("having (id < :p1 and id > :p2) or id > :p3")
63          );
64      }
65  
66      private Optional<FragmentAndParameters> renderHavingModel(HavingModel havingModel) {
67          RenderingContext renderingContext = RenderingContext
68                  .withRenderingStrategy(RenderingStrategies.SPRING_NAMED_PARAMETER)
69                  .withStatementConfiguration(new StatementConfiguration())
70                  .build();
71  
72          return HavingRenderer.withHavingModel(havingModel)
73                  .withRenderingContext(renderingContext)
74                  .build()
75                  .render();
76      }
77  }