2727import net .sf .jsqlparser .statement .select .Join ;
2828import net .sf .jsqlparser .statement .select .PlainSelect ;
2929import 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