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