-
Notifications
You must be signed in to change notification settings - Fork 209
/
delete.go
140 lines (118 loc) · 2.88 KB
/
delete.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package dbr
import (
"context"
"database/sql"
"strconv"
)
// DeleteStmt builds `DELETE ...`.
type DeleteStmt struct {
Runner
EventReceiver
Dialect
raw
Table string
WhereCond []Builder
LimitCount int64
comments Comments
}
type DeleteBuilder = DeleteStmt
func (b *DeleteStmt) Build(d Dialect, buf Buffer) error {
if b.raw.Query != "" {
return b.raw.Build(d, buf)
}
if b.Table == "" {
return ErrTableNotSpecified
}
err := b.comments.Build(d, buf)
if err != nil {
return err
}
buf.WriteString("DELETE FROM ")
buf.WriteString(d.QuoteIdent(b.Table))
if len(b.WhereCond) > 0 {
buf.WriteString(" WHERE ")
err := And(b.WhereCond...).Build(d, buf)
if err != nil {
return err
}
}
if b.LimitCount >= 0 {
buf.WriteString(" LIMIT ")
buf.WriteString(strconv.FormatInt(b.LimitCount, 10))
}
return nil
}
// DeleteFrom creates a DeleteStmt.
func DeleteFrom(table string) *DeleteStmt {
return &DeleteStmt{
Table: table,
LimitCount: -1,
}
}
// DeleteFrom creates a DeleteStmt.
func (sess *Session) DeleteFrom(table string) *DeleteStmt {
b := DeleteFrom(table)
b.Runner = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
// DeleteFrom creates a DeleteStmt.
func (tx *Tx) DeleteFrom(table string) *DeleteStmt {
b := DeleteFrom(table)
b.Runner = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
// DeleteBySql creates a DeleteStmt from raw query.
func DeleteBySql(query string, value ...interface{}) *DeleteStmt {
return &DeleteStmt{
raw: raw{
Query: query,
Value: value,
},
LimitCount: -1,
}
}
// DeleteBySql creates a DeleteStmt from raw query.
func (sess *Session) DeleteBySql(query string, value ...interface{}) *DeleteStmt {
b := DeleteBySql(query, value...)
b.Runner = sess
b.EventReceiver = sess.EventReceiver
b.Dialect = sess.Dialect
return b
}
// DeleteBySql creates a DeleteStmt from raw query.
func (tx *Tx) DeleteBySql(query string, value ...interface{}) *DeleteStmt {
b := DeleteBySql(query, value...)
b.Runner = tx
b.EventReceiver = tx.EventReceiver
b.Dialect = tx.Dialect
return b
}
// Where adds a where condition.
// query can be Builder or string. value is used only if query type is string.
func (b *DeleteStmt) Where(query interface{}, value ...interface{}) *DeleteStmt {
switch query := query.(type) {
case string:
b.WhereCond = append(b.WhereCond, Expr(query, value...))
case Builder:
b.WhereCond = append(b.WhereCond, query)
}
return b
}
func (b *DeleteStmt) Limit(n uint64) *DeleteStmt {
b.LimitCount = int64(n)
return b
}
func (b *DeleteStmt) Comment(comment string) *DeleteStmt {
b.comments = b.comments.Append(comment)
return b
}
func (b *DeleteStmt) Exec() (sql.Result, error) {
return b.ExecContext(context.Background())
}
func (b *DeleteStmt) ExecContext(ctx context.Context) (sql.Result, error) {
return exec(ctx, b.Runner, b.EventReceiver, b, b.Dialect)
}