-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdelete.go
64 lines (53 loc) · 1.41 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
package sqlbuilder
import (
"bytes"
)
//
// Author: 陈永佳 [email protected], [email protected]
//
type DeleteBuilder struct {
ctx *SQLContext
table string
ensure bool // 删除全表时,需要强制设置一个标记位。
}
func newDeleteBuilder(ctx *SQLContext, table string) *DeleteBuilder {
return &DeleteBuilder{
ctx: ctx,
table: table,
ensure: false,
}
}
func (slf *DeleteBuilder) Table(table string) *DeleteBuilder {
slf.table = table
return slf
}
func (slf *DeleteBuilder) compile() *bytes.Buffer {
if "" == slf.table {
panic("Table name not found, you should call 'Table(table)' method to set it")
}
buf := new(bytes.Buffer)
buf.WriteString("DELETE FROM ")
buf.WriteString(slf.ctx.escapeName(slf.table))
return buf
}
func (slf *DeleteBuilder) YesImSureDeleteTable() *DeleteBuilder {
slf.ensure = true
return slf
}
func (slf *DeleteBuilder) Where(conditions SQLStatement) *WhereBuilder {
return newWhereBuilder(slf.ctx, slf.Compile(), conditions)
}
func (slf *DeleteBuilder) Compile() string {
return slf.compile().String()
}
func (slf *DeleteBuilder) ToSQL() string {
sqlTxt := sqlEndpoint(slf.compile())
if slf.ensure {
return sqlTxt
} else {
panic("Warning for FULL-DELETE the table, you must call 'YesImSureDeleteTable(bool)' to ensure. SQLText: " + sqlTxt)
}
}
func (slf *DeleteBuilder) Execute() *Executor {
return newExecute(slf.ToSQL(), slf.ctx.prepare)
}