27
27
import net .sf .jsqlparser .statement .select .Join ;
28
28
import net .sf .jsqlparser .statement .select .PlainSelect ;
29
29
import net .sf .jsqlparser .statement .update .Update ;
30
+ import net .sf .jsqlparser .statement .select .SelectVisitor ;
30
31
31
32
/**
32
33
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a
@@ -36,16 +37,22 @@ public class UpdateDeParser {
36
37
37
38
private StringBuilder buffer ;
38
39
private ExpressionVisitor expressionVisitor ;
40
+ private SelectVisitor selectVisitor ;
39
41
40
42
/**
41
43
* @param expressionVisitor a {@link ExpressionVisitor} to de-parse
42
44
* expressions. It has to share the same<br>
43
45
* 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
44
50
* @param buffer the buffer that will be filled with the select
45
51
*/
46
- public UpdateDeParser (ExpressionVisitor expressionVisitor , StringBuilder buffer ) {
52
+ public UpdateDeParser (ExpressionVisitor expressionVisitor , SelectVisitor selectVisitor , StringBuilder buffer ) {
47
53
this .buffer = buffer ;
48
54
this .expressionVisitor = expressionVisitor ;
55
+ this .selectVisitor = selectVisitor ;
49
56
}
50
57
51
58
public StringBuilder getBuffer () {
@@ -58,17 +65,39 @@ public void setBuffer(StringBuilder buffer) {
58
65
59
66
public void deParse (Update update ) {
60
67
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 ("(" );
69
83
}
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 (")" );
70
99
}
71
-
100
+
72
101
if (update .getFromItem () != null ) {
73
102
buffer .append (" FROM " ).append (update .getFromItem ());
74
103
if (update .getJoins () != null ) {
0 commit comments