forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_builder.go
97 lines (82 loc) · 2.01 KB
/
update_builder.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
package dbr
import (
"context"
"database/sql"
"fmt"
)
type UpdateBuilder struct {
runner
EventReceiver
Dialect Dialect
*UpdateStmt
LimitCount int64
}
func (sess *Session) Update(table string) *UpdateBuilder {
return &UpdateBuilder{
runner: sess,
EventReceiver: sess,
Dialect: sess.Dialect,
UpdateStmt: Update(table),
LimitCount: -1,
}
}
func (tx *Tx) Update(table string) *UpdateBuilder {
return &UpdateBuilder{
runner: tx,
EventReceiver: tx,
Dialect: tx.Dialect,
UpdateStmt: Update(table),
LimitCount: -1,
}
}
func (sess *Session) UpdateBySql(query string, value ...interface{}) *UpdateBuilder {
return &UpdateBuilder{
runner: sess,
EventReceiver: sess,
Dialect: sess.Dialect,
UpdateStmt: UpdateBySql(query, value...),
LimitCount: -1,
}
}
func (tx *Tx) UpdateBySql(query string, value ...interface{}) *UpdateBuilder {
return &UpdateBuilder{
runner: tx,
EventReceiver: tx,
Dialect: tx.Dialect,
UpdateStmt: UpdateBySql(query, value...),
LimitCount: -1,
}
}
func (b *UpdateBuilder) Exec() (sql.Result, error) {
return b.ExecContext(context.Background())
}
func (b *UpdateBuilder) ExecContext(ctx context.Context) (sql.Result, error) {
return exec(ctx, b.runner, b.EventReceiver, b, b.Dialect)
}
func (b *UpdateBuilder) Set(column string, value interface{}) *UpdateBuilder {
b.UpdateStmt.Set(column, value)
return b
}
func (b *UpdateBuilder) SetMap(m map[string]interface{}) *UpdateBuilder {
b.UpdateStmt.SetMap(m)
return b
}
func (b *UpdateBuilder) Where(query interface{}, value ...interface{}) *UpdateBuilder {
b.UpdateStmt.Where(query, value...)
return b
}
func (b *UpdateBuilder) Limit(n uint64) *UpdateBuilder {
b.LimitCount = int64(n)
return b
}
func (b *UpdateBuilder) Build(d Dialect, buf Buffer) error {
err := b.UpdateStmt.Build(b.Dialect, buf)
if err != nil {
return err
}
if b.LimitCount >= 0 {
buf.WriteString(" LIMIT ")
buf.WriteString(fmt.Sprint(b.LimitCount))
}
return nil
}