-
Notifications
You must be signed in to change notification settings - Fork 36
/
delete.go
84 lines (69 loc) · 1.81 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
package dialect
import (
"io"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/clause"
)
// Trying to represent the query structure as documented in
// https://dev.mysql.com/doc/refman/8.0/en/delete.html
type DeleteQuery struct {
hints
clause.With
modifiers[string]
Tables []clause.Table
clause.From
clause.Where
clause.OrderBy
clause.Limit
}
func (d DeleteQuery) WriteSQL(w io.Writer, dl bob.Dialect, start int) ([]any, error) {
var args []any
withArgs, err := bob.ExpressIf(w, dl, start+len(args), d.With,
len(d.With.CTEs) > 0, "\n", "")
if err != nil {
return nil, err
}
args = append(args, withArgs...)
w.Write([]byte("DELETE "))
// no optimizer hint args
_, err = bob.ExpressIf(w, dl, start+len(args), d.hints,
len(d.hints.hints) > 0, "\n", "\n")
if err != nil {
return nil, err
}
// no modifiers args
_, err = bob.ExpressIf(w, dl, start+len(args), d.modifiers,
len(d.modifiers.modifiers) > 0, "", " ")
if err != nil {
return nil, err
}
tableArgs, err := bob.ExpressSlice(w, dl, start+len(args), d.Tables, "FROM ", ", ", "")
if err != nil {
return nil, err
}
args = append(args, tableArgs...)
usingArgs, err := bob.ExpressIf(w, dl, start+len(args), d.From,
d.From.Table != nil, "\nUSING ", "")
if err != nil {
return nil, err
}
args = append(args, usingArgs...)
whereArgs, err := bob.ExpressIf(w, dl, start+len(args), d.Where,
len(d.Where.Conditions) > 0, "\n", "")
if err != nil {
return nil, err
}
args = append(args, whereArgs...)
orderArgs, err := bob.ExpressIf(w, dl, start+len(args), d.OrderBy,
len(d.OrderBy.Expressions) > 0, "\n", "")
if err != nil {
return nil, err
}
args = append(args, orderArgs...)
_, err = bob.ExpressIf(w, dl, start+len(args), d.Limit,
d.Limit.Count != nil, "\n", "")
if err != nil {
return nil, err
}
return args, nil
}