-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathupdate_builder.go
124 lines (108 loc) · 2.95 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
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
package dbr
import (
"context"
"database/sql"
"fmt"
)
// UpdateBuilder builds `UPDATE ...`
type UpdateBuilder interface {
Builder
EventReceiver
Executer
Where(query interface{}, value ...interface{}) UpdateBuilder
Set(column string, value interface{}) UpdateBuilder
SetMap(m map[string]interface{}) UpdateBuilder
Limit(n uint64) UpdateBuilder
}
type updateBuilder struct {
EventReceiver
runner
Dialect Dialect
updateStmt *updateStmt
LimitCount int64
ctx context.Context
}
// Update creates a UpdateBuilder
func (sess *Session) Update(table string) UpdateBuilder {
return &updateBuilder{
runner: sess,
EventReceiver: sess.EventReceiver,
Dialect: sess.Dialect,
updateStmt: createUpdateStmt(table),
LimitCount: -1,
ctx: sess.ctx,
}
}
// Update creates a UpdateBuilder
func (tx *Tx) Update(table string) UpdateBuilder {
return &updateBuilder{
runner: tx,
EventReceiver: tx.EventReceiver,
Dialect: tx.Dialect,
updateStmt: createUpdateStmt(table),
LimitCount: -1,
ctx: tx.ctx,
}
}
// UpdateBySql creates a UpdateBuilder from raw query
func (sess *Session) UpdateBySql(query string, value ...interface{}) UpdateBuilder {
return &updateBuilder{
runner: sess,
EventReceiver: sess.EventReceiver,
Dialect: sess.Dialect,
updateStmt: createUpdateStmtBySQL(query, value),
LimitCount: -1,
ctx: sess.ctx,
}
}
// UpdateBySql creates a UpdateBuilder from raw query
func (tx *Tx) UpdateBySql(query string, value ...interface{}) UpdateBuilder {
return &updateBuilder{
runner: tx,
EventReceiver: tx.EventReceiver,
Dialect: tx.Dialect,
updateStmt: createUpdateStmtBySQL(query, value),
LimitCount: -1,
ctx: tx.ctx,
}
}
// Exec executes the stmt with background context
func (b *updateBuilder) Exec() (sql.Result, error) {
return b.ExecContext(b.ctx)
}
// ExecContext executes the stmt
func (b *updateBuilder) ExecContext(ctx context.Context) (sql.Result, error) {
return exec(ctx, b.runner, b.EventReceiver, b, b.Dialect)
}
// Set adds "SET column=value"
func (b *updateBuilder) Set(column string, value interface{}) UpdateBuilder {
b.updateStmt.Set(column, value)
return b
}
// SetMap adds "SET column=value" for each key value pair in m
func (b *updateBuilder) SetMap(m map[string]interface{}) UpdateBuilder {
b.updateStmt.SetMap(m)
return b
}
// Where adds condition to the stmt
func (b *updateBuilder) Where(query interface{}, value ...interface{}) UpdateBuilder {
b.updateStmt.Where(query, value...)
return b
}
// Limit adds LIMIT
func (b *updateBuilder) Limit(n uint64) UpdateBuilder {
b.LimitCount = int64(n)
return b
}
// Build builds `UPDATE ...` in dialect
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
}