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