-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_update.go
More file actions
63 lines (51 loc) · 1.08 KB
/
sql_update.go
File metadata and controls
63 lines (51 loc) · 1.08 KB
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
package StitchingSQLGo
/*
postgres https://www.postgresql.org/docs/current/sql-update.html
*/
type Update struct {
Table `validate:"required"`
Set map[Field]interface{} `validate:"required"`
Where
Returning
}
func (u Update) SQL() (string, []interface{}, error) {
if err := validate.Struct(u); err != nil {
return "", nil, err
}
s := SqlBuilder{}
// update
s.WriteString("update")
// table
if err := u.Table.Table(&s); err != nil {
return "", nil, err
}
// set
s.WriteString(" set")
// field1 = value1 , field2 = value2
i := 0
for f, v := range u.Set {
if err := f.Field(&s, false); err != nil {
return "", nil, err
}
s.WriteString(" =")
if err := s.push(v); err != nil {
return "", nil, err
}
if i == len(u.Set)-1 {
break
}
s.WriteByte(',')
i++
}
// where field1 = value1 X field2 = value2
if err := u.Where.where(&s); err != nil {
return "", nil, err
}
return s.String(), s.args, nil
}
func (u Update) Exec() (string, []interface{}, error) {
return u.SQL()
}
func (u Update) ExecWithReturning() Returning {
return u.Returning
}