-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
104 lines (91 loc) · 2 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
package gosql
import "strings"
// Delete query
type Delete struct {
// with query
with with
// from
from string
// using
using []string
// where condition
where Condition
// returning
returning expression
}
// IsEmpty check if query is empty
func (d *Delete) IsEmpty() bool {
return d == nil || (d.with.Len() == 0 &&
d.from == "" &&
len(d.using) == 0 &&
d.where.IsEmpty() &&
d.returning.Len() == 0)
}
// From Set from value
func (d *Delete) From(from string) *Delete {
d.from = from
return d
}
// ResetFrom Set from empty string
func (d *Delete) ResetFrom() *Delete {
d.from = ""
return d
}
// Using add using
func (d *Delete) Using(using ...string) *Delete {
d.using = append(d.using, using...)
return d
}
// ResetUsing clear using
func (d *Delete) ResetUsing() *Delete {
d.using = d.using[:0]
return d
}
// Where set condition
func (d *Delete) Where() *Condition {
return &d.where
}
// Returning Append returning expression
func (d *Delete) Returning() *expression {
return &d.returning
}
// GetGetArguments get all values
func (d *Delete) GetGetArguments() []any {
return append(append(d.with.GetArguments(), d.where.GetArguments()...))
}
// SQL Get sql query
func (d *Delete) SQL() (query string, params []any, returning []any) {
return d.String(), d.GetGetArguments(), d.returning.GetArguments()
}
// String return result query
func (d *Delete) String() string {
if d.IsEmpty() {
return ""
}
b := strings.Builder{}
if d.with.Len() > 0 {
b.WriteString(d.with.String() + " ")
}
b.WriteString("DELETE")
if d.from != "" {
b.WriteString(" FROM " + d.from)
}
if len(d.using) > 0 {
b.WriteString(" USING " + strings.Join(d.using, ", "))
}
if !d.where.IsEmpty() {
b.WriteString(" WHERE " + d.where.String())
}
if d.returning.Len() > 0 {
b.WriteString(" RETURNING " + d.returning.String(", "))
}
b.WriteString(";")
return b.String()
}
func NewDelete() *Delete {
return &Delete{
where: Condition{operator: ConditionOperatorAnd},
with: with{
keys: make(map[int]string),
}}
}