22
22
import static org .assertj .core .api .Assertions .assertThat ;
23
23
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
24
24
import static org .assertj .core .api .Assertions .entry ;
25
+ import static org .mybatis .dynamic .sql .SqlBuilder .add ;
25
26
import static org .mybatis .dynamic .sql .SqlBuilder .and ;
26
27
import static org .mybatis .dynamic .sql .SqlBuilder .cast ;
28
+ import static org .mybatis .dynamic .sql .SqlBuilder .constant ;
27
29
import static org .mybatis .dynamic .sql .SqlBuilder .group ;
28
30
import static org .mybatis .dynamic .sql .SqlBuilder .isEqualTo ;
29
31
import static org .mybatis .dynamic .sql .SqlBuilder .isEqualToWhenPresent ;
36
38
37
39
import java .io .InputStream ;
38
40
import java .io .InputStreamReader ;
41
+ import java .math .BigDecimal ;
39
42
import java .sql .Connection ;
40
43
import java .sql .DriverManager ;
41
44
import java .util .List ;
51
54
import org .apache .ibatis .transaction .jdbc .JdbcTransactionFactory ;
52
55
import org .junit .jupiter .api .BeforeEach ;
53
56
import org .junit .jupiter .api .Test ;
54
- import org .mybatis .dynamic .sql .SqlBuilder ;
55
57
import org .mybatis .dynamic .sql .exception .InvalidSqlException ;
56
58
import org .mybatis .dynamic .sql .render .RenderingStrategies ;
57
59
import org .mybatis .dynamic .sql .select .caseexpression .SearchedCaseDSL ;
@@ -345,7 +347,7 @@ void testSimpleCassLessThan() {
345
347
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
346
348
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
347
349
348
- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (brainWeight )
350
+ SelectStatementProvider selectStatement = select (animalName , case_ (brainWeight )
349
351
.when (isLessThan (4.0 )).then ("small brain" )
350
352
.else_ ("large brain" ).end ().as ("brain_size" ))
351
353
.from (animalData )
@@ -372,7 +374,7 @@ void testSimpleCaseWithStrings() {
372
374
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
373
375
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
374
376
375
- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
377
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
376
378
.when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then ("yes" )
377
379
.else_ ("no" ).end ().as ("IsAFox" ))
378
380
.from (animalData )
@@ -410,7 +412,7 @@ void testSimpleCaseBasicWithStrings() {
410
412
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
411
413
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
412
414
413
- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
415
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
414
416
.when ("Artic fox" , "Red fox" ).then ("yes" )
415
417
.else_ ("no" ).end ().as ("IsAFox" ))
416
418
.from (animalData )
@@ -448,7 +450,7 @@ void testSimpleCaseWithBooleans() {
448
450
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
449
451
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
450
452
451
- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
453
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
452
454
.when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then (true )
453
455
.else_ (false ).end ().as ("IsAFox" ))
454
456
.from (animalData )
@@ -486,7 +488,7 @@ void testSimpleCaseWithBoundValues() {
486
488
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
487
489
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
488
490
489
- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
491
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
490
492
.when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then (value ("yes" ))
491
493
.else_ (cast (value ("no" )).as ("VARCHAR(30)" )).end ().as ("IsAFox" ))
492
494
.from (animalData )
@@ -527,7 +529,7 @@ void testSimpleCaseBasicWithBooleans() {
527
529
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
528
530
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
529
531
530
- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
532
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
531
533
.when ("Artic fox" , "Red fox" ).then (true )
532
534
.else_ (false ).end ().as ("IsAFox" ))
533
535
.from (animalData )
@@ -565,7 +567,7 @@ void testSimpleCaseNoElse() {
565
567
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
566
568
CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
567
569
568
- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
570
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
569
571
.when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then ("yes" )
570
572
.end ().as ("IsAFox" ))
571
573
.from (animalData )
@@ -598,6 +600,117 @@ void testSimpleCaseNoElse() {
598
600
}
599
601
}
600
602
603
+ @ Test
604
+ void testSimpleCaseLongs () {
605
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
606
+ CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
607
+
608
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
609
+ .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then (1L )
610
+ .else_ (2L )
611
+ .end ().as ("IsAFox" ))
612
+ .from (animalData )
613
+ .where (id , isIn (31 , 32 , 38 , 39 ))
614
+ .orderBy (id )
615
+ .build ()
616
+ .render (RenderingStrategies .MYBATIS3 );
617
+
618
+ String expected = "select animal_name, " +
619
+ "case animal_name when = #{parameters.p1,jdbcType=VARCHAR}, = #{parameters.p2,jdbcType=VARCHAR} then 1 else 2 end " +
620
+ "as IsAFox from AnimalData where id in " +
621
+ "(#{parameters.p3,jdbcType=INTEGER},#{parameters.p4,jdbcType=INTEGER},#{parameters.p5,jdbcType=INTEGER},#{parameters.p6,jdbcType=INTEGER}) " +
622
+ "order by id" ;
623
+ assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected );
624
+ assertThat (selectStatement .getParameters ()).containsOnly (
625
+ entry ("p1" , "Artic fox" ),
626
+ entry ("p2" , "Red fox" ),
627
+ entry ("p3" , 31 ),
628
+ entry ("p4" , 32 ),
629
+ entry ("p5" , 38 ),
630
+ entry ("p6" , 39 )
631
+ );
632
+
633
+ List <Map <String , Object >> records = mapper .selectManyMappedRows (selectStatement );
634
+ assertThat (records ).hasSize (4 );
635
+ assertThat (records .get (0 )).containsOnly (entry ("ANIMAL_NAME" , "Cat" ), entry ("ISAFOX" , 2 ));
636
+ assertThat (records .get (1 )).containsOnly (entry ("ANIMAL_NAME" , "Artic fox" ), entry ("ISAFOX" , 1 ));
637
+ assertThat (records .get (2 )).containsOnly (entry ("ANIMAL_NAME" , "Red fox" ), entry ("ISAFOX" , 1 ));
638
+ assertThat (records .get (3 )).containsOnly (entry ("ANIMAL_NAME" , "Raccoon" ), entry ("ISAFOX" , 2 ));
639
+ }
640
+ }
641
+
642
+ @ Test
643
+ void testSimpleCaseDoubles () {
644
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
645
+ CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
646
+
647
+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
648
+ .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then (1.1 )
649
+ .else_ (2.2 )
650
+ .end ().as ("IsAFox" ))
651
+ .from (animalData )
652
+ .where (id , isIn (31 , 32 , 38 , 39 ))
653
+ .orderBy (id )
654
+ .build ()
655
+ .render (RenderingStrategies .MYBATIS3 );
656
+
657
+ String expected = "select animal_name, " +
658
+ "case animal_name when = #{parameters.p1,jdbcType=VARCHAR}, = #{parameters.p2,jdbcType=VARCHAR} then 1.1 else 2.2 end " +
659
+ "as IsAFox from AnimalData where id in " +
660
+ "(#{parameters.p3,jdbcType=INTEGER},#{parameters.p4,jdbcType=INTEGER},#{parameters.p5,jdbcType=INTEGER},#{parameters.p6,jdbcType=INTEGER}) " +
661
+ "order by id" ;
662
+ assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected );
663
+ assertThat (selectStatement .getParameters ()).containsOnly (
664
+ entry ("p1" , "Artic fox" ),
665
+ entry ("p2" , "Red fox" ),
666
+ entry ("p3" , 31 ),
667
+ entry ("p4" , 32 ),
668
+ entry ("p5" , 38 ),
669
+ entry ("p6" , 39 )
670
+ );
671
+
672
+ List <Map <String , Object >> records = mapper .selectManyMappedRows (selectStatement );
673
+ assertThat (records ).hasSize (4 );
674
+ assertThat (records .get (0 )).containsOnly (entry ("ANIMAL_NAME" , "Cat" ), entry ("ISAFOX" , new BigDecimal ("2.2" )));
675
+ assertThat (records .get (1 )).containsOnly (entry ("ANIMAL_NAME" , "Artic fox" ), entry ("ISAFOX" , new BigDecimal ("1.1" )));
676
+ assertThat (records .get (2 )).containsOnly (entry ("ANIMAL_NAME" , "Red fox" ), entry ("ISAFOX" , new BigDecimal ("1.1" )));
677
+ assertThat (records .get (3 )).containsOnly (entry ("ANIMAL_NAME" , "Raccoon" ), entry ("ISAFOX" , new BigDecimal ("2.2" )));
678
+ }
679
+ }
680
+
681
+ @ Test
682
+ void testAliasCast () {
683
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
684
+ CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
685
+
686
+ SelectStatementProvider selectStatement = select (animalName , cast (add (id , constant ("20" ))).as ("INTEGER" ).as ("BIG_ID" ))
687
+ .from (animalData )
688
+ .where (id , isIn (31 , 32 , 38 , 39 ))
689
+ .orderBy (id )
690
+ .build ()
691
+ .render (RenderingStrategies .MYBATIS3 );
692
+
693
+ String expected = "select animal_name, cast((id + 20) as INTEGER) as BIG_ID " +
694
+ "from AnimalData where id in " +
695
+ "(#{parameters.p1,jdbcType=INTEGER},#{parameters.p2,jdbcType=INTEGER},#{parameters.p3,jdbcType=INTEGER},#{parameters.p4,jdbcType=INTEGER}) " +
696
+ "order by id" ;
697
+ assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected );
698
+ assertThat (selectStatement .getParameters ()).containsOnly (
699
+ entry ("p1" , 31 ),
700
+ entry ("p2" , 32 ),
701
+ entry ("p3" , 38 ),
702
+ entry ("p4" , 39 )
703
+ );
704
+
705
+ List <Map <String , Object >> records = mapper .selectManyMappedRows (selectStatement );
706
+ assertThat (records ).hasSize (4 );
707
+ assertThat (records .get (0 )).containsOnly (entry ("ANIMAL_NAME" , "Cat" ), entry ("BIG_ID" , 51 ));
708
+ assertThat (records .get (1 )).containsOnly (entry ("ANIMAL_NAME" , "Artic fox" ), entry ("BIG_ID" , 52 ));
709
+ assertThat (records .get (2 )).containsOnly (entry ("ANIMAL_NAME" , "Red fox" ), entry ("BIG_ID" , 58 ));
710
+ assertThat (records .get (3 )).containsOnly (entry ("ANIMAL_NAME" , "Raccoon" ), entry ("BIG_ID" , 59 ));
711
+ }
712
+ }
713
+
601
714
@ Test
602
715
void testInvalidSearchedCaseNoConditionsRender () {
603
716
SelectModel model = select (animalName , case_ ()
@@ -612,7 +725,7 @@ void testInvalidSearchedCaseNoConditionsRender() {
612
725
613
726
@ Test
614
727
void testInvalidSimpleCaseNoConditionsRender () {
615
- SelectModel model = select (SqlBuilder . case_ (animalName )
728
+ SelectModel model = select (case_ (animalName )
616
729
.when (isEqualToWhenPresent ((String ) null )).then ("Fred" ).end ())
617
730
.from (animalData )
618
731
.build ();
0 commit comments