Skip to content

#1527 DELETE ... RETURNING ... #1528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
95 changes: 95 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/OutputClause.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.expression.UserVariable;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.SelectItem;

import java.util.List;
import java.util.Objects;

/**
* T-SQL Output Clause
*
* @see <a href="https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql?view=sql-server-ver15">OUTPUT Clause (Transact-SQL)</a>
*
* <pre>
* &lt;OUTPUT_CLAUSE&gt; ::=
* {
* [ OUTPUT &lt;dml_select_list&gt; INTO { @table_variable | output_table } [ ( column_list ) ] ]
* [ OUTPUT &lt;dml_select_list&gt; ]
* }
* &lt;dml_select_list&gt; ::=
* { &lt;column_name&gt; | scalar_expression } [ [AS] column_alias_identifier ]
* [ ,...n ]
*
* &lt;column_name&gt; ::=
* { DELETED | INSERTED | from_table_name } . { * | column_name }
* | $action
* </pre>
*/
public class OutputClause {
List<SelectItem> selectItemList;
UserVariable tableVariable;
Table outputTable;
List<String> columnList;

public OutputClause(List<SelectItem> selectItemList, UserVariable tableVariable, Table outputTable, List<String> columnList) {
this.selectItemList = Objects.requireNonNull(selectItemList, "The Select List of the Output Clause must not be null.");
this.tableVariable = tableVariable;
this.outputTable = outputTable;
this.columnList = columnList;
}

public List<SelectItem> getSelectItemList() {
return selectItemList;
}

public void setSelectItemList(List<SelectItem> selectItemList) {
this.selectItemList = selectItemList;
}

public UserVariable getTableVariable() {
return tableVariable;
}

public void setTableVariable(UserVariable tableVariable) {
this.tableVariable = tableVariable;
}

public Table getOutputTable() {
return outputTable;
}

public void setOutputTable(Table outputTable) {
this.outputTable = outputTable;
}

public List<String> getColumnList() {
return columnList;
}

public void setColumnList(List<String> columnList) {
this.columnList = columnList;
}

public StringBuilder appendTo(StringBuilder builder) {
builder.append(" OUTPUT ");
PlainSelect.appendStringListTo(builder, selectItemList, true, false);

if (tableVariable != null) {
builder.append(" INTO ").append(tableVariable);
} else if (outputTable != null) {
builder.append(" INTO ").append(outputTable);
}

PlainSelect.appendStringListTo(builder, columnList, true, false);

return builder.append(" ");
}

@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
}
36 changes: 36 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 @@ -20,12 +20,14 @@
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.OracleHint;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.OutputClause;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
import net.sf.jsqlparser.statement.select.Join;
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 @@ -43,6 +45,29 @@ public class Delete implements Statement {
private DeleteModifierPriority modifierPriority;
private boolean modifierIgnore;
private boolean modifierQuick;
private List<SelectItem> returningExpressionList = null;
private OutputClause outputClause;

public OutputClause getOutputClause() {
return outputClause;
}

public void setOutputClause(OutputClause outputClause) {
this.outputClause = outputClause;
}

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 @@ -181,6 +206,11 @@ public String toString() {
.collect(joining(", ")));
}

if (outputClause!=null) {
outputClause.appendTo(b);
}


if (hasFrom) {
b.append(" FROM");
}
Expand Down Expand Up @@ -214,6 +244,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
52 changes: 25 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 @@ -20,11 +20,12 @@
import net.sf.jsqlparser.expression.operators.relational.ItemsList;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.OutputClause;
import net.sf.jsqlparser.statement.Statement;
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,15 +44,23 @@ 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;
private List<Expression> setExpressionList;
private List<WithItem> withItemsList;

private OutputClause outputClause;

public OutputClause getOutputClause() {
return outputClause;
}

public void setOutputClause(OutputClause outputClause) {
this.outputClause = outputClause;
}

@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
Expand Down Expand Up @@ -102,19 +111,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 @@ -234,6 +235,10 @@ public String toString() {
sql.append(PlainSelect.getStringList(columns, true, true)).append(" ");
}

if (outputClause!=null) {
outputClause.appendTo(sql);
}

if (useValues) {
sql.append("VALUES ");
}
Expand Down Expand Up @@ -274,9 +279,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 +332,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 +408,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
36 changes: 23 additions & 13 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,28 +532,38 @@ public static String getStringList(List<?> list) {
* @return comma separated list of the elements in the list
*/
public static String getStringList(List<?> list, boolean useComma, boolean useBrackets) {
StringBuilder ans = new StringBuilder();
String comma = ",";
if (!useComma) {
comma = "";
}
return appendStringListTo(new StringBuilder(), list, useComma, useBrackets).toString();
}

/**
* Append the toString out put of the objects in the List (that can be comma
* separated). If the List is null or empty an empty string is returned.
*
* @param list list of objects with toString methods
* @param useComma true if the list has to be comma separated
* @param useBrackets true if the list has to be enclosed in brackets
* @return comma separated list of the elements in the list
*/
public static StringBuilder appendStringListTo(StringBuilder builder, List<?> list, boolean useComma, boolean useBrackets) {
if (list != null) {
String comma = useComma ? "," : "";

if (useBrackets) {
ans.append("(");
builder.append("(");
}

for (int i = 0; i < list.size(); i++) {
ans.append(list.get(i)).append( i < list.size() - 1
? comma + " "
: "" );
int size = list.size();
for (int i = 0; i < size; i++) {
builder.append(list.get(i)).append( i < size - 1
? comma + " "
: "" );
}

if (useBrackets) {
ans.append(")");
builder.append(")");
}
}

return ans.toString();
return builder;
}

public PlainSelect withMySqlSqlCalcFoundRows(boolean mySqlCalcFoundRows) {
Expand Down
Loading