1616package org .mybatis .dynamic .sql ;
1717
1818import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
19+ import static org .mybatis .dynamic .sql .SqlBuilder .insert ;
20+ import static org .mybatis .dynamic .sql .SqlBuilder .insertInto ;
21+ import static org .mybatis .dynamic .sql .SqlBuilder .update ;
1922
2023import java .util .ArrayList ;
2124import java .util .Collections ;
2831import org .mybatis .dynamic .sql .insert .InsertColumnListModel ;
2932import org .mybatis .dynamic .sql .insert .InsertModel ;
3033import org .mybatis .dynamic .sql .insert .MultiRowInsertModel ;
34+ import org .mybatis .dynamic .sql .render .RenderingStrategies ;
3135import org .mybatis .dynamic .sql .select .GroupByModel ;
3236import org .mybatis .dynamic .sql .select .OrderByModel ;
3337import org .mybatis .dynamic .sql .select .QueryExpressionModel ;
4044class InvalidSQLTest {
4145
4246 private static final SqlTable person = new SqlTable ("person" );
47+ private static final SqlColumn <Integer > id = person .column ("id" );
4348
4449 @ Test
4550 void testInvalidGeneralInsertStatement () {
@@ -50,6 +55,17 @@ void testInvalidGeneralInsertStatement() {
5055 .withMessage ("General insert statements must have at least one column mapping" );
5156 }
5257
58+ @ Test
59+ void testInvalidGeneralInsertStatementWhenAllOptionalsAreDropped () {
60+ GeneralInsertModel model = insertInto (person )
61+ .set (id ).toValueWhenPresent ((Integer ) null )
62+ .build ();
63+
64+ assertThatExceptionOfType (InvalidSqlException .class )
65+ .isThrownBy (() -> model .render (RenderingStrategies .SPRING_NAMED_PARAMETER ))
66+ .withMessage ("All optional set phrases were dropped when rendering the general insert statement" );
67+ }
68+
5369 @ Test
5470 void testInvalidInsertStatement () {
5571 InsertModel .Builder <String > builder = new InsertModel .Builder <String >()
@@ -60,6 +76,20 @@ void testInvalidInsertStatement() {
6076 .withMessage ("Insert statements must have at least one column mapping" );
6177 }
6278
79+ @ Test
80+ void testInvalidInsertStatementWhenAllOptionalsAreDropped () {
81+ TestRow testRow = new TestRow ();
82+
83+ InsertModel <TestRow > model = insert (testRow )
84+ .into (person )
85+ .map (id ).toPropertyWhenPresent ("id" , testRow ::getId )
86+ .build ();
87+
88+ assertThatExceptionOfType (InvalidSqlException .class )
89+ .isThrownBy (() -> model .render (RenderingStrategies .SPRING_NAMED_PARAMETER ))
90+ .withMessage ("All optional column mappings were dropped when rendering the insert statement" );
91+ }
92+
6393 @ Test
6494 void testInvalidMultipleInsertStatementNoRecords () {
6595 MultiRowInsertModel .Builder <String > builder = new MultiRowInsertModel .Builder <String >()
@@ -176,4 +206,27 @@ void testInvalidUpdateStatement() {
176206 assertThatExceptionOfType (InvalidSqlException .class ).isThrownBy (builder ::build )
177207 .withMessage ("Update statements must have at least one set phrase" );
178208 }
209+
210+ @ Test
211+ void testInvalidUpdateStatementWhenAllOptionalsAreDropped () {
212+ UpdateModel model = update (person )
213+ .set (id ).equalToWhenPresent ((Integer ) null )
214+ .build ();
215+
216+ assertThatExceptionOfType (InvalidSqlException .class )
217+ .isThrownBy (() -> model .render (RenderingStrategies .SPRING_NAMED_PARAMETER ))
218+ .withMessage ("All optional set phrases were dropped when rendering the update statement" );
219+ }
220+
221+ static class TestRow {
222+ private Integer id ;
223+
224+ public Integer getId () {
225+ return id ;
226+ }
227+
228+ public void setId (Integer id ) {
229+ this .id = id ;
230+ }
231+ }
179232}
0 commit comments