Skip to content

Commit 7be5d8e

Browse files
Add Delete / Update modifiers for MySQL #1254 (#1396)
* Add Delete / Update modifiers for MySQL #1254 * fix codacy issues + pr return * simplify low_priority
1 parent cdf0f09 commit 7be5d8e

File tree

9 files changed

+230
-9
lines changed

9 files changed

+230
-9
lines changed

src/main/java/net/sf/jsqlparser/statement/delete/Delete.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public class Delete implements Statement {
4040
private Limit limit;
4141
private List<OrderByElement> orderByElements;
4242
private boolean hasFrom = true;
43+
private DeleteModifierPriority modifierPriority;
44+
private boolean modifierIgnore;
45+
private boolean modifierQuick;
46+
4347
public List<WithItem> getWithItemsList() {
4448
return withItemsList;
4549
}
@@ -160,10 +164,20 @@ public String toString() {
160164

161165
b.append("DELETE");
162166

167+
if (modifierPriority != null) {
168+
b.append(" ").append(modifierPriority.name());
169+
}
170+
if (modifierQuick) {
171+
b.append(" QUICK");
172+
}
173+
if (modifierIgnore) {
174+
b.append(" IGNORE");
175+
}
176+
163177
if (tables != null && tables.size() > 0) {
164178
b.append(" ");
165179
b.append(tables.stream()
166-
.map(t -> t.toString())
180+
.map(Table::toString)
167181
.collect(joining(", ")));
168182
}
169183

@@ -243,6 +257,45 @@ public Delete withHasFrom(boolean hasFrom) {
243257
return this;
244258
}
245259

260+
public Delete withModifierPriority(DeleteModifierPriority modifierPriority){
261+
this.setModifierPriority(modifierPriority);
262+
return this;
263+
}
264+
265+
public Delete withModifierIgnore(boolean modifierIgnore){
266+
this.setModifierIgnore(modifierIgnore);
267+
return this;
268+
}
269+
270+
public Delete withModifierQuick(boolean modifierQuick){
271+
this.setModifierQuick(modifierQuick);
272+
return this;
273+
}
274+
275+
public void setModifierPriority(DeleteModifierPriority modifierPriority) {
276+
this.modifierPriority = modifierPriority;
277+
}
278+
279+
public DeleteModifierPriority getModifierPriority() {
280+
return modifierPriority;
281+
}
282+
283+
public void setModifierIgnore(boolean modifierIgnore) {
284+
this.modifierIgnore = modifierIgnore;
285+
}
286+
287+
public void setModifierQuick(boolean modifierQuick) {
288+
this.modifierQuick = modifierQuick;
289+
}
290+
291+
public boolean isModifierIgnore() {
292+
return modifierIgnore;
293+
}
294+
295+
public boolean isModifierQuick() {
296+
return modifierQuick;
297+
}
298+
246299
public Delete addTables(Table... tables) {
247300
List<Table> collection = Optional.ofNullable(getTables()).orElseGet(ArrayList::new);
248301
Collections.addAll(collection, tables);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.delete;
11+
12+
public enum DeleteModifierPriority {
13+
LOW_PRIORITY
14+
}

src/main/java/net/sf/jsqlparser/statement/update/Update.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class Update implements Statement {
3838
private Limit limit;
3939
private boolean returningAllColumns = false;
4040
private List<SelectExpressionItem> returningExpressionList = null;
41+
private UpdateModifierPriority modifierPriority;
42+
private boolean modifierIgnore;
4143

4244
public ArrayList<UpdateSet> getUpdateSets() {
4345
return updateSets;
@@ -233,6 +235,23 @@ public void setReturningExpressionList(List<SelectExpressionItem> returningExpre
233235
this.returningExpressionList = returningExpressionList;
234236
}
235237

238+
public UpdateModifierPriority getModifierPriority() {
239+
return modifierPriority;
240+
}
241+
242+
public void setModifierPriority(UpdateModifierPriority modifierPriority) {
243+
this.modifierPriority = modifierPriority;
244+
}
245+
246+
public boolean isModifierIgnore() {
247+
return modifierIgnore;
248+
}
249+
250+
public void setModifierIgnore(boolean modifierIgnore) {
251+
this.modifierIgnore = modifierIgnore;
252+
}
253+
254+
236255
@Override
237256
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.NPathComplexity", "PMD.ExcessiveMethodLength"})
238257
public String toString() {
@@ -250,6 +269,12 @@ public String toString() {
250269
}
251270
}
252271
b.append("UPDATE ");
272+
if (modifierPriority != null) {
273+
b.append(modifierPriority.name()).append(" ");
274+
}
275+
if (modifierIgnore) {
276+
b.append("IGNORE ");
277+
}
253278
b.append(table);
254279
if (startJoins != null) {
255280
for (Join join : startJoins) {
@@ -405,6 +430,16 @@ public Update withExpressions(List<Expression> expressions) {
405430
return this;
406431
}
407432

433+
public Update withModifierPriority(UpdateModifierPriority modifierPriority){
434+
this.setModifierPriority(modifierPriority);
435+
return this;
436+
}
437+
438+
public Update withModifierIgnore(boolean modifierIgnore){
439+
this.setModifierIgnore(modifierIgnore);
440+
return this;
441+
}
442+
408443
public Update addColumns(Column... columns) {
409444
List<Column> collection = Optional.ofNullable(getColumns()).orElseGet(ArrayList::new);
410445
Collections.addAll(collection, columns);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*-
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
8+
* #L%
9+
*/
10+
package net.sf.jsqlparser.statement.update;
11+
12+
public enum UpdateModifierPriority {
13+
LOW_PRIORITY
14+
}

src/main/java/net/sf/jsqlparser/util/deparser/DeleteDeParser.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public void deParse(Delete delete) {
4747
}
4848
}
4949
buffer.append("DELETE");
50+
if (delete.getModifierPriority() != null) {
51+
buffer.append(" ").append(delete.getModifierPriority());
52+
}
53+
if (delete.isModifierQuick()) {
54+
buffer.append(" QUICK");
55+
}
56+
if (delete.isModifierIgnore()) {
57+
buffer.append(" IGNORE");
58+
}
5059
if (delete.getTables() != null && !delete.getTables().isEmpty()) {
5160
buffer.append(
5261
delete.getTables().stream().map(Table::getFullyQualifiedName).collect(joining(", ", " ", "")));

src/main/java/net/sf/jsqlparser/util/deparser/UpdateDeParser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,14 @@ public void deParse(Update update) {
4848
buffer.append(" ");
4949
}
5050
}
51-
buffer.append("UPDATE ").append(update.getTable());
51+
buffer.append("UPDATE ");
52+
if (update.getModifierPriority() != null) {
53+
buffer.append(update.getModifierPriority()).append(" ");
54+
}
55+
if (update.isModifierIgnore()) {
56+
buffer.append("IGNORE ");
57+
}
58+
buffer.append(update.getTable());
5259
if (update.getStartJoins() != null) {
5360
for (Join join : update.getStartJoins()) {
5461
if (join.isSimple()) {

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
326326
| <K_PUBLIC:"PUBLIC">
327327
| <K_PURGE:"PURGE">
328328
| <K_QUERY:"QUERY">
329+
| <K_QUICK : "QUICK">
329330
| <K_QUIESCE: "QUIESCE">
330331
| <K_RANGE: "RANGE">
331332
| <K_READ: "READ" >
@@ -1124,11 +1125,15 @@ Update Update( List<WithItem> with ):
11241125
List<OrderByElement> orderByElements;
11251126
boolean useColumnsBrackets = false;
11261127
List<SelectExpressionItem> returning = null;
1128+
Token tk = null;
1129+
UpdateModifierPriority modifierPriority = null;
1130+
boolean modifierIgnore = false;
11271131
}
11281132
{
1129-
<K_UPDATE> { update.setOracleHint(getOracleHint()); } table=TableWithAlias()
1130-
startJoins=JoinsList()
1131-
1133+
<K_UPDATE> { update.setOracleHint(getOracleHint()); }
1134+
[<K_LOW_PRIORITY> { modifierPriority = UpdateModifierPriority.LOW_PRIORITY; }]
1135+
[<K_IGNORE> { modifierIgnore = true; }]
1136+
table=TableWithAlias() startJoins=JoinsList()
11321137
<K_SET>
11331138
(
11341139
LOOKAHEAD(3) tableColumn=Column() "=" valueExpression=SimpleExpression() { update.addUpdateSet(tableColumn, valueExpression); }
@@ -1193,6 +1198,8 @@ Update Update( List<WithItem> with ):
11931198
.withStartJoins(startJoins)
11941199
.withFromItem(fromItem)
11951200
.withJoins(joins)
1201+
.withModifierPriority(modifierPriority)
1202+
.withModifierIgnore(modifierIgnore)
11961203
.withReturningExpressionList(returning);
11971204
}
11981205
}
@@ -1480,9 +1487,17 @@ Delete Delete( List<WithItem> with ):
14801487
Limit limit = null;
14811488
List<OrderByElement> orderByElements;
14821489
boolean hasFrom = false;
1490+
Token tk = null;
1491+
DeleteModifierPriority modifierPriority = null;
1492+
boolean modifierIgnore = false;
1493+
boolean modifierQuick = false;
14831494
}
14841495
{
1485-
<K_DELETE> { delete.setOracleHint(getOracleHint()); } [LOOKAHEAD(4) (table=TableWithAlias() { tables.add(table); }
1496+
<K_DELETE> { delete.setOracleHint(getOracleHint()); }
1497+
[<K_LOW_PRIORITY> { modifierPriority = DeleteModifierPriority.LOW_PRIORITY; }]
1498+
[<K_QUICK> { modifierQuick = true; }]
1499+
[<K_IGNORE> { modifierIgnore = true; }]
1500+
[LOOKAHEAD(4) (table=TableWithAlias() { tables.add(table); }
14861501
("," table=TableWithAlias() { tables.add(table); } )*
14871502
<K_FROM> | <K_FROM>) { hasFrom = true; }]
14881503

@@ -1497,7 +1512,13 @@ Delete Delete( List<WithItem> with ):
14971512
delete.setJoins(joins);
14981513
}
14991514
return delete.withWithItemsList(with)
1500-
.withTables(tables).withTable(table).withHasFrom(hasFrom).withUsingList(usingList);
1515+
.withTables(tables)
1516+
.withTable(table)
1517+
.withHasFrom(hasFrom)
1518+
.withUsingList(usingList)
1519+
.withModifierPriority(modifierPriority)
1520+
.withModifierIgnore(modifierIgnore)
1521+
.withModifierQuick(modifierQuick);
15011522
}
15021523
}
15031524

src/test/java/net/sf/jsqlparser/statement/delete/DeleteTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
1313
import static net.sf.jsqlparser.test.TestUtils.assertOracleHintExists;
1414
import static org.junit.Assert.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1517

1618
import java.io.StringReader;
1719

@@ -137,4 +139,45 @@ public void testUsing() throws JSQLParserException {
137139
String statement = "DELETE A USING B.C D WHERE D.Z = 1";
138140
assertSqlCanBeParsedAndDeparsed(statement);
139141
}
142+
143+
@Test
144+
public void testDeleteLowPriority() throws JSQLParserException {
145+
String stmt = "DELETE LOW_PRIORITY FROM tablename";
146+
Delete delete = (Delete) assertSqlCanBeParsedAndDeparsed(stmt);
147+
assertEquals(delete.getModifierPriority(), DeleteModifierPriority.LOW_PRIORITY);
148+
}
149+
150+
@Test
151+
public void testDeleteQuickModifier() throws JSQLParserException {
152+
String stmt = "DELETE QUICK FROM tablename";
153+
Delete delete = (Delete) assertSqlCanBeParsedAndDeparsed(stmt);
154+
assertTrue(delete.isModifierQuick());
155+
String stmt2 = "DELETE FROM tablename";
156+
Delete delete2 = (Delete) assertSqlCanBeParsedAndDeparsed(stmt2);
157+
assertFalse(delete2.isModifierQuick());
158+
}
159+
160+
@Test
161+
public void testDeleteIgnoreModifier() throws JSQLParserException {
162+
String stmt = "DELETE IGNORE FROM tablename";
163+
Delete delete = (Delete) assertSqlCanBeParsedAndDeparsed(stmt);
164+
assertTrue(delete.isModifierIgnore());
165+
String stmt2 = "DELETE FROM tablename";
166+
Delete delete2 = (Delete) assertSqlCanBeParsedAndDeparsed(stmt2);
167+
assertFalse(delete2.isModifierIgnore());
168+
}
169+
170+
@Test
171+
public void testDeleteMultipleModifiers() throws JSQLParserException {
172+
String stmt = "DELETE LOW_PRIORITY QUICK FROM tablename";
173+
Delete delete = (Delete) assertSqlCanBeParsedAndDeparsed(stmt);
174+
assertEquals(delete.getModifierPriority(), DeleteModifierPriority.LOW_PRIORITY);
175+
assertTrue(delete.isModifierQuick());
176+
177+
String stmt2 = "DELETE LOW_PRIORITY QUICK IGNORE FROM tablename";
178+
Delete delete2 = (Delete) assertSqlCanBeParsedAndDeparsed(stmt2);
179+
assertEquals(delete2.getModifierPriority(), DeleteModifierPriority.LOW_PRIORITY);
180+
assertTrue(delete2.isModifierIgnore());
181+
assertTrue(delete2.isModifierQuick());
182+
}
140183
}

src/test/java/net/sf/jsqlparser/statement/update/UpdateTest.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
1919
import net.sf.jsqlparser.parser.CCJSqlParserManager;
2020
import static net.sf.jsqlparser.test.TestUtils.*;
21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertTrue;
21+
import static org.junit.Assert.*;
22+
import static org.junit.Assert.assertFalse;
2323

2424
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
2525
import org.junit.Assert;
@@ -248,4 +248,29 @@ public void testUpdateSetsIssue1316() throws JSQLParserException {
248248
Assert.assertEquals(1, update.getUpdateSets().get(2).getColumns().size());
249249
Assert.assertEquals(1, update.getUpdateSets().get(2).getExpressions().size());
250250
}
251+
252+
@Test
253+
public void testUpdateLowPriority() throws JSQLParserException {
254+
String stmt = "UPDATE LOW_PRIORITY table1 A SET A.columna = 'XXX'";
255+
Update update = (Update)assertSqlCanBeParsedAndDeparsed(stmt);
256+
assertEquals(update.getModifierPriority(), UpdateModifierPriority.LOW_PRIORITY);
257+
}
258+
259+
@Test
260+
public void testUpdateIgnoreModifier() throws JSQLParserException {
261+
String stmt = "UPDATE IGNORE table1 A SET A.columna = 'XXX'";
262+
Update update = (Update)assertSqlCanBeParsedAndDeparsed(stmt);
263+
assertTrue(update.isModifierIgnore());
264+
String stmt2 = "UPDATE table1 A SET A.columna = 'XXX'";
265+
Update update2 = (Update)assertSqlCanBeParsedAndDeparsed(stmt2);
266+
assertFalse(update2.isModifierIgnore());
267+
}
268+
269+
@Test
270+
public void testUpdateMultipleModifiers() throws JSQLParserException {
271+
String stmt = "UPDATE LOW_PRIORITY IGNORE table1 A SET A.columna = 'XXX'";
272+
Update update = (Update)assertSqlCanBeParsedAndDeparsed(stmt);
273+
assertEquals(update.getModifierPriority(), UpdateModifierPriority.LOW_PRIORITY);
274+
assertTrue(update.isModifierIgnore());
275+
}
251276
}

0 commit comments

Comments
 (0)