-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
166 lines (134 loc) · 3.17 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package sqlbuilder
type UpdateColumns map[*BasicColumn]AsExpr
func (m UpdateColumns) asFields() []UpdateField {
var a []UpdateField
for k, e := range m {
a = append(a, UpdateField{k, e})
}
return a
}
type UpdateField struct {
Name AsNamedShort
Value AsExpr
}
type UpdateStatement struct {
with []AsCommonTableExpression
target AsTableOrSubquery
columns UpdateColumns
fields []UpdateField
from AsTableOrSubquery
where AsExpr
returning []AsExpr
}
func (s *UpdateStatement) clone() *UpdateStatement {
return &UpdateStatement{
with: s.with,
target: s.target,
columns: s.columns,
fields: s.fields,
from: s.from,
where: s.where,
returning: s.returning,
}
}
func Update() *UpdateStatement {
return &UpdateStatement{columns: make(UpdateColumns)}
}
func (s *UpdateStatement) With(with ...AsCommonTableExpression) *UpdateStatement {
c := s.clone()
c.with = with
return c
}
func (s *UpdateStatement) AndWith(with ...AsCommonTableExpression) *UpdateStatement {
c := s.clone()
c.with = append(c.with, with...)
return c
}
func (s *UpdateStatement) Table(table *Table) *UpdateStatement {
return s.Target(table)
}
func (s *UpdateStatement) Target(target AsTableOrSubquery) *UpdateStatement {
c := s.clone()
c.target = target
return c
}
func (s *UpdateStatement) GetTarget() AsTableOrSubquery {
return s.target
}
func (s *UpdateStatement) Set(columns UpdateColumns) *UpdateStatement {
c := s.clone()
c.columns = columns
c.fields = nil
return c
}
func (s *UpdateStatement) AndSet(column *BasicColumn, expr AsExpr) *UpdateStatement {
c := s.clone()
m := make(UpdateColumns)
for k, e := range c.columns {
m[k] = e
}
m[column] = expr
c.columns = m
return c
}
func (s *UpdateStatement) Fields(fields []UpdateField) *UpdateStatement {
c := s.clone()
c.columns = nil
c.fields = fields
return c
}
func (s *UpdateStatement) AndField(field UpdateField) *UpdateStatement {
c := s.clone()
c.fields = append(c.fields[:], field)
return c
}
func (s *UpdateStatement) From(from AsTableOrSubquery) *UpdateStatement {
c := s.clone()
c.from = from
return c
}
func (s *UpdateStatement) Where(where AsExpr) *UpdateStatement {
c := s.clone()
c.where = where
return c
}
func (s *UpdateStatement) Returning(returning ...AsExpr) *UpdateStatement {
c := s.clone()
c.returning = returning
return c
}
func (q *UpdateStatement) AsStatement(s *Serializer) {
if len(q.with) > 0 {
s.D("WITH ")
for _, w := range q.with {
if w.IsRecursive() {
s.D("RECURSIVE ")
break
}
}
for i, w := range q.with {
s.F(w.AsCommonTableExpression).DC(",", i != len(q.with)-1).D(" ")
}
}
s.D("UPDATE ").F(q.target.AsTableOrSubquery).D(" SET ")
for i, f := range append(q.columns.asFields(), q.fields...) {
s.DC(", ", i != 0).F(f.Name.AsNamedShort).D(" = ").F(f.Value.AsExpr)
}
if q.from != nil {
s.D(" FROM ").F(q.from.AsTableOrSubquery)
}
if q.where != nil {
s.D(" WHERE ").F(q.where.AsExpr)
}
if len(q.returning) > 0 {
s.D(" RETURNING ")
for i, c := range q.returning {
if a, ok := c.(AsResultColumn); ok {
s.F(a.AsResultColumn)
} else {
s.F(c.AsExpr)
}
s.DC(", ", i < len(q.returning)-1)
}
}
}