-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
54 lines (46 loc) · 1.12 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
package sqlbuilder
// UpdateBuilder is a builder for update queries
type UpdateBuilder struct {
*Builder
table string
columns map[string]interface{}
where BuildFunc
}
// Update creates a builder for update queries
func Update(table string) *UpdateBuilder {
b := &UpdateBuilder{
table: table,
columns: map[string]interface{}{},
Builder: NewBuilder(),
}
return b
}
// Set sets the column to the provided value
func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder {
b.columns[column] = value
return b
}
// Where adds a where condition to the update query
func (b *UpdateBuilder) Where(cond BuildFunc) *UpdateBuilder {
b.where = cond
return b
}
// Query builds the sql query and returns it along with its arguments
func (b *UpdateBuilder) Query() (string, []interface{}) {
b.WriteString("UPDATE ")
b.WriteString(b.table)
b.WriteString(" SET ")
for c, v := range b.columns {
if b.paramsCount > 0 {
b.WriteString(", ")
}
b.WriteString(c)
b.WriteString(" = ")
b.WriteArg(v)
}
if b.where != nil {
b.WriteString(" WHERE ")
b.where(b.Builder)
}
return b.String(), b.args
}