11
11
12
12
import java .io .StringReader ;
13
13
import net .sf .jsqlparser .JSQLParserException ;
14
+ import net .sf .jsqlparser .expression .DoubleValue ;
14
15
import net .sf .jsqlparser .expression .JdbcParameter ;
15
16
import net .sf .jsqlparser .expression .LongValue ;
16
17
import net .sf .jsqlparser .expression .StringValue ;
17
18
import net .sf .jsqlparser .expression .operators .relational .GreaterThanEquals ;
18
19
import net .sf .jsqlparser .parser .CCJSqlParserManager ;
19
20
import net .sf .jsqlparser .parser .CCJSqlParserUtil ;
21
+ import net .sf .jsqlparser .schema .Column ;
20
22
import static net .sf .jsqlparser .test .TestUtils .*;
21
23
import static org .junit .jupiter .api .Assertions .assertEquals ;
22
24
import static org .junit .jupiter .api .Assertions .assertFalse ;
26
28
27
29
public class UpdateTest {
28
30
29
- private static CCJSqlParserManager parserManager = new CCJSqlParserManager ();
31
+ private static final CCJSqlParserManager PARSER_MANAGER = new CCJSqlParserManager ();
30
32
31
33
@ Test
32
34
public void testUpdate () throws JSQLParserException {
33
35
String statement = "UPDATE mytable set col1='as', col2=?, col3=565 Where o >= 3" ;
34
- Update update = (Update ) parserManager .parse (new StringReader (statement ));
36
+ Update update = (Update ) PARSER_MANAGER .parse (new StringReader (statement ));
35
37
assertEquals ("mytable" , update .getTable ().toString ());
36
38
assertEquals (3 , update .getUpdateSets ().size ());
37
39
assertEquals ("col1" , update .getUpdateSets ().get (0 ).getColumns ().get (0 ).getColumnName ());
@@ -47,7 +49,7 @@ public void testUpdate() throws JSQLParserException {
47
49
@ Test
48
50
public void testUpdateWAlias () throws JSQLParserException {
49
51
String statement = "UPDATE table1 A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY'" ;
50
- Update update = (Update ) parserManager .parse (new StringReader (statement ));
52
+ Update update = (Update ) PARSER_MANAGER .parse (new StringReader (statement ));
51
53
}
52
54
53
55
@ Test
@@ -113,7 +115,7 @@ public void testUpdateWithReturningList() throws JSQLParserException {
113
115
@ Test
114
116
public void testUpdateDoesNotAllowLimitOffset () {
115
117
String statement = "UPDATE table1 A SET A.columna = 'XXX' WHERE A.cod_table = 'YYY' LIMIT 3,4" ;
116
- assertThrows (JSQLParserException .class , () -> parserManager .parse (new StringReader (statement )));
118
+ assertThrows (JSQLParserException .class , () -> PARSER_MANAGER .parse (new StringReader (statement )));
117
119
}
118
120
119
121
@ Test
@@ -275,32 +277,44 @@ public void testUpdateMultipleModifiers() throws JSQLParserException {
275
277
@ Test
276
278
public void testUpdateOutputClause () throws JSQLParserException {
277
279
assertSqlCanBeParsedAndDeparsed (
278
- "UPDATE /* TOP (10) */ HumanResources.Employee \n " +
279
- "SET VacationHours = VacationHours * 1.25, \n " +
280
- " ModifiedDate = GETDATE() \n " +
281
- "OUTPUT inserted.BusinessEntityID, \n " +
282
- " deleted.VacationHours, \n " +
283
- " inserted.VacationHours, \n " +
284
- " inserted.ModifiedDate \n " +
285
- "INTO @MyTableVar"
286
- , true
280
+ "UPDATE /* TOP (10) */ HumanResources.Employee \n "
281
+ + "SET VacationHours = VacationHours * 1.25, \n "
282
+ + " ModifiedDate = GETDATE() \n "
283
+ + "OUTPUT inserted.BusinessEntityID, \n "
284
+ + " deleted.VacationHours, \n "
285
+ + " inserted.VacationHours, \n "
286
+ + " inserted.ModifiedDate \n "
287
+ + "INTO @MyTableVar" ,
288
+ true
287
289
);
288
290
289
291
assertSqlCanBeParsedAndDeparsed (
290
- "UPDATE Production.WorkOrder \n " +
291
- "SET ScrapReasonID = 4 \n " +
292
- "OUTPUT deleted.ScrapReasonID, \n " +
293
- " inserted.ScrapReasonID, \n " +
294
- " inserted.WorkOrderID, \n " +
295
- " inserted.ProductID, \n " +
296
- " p.Name \n " +
297
- " INTO @MyTestVar \n " +
298
- "FROM Production.WorkOrder AS wo \n " +
299
- " INNER JOIN Production.Product AS p \n " +
300
- " ON wo.ProductID = p.ProductID \n " +
301
- " AND wo.ScrapReasonID= 16 \n " +
302
- " AND p.ProductID = 733"
303
- , true
292
+ "UPDATE Production.WorkOrder \n "
293
+ + "SET ScrapReasonID = 4 \n "
294
+ + "OUTPUT deleted.ScrapReasonID, \n "
295
+ + " inserted.ScrapReasonID, \n "
296
+ + " inserted.WorkOrderID, \n "
297
+ + " inserted.ProductID, \n "
298
+ + " p.Name \n "
299
+ + " INTO @MyTestVar \n "
300
+ + "FROM Production.WorkOrder AS wo \n "
301
+ + " INNER JOIN Production.Product AS p \n "
302
+ + " ON wo.ProductID = p.ProductID \n "
303
+ + " AND wo.ScrapReasonID= 16 \n "
304
+ + " AND p.ProductID = 733" ,
305
+ true
304
306
);
305
307
}
308
+
309
+ @ Test
310
+ public void testUpdateSetsIssue1590 () throws JSQLParserException {
311
+ Update update = (Update ) CCJSqlParserUtil .parse ("update mytable set a=5 where b = 2" );
312
+ assertEquals (1 , update .getUpdateSets ().size ());
313
+ update .addColumns (new Column ("y" ));
314
+ update .addExpressions (new DoubleValue ("6" ));
315
+ update .getUpdateSets ().get (0 ).setUsingBracketsForColumns (true );
316
+ update .getUpdateSets ().get (0 ).setUsingBracketsForValues (true );
317
+
318
+ assertEquals ("UPDATE mytable SET (a, y) = (5, 6) WHERE b = 2" , update .toString ());
319
+ }
306
320
}
0 commit comments