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.subselect;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.junit.jupiter.api.Assertions.assertAll;
20  import static org.mybatis.dynamic.sql.SqlBuilder.*;
21  import static org.mybatis.dynamic.sql.subselect.FooDynamicSqlSupport.*;
22  
23  import java.util.Date;
24  
25  import org.junit.jupiter.api.Test;
26  import org.mybatis.dynamic.sql.render.RenderingStrategies;
27  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
28  
29  class SubSelectTest {
30  
31      @Test
32      void testInSubSelect() {
33          Date d = new Date();
34  
35          Foo foo2 = new Foo();
36  
37          SelectStatementProvider selectStatement = select(column1.as("A_COLUMN1"), column2)
38                  .from(foo, "a")
39                  .where(column2, isIn(
40                          select(foo2.column2)
41                                  .from(foo2)
42                                  .where(foo2.column2, isEqualTo(3))
43                      )
44                  )
45                  .and(column1, isLessThan(d))
46                  .build()
47                  .render(RenderingStrategies.MYBATIS3);
48  
49          String expectedFullStatement = "select a.column1 as A_COLUMN1, a.column2 "
50                  + "from foo a "
51                  + "where a.column2 in (select column2 from foo where column2 = #{parameters.p1,jdbcType=INTEGER}) "
52                  + "and a.column1 < #{parameters.p2,jdbcType=DATE}";
53  
54          assertAll(
55                  () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedFullStatement),
56                  () -> assertThat(selectStatement.getParameters()).containsEntry("p1", 3),
57                  () -> assertThat(selectStatement.getParameters()).containsEntry("p2", d)
58          );
59      }
60  
61      @Test
62      void testNotInSubSelect() {
63          Date d = new Date();
64  
65          Foo foo2 = new Foo();
66  
67          SelectStatementProvider selectStatement = select(column1.as("A_COLUMN1"), column2)
68                  .from(foo, "a")
69                  .where(column2, isNotIn(
70                          select(foo2.column2)
71                                  .from(foo2)
72                                  .where(foo2.column2, isEqualTo(3))
73                      )
74                  )
75                  .and(column1, isLessThan(d))
76                  .build()
77                  .render(RenderingStrategies.MYBATIS3);
78  
79          String expectedFullStatement = "select a.column1 as A_COLUMN1, a.column2 "
80                  + "from foo a "
81                  + "where a.column2 not in (select column2 from foo where column2 = #{parameters.p1,jdbcType=INTEGER})"
82                  + " and a.column1 < #{parameters.p2,jdbcType=DATE}";
83  
84          assertAll(
85                  () -> assertThat(selectStatement.getSelectStatement()).isEqualTo(expectedFullStatement),
86                  () -> assertThat(selectStatement.getParameters()).containsEntry("p1", 3),
87                  () -> assertThat(selectStatement.getParameters()).containsEntry("p2", d)
88          );
89      }
90  }