@@ -3,29 +3,55 @@ package sqlbuilder
3
3
type UpdateColumns map [* BasicColumn ]AsExpr
4
4
5
5
type UpdateStatement struct {
6
- table * Table
7
- where AsExpr
8
- set UpdateColumns
6
+ with []AsCommonTableExpression
7
+ target AsTableOrSubquery
8
+ set UpdateColumns
9
+ from AsTableOrSubquery
10
+ where AsExpr
11
+ returning []AsExpr
9
12
}
10
13
11
14
func (s * UpdateStatement ) clone () * UpdateStatement {
12
15
return & UpdateStatement {
13
- table : s .table ,
14
- where : s .where ,
15
- set : s .set ,
16
+ with : s .with ,
17
+ target : s .target ,
18
+ set : s .set ,
19
+ from : s .from ,
20
+ where : s .where ,
21
+ returning : s .returning ,
16
22
}
17
23
}
18
24
19
25
func Update () * UpdateStatement {
20
26
return & UpdateStatement {set : make (UpdateColumns )}
21
27
}
22
28
29
+ func (s * UpdateStatement ) With (with ... AsCommonTableExpression ) * UpdateStatement {
30
+ c := s .clone ()
31
+ c .with = with
32
+ return c
33
+ }
34
+
35
+ func (s * UpdateStatement ) AndWith (with ... AsCommonTableExpression ) * UpdateStatement {
36
+ c := s .clone ()
37
+ c .with = append (c .with , with ... )
38
+ return c
39
+ }
40
+
23
41
func (s * UpdateStatement ) Table (table * Table ) * UpdateStatement {
42
+ return s .Target (table )
43
+ }
44
+
45
+ func (s * UpdateStatement ) Target (target AsTableOrSubquery ) * UpdateStatement {
24
46
c := s .clone ()
25
- c .table = table
47
+ c .target = target
26
48
return c
27
49
}
28
50
51
+ func (s * UpdateStatement ) GetTarget () AsTableOrSubquery {
52
+ return s .target
53
+ }
54
+
29
55
func (s * UpdateStatement ) Set (set UpdateColumns ) * UpdateStatement {
30
56
c := s .clone ()
31
57
c .set = set
@@ -47,22 +73,67 @@ func (s *UpdateStatement) AndSet(column *BasicColumn, expr AsExpr) *UpdateStatem
47
73
return c
48
74
}
49
75
76
+ func (s * UpdateStatement ) From (from AsTableOrSubquery ) * UpdateStatement {
77
+ c := s .clone ()
78
+ c .from = from
79
+ return c
80
+ }
81
+
50
82
func (s * UpdateStatement ) Where (where AsExpr ) * UpdateStatement {
51
83
c := s .clone ()
52
84
c .where = where
53
85
return c
54
86
}
55
87
88
+ func (s * UpdateStatement ) Returning (returning ... AsExpr ) * UpdateStatement {
89
+ c := s .clone ()
90
+ c .returning = returning
91
+ return c
92
+ }
93
+
56
94
func (q * UpdateStatement ) AsStatement (s * Serializer ) {
57
- s .D ("UPDATE " ).F (q .table .AsNamed ).D (" SET " )
95
+ if len (q .with ) > 0 {
96
+ s .D ("WITH " )
97
+
98
+ for _ , w := range q .with {
99
+ if w .IsRecursive () {
100
+ s .D ("RECURSIVE " )
101
+ break
102
+ }
103
+ }
104
+
105
+ for i , w := range q .with {
106
+ s .F (w .AsCommonTableExpression ).DC ("," , i != len (q .with )- 1 ).D (" " )
107
+ }
108
+ }
109
+
110
+ s .D ("UPDATE " ).F (q .target .AsTableOrSubquery ).D (" SET " )
58
111
59
112
i := 0
60
113
for k , e := range q .set {
61
114
s .F (k .AsNamedShort ).D (" = " ).F (e .AsExpr ).DC (", " , i < len (q .set )- 1 )
62
115
i ++
63
116
}
64
117
118
+ if q .from != nil {
119
+ s .D (" FROM " ).F (q .from .AsTableOrSubquery )
120
+ }
121
+
65
122
if q .where != nil {
66
123
s .D (" WHERE " ).F (q .where .AsExpr )
67
124
}
125
+
126
+ if len (q .returning ) > 0 {
127
+ s .D (" RETURNING " )
128
+
129
+ for i , c := range q .returning {
130
+ if a , ok := c .(AsResultColumn ); ok {
131
+ s .F (a .AsResultColumn )
132
+ } else {
133
+ s .F (c .AsExpr )
134
+ }
135
+
136
+ s .DC (", " , i < len (q .returning )- 1 )
137
+ }
138
+ }
68
139
}
0 commit comments