-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delete.go
58 lines (45 loc) · 1.04 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
package qry
import (
"fmt"
)
func Delete() DeleteQuery {
return DeleteQuery{}
}
type DeleteQuery struct {
Table string
Condition Condition
Limit int64
Offset int64
}
func (query DeleteQuery) Build() (string, []any) {
stmt := fmt.Sprintf(
"DELETE FROM %s",
query.Table,
)
args := make([]any, 0)
if query.Condition != nil {
if conditionsStmt, conditionArgs := query.Condition.Build(); len(conditionsStmt) > 0 {
stmt += fmt.Sprintf(" WHERE %s", conditionsStmt)
args = append(args, conditionArgs...)
}
}
if query.Limit > 0 {
stmt += fmt.Sprintf(" LIMIT %d", query.Limit)
}
if query.Offset > 0 {
stmt += fmt.Sprintf(" OFFSET %d", query.Offset)
}
return stmt, args
}
type TypedDeleteQuery[T any] struct {
DeleteQuery
Condition func(target *T) Condition
Target *T
}
func (query TypedDeleteQuery[T]) Prepare() DeleteQuery {
query.DeleteQuery.Condition = query.Condition(query.Target)
return query.DeleteQuery
}
func (query TypedDeleteQuery[T]) Build() (string, []any) {
return query.Prepare().Build()
}