@@ -767,6 +767,36 @@ void testStringConstant() {
767
767
}
768
768
}
769
769
770
+ @ Test
771
+ void testDeprecatedAdd () {
772
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
773
+ AnimalDataMapper mapper = sqlSession .getMapper (AnimalDataMapper .class );
774
+
775
+ SelectStatementProvider selectStatement = select (id , animalName , DeprecatedAdd .of (bodyWeight , brainWeight ).as ("calculated_weight" ))
776
+ .from (animalData , "a" )
777
+ .where (DeprecatedAdd .of (bodyWeight , brainWeight ), isGreaterThan (10000.0 ))
778
+ .build ()
779
+ .render (RenderingStrategies .MYBATIS3 );
780
+
781
+ String expected = "select a.id, a.animal_name, (a.body_weight + a.brain_weight) as calculated_weight "
782
+ + "from AnimalData a "
783
+ + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}" ;
784
+
785
+ List <Map <String , Object >> animals = mapper .generalSelect (selectStatement );
786
+
787
+ assertAll (
788
+ () -> assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected ),
789
+ () -> assertThat (animals ).hasSize (3 ),
790
+ () -> assertThat (animals .get (0 )).containsEntry ("ANIMAL_NAME" , "African elephant" ),
791
+ () -> assertThat (animals .get (0 )).containsEntry ("CALCULATED_WEIGHT" , 12366.0 ),
792
+ () -> assertThat (animals .get (1 )).containsEntry ("ANIMAL_NAME" , "Dipliodocus" ),
793
+ () -> assertThat (animals .get (1 )).containsEntry ("CALCULATED_WEIGHT" , 11750.0 ),
794
+ () -> assertThat (animals .get (2 )).containsEntry ("ANIMAL_NAME" , "Brachiosaurus" ),
795
+ () -> assertThat (animals .get (2 )).containsEntry ("CALCULATED_WEIGHT" , 87154.5 )
796
+ );
797
+ }
798
+ }
799
+
770
800
@ Test
771
801
void testAdd () {
772
802
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
@@ -827,6 +857,36 @@ void testAddConstant() {
827
857
}
828
858
}
829
859
860
+ @ Test
861
+ void testAddConstantWithConstantFirst () {
862
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
863
+ AnimalDataMapper mapper = sqlSession .getMapper (AnimalDataMapper .class );
864
+
865
+ SelectStatementProvider selectStatement = select (id , animalName , add (constant ("22" ), bodyWeight , constant ("33" )).as ("calculated_weight" ))
866
+ .from (animalData , "a" )
867
+ .where (add (bodyWeight , brainWeight ), isGreaterThan (10000.0 ))
868
+ .build ()
869
+ .render (RenderingStrategies .MYBATIS3 );
870
+
871
+ String expected = "select a.id, a.animal_name, (22 + a.body_weight + 33) as calculated_weight "
872
+ + "from AnimalData a "
873
+ + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}" ;
874
+
875
+ List <Map <String , Object >> animals = mapper .generalSelect (selectStatement );
876
+
877
+ assertAll (
878
+ () -> assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected ),
879
+ () -> assertThat (animals ).hasSize (3 ),
880
+ () -> assertThat (animals .get (0 )).containsEntry ("ANIMAL_NAME" , "African elephant" ),
881
+ () -> assertThat (animals .get (0 )).containsEntry ("CALCULATED_WEIGHT" , 5767.0 ),
882
+ () -> assertThat (animals .get (1 )).containsEntry ("ANIMAL_NAME" , "Dipliodocus" ),
883
+ () -> assertThat (animals .get (1 )).containsEntry ("CALCULATED_WEIGHT" , 105.0 ),
884
+ () -> assertThat (animals .get (2 )).containsEntry ("ANIMAL_NAME" , "Brachiosaurus" ),
885
+ () -> assertThat (animals .get (2 )).containsEntry ("CALCULATED_WEIGHT" , 209.5 )
886
+ );
887
+ }
888
+ }
889
+
830
890
@ Test
831
891
void testConcatenate () {
832
892
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
@@ -854,6 +914,33 @@ void testConcatenate() {
854
914
}
855
915
}
856
916
917
+ @ Test
918
+ void testConcatenateConstantFirst () {
919
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
920
+ AnimalDataMapper mapper = sqlSession .getMapper (AnimalDataMapper .class );
921
+
922
+ SelectStatementProvider selectStatement = select (id , concatenate (stringConstant ("Name: " ), animalName ).as ("display_name" ))
923
+ .from (animalData , "a" )
924
+ .where (add (bodyWeight , brainWeight ), isGreaterThan (10000.0 ))
925
+ .build ()
926
+ .render (RenderingStrategies .MYBATIS3 );
927
+
928
+ String expected = "select a.id, ('Name: ' || a.animal_name) as display_name "
929
+ + "from AnimalData a "
930
+ + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}" ;
931
+
932
+ List <Map <String , Object >> animals = mapper .generalSelect (selectStatement );
933
+
934
+ assertAll (
935
+ () -> assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected ),
936
+ () -> assertThat (animals ).hasSize (3 ),
937
+ () -> assertThat (animals .get (0 )).containsEntry ("DISPLAY_NAME" , "Name: African elephant" ),
938
+ () -> assertThat (animals .get (1 )).containsEntry ("DISPLAY_NAME" , "Name: Dipliodocus" ),
939
+ () -> assertThat (animals .get (2 )).containsEntry ("DISPLAY_NAME" , "Name: Brachiosaurus" )
940
+ );
941
+ }
942
+ }
943
+
857
944
@ Test
858
945
void testDivide () {
859
946
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
@@ -1034,6 +1121,36 @@ void testSubtractConstant() {
1034
1121
}
1035
1122
}
1036
1123
1124
+ @ Test
1125
+ void testGeneralOperator () {
1126
+ try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
1127
+ AnimalDataMapper mapper = sqlSession .getMapper (AnimalDataMapper .class );
1128
+
1129
+ SelectStatementProvider selectStatement = select (id , animalName , applyOperator ("-" , bodyWeight , brainWeight ).as ("calculated_weight" ))
1130
+ .from (animalData , "a" )
1131
+ .where (add (bodyWeight , brainWeight ), isGreaterThan (10000.0 ))
1132
+ .build ()
1133
+ .render (RenderingStrategies .MYBATIS3 );
1134
+
1135
+ String expected = "select a.id, a.animal_name, (a.body_weight - a.brain_weight) as calculated_weight "
1136
+ + "from AnimalData a "
1137
+ + "where (a.body_weight + a.brain_weight) > #{parameters.p1,jdbcType=DOUBLE}" ;
1138
+
1139
+ List <Map <String , Object >> animals = mapper .generalSelect (selectStatement );
1140
+
1141
+ assertAll (
1142
+ () -> assertThat (selectStatement .getSelectStatement ()).isEqualTo (expected ),
1143
+ () -> assertThat (animals ).hasSize (3 ),
1144
+ () -> assertThat (animals .get (0 )).containsEntry ("ANIMAL_NAME" , "African elephant" ),
1145
+ () -> assertThat (animals .get (0 )).containsEntry ("CALCULATED_WEIGHT" , -942.0 ),
1146
+ () -> assertThat (animals .get (1 )).containsEntry ("ANIMAL_NAME" , "Dipliodocus" ),
1147
+ () -> assertThat (animals .get (1 )).containsEntry ("CALCULATED_WEIGHT" , -11650.0 ),
1148
+ () -> assertThat (animals .get (2 )).containsEntry ("ANIMAL_NAME" , "Brachiosaurus" ),
1149
+ () -> assertThat (animals .get (2 )).containsEntry ("CALCULATED_WEIGHT" , -86845.5 )
1150
+ );
1151
+ }
1152
+ }
1153
+
1037
1154
@ Test
1038
1155
void testComplexExpression () {
1039
1156
try (SqlSession sqlSession = sqlSessionFactory .openSession ()) {
0 commit comments