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.gh430;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  import static org.mybatis.dynamic.sql.SqlBuilder.*;
20  import static org.mybatis.dynamic.sql.subselect.FooDynamicSqlSupport.*;
21  
22  import java.util.Collections;
23  import java.util.Date;
24  import java.util.List;
25  
26  import org.junit.jupiter.api.Test;
27  import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
28  import org.mybatis.dynamic.sql.render.RenderingStrategies;
29  import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
30  
31  class NoInitialConditionTest {
32  
33      @Test
34      void testNoInitialConditionEmptyList() {
35          List<AndOrCriteriaGroup> criteria = Collections.emptyList();
36  
37          SelectStatementProvider selectStatement = buildSelectStatement(criteria);
38  
39          String expected = "select column1, column2 from foo where column1 < :p1";
40  
41          assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
42      }
43  
44      @Test
45      void testNoInitialConditionSingleSub() {
46          List<AndOrCriteriaGroup> criteria = List.of(or(column2, isEqualTo(3)));
47  
48          SelectStatementProvider selectStatement = buildSelectStatement(criteria);
49  
50          String expected = "select column1, column2 from foo where column1 < :p1 " +
51                  "and column2 = :p2";
52  
53          assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
54      }
55  
56      @Test
57      void testNoInitialConditionMultipleSubs() {
58          List<AndOrCriteriaGroup> criteria = List.of(
59              or(column2, isEqualTo(3)),
60              or(column2, isEqualTo(4)),
61              or(column2, isEqualTo(5)));
62  
63          SelectStatementProvider selectStatement = buildSelectStatement(criteria);
64  
65          String expected = "select column1, column2 from foo where column1 < :p1 " +
66                  "and (column2 = :p2 or column2 = :p3 or column2 = :p4)";
67  
68          assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
69      }
70  
71      @Test
72      void testNoInitialConditionWhereMultipleSubs() {
73          List<AndOrCriteriaGroup> criteria = List.of(
74              or(column2, isEqualTo(3)),
75              or(column2, isEqualTo(4)),
76              or(column2, isEqualTo(5)));
77  
78          SelectStatementProvider selectStatement = select(column1, column2)
79                  .from(foo)
80                  .where(criteria)
81                  .build()
82                  .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
83  
84          String expected = "select column1, column2 from foo where " +
85                  "column2 = :p1 or column2 = :p2 or column2 = :p3";
86  
87          assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
88      }
89  
90      @Test
91      void testNoInitialConditionWhereNotMultipleSubs() {
92          List<AndOrCriteriaGroup> criteria = List.of(
93                  or(column2, isEqualTo(3)),
94                  or(column2, isEqualTo(4)),
95                  or(column2, isEqualTo(5)));
96  
97          SelectStatementProvider selectStatement = select(column1, column2)
98                  .from(foo)
99                  .where(not(criteria), and(column1, isLessThan(new Date())))
100                 .build()
101                 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
102 
103         String expected = "select column1, column2 from foo where not " +
104                 "(column2 = :p1 or column2 = :p2 or column2 = :p3) " +
105                 "and column1 < :p4";
106 
107         assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
108     }
109 
110     @Test
111     void testNoInitialConditionWhereGroupMultipleSubs() {
112         List<AndOrCriteriaGroup> criteria = List.of(
113                 or(column2, isEqualTo(3)),
114                 or(column2, isEqualTo(4)),
115                 or(column2, isEqualTo(5)));
116 
117         SelectStatementProvider selectStatement = select(column1, column2)
118                 .from(foo)
119                 .where(group(criteria), and(column1, isLessThan(new Date())))
120                 .build()
121                 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
122 
123         String expected = "select column1, column2 from foo where " +
124                 "(column2 = :p1 or column2 = :p2 or column2 = :p3) " +
125                 "and column1 < :p4";
126 
127         assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
128     }
129 
130     @Test
131     void testNoInitialConditionWhereCCAndMultipleSubs() {
132         List<AndOrCriteriaGroup> criteria = List.of(
133                 or(column2, isEqualTo(3)),
134                 or(column2, isEqualTo(4)),
135                 or(column2, isEqualTo(5)));
136 
137         SelectStatementProvider selectStatement = select(column1, column2)
138                 .from(foo)
139                 .where(column1, isLessThan(new Date()), and(criteria))
140                 .build()
141                 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
142 
143         String expected = "select column1, column2 from foo where " +
144                 "column1 < :p1 and (column2 = :p2 or column2 = :p3 or column2 = :p4)";
145 
146         assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
147     }
148 
149     @Test
150     void testNoInitialConditionWhereCCOrMultipleSubs() {
151         List<AndOrCriteriaGroup> criteria = List.of(
152                 or(column2, isEqualTo(3)),
153                 or(column2, isEqualTo(4)),
154                 or(column2, isEqualTo(5)));
155 
156         SelectStatementProvider selectStatement = select(column1, column2)
157                 .from(foo)
158                 .where(column1, isLessThan(new Date()), or(criteria))
159                 .build()
160                 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
161 
162         String expected = "select column1, column2 from foo where " +
163                 "column1 < :p1 or (column2 = :p2 or column2 = :p3 or column2 = :p4)";
164 
165         assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
166     }
167 
168     @Test
169     void testNoInitialConditionWhereOrMultipleSubs() {
170         List<AndOrCriteriaGroup> criteria = List.of(
171                 or(column2, isEqualTo(3)),
172                 or(column2, isEqualTo(4)),
173                 or(column2, isEqualTo(5)));
174 
175         SelectStatementProvider selectStatement = select(column1, column2)
176                 .from(foo)
177                 .where(column1, isLessThan(new Date()))
178                 .or(criteria)
179                 .build()
180                 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
181 
182         String expected = "select column1, column2 from foo where column1 < :p1 " +
183                 "or (column2 = :p2 or column2 = :p3 or column2 = :p4)";
184 
185         assertThat(selectStatement.getSelectStatement()).isEqualTo(expected);
186     }
187 
188     private SelectStatementProvider buildSelectStatement(List<AndOrCriteriaGroup> criteria) {
189         return select(column1, column2)
190                 .from(foo)
191                 .where(column1, isLessThan(new Date()))
192                 .and(criteria)
193                 .build()
194                 .render(RenderingStrategies.SPRING_NAMED_PARAMETER);
195     }
196 }