Skip to content

Commit 17a1467

Browse files
committed
Merge pull request #79 from CeeKayGit/patch-1
Update with Select - add test SQL and extend deparser
2 parents 79ce9f2 + f2fecb7 commit 17a1467

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

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

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
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.Select;
31+
import net.sf.jsqlparser.statement.select.SelectVisitor;
3032

3133
/**
3234
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a
@@ -36,16 +38,22 @@ public class UpdateDeParser {
3638

3739
private StringBuilder buffer;
3840
private ExpressionVisitor expressionVisitor;
41+
private SelectVisitor selectVisitor;
3942

4043
/**
4144
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse
4245
* expressions. It has to share the same<br>
4346
* StringBuilder (buffer parameter) as this object in order to work
47+
* @param selectVisitor a {@link SelectVisitor} to de-parse
48+
* {@link net.sf.jsqlparser.statement.select.Select}s. It has to share the
49+
* same<br>
50+
* StringBuilder (buffer parameter) as this object in order to work
4451
* @param buffer the buffer that will be filled with the select
4552
*/
46-
public UpdateDeParser(ExpressionVisitor expressionVisitor, StringBuilder buffer) {
53+
public UpdateDeParser(ExpressionVisitor expressionVisitor, SelectVisitor selectVisitor, StringBuilder buffer) {
4754
this.buffer = buffer;
4855
this.expressionVisitor = expressionVisitor;
56+
this.selectVisitor = selectVisitor;
4957
}
5058

5159
public StringBuilder getBuffer() {
@@ -58,17 +66,39 @@ public void setBuffer(StringBuilder buffer) {
5866

5967
public void deParse(Update update) {
6068
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(", ");
69+
70+
if (!update.isUseSelect()) {
71+
for (int i = 0; i < update.getColumns().size(); i++) {
72+
Column column = update.getColumns().get(i);
73+
buffer.append(column.getFullyQualifiedName()).append(" = ");
74+
75+
Expression expression = update.getExpressions().get(i);
76+
expression.accept(expressionVisitor);
77+
if (i < update.getColumns().size() - 1) {
78+
buffer.append(", ");
79+
}
80+
}
81+
} else {
82+
if (update.isUseColumnsBrackets()) {
83+
buffer.append("(");
6984
}
85+
for (int i = 0; i < update.getColumns().size(); i++) {
86+
if (i != 0) {
87+
buffer.append(", ");
88+
}
89+
Column column = update.getColumns().get(i);
90+
buffer.append(column.getFullyQualifiedName());
91+
}
92+
if (update.isUseColumnsBrackets()) {
93+
buffer.append(")");
94+
}
95+
buffer.append(" = ");
96+
buffer.append("(");
97+
Select select = update.getSelect();
98+
select.getSelectBody().accept(selectVisitor);
99+
buffer.append(")");
70100
}
71-
101+
72102
if (update.getFromItem() != null) {
73103
buffer.append(" FROM ").append(update.getFromItem());
74104
if (update.getJoins() != null) {

0 commit comments

Comments
 (0)