-
Notifications
You must be signed in to change notification settings - Fork 36
/
delete_builder.go
110 lines (96 loc) · 2.55 KB
/
delete_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
package dbr
import (
"context"
"database/sql"
"fmt"
)
// DeleteBuilder builds "DELETE ..." stmt
type DeleteBuilder interface {
Builder
EventReceiver
Executer
Where(query interface{}, value ...interface{}) DeleteBuilder
Limit(n uint64) DeleteBuilder
}
type deleteBuilder struct {
runner
EventReceiver
Dialect Dialect
deleteStmt *deleteStmt
LimitCount int64
ctx context.Context
}
// DeleteFrom creates a DeleteBuilder
func (sess *Session) DeleteFrom(table string) DeleteBuilder {
return &deleteBuilder{
runner: sess,
EventReceiver: sess.EventReceiver,
Dialect: sess.Dialect,
deleteStmt: createDeleteStmt(table),
LimitCount: -1,
ctx: sess.ctx,
}
}
// DeleteFrom creates a DeleteBuilder
func (tx *Tx) DeleteFrom(table string) DeleteBuilder {
return &deleteBuilder{
runner: tx,
EventReceiver: tx.EventReceiver,
Dialect: tx.Dialect,
deleteStmt: createDeleteStmt(table),
LimitCount: -1,
ctx: tx.ctx,
}
}
// DeleteBySql creates a DeleteBuilder from raw query
func (sess *Session) DeleteBySql(query string, value ...interface{}) DeleteBuilder {
return &deleteBuilder{
runner: sess,
EventReceiver: sess.EventReceiver,
Dialect: sess.Dialect,
deleteStmt: createDeleteStmtBySQL(query, value),
LimitCount: -1,
ctx: sess.ctx,
}
}
// DeleteBySql creates a DeleteBuilder from raw query
func (tx *Tx) DeleteBySql(query string, value ...interface{}) DeleteBuilder {
return &deleteBuilder{
runner: tx,
EventReceiver: tx.EventReceiver,
Dialect: tx.Dialect,
deleteStmt: createDeleteStmtBySQL(query, value),
LimitCount: -1,
ctx: tx.ctx,
}
}
// Exec executes the stmt with background context
func (b *deleteBuilder) Exec() (sql.Result, error) {
return b.ExecContext(b.ctx)
}
// ExecContext executes the stmt
func (b *deleteBuilder) ExecContext(ctx context.Context) (sql.Result, error) {
return exec(ctx, b.runner, b.EventReceiver, b, b.Dialect)
}
// Where adds condition to the stmt
func (b *deleteBuilder) Where(query interface{}, value ...interface{}) DeleteBuilder {
b.deleteStmt.Where(query, value...)
return b
}
// Limit adds LIMIT
func (b *deleteBuilder) Limit(n uint64) DeleteBuilder {
b.LimitCount = int64(n)
return b
}
// Build builds `DELETE ...` in dialect
func (b *deleteBuilder) Build(d Dialect, buf Buffer) error {
err := b.deleteStmt.Build(b.Dialect, buf)
if err != nil {
return err
}
if b.LimitCount >= 0 {
buf.WriteString(" LIMIT ")
buf.WriteString(fmt.Sprint(b.LimitCount))
}
return nil
}