17
17
18
18
import static examples .animal .data .AnimalDataDynamicSqlSupport .animalData ;
19
19
import static examples .animal .data .AnimalDataDynamicSqlSupport .animalName ;
20
+ import static examples .animal .data .AnimalDataDynamicSqlSupport .brainWeight ;
20
21
import static examples .animal .data .AnimalDataDynamicSqlSupport .id ;
21
22
import static org .assertj .core .api .Assertions .assertThat ;
22
23
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
26
27
import static org .mybatis .dynamic .sql .SqlBuilder .isEqualTo ;
27
28
import static org .mybatis .dynamic .sql .SqlBuilder .isEqualToWhenPresent ;
28
29
import static org .mybatis .dynamic .sql .SqlBuilder .isIn ;
30
+ import static org .mybatis .dynamic .sql .SqlBuilder .isLessThan ;
29
31
import static org .mybatis .dynamic .sql .SqlBuilder .or ;
30
32
import static org .mybatis .dynamic .sql .SqlBuilder .searchedCase ;
31
33
import static org .mybatis .dynamic .sql .SqlBuilder .select ;
@@ -85,9 +87,9 @@ void testSearchedCase() {
85
87
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
86
88
87
89
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" ))
91
93
.from (animalData , "a" )
92
94
.where (id , isIn (2 , 3 , 31 , 32 , 38 , 39 ))
93
95
.orderBy (id )
@@ -134,8 +136,8 @@ void testSearchedCaseNoElse() {
134
136
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
135
137
136
138
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'" )
139
141
.end ().as ("AnimalType" ))
140
142
.from (animalData , "a" )
141
143
.where (id , isIn (2 , 3 , 31 , 32 , 38 , 39 ))
@@ -183,10 +185,10 @@ void testSearchedCaseWithGroup() {
183
185
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
184
186
185
187
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" ))
190
192
.from (animalData , "a" )
191
193
.where (id , isIn (2 , 3 , 4 , 31 , 32 , 38 , 39 ))
192
194
.orderBy (id )
@@ -233,14 +235,41 @@ void testSearchedCaseWithGroup() {
233
235
}
234
236
}
235
237
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
+
236
265
@ Test
237
266
void testSimpleCase () {
238
267
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
239
268
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
240
269
241
270
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" ))
244
273
.from (animalData )
245
274
.where (id , isIn (31 , 32 , 38 , 39 ))
246
275
.orderBy (id )
@@ -277,7 +306,7 @@ void testSimpleCaseNoElse() {
277
306
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
278
307
279
308
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'" )
281
310
.end ().as ("IsAFox" ))
282
311
.from (animalData )
283
312
.where (id , isIn (31 , 32 , 38 , 39 ))
@@ -312,7 +341,7 @@ void testSimpleCaseNoElse() {
312
341
@ Test
313
342
void testInvalidSearchedCaseNoConditionsRender () {
314
343
SelectModel model = select (animalName , searchedCase ()
315
- .when (animalName , isEqualToWhenPresent ((String ) null )).thenConstant ("Fred" ).end ())
344
+ .when (animalName , isEqualToWhenPresent ((String ) null )).then ("Fred" ).end ())
316
345
.from (animalData )
317
346
.build ();
318
347
@@ -324,7 +353,7 @@ void testInvalidSearchedCaseNoConditionsRender() {
324
353
@ Test
325
354
void testInvalidSimpleCaseNoConditionsRender () {
326
355
SelectModel model = select (simpleCase (animalName )
327
- .when (isEqualToWhenPresent ((String ) null )).thenConstant ("Fred" ).end ())
356
+ .when (isEqualToWhenPresent ((String ) null )).then ("Fred" ).end ())
328
357
.from (animalData )
329
358
.build ();
330
359
0 commit comments