This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
119 lines (100 loc) · 3.06 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
package dat
// DeleteBuilder contains the clauses for a DELETE statement
type DeleteBuilder struct {
Execer
table string
whereFragments []*whereFragment
orderBys []string
limitCount uint64
limitValid bool
offsetCount uint64
offsetValid bool
id int
isInterpolated bool
scope Scope
}
// NewDeleteBuilder creates a new DeleteBuilder for the given table.
func NewDeleteBuilder(table string) *DeleteBuilder {
if table == "" {
logger.Error("DeleteFrom requires a table name.")
return nil
}
return &DeleteBuilder{table: table, isInterpolated: EnableInterpolation}
}
// ScopeMap uses a predefined scope in place of WHERE.
func (b *DeleteBuilder) ScopeMap(mapScope *MapScope, m M) *DeleteBuilder {
b.scope = mapScope.mergeClone(m)
return b
}
// Scope uses a predefined scope in place of WHERE.
func (b *DeleteBuilder) Scope(sql string, args ...interface{}) *DeleteBuilder {
b.scope = ScopeFunc(func(table string) (string, []interface{}) {
return escapeScopeTable(sql, table), args
})
return b
}
// Where appends a WHERE clause to the statement whereSqlOrMap can be a
// string or map. If it's a string, args wil replaces any places holders
func (b *DeleteBuilder) Where(whereSqlOrMap interface{}, args ...interface{}) *DeleteBuilder {
b.whereFragments = append(b.whereFragments, newWhereFragment(whereSqlOrMap, args))
return b
}
// OrderBy appends an ORDER BY clause to the statement
func (b *DeleteBuilder) OrderBy(ord string) *DeleteBuilder {
b.orderBys = append(b.orderBys, ord)
return b
}
// Limit sets a LIMIT clause for the statement; overrides any existing LIMIT
func (b *DeleteBuilder) Limit(limit uint64) *DeleteBuilder {
b.limitCount = limit
b.limitValid = true
return b
}
// Offset sets an OFFSET clause for the statement; overrides any existing OFFSET
func (b *DeleteBuilder) Offset(offset uint64) *DeleteBuilder {
b.offsetCount = offset
b.offsetValid = true
return b
}
// ToSQL serialized the DeleteBuilder to a SQL string
// It returns the string with placeholders and a slice of query arguments
func (b *DeleteBuilder) ToSQL() (string, []interface{}) {
if len(b.table) == 0 {
panic("no table specified")
}
buf := bufPool.Get()
defer bufPool.Put(buf)
var args []interface{}
buf.WriteString("DELETE FROM ")
buf.WriteString(b.table)
var placeholderStartPos int64 = 1
// Write WHERE clause if we have any fragments
if b.scope == nil {
if len(b.whereFragments) > 0 {
buf.WriteString(" WHERE ")
writeWhereFragmentsToSql(buf, b.whereFragments, &args, &placeholderStartPos)
}
} else {
whereFragment := newWhereFragment(b.scope.ToSQL(b.table))
writeScopeCondition(buf, whereFragment, &args, &placeholderStartPos)
}
// Ordering and limiting
if len(b.orderBys) > 0 {
buf.WriteString(" ORDER BY ")
for i, s := range b.orderBys {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(s)
}
}
if b.limitValid {
buf.WriteString(" LIMIT ")
writeUint64(buf, b.limitCount)
}
if b.offsetValid {
buf.WriteString(" OFFSET ")
writeUint64(buf, b.offsetCount)
}
return buf.String(), args
}