1717
1818import static examples .animal .data .AnimalDataDynamicSqlSupport .animalData ;
1919import static examples .animal .data .AnimalDataDynamicSqlSupport .animalName ;
20+ import static examples .animal .data .AnimalDataDynamicSqlSupport .brainWeight ;
2021import static examples .animal .data .AnimalDataDynamicSqlSupport .id ;
2122import static org .assertj .core .api .Assertions .assertThat ;
2223import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
2627import static org .mybatis .dynamic .sql .SqlBuilder .isEqualTo ;
2728import static org .mybatis .dynamic .sql .SqlBuilder .isEqualToWhenPresent ;
2829import static org .mybatis .dynamic .sql .SqlBuilder .isIn ;
30+ import static org .mybatis .dynamic .sql .SqlBuilder .isLessThan ;
2931import static org .mybatis .dynamic .sql .SqlBuilder .or ;
3032import static org .mybatis .dynamic .sql .SqlBuilder .searchedCase ;
3133import static org .mybatis .dynamic .sql .SqlBuilder .select ;
@@ -85,9 +87,9 @@ void testSearchedCase() {
8587 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
8688
8789 SelectStatementProvider selectStatement = select (animalName , searchedCase ()
88- .when (animalName , isEqualTo ("Artic fox" )).or (animalName , isEqualTo ("Red fox" )).thenConstant ("'Fox'" )
89- .when (animalName , isEqualTo ("Little brown bat" )).or (animalName , isEqualTo ("Big brown bat" )).thenConstant ("'Bat'" )
90- .elseConstant ("cast('Not a Fox or a bat' as varchar(25))" ).end ().as ("AnimalType" ))
90+ .when (animalName , isEqualTo ("Artic fox" )).or (animalName , isEqualTo ("Red fox" )).then ("'Fox'" )
91+ .when (animalName , isEqualTo ("Little brown bat" )).or (animalName , isEqualTo ("Big brown bat" )).then ("'Bat'" )
92+ .else_ ("cast('Not a Fox or a bat' as varchar(25))" ).end ().as ("AnimalType" ))
9193 .from (animalData , "a" )
9294 .where (id , isIn (2 , 3 , 31 , 32 , 38 , 39 ))
9395 .orderBy (id )
@@ -134,8 +136,8 @@ void testSearchedCaseNoElse() {
134136 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
135137
136138 SelectStatementProvider selectStatement = select (animalName , searchedCase ()
137- .when (animalName , isEqualTo ("Artic fox" )).or (animalName , isEqualTo ("Red fox" )).thenConstant ("'Fox'" )
138- .when (animalName , isEqualTo ("Little brown bat" )).or (animalName , isEqualTo ("Big brown bat" )).thenConstant ("'Bat'" )
139+ .when (animalName , isEqualTo ("Artic fox" )).or (animalName , isEqualTo ("Red fox" )).then ("'Fox'" )
140+ .when (animalName , isEqualTo ("Little brown bat" )).or (animalName , isEqualTo ("Big brown bat" )).then ("'Bat'" )
139141 .end ().as ("AnimalType" ))
140142 .from (animalData , "a" )
141143 .where (id , isIn (2 , 3 , 31 , 32 , 38 , 39 ))
@@ -183,10 +185,10 @@ void testSearchedCaseWithGroup() {
183185 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
184186
185187 SelectStatementProvider selectStatement = select (animalName , searchedCase ()
186- .when (animalName , isEqualTo ("Artic fox" )).or (animalName , isEqualTo ("Red fox" )).thenConstant ("'Fox'" )
187- .when (animalName , isEqualTo ("Little brown bat" )).or (animalName , isEqualTo ("Big brown bat" )).thenConstant ("'Bat'" )
188- .when (group (animalName , isEqualTo ("Cat" ), and (id , isEqualTo (31 ))), or (id , isEqualTo (39 ))).thenConstant ("'Fred'" )
189- .elseConstant ("cast('Not a Fox or a bat' as varchar(25))" ).end ().as ("AnimalType" ))
188+ .when (animalName , isEqualTo ("Artic fox" )).or (animalName , isEqualTo ("Red fox" )).then ("'Fox'" )
189+ .when (animalName , isEqualTo ("Little brown bat" )).or (animalName , isEqualTo ("Big brown bat" )).then ("'Bat'" )
190+ .when (group (animalName , isEqualTo ("Cat" ), and (id , isEqualTo (31 ))), or (id , isEqualTo (39 ))).then ("'Fred'" )
191+ .else_ ("cast('Not a Fox or a bat' as varchar(25))" ).end ().as ("AnimalType" ))
190192 .from (animalData , "a" )
191193 .where (id , isIn (2 , 3 , 4 , 31 , 32 , 38 , 39 ))
192194 .orderBy (id )
@@ -233,14 +235,41 @@ void testSearchedCaseWithGroup() {
233235 }
234236 }
235237
238+ @ Test
239+ void testSimpleCassLessThan () {
240+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
241+ CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
242+
243+ SelectStatementProvider selectStatement = select (animalName , simpleCase (brainWeight )
244+ .when (isLessThan (4.0 )).then ("'small brain'" )
245+ .else_ ("'large brain'" ).end ().as ("brain_size" ))
246+ .from (animalData )
247+ .where (id , isIn (31 , 32 , 38 , 39 ))
248+ .orderBy (id )
249+ .build ()
250+ .render (RenderingStrategies .MYBATIS3 );
251+
252+ String expected = "select animal_name, case brain_weight " +
253+ "when < #{parameters.p1,jdbcType=DOUBLE} then 'small brain' " +
254+ "else 'large brain' end as brain_size " +
255+ "from AnimalData where id in (" +
256+ "#{parameters.p2,jdbcType=INTEGER},#{parameters.p3,jdbcType=INTEGER}," +
257+ "#{parameters.p4,jdbcType=INTEGER},#{parameters.p5,jdbcType=INTEGER}) " +
258+ "order by id" ;
259+ assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected );
260+ List <Map <String , Object >> records = mapper .selectManyMappedRows (selectStatement );
261+ assertThat (records ).hasSize (7 );
262+ }
263+ }
264+
236265 @ Test
237266 void testSimpleCase () {
238267 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
239268 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
240269
241270 SelectStatementProvider selectStatement = select (animalName , simpleCase (animalName )
242- .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).thenConstant ("'yes'" )
243- .elseConstant ("cast('no' as VARCHAR(3))" ).end ().as ("IsAFox" ))
271+ .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then ("'yes'" )
272+ .else_ ("cast('no' as VARCHAR(3))" ).end ().as ("IsAFox" ))
244273 .from (animalData )
245274 .where (id , isIn (31 , 32 , 38 , 39 ))
246275 .orderBy (id )
@@ -277,7 +306,7 @@ void testSimpleCaseNoElse() {
277306 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
278307
279308 SelectStatementProvider selectStatement = select (animalName , simpleCase (animalName )
280- .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).thenConstant ("'yes'" )
309+ .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then ("'yes'" )
281310 .end ().as ("IsAFox" ))
282311 .from (animalData )
283312 .where (id , isIn (31 , 32 , 38 , 39 ))
@@ -312,7 +341,7 @@ void testSimpleCaseNoElse() {
312341 @ Test
313342 void testInvalidSearchedCaseNoConditionsRender () {
314343 SelectModel model = select (animalName , searchedCase ()
315- .when (animalName , isEqualToWhenPresent ((String ) null )).thenConstant ("Fred" ).end ())
344+ .when (animalName , isEqualToWhenPresent ((String ) null )).then ("Fred" ).end ())
316345 .from (animalData )
317346 .build ();
318347
@@ -324,7 +353,7 @@ void testInvalidSearchedCaseNoConditionsRender() {
324353 @ Test
325354 void testInvalidSimpleCaseNoConditionsRender () {
326355 SelectModel model = select (simpleCase (animalName )
327- .when (isEqualToWhenPresent ((String ) null )).thenConstant ("Fred" ).end ())
356+ .when (isEqualToWhenPresent ((String ) null )).then ("Fred" ).end ())
328357 .from (animalData )
329358 .build ();
330359
0 commit comments