Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 105 additions & 6 deletions src/main/java/net/sf/jsqlparser/statement/delete/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.sf.jsqlparser.statement.ReturningClause;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.OrderByElement;
Expand All @@ -29,6 +30,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.joining;

Expand All @@ -38,7 +40,7 @@ public class Delete implements Statement {
private Table table;
private OracleHint oracleHint = null;
private List<Table> tables;
private List<Table> usingList;
private List<FromItem> usingFromItemList;
private List<Join> joins;
private Expression where;
private PreferringClause preferringClause;
Expand Down Expand Up @@ -157,12 +159,29 @@ public void setTables(List<Table> tables) {
this.tables = tables;
}

/**
* This is compatible with the old logic. When calling this method, you need to ensure that the
* specific table is used after using.
*
* @return Table collection used in using.
*/
@Deprecated
public List<Table> getUsingList() {
return usingList;
if (usingFromItemList == null || usingFromItemList.isEmpty()) {
return new ArrayList<>();
}
return usingFromItemList.stream().map(ele -> (Table) ele).collect(Collectors.toList());
}

/**
* This is compatible with the old logic. When calling this method, you need to ensure that the
* specific table is used after using.
*
* @param usingList Table collection used in using.
*/
@Deprecated
public void setUsingList(List<Table> usingList) {
this.usingList = usingList;
this.usingFromItemList = new ArrayList<>(usingList);
}

public List<Join> getJoins() {
Expand Down Expand Up @@ -228,10 +247,10 @@ public String toString() {
}
b.append(" ").append(table);

if (usingList != null && usingList.size() > 0) {
if (usingFromItemList != null && !usingFromItemList.isEmpty()) {
b.append(" USING ");
b.append(usingList.stream()
.map(Table::toString)
b.append(usingFromItemList.stream()
.map(Object::toString)
.collect(joining(", ")));
}

Expand Down Expand Up @@ -273,11 +292,30 @@ public Delete withTables(List<Table> tables) {
return this;
}

/**
* The old method has been replaced by withUsingFromItemList.
*
* @param usingList
* @return
* @see Delete#withUsingFromItemList
*/
@Deprecated
public Delete withUsingList(List<Table> usingList) {
this.setUsingList(usingList);
return this;
}

/**
* New using syntax method.Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList
* @return
*/
public Delete withUsingFromItemList(List<FromItem> usingFromItemList) {
this.setUsingFromItemList(usingFromItemList);
return this;
}

public Delete withJoins(List<Join> joins) {
this.setJoins(joins);
return this;
Expand Down Expand Up @@ -364,18 +402,60 @@ public Delete addTables(Collection<? extends Table> tables) {
return this.withTables(collection);
}

/**
* The old method has been replaced by addUsingFromItemList.
*
* @param usingList
* @return
* @see Delete#addUsingFromItemList
*/
@Deprecated
public Delete addUsingList(Table... usingList) {
List<Table> collection = Optional.ofNullable(getUsingList()).orElseGet(ArrayList::new);
Collections.addAll(collection, usingList);
return this.withUsingList(collection);
}

/**
* New using syntax method.Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList
* @return
*/
public Delete addUsingFromItemList(FromItem... usingFromItemList) {
List<FromItem> collection =
Optional.ofNullable(getUsingFromItemList()).orElseGet(ArrayList::new);
Collections.addAll(collection, usingFromItemList);
return this.withUsingFromItemList(collection);
}

/**
* The old method has been replaced by addUsingFromItemList.
*
* @param usingList
* @return
* @see Delete#addUsingFromItemList
*/
@Deprecated
public Delete addUsingList(Collection<? extends Table> usingList) {
List<Table> collection = Optional.ofNullable(getUsingList()).orElseGet(ArrayList::new);
collection.addAll(usingList);
return this.withUsingList(collection);
}

/**
* New using syntax method. Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList
* @return
*/
public Delete addUsingFromItemList(Collection<? extends Table> usingFromItemList) {
List<FromItem> collection =
Optional.ofNullable(getUsingFromItemList()).orElseGet(ArrayList::new);
collection.addAll(usingFromItemList);
return this.withUsingFromItemList(collection);
}

public Delete addJoins(Join... joins) {
List<Join> collection = Optional.ofNullable(getJoins()).orElseGet(ArrayList::new);
Collections.addAll(collection, joins);
Expand Down Expand Up @@ -405,4 +485,23 @@ public Delete addOrderByElements(Collection<? extends OrderByElement> orderByEle
public <E extends Expression> E getWhere(Class<E> type) {
return type.cast(getWhere());
}

/**
* Return the content after using. Supports the complete using syntax of pg, such as subqueries,
* etc.
*
* @return
*/
public List<FromItem> getUsingFromItemList() {
return usingFromItemList;
}

/**
* Supports the complete using syntax of pg, such as subqueries, etc.
*
* @param usingFromItemList The content after using.
*/
public void setUsingFromItemList(List<FromItem> usingFromItemList) {
this.usingFromItemList = usingFromItemList;
}
}
7 changes: 4 additions & 3 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.AllTableColumns;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
import net.sf.jsqlparser.statement.select.FunctionAllColumns;
import net.sf.jsqlparser.statement.select.Join;
Expand Down Expand Up @@ -1014,9 +1015,9 @@ public <S> Void visit(MySQLGroupConcat groupConcat, S context) {
public <S> Void visit(Delete delete, S context) {
visit(delete.getTable(), context);

if (delete.getUsingList() != null) {
for (Table using : delete.getUsingList()) {
visit(using, context);
if (delete.getUsingFromItemList() != null) {
for (FromItem usingFromItem : delete.getUsingFromItemList()) {
usingFromItem.accept(this, context);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void deParse(Delete delete) {
}
builder.append(" ").append(delete.getTable().toString());

if (delete.getUsingList() != null && !delete.getUsingList().isEmpty()) {
if (delete.getUsingFromItemList() != null && !delete.getUsingFromItemList().isEmpty()) {
builder.append(" USING").append(
delete.getUsingList().stream().map(Table::toString)
delete.getUsingFromItemList().stream().map(Object::toString)
.collect(joining(", ", " ", "")));
}
if (delete.getJoins() != null) {
Expand Down
10 changes: 5 additions & 5 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -3012,8 +3012,8 @@ Delete Delete():
Table table = null;
List<Table> tables = new ArrayList<Table>();
List<WithItem<?>> with = null;
Table usingTable = null;
List<Table> usingList = new ArrayList<Table>();
FromItem usingFromItem = null;
List<FromItem> usingFromItemList = new ArrayList<FromItem>();
List<Join> joins = null;
Expression where = null;
PreferringClause preferringClause = null;
Expand All @@ -3039,8 +3039,8 @@ Delete Delete():
<K_FROM> | <K_FROM>) { hasFrom = true; }]

[ LOOKAHEAD(3) table=TableWithAlias() [ LOOKAHEAD(2) joins=JoinsList() ] ]
[ <K_USING> usingTable=TableWithAlias() { usingList.add(usingTable); }
("," usingTable=TableWithAlias() { usingList.add(usingTable); } )*]
[ <K_USING> usingFromItem=FromItem() { usingFromItemList.add(usingFromItem); }
("," usingFromItem=FromItem() { usingFromItemList.add(usingFromItem); } )*]
[where=WhereClause() { delete.setWhere(where); } ]
[preferringClause=PreferringClause() { delete.setPreferringClause(preferringClause);} ]
[orderByElements = OrderByElements() { delete.setOrderByElements(orderByElements); } ]
Expand All @@ -3055,7 +3055,7 @@ Delete Delete():
.withTables(tables)
.withTable(table)
.withHasFrom(hasFrom)
.withUsingList(usingList)
.withUsingFromItemList(usingFromItemList)
.withModifierPriority(modifierPriority)
.withModifierIgnore(modifierIgnore)
.withModifierQuick(modifierQuick);
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/delete/DeleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.WithItem;
Expand Down Expand Up @@ -397,4 +399,19 @@ public void testDeleteWithSkylineKeywords() throws JSQLParserException {
delete.getWhere().toString());
}

@Test
public void testDeleteUsingFromItem() throws JSQLParserException {
String statement =
"DELETE A USING B.C D,(SELECT id FROM producers WHERE active = false) p WHERE D.Z = 1 and p.id = D.id";
Delete delete = (Delete) assertSqlCanBeParsedAndDeparsed(statement);
assertEquals("B.C",
((Table) delete.getUsingFromItemList().get(0)).getFullyQualifiedName());
assertEquals("D",
((Table) delete.getUsingFromItemList().get(0)).getAlias().getName());
assertEquals("producers",
((Table) ((ParenthesedSelect) delete.getUsingFromItemList().get(1)).getPlainSelect()
.getFromItem()).getFullyQualifiedName());
assertEquals("p",
delete.getUsingFromItemList().get(1).getAlias().getName());
}
}
Loading