-
Notifications
You must be signed in to change notification settings - Fork 39
/
delete.go
52 lines (42 loc) · 1.12 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
package dialect
import (
"io"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/clause"
)
// Trying to represent the select query structure as documented in
// https://www.sqlite.org/lang_delete.html
type DeleteQuery struct {
clause.With
clause.From
clause.Where
clause.Returning
}
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 FROM"))
tableArgs, err := bob.ExpressIf(w, dl, start+len(args), d.From, true, " ", "")
if err != nil {
return nil, err
}
args = append(args, tableArgs...)
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...)
retArgs, err := bob.ExpressIf(w, dl, start+len(args), d.Returning,
len(d.Returning.Expressions) > 0, "\n", "")
if err != nil {
return nil, err
}
args = append(args, retArgs...)
return args, nil
}