View Javadoc
1   /*
2    *    Copyright 2016-2026 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  
23  import org.junit.jupiter.api.Test;
24  import org.mybatis.dynamic.sql.SqlColumn;
25  import org.mybatis.dynamic.sql.SqlTable;
26  import org.mybatis.dynamic.sql.render.RenderingStrategies;
27  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
28  
29  class HavingModelTest {
30      @Test
31      void testSimpleHaving() {
32          SqlTable table = SqlTable.of("foo");
33          SqlColumn<Integer> id = table.column("id", JDBCType.INTEGER);
34  
35          HavingApplier havingApplier = having(id, isLessThan(4))
36                  .or(id, isGreaterThan(14))
37                  .toHavingApplier();
38  
39          SelectStatementProvider selectStatementProvider = select(table.allColumns())
40                  .from(table)
41                  .applyHaving(havingApplier)
42                  .build()
43                  .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
44  
45          assertThat(selectStatementProvider.getSelectStatement()).isEqualTo("select * from foo having id < :p1 or id > :p2");
46      }
47  
48      @Test
49      void testComplexHaving() {
50          SqlTable table = SqlTable.of("foo");
51          SqlColumn<Integer> id = table.column("id", JDBCType.INTEGER);
52  
53          HavingApplier havingApplier = having(group(id, isLessThan(10), and(id, isGreaterThan(4))))
54                  .or(id, isGreaterThan(14))
55                  .toHavingApplier();
56  
57          SelectStatementProvider selectStatementProvider = select(table.allColumns())
58                  .from(table)
59                  .applyHaving(havingApplier)
60                  .build()
61                  .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
62  
63          assertThat(selectStatementProvider.getSelectStatement()).isEqualTo("select * from foo having (id < :p1 and id > :p2) or id > :p3");
64      }
65  }