forked from gocraft/dbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_builder.go
91 lines (78 loc) · 1.83 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
package dbr
import (
"database/sql"
"fmt"
)
type DeleteBuilder struct {
runner
EventReceiver
Dialect Dialect
*DeleteStmt
LimitCount int64
}
func (sess *Session) DeleteFrom(table string) *DeleteBuilder {
return &DeleteBuilder{
runner: sess,
EventReceiver: sess,
Dialect: sess.Dialect,
DeleteStmt: DeleteFrom(table),
LimitCount: -1,
}
}
func (tx *Tx) DeleteFrom(table string) *DeleteBuilder {
return &DeleteBuilder{
runner: tx,
EventReceiver: tx,
Dialect: tx.Dialect,
DeleteStmt: DeleteFrom(table),
LimitCount: -1,
}
}
func (sess *Session) DeleteBySql(query string, value ...interface{}) *DeleteBuilder {
return &DeleteBuilder{
runner: sess,
EventReceiver: sess,
Dialect: sess.Dialect,
DeleteStmt: DeleteBySql(query, value...),
LimitCount: -1,
}
}
func (tx *Tx) DeleteBySql(query string, value ...interface{}) *DeleteBuilder {
return &DeleteBuilder{
runner: tx,
EventReceiver: tx,
Dialect: tx.Dialect,
DeleteStmt: DeleteBySql(query, value...),
LimitCount: -1,
}
}
func (b *DeleteBuilder) ToSql() (string, []interface{}) {
buf := NewBuffer()
err := b.Build(b.Dialect, buf)
if err != nil {
panic(err)
}
return buf.String(), buf.Value()
}
func (b *DeleteBuilder) Exec() (sql.Result, error) {
return exec(b.runner, b.EventReceiver, b, b.Dialect)
}
func (b *DeleteBuilder) Where(query interface{}, value ...interface{}) *DeleteBuilder {
b.DeleteStmt.Where(query, value...)
return b
}
func (b *DeleteBuilder) Limit(n uint64) *DeleteBuilder {
b.LimitCount = int64(n)
return b
}
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
}