Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
#1527 DELETE ... RETURNING ...
Fixes #1527
Add DELETE... RETURNING ... expression
Simplify INSERT ... RETURNING ... expression
Simply UPDATE ... RETURNING ... expression
  • Loading branch information
manticore-projects committed May 6, 2022
commit 32eba58dcf77c52ec66c185d654909c518be984f
22 changes: 22 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/delete/Delete.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.sf.jsqlparser.statement.select.Limit;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.WithItem;

public class Delete implements Statement {
Expand All @@ -44,6 +45,21 @@ public class Delete implements Statement {
private boolean modifierIgnore;
private boolean modifierQuick;

private List<SelectItem> returningExpressionList = null;

public List<SelectItem> getReturningExpressionList() {
return returningExpressionList;
}

public void setReturningExpressionList(List<SelectItem> returningExpressionList) {
this.returningExpressionList = returningExpressionList;
}

public Delete withReturningExpressionList(List<SelectItem> returningExpressionList) {
this.returningExpressionList = returningExpressionList;
return this;
}

public List<WithItem> getWithItemsList() {
return withItemsList;
}
Expand Down Expand Up @@ -214,6 +230,12 @@ public String toString() {
if (limit != null) {
b.append(limit);
}

if (getReturningExpressionList() != null) {
b.append(" RETURNING ").append(PlainSelect.
getStringList(getReturningExpressionList(), true, false));
}

return b.toString();
}

Expand Down
37 changes: 10 additions & 27 deletions src/main/java/net/sf/jsqlparser/statement/insert/Insert.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.WithItem;

@SuppressWarnings({"PMD.CyclomaticComplexity"})
Expand All @@ -43,9 +43,7 @@ public class Insert implements Statement {
private InsertModifierPriority modifierPriority = null;
private boolean modifierIgnore = false;

private boolean returningAllColumns = false;

private List<SelectExpressionItem> returningExpressionList = null;
private List<SelectItem> returningExpressionList = null;

private boolean useSet = false;
private List<Column> setColumns;
Expand Down Expand Up @@ -102,19 +100,11 @@ public void setUseValues(boolean useValues) {
this.useValues = useValues;
}

public boolean isReturningAllColumns() {
return returningAllColumns;
}

public void setReturningAllColumns(boolean returningAllColumns) {
this.returningAllColumns = returningAllColumns;
}

public List<SelectExpressionItem> getReturningExpressionList() {
public List<SelectItem> getReturningExpressionList() {
return returningExpressionList;
}

public void setReturningExpressionList(List<SelectExpressionItem> returningExpressionList) {
public void setReturningExpressionList(List<SelectItem> returningExpressionList) {
this.returningExpressionList = returningExpressionList;
}

Expand Down Expand Up @@ -274,9 +264,7 @@ public String toString() {
}
}

if (isReturningAllColumns()) {
sql.append(" RETURNING *");
} else if (getReturningExpressionList() != null) {
if (getReturningExpressionList() != null) {
sql.append(" RETURNING ").append(PlainSelect.
getStringList(getReturningExpressionList(), true, false));
}
Expand Down Expand Up @@ -329,12 +317,7 @@ public Insert withModifierIgnore(boolean modifierIgnore) {
return this;
}

public Insert withReturningAllColumns(boolean returningAllColumns) {
this.setReturningAllColumns(returningAllColumns);
return this;
}

public Insert withReturningExpressionList(List<SelectExpressionItem> returningExpressionList) {
public Insert withReturningExpressionList(List<SelectItem> returningExpressionList) {
this.setReturningExpressionList(returningExpressionList);
return this;
}
Expand Down Expand Up @@ -410,14 +393,14 @@ public Insert addDuplicateUpdateExpressionList(Collection<? extends Expression>
return this.withDuplicateUpdateExpressionList(collection);
}

public Insert addReturningExpressionList(SelectExpressionItem... returningExpressionList) {
List<SelectExpressionItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
public Insert addReturningExpressionList(SelectItem... returningExpressionList) {
List<SelectItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
Collections.addAll(collection, returningExpressionList);
return this.withReturningExpressionList(collection);
}

public Insert addReturningExpressionList(Collection<? extends SelectExpressionItem> returningExpressionList) {
List<SelectExpressionItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
public Insert addReturningExpressionList(Collection<? extends SelectItem> returningExpressionList) {
List<SelectItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
collection.addAll(returningExpressionList);
return this.withReturningExpressionList(collection);
}
Expand Down
32 changes: 9 additions & 23 deletions src/main/java/net/sf/jsqlparser/statement/update/Update.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public class Update implements Statement {
private OracleHint oracleHint = null;
private List<OrderByElement> orderByElements;
private Limit limit;
private boolean returningAllColumns = false;
private List<SelectExpressionItem> returningExpressionList = null;
private List<SelectItem> returningExpressionList = null;
private UpdateModifierPriority modifierPriority;
private boolean modifierIgnore;

Expand Down Expand Up @@ -219,19 +218,12 @@ public Limit getLimit() {
return limit;
}

public boolean isReturningAllColumns() {
return returningAllColumns;
}

public void setReturningAllColumns(boolean returningAllColumns) {
this.returningAllColumns = returningAllColumns;
}

public List<SelectExpressionItem> getReturningExpressionList() {
public List<SelectItem> getReturningExpressionList() {
return returningExpressionList;
}

public void setReturningExpressionList(List<SelectExpressionItem> returningExpressionList) {
public void setReturningExpressionList(List<SelectItem> returningExpressionList) {
this.returningExpressionList = returningExpressionList;
}

Expand Down Expand Up @@ -350,9 +342,7 @@ public String toString() {
b.append(limit);
}

if (isReturningAllColumns()) {
b.append(" RETURNING *");
} else if (getReturningExpressionList() != null) {
if (getReturningExpressionList() != null) {
b.append(" RETURNING ").append(PlainSelect.
getStringList(getReturningExpressionList(), true, false));
}
Expand Down Expand Up @@ -405,12 +395,8 @@ public Update withLimit(Limit limit) {
return this;
}

public Update withReturningAllColumns(boolean returningAllColumns) {
this.setReturningAllColumns(returningAllColumns);
return this;
}

public Update withReturningExpressionList(List<SelectExpressionItem> returningExpressionList) {
public Update withReturningExpressionList(List<SelectItem> returningExpressionList) {
this.setReturningExpressionList(returningExpressionList);
return this;
}
Expand Down Expand Up @@ -500,14 +486,14 @@ public Update addOrderByElements(Collection<? extends OrderByElement> orderByEle
return this.withOrderByElements(collection);
}

public Update addReturningExpressionList(SelectExpressionItem... returningExpressionList) {
List<SelectExpressionItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
public Update addReturningExpressionList(SelectItem... returningExpressionList) {
List<SelectItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
Collections.addAll(collection, returningExpressionList);
return this.withReturningExpressionList(collection);
}

public Update addReturningExpressionList(Collection<? extends SelectExpressionItem> returningExpressionList) {
List<SelectExpressionItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
public Update addReturningExpressionList(Collection<? extends SelectItem> returningExpressionList) {
List<SelectItem> collection = Optional.ofNullable(getReturningExpressionList()).orElseGet(ArrayList::new);
collection.addAll(returningExpressionList);
return this.withReturningExpressionList(collection);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.WithItem;

public class DeleteDeParser extends AbstractDeParser<Delete> {
Expand Down Expand Up @@ -91,6 +92,11 @@ public void deParse(Delete delete) {
new LimitDeparser(buffer).deParse(delete.getLimit());
}

if (delete.getReturningExpressionList() != null) {
buffer.append(" RETURNING ").append(PlainSelect.
getStringList(delete.getReturningExpressionList(), true, false));
}

}

public ExpressionVisitor getExpressionVisitor() {
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/net/sf/jsqlparser/util/deparser/InsertDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import net.sf.jsqlparser.expression.operators.relational.NamedExpressionList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectVisitor;
import net.sf.jsqlparser.statement.select.SubSelect;
import net.sf.jsqlparser.statement.select.WithItem;
Expand Down Expand Up @@ -129,17 +129,9 @@ public void deParse(Insert insert) {
}
}

if (insert.isReturningAllColumns()) {
buffer.append(" RETURNING *");
} else if (insert.getReturningExpressionList() != null) {
buffer.append(" RETURNING ");
for (Iterator<SelectExpressionItem> iter = insert.getReturningExpressionList().iterator(); iter
.hasNext();) {
buffer.append(iter.next().toString());
if (iter.hasNext()) {
buffer.append(", ");
}
}
if (insert.getReturningExpressionList() != null) {
buffer.append(" RETURNING ").append(PlainSelect.
getStringList(insert.getReturningExpressionList(), true, false));
}
}

Expand Down
16 changes: 4 additions & 12 deletions src/main/java/net/sf/jsqlparser/util/deparser/UpdateDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.OrderByVisitor;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.update.UpdateSet;
Expand Down Expand Up @@ -127,17 +127,9 @@ public void deParse(Update update) {
new LimitDeparser(buffer).deParse(update.getLimit());
}

if (update.isReturningAllColumns()) {
buffer.append(" RETURNING *");
} else if (update.getReturningExpressionList() != null) {
buffer.append(" RETURNING ");
for (Iterator<SelectExpressionItem> iter = update.getReturningExpressionList().iterator(); iter
.hasNext();) {
buffer.append(iter.next().toString());
if (iter.hasNext()) {
buffer.append(", ");
}
}
if (update.getReturningExpressionList() != null) {
buffer.append(" RETURNING ").append(PlainSelect.
getStringList(update.getReturningExpressionList(), true, false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void validate(Delete delete) {
validateOptionalFeature(c, delete.getJoins(), Feature.deleteJoin);
validateOptionalFeature(c, delete.getLimit(), Feature.deleteLimit);
validateOptionalFeature(c, delete.getOrderByElements(), Feature.deleteOrderBy);
validateOptionalFeature(c, delete.getReturningExpressionList(), Feature.insertReturningExpressionList);
}

SelectValidator v = getValidator(SelectValidator.class);
Expand All @@ -46,6 +47,10 @@ public void validate(Delete delete) {
getValidator(LimitValidator.class).validate(delete.getLimit());
}

if (isNotEmpty(delete.getReturningExpressionList())) {
delete.getReturningExpressionList().forEach(c -> c .accept(v));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void validate(Insert insert) {
validateOptionalFeature(c, insert.getSelect(), Feature.insertFromSelect);
validateFeature(c, insert.isUseSet(), Feature.insertUseSet);
validateFeature(c, insert.isUseDuplicate(), Feature.insertUseDuplicateKeyUpdate);
validateFeature(c, insert.isReturningAllColumns(), Feature.insertReturningAll);
validateOptionalFeature(c, insert.getReturningExpressionList(), Feature.insertReturningExpressionList);
}

Expand Down Expand Up @@ -60,8 +59,8 @@ public void validate(Insert insert) {
}

if (isNotEmpty(insert.getReturningExpressionList())) {
ExpressionValidator v = getValidator(ExpressionValidator.class);
insert.getReturningExpressionList().forEach(c -> c.getExpression().accept(v));
SelectValidator v = getValidator(SelectValidator.class);
insert.getReturningExpressionList().forEach(c -> c .accept(v));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
*/
package net.sf.jsqlparser.util.validation.validator;

import java.util.stream.Collectors;

import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.statement.select.SelectExpressionItem;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.util.validation.ValidationCapability;

Expand All @@ -31,9 +28,7 @@ public void validate(Update update) {
validateFeature(c, update.isUseSelect(), Feature.updateUseSelect);
validateOptionalFeature(c, update.getOrderByElements(), Feature.updateOrderBy);
validateOptionalFeature(c, update.getLimit(), Feature.updateLimit);
if (isNotEmpty(update.getReturningExpressionList()) || update.isReturningAllColumns()) {
validateFeature(c, Feature.updateReturning);
}
validateOptionalFeature(c, update.getReturningExpressionList(), Feature.updateReturning);
}

validateOptionalFromItem(update.getTable());
Expand Down Expand Up @@ -62,9 +57,9 @@ public void validate(Update update) {
getValidator(LimitValidator.class).validate(update.getLimit());
}

if (update.getReturningExpressionList() != null) {
validateOptionalExpressions(update.getReturningExpressionList().stream()
.map(SelectExpressionItem::getExpression).collect(Collectors.toList()));
if (isNotEmpty(update.getReturningExpressionList())) {
SelectValidator v = getValidator(SelectValidator.class);
update.getReturningExpressionList().forEach(c -> c.accept(v));
}
}

Expand Down
Loading