1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 }