2222import static org .assertj .core .api .Assertions .assertThat ;
2323import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
2424import static org .assertj .core .api .Assertions .entry ;
25+ import static org .mybatis .dynamic .sql .SqlBuilder .add ;
2526import static org .mybatis .dynamic .sql .SqlBuilder .and ;
2627import static org .mybatis .dynamic .sql .SqlBuilder .cast ;
28+ import static org .mybatis .dynamic .sql .SqlBuilder .constant ;
2729import static org .mybatis .dynamic .sql .SqlBuilder .group ;
2830import static org .mybatis .dynamic .sql .SqlBuilder .isEqualTo ;
2931import static org .mybatis .dynamic .sql .SqlBuilder .isEqualToWhenPresent ;
3638
3739import java .io .InputStream ;
3840import java .io .InputStreamReader ;
41+ import java .math .BigDecimal ;
3942import java .sql .Connection ;
4043import java .sql .DriverManager ;
4144import java .util .List ;
5154import org .apache .ibatis .transaction .jdbc .JdbcTransactionFactory ;
5255import org .junit .jupiter .api .BeforeEach ;
5356import org .junit .jupiter .api .Test ;
54- import org .mybatis .dynamic .sql .SqlBuilder ;
5557import org .mybatis .dynamic .sql .exception .InvalidSqlException ;
5658import org .mybatis .dynamic .sql .render .RenderingStrategies ;
5759import org .mybatis .dynamic .sql .select .caseexpression .SearchedCaseDSL ;
@@ -345,7 +347,7 @@ void testSimpleCassLessThan() {
345347 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
346348 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
347349
348- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (brainWeight )
350+ SelectStatementProvider selectStatement = select (animalName , case_ (brainWeight )
349351 .when (isLessThan (4.0 )).then ("small brain" )
350352 .else_ ("large brain" ).end ().as ("brain_size" ))
351353 .from (animalData )
@@ -372,7 +374,7 @@ void testSimpleCaseWithStrings() {
372374 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
373375 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
374376
375- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
377+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
376378 .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then ("yes" )
377379 .else_ ("no" ).end ().as ("IsAFox" ))
378380 .from (animalData )
@@ -410,7 +412,7 @@ void testSimpleCaseBasicWithStrings() {
410412 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
411413 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
412414
413- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
415+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
414416 .when ("Artic fox" , "Red fox" ).then ("yes" )
415417 .else_ ("no" ).end ().as ("IsAFox" ))
416418 .from (animalData )
@@ -448,7 +450,7 @@ void testSimpleCaseWithBooleans() {
448450 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
449451 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
450452
451- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
453+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
452454 .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then (true )
453455 .else_ (false ).end ().as ("IsAFox" ))
454456 .from (animalData )
@@ -486,7 +488,7 @@ void testSimpleCaseWithBoundValues() {
486488 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
487489 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
488490
489- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
491+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
490492 .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then (value ("yes" ))
491493 .else_ (cast (value ("no" )).as ("VARCHAR(30)" )).end ().as ("IsAFox" ))
492494 .from (animalData )
@@ -527,7 +529,7 @@ void testSimpleCaseBasicWithBooleans() {
527529 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
528530 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
529531
530- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
532+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
531533 .when ("Artic fox" , "Red fox" ).then (true )
532534 .else_ (false ).end ().as ("IsAFox" ))
533535 .from (animalData )
@@ -565,7 +567,7 @@ void testSimpleCaseNoElse() {
565567 try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
566568 CommonSelectMapper mapper = sqlSession .getMapper (CommonSelectMapper .class );
567569
568- SelectStatementProvider selectStatement = select (animalName , SqlBuilder . case_ (animalName )
570+ SelectStatementProvider selectStatement = select (animalName , case_ (animalName )
569571 .when (isEqualTo ("Artic fox" ), isEqualTo ("Red fox" )).then ("yes" )
570572 .end ().as ("IsAFox" ))
571573 .from (animalData )
@@ -598,6 +600,117 @@ void testSimpleCaseNoElse() {
598600 }
599601 }
600602
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+
601714 @ Test
602715 void testInvalidSearchedCaseNoConditionsRender () {
603716 SelectModel model = select (animalName , case_ ()
@@ -612,7 +725,7 @@ void testInvalidSearchedCaseNoConditionsRender() {
612725
613726 @ Test
614727 void testInvalidSimpleCaseNoConditionsRender () {
615- SelectModel model = select (SqlBuilder . case_ (animalName )
728+ SelectModel model = select (case_ (animalName )
616729 .when (isEqualToWhenPresent ((String ) null )).then ("Fred" ).end ())
617730 .from (animalData )
618731 .build ();
0 commit comments