Skip to content

Commit 96cd483

Browse files
Support DELETE FROM T1 USING T2 WHERE ... (#1228)
Co-authored-by: Francois Secherre <[email protected]>
1 parent 27e6a9f commit 96cd483

File tree

6 files changed

+69
-1
lines changed

6 files changed

+69
-1
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Iterator;
1616
import java.util.List;
1717
import java.util.Optional;
18+
1819
import static java.util.stream.Collectors.joining;
1920
import net.sf.jsqlparser.expression.Expression;
2021
import net.sf.jsqlparser.expression.OracleHint;
@@ -33,6 +34,7 @@ public class Delete implements Statement {
3334
private Table table;
3435
private OracleHint oracleHint = null;
3536
private List<Table> tables;
37+
private List<Table> usingList;
3638
private List<Join> joins;
3739
private Expression where;
3840
private Limit limit;
@@ -116,6 +118,14 @@ public void setTables(List<Table> tables) {
116118
this.tables = tables;
117119
}
118120

121+
public List<Table> getUsingList() {
122+
return usingList;
123+
}
124+
125+
public void setUsingList(List<Table> usingList) {
126+
this.usingList = usingList;
127+
}
128+
119129
public List<Join> getJoins() {
120130
return joins;
121131
}
@@ -162,6 +172,13 @@ public String toString() {
162172
}
163173
b.append(" ").append(table);
164174

175+
if (usingList != null && usingList.size()>0) {
176+
b.append(" USING ");
177+
b.append(usingList.stream()
178+
.map(Table::toString)
179+
.collect(joining(", ")));
180+
}
181+
165182
if (joins != null) {
166183
for (Join join : joins) {
167184
if (join.isSimple()) {
@@ -191,6 +208,11 @@ public Delete withTables(List<Table> tables) {
191208
return this;
192209
}
193210

211+
public Delete withUsingList(List<Table> usingList) {
212+
this.setUsingList(usingList);
213+
return this;
214+
}
215+
194216
public Delete withJoins(List<Join> joins) {
195217
this.setJoins(joins);
196218
return this;
@@ -233,6 +255,18 @@ public Delete addTables(Collection<? extends Table> tables) {
233255
return this.withTables(collection);
234256
}
235257

258+
public Delete addUsingList(Table... usingList) {
259+
List<Table> collection = Optional.ofNullable(getUsingList()).orElseGet(ArrayList::new);
260+
Collections.addAll(collection, usingList);
261+
return this.withUsingList(collection);
262+
}
263+
264+
public Delete addUsingList(Collection<? extends Table> usingList) {
265+
List<Table> collection = Optional.ofNullable(getUsingList()).orElseGet(ArrayList::new);
266+
collection.addAll(usingList);
267+
return this.withUsingList(collection);
268+
}
269+
236270
public Delete addJoins(Join... joins) {
237271
List<Join> collection = Optional.ofNullable(getJoins()).orElseGet(ArrayList::new);
238272
Collections.addAll(collection, joins);

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,12 @@ public void visit(ValueListExpression valueList) {
647647
public void visit(Delete delete) {
648648
visit(delete.getTable());
649649

650+
if (delete.getUsingList() != null) {
651+
for (Table using : delete.getUsingList()) {
652+
visit(using);
653+
}
654+
}
655+
650656
if (delete.getJoins() != null) {
651657
for (Join join : delete.getJoins()) {
652658
join.getRightItem().accept(this);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public void deParse(Delete delete) {
5656
}
5757
buffer.append(" ").append(delete.getTable().toString());
5858

59+
if (delete.getUsingList() != null && !delete.getUsingList().isEmpty()) {
60+
buffer.append(" USING").append(
61+
delete.getUsingList().stream().map(Table::toString).collect(joining(", ", " ", "")));
62+
}
5963
if (delete.getJoins() != null) {
6064
for (Join join : delete.getJoins()) {
6165
if (join.isSimple()) {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,8 @@ Delete Delete( List<WithItem> with ):
12021202
Delete delete = new Delete();
12031203
Table table = null;
12041204
List<Table> tables = new ArrayList<Table>();
1205+
Table usingTable = null;
1206+
List<Table> usingList = new ArrayList<Table>();
12051207
List<Join> joins = null;
12061208
Expression where = null;
12071209
Limit limit = null;
@@ -1214,6 +1216,8 @@ Delete Delete( List<WithItem> with ):
12141216
<K_FROM> | <K_FROM>) { hasFrom = true; }]
12151217

12161218
[ LOOKAHEAD(3) table=TableWithAlias() joins=JoinsList() ]
1219+
[ <K_USING> usingTable=TableWithAlias() { usingList.add(usingTable); }
1220+
("," usingTable=TableWithAlias() { usingList.add(usingTable); } )*]
12171221
[where=WhereClause() { delete.setWhere(where); } ]
12181222
[orderByElements = OrderByElements() { delete.setOrderByElements(orderByElements); } ]
12191223
[limit=PlainLimit() {delete.setLimit(limit); } ]
@@ -1222,7 +1226,7 @@ Delete Delete( List<WithItem> with ):
12221226
delete.setJoins(joins);
12231227
}
12241228
return delete.withWithItemsList(with)
1225-
.withTables(tables).withTable(table).withHasFrom(hasFrom);
1229+
.withTables(tables).withTable(table).withHasFrom(hasFrom).withUsingList(usingList);
12261230
}
12271231
}
12281232

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,10 @@ public void testNoFromWithSchema() throws JSQLParserException {
131131
String statement = "DELETE A.B WHERE Z = 1";
132132
assertSqlCanBeParsedAndDeparsed(statement);
133133
}
134+
135+
@Test
136+
public void testUsing() throws JSQLParserException {
137+
String statement = "DELETE A USING B.C D WHERE D.Z = 1";
138+
assertSqlCanBeParsedAndDeparsed(statement);
139+
}
134140
}

src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.StringReader;
3838
import java.util.Iterator;
3939
import java.util.List;
40+
4041
import static org.assertj.core.api.Assertions.assertThat;
4142
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4243
import static org.junit.Assert.assertEquals;
@@ -664,4 +665,17 @@ public void testAtTimeZoneExpression() throws JSQLParserException {
664665
assertEquals(1, tableList.size());
665666
assertTrue(tableList.contains("mytbl"));
666667
}
668+
669+
670+
@Test
671+
public void testUsing() throws JSQLParserException {
672+
String sql = "DELETE A USING B.C D WHERE D.Z = 1";
673+
Statement stmt = CCJSqlParserUtil.parse(sql);
674+
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
675+
List<String> tableList = tablesNamesFinder.getTableList(stmt);
676+
assertEquals(2, tableList.size());
677+
assertTrue(tableList.contains("A"));
678+
assertTrue(tableList.contains("B.C"));
679+
}
680+
667681
}

0 commit comments

Comments
 (0)