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 issues.gh655;
17  
18  import static examples.mariadb.ItemsDynamicSQLSupport.id;
19  import static examples.mariadb.ItemsDynamicSQLSupport.items;
20  import static org.assertj.core.api.Assertions.assertThat;
21  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22  import static org.mybatis.dynamic.sql.SqlBuilder.add;
23  import static org.mybatis.dynamic.sql.SqlBuilder.constant;
24  import static org.mybatis.dynamic.sql.SqlBuilder.isEqualToWhenPresent;
25  import static org.mybatis.dynamic.sql.SqlBuilder.isGreaterThan;
26  import static org.mybatis.dynamic.sql.SqlBuilder.select;
27  import static org.mybatis.dynamic.sql.SqlBuilder.sum;
28  
29  import config.TestContainersConfiguration;
30  import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
31  import org.apache.ibatis.mapping.Environment;
32  import org.apache.ibatis.session.Configuration;
33  import org.apache.ibatis.session.SqlSession;
34  import org.apache.ibatis.session.SqlSessionFactory;
35  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
36  import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
37  import org.junit.jupiter.api.BeforeAll;
38  import org.junit.jupiter.api.Test;
39  import org.mybatis.dynamic.sql.exception.InvalidSqlException;
40  import org.mybatis.dynamic.sql.render.RenderingStrategies;
41  import org.mybatis.dynamic.sql.select.SelectModel;
42  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
43  import org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper;
44  import org.testcontainers.junit.jupiter.Container;
45  import org.testcontainers.junit.jupiter.Testcontainers;
46  import org.testcontainers.mariadb.MariaDBContainer;
47  
48  @Testcontainers
49  class Gh655Test {
50  
51      @Container
52      private static final MariaDBContainer mariadb =
53              new MariaDBContainer(TestContainersConfiguration.MARIADB_LATEST)
54                      .withInitScript("examples/mariadb/CreateDB.sql");
55  
56      private static SqlSessionFactory sqlSessionFactory;
57  
58      @BeforeAll
59      static void setup() {
60          UnpooledDataSource ds = new UnpooledDataSource(mariadb.getDriverClassName(), mariadb.getJdbcUrl(),
61                  mariadb.getUsername(), mariadb.getPassword());
62          Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
63          Configuration config = new Configuration(environment);
64          config.addMapper(CommonSelectMapper.class);
65          sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
66      }
67  
68      @Test
69      void sumTest() {
70          try (SqlSession session = sqlSessionFactory.openSession()) {
71              CommonSelectMapper mapper = session.getMapper(CommonSelectMapper.class);
72  
73              SelectStatementProvider selectStatement = select(sum(id, isGreaterThan(5)).as("numrows"))
74                      .from(items)
75                      .build()
76                      .render(RenderingStrategies.MYBATIS3);
77  
78              String expected = "select sum(id > #{parameters.p1,jdbcType=INTEGER}) as numrows from items";
79              assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
80              assertThat(selectStatement.getParameters()).containsEntry("p1", 5);
81  
82              Long numrows = mapper.selectOneLong(selectStatement);
83              assertThat(numrows).isEqualTo(15L);
84          }
85      }
86  
87      @Test
88      void sumWithOptionalTest() {
89          SelectModel selectModel = select(sum(id, isEqualToWhenPresent((Integer) null)).as("numrows"))
90                  .from(items)
91                  .build();
92  
93          assertThatExceptionOfType(InvalidSqlException.class)
94                  .isThrownBy(() -> selectModel.render(RenderingStrategies.MYBATIS3))
95                  .withMessage("The \"sum\" function does not support conditions that fail to render");
96      }
97  
98      @Test
99      void sumAndAddTest() {
100         try (SqlSession session = sqlSessionFactory.openSession()) {
101             CommonSelectMapper mapper = session.getMapper(CommonSelectMapper.class);
102 
103             SelectStatementProvider selectStatement = select(add(sum(id, isGreaterThan(5)), constant("3")).as("numrows"))
104                     .from(items)
105                     .build()
106                     .render(RenderingStrategies.MYBATIS3);
107 
108             String expected = "select (sum(id > #{parameters.p1,jdbcType=INTEGER}) + 3) as numrows from items";
109             assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
110             assertThat(selectStatement.getParameters()).containsEntry("p1", 5);
111 
112             Long numrows = mapper.selectOneLong(selectStatement);
113             assertThat(numrows).isEqualTo(18L);
114         }
115     }
116 
117     @Test
118     void columnComparisonTest() {
119         // this is a nonsensical query just to test that rendering works as expected
120         SelectStatementProvider selectStatement = select(sum(id, isGreaterThan(5)).as("numrows"))
121                 .from(items)
122                 .where(id, isGreaterThan(sum(id, isGreaterThan(5))))
123                 .build()
124                 .render(RenderingStrategies.MYBATIS3);
125 
126         String expected = "select sum(id > #{parameters.p1,jdbcType=INTEGER}) as numrows from items " +
127                 "where id > sum(id > #{parameters.p2,jdbcType=INTEGER})";
128         assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
129         assertThat(selectStatement.getParameters()).containsEntry("p1", 5);
130         assertThat(selectStatement.getParameters()).containsEntry("p2", 5);
131     }
132 }