16
16
package org .mybatis .dynamic .sql ;
17
17
18
18
import 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 ;
19
22
20
23
import java .util .ArrayList ;
21
24
import java .util .Collections ;
28
31
import org .mybatis .dynamic .sql .insert .InsertColumnListModel ;
29
32
import org .mybatis .dynamic .sql .insert .InsertModel ;
30
33
import org .mybatis .dynamic .sql .insert .MultiRowInsertModel ;
34
+ import org .mybatis .dynamic .sql .render .RenderingStrategies ;
31
35
import org .mybatis .dynamic .sql .select .GroupByModel ;
32
36
import org .mybatis .dynamic .sql .select .OrderByModel ;
33
37
import org .mybatis .dynamic .sql .select .QueryExpressionModel ;
40
44
class InvalidSQLTest {
41
45
42
46
private static final SqlTable person = new SqlTable ("person" );
47
+ private static final SqlColumn <Integer > id = person .column ("id" );
43
48
44
49
@ Test
45
50
void testInvalidGeneralInsertStatement () {
@@ -50,6 +55,17 @@ void testInvalidGeneralInsertStatement() {
50
55
.withMessage ("General insert statements must have at least one column mapping" );
51
56
}
52
57
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
+
53
69
@ Test
54
70
void testInvalidInsertStatement () {
55
71
InsertModel .Builder <String > builder = new InsertModel .Builder <String >()
@@ -60,6 +76,20 @@ void testInvalidInsertStatement() {
60
76
.withMessage ("Insert statements must have at least one column mapping" );
61
77
}
62
78
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
+
63
93
@ Test
64
94
void testInvalidMultipleInsertStatementNoRecords () {
65
95
MultiRowInsertModel .Builder <String > builder = new MultiRowInsertModel .Builder <String >()
@@ -176,4 +206,27 @@ void testInvalidUpdateStatement() {
176
206
assertThatExceptionOfType (InvalidSqlException .class ).isThrownBy (builder ::build )
177
207
.withMessage ("Update statements must have at least one set phrase" );
178
208
}
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
+ }
179
232
}
0 commit comments