-
Notifications
You must be signed in to change notification settings - Fork 36
/
insert.go
66 lines (55 loc) · 1.47 KB
/
insert.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
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.postgresql.org/docs/current/sql-insert.html
type InsertQuery struct {
clause.With
Overriding string
clause.Table
clause.Values
clause.Conflict
clause.Returning
}
func (i InsertQuery) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
var args []any
withArgs, err := bob.ExpressIf(w, d, start+len(args), i.With,
len(i.With.CTEs) > 0, "", "\n")
if err != nil {
return nil, err
}
args = append(args, withArgs...)
tableArgs, err := bob.ExpressIf(w, d, start+len(args), i.Table,
true, "INSERT INTO ", "")
if err != nil {
return nil, err
}
args = append(args, tableArgs...)
_, err = bob.ExpressIf(w, d, start+len(args), i.Overriding,
i.Overriding != "", "\nOVERRIDING ", " VALUE")
if err != nil {
return nil, err
}
valArgs, err := bob.ExpressIf(w, d, start+len(args), i.Values, true, "\n", "")
if err != nil {
return nil, err
}
args = append(args, valArgs...)
conflictArgs, err := bob.ExpressIf(w, d, start+len(args), i.Conflict,
i.Conflict.Do != "", "\n", "")
if err != nil {
return nil, err
}
args = append(args, conflictArgs...)
retArgs, err := bob.ExpressIf(w, d, start+len(args), i.Returning,
len(i.Returning.Expressions) > 0, "\n", "")
if err != nil {
return nil, err
}
args = append(args, retArgs...)
w.Write([]byte("\n"))
return args, nil
}