Skip to content

Commit 3a7bf9d

Browse files
committed
Update UpdateDeParser.java
Extend the deparser to support DB2 Updates with Select clause
1 parent 232795f commit 3a7bf9d

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

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

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import net.sf.jsqlparser.statement.select.Join;
2828
import net.sf.jsqlparser.statement.select.PlainSelect;
2929
import net.sf.jsqlparser.statement.update.Update;
30+
import net.sf.jsqlparser.statement.select.SelectVisitor;
3031

3132
/**
3233
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a
@@ -36,16 +37,22 @@ public class UpdateDeParser {
3637

3738
private StringBuilder buffer;
3839
private ExpressionVisitor expressionVisitor;
40+
private SelectVisitor selectVisitor;
3941

4042
/**
4143
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse
4244
* expressions. It has to share the same<br>
4345
* StringBuilder (buffer parameter) as this object in order to work
46+
* @param selectVisitor a {@link SelectVisitor} to de-parse
47+
* {@link net.sf.jsqlparser.statement.select.Select}s. It has to share the
48+
* same<br>
49+
* StringBuilder (buffer parameter) as this object in order to work
4450
* @param buffer the buffer that will be filled with the select
4551
*/
46-
public UpdateDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer) {
52+
public UpdateDeParser(ExpressionVisitor expressionVisitor, SelectVisitor selectVisitor, StringBuilder buffer) {
4753
this.buffer = buffer;
4854
this.expressionVisitor = expressionVisitor;
55+
this.selectVisitor = selectVisitor;
4956
}
5057

5158
public StringBuilder getBuffer() {
@@ -58,17 +65,39 @@ public void setBuffer(StringBuilder buffer) {
5865

5966
public void deParse(Update update) {
6067
buffer.append("UPDATE ").append(PlainSelect.getStringList(update.getTables(), true, false)).append(" SET ");
61-
for (int i = 0; i < update.getColumns().size(); i++) {
62-
Column column = update.getColumns().get(i);
63-
buffer.append(column.getFullyQualifiedName()).append(" = ");
64-
65-
Expression expression = update.getExpressions().get(i);
66-
expression.accept(expressionVisitor);
67-
if (i < update.getColumns().size() - 1) {
68-
buffer.append(", ");
68+
69+
if (!update.isUseSelect()) {
70+
for (int i = 0; i < update.getColumns().size(); i++) {
71+
Column column = update.getColumns().get(i);
72+
buffer.append(column.getFullyQualifiedName()).append(" = ");
73+
74+
Expression expression = update.getExpressions().get(i);
75+
expression.accept(expressionVisitor);
76+
if (i < update.getColumns().size() - 1) {
77+
buffer.append(", ");
78+
}
79+
}
80+
} else {
81+
if (update.isUseColumnsBrackets()) {
82+
buffer.append("(");
6983
}
84+
for (int i = 0; i < update.getColumns().size(); i++) {
85+
if (i != 0) {
86+
buffer.append(", ");
87+
}
88+
Column column = update.getColumns().get(i);
89+
buffer.append(column.getFullyQualifiedName());
90+
}
91+
if (update.isUseColumnsBrackets()) {
92+
buffer.append(")");
93+
}
94+
buffer.append(" = ");
95+
buffer.append("(");
96+
Select select = update.getSelect();
97+
select.accept(selectVisitor);
98+
buffer.append(")");
7099
}
71-
100+
72101
if (update.getFromItem() != null) {
73102
buffer.append(" FROM ").append(update.getFromItem());
74103
if (update.getJoins() != null) {

0 commit comments

Comments
 (0)