-
Notifications
You must be signed in to change notification settings - Fork 36
/
conflict.go
70 lines (55 loc) · 1.42 KB
/
conflict.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
package clause
import (
"io"
"github.com/stephenafamo/bob"
)
type Conflict struct {
Do string // DO NOTHING | DO UPDATE
Target ConflictTarget
Set
Where
}
func (c *Conflict) SetConflict(conflict Conflict) {
*c = conflict
}
func (c Conflict) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
w.Write([]byte("ON CONFLICT"))
args, err := bob.ExpressIf(w, d, start, c.Target, true, "", "")
if err != nil {
return nil, err
}
w.Write([]byte(" DO "))
w.Write([]byte(c.Do))
setArgs, err := bob.ExpressIf(w, d, start+len(args), c.Set, len(c.Set.Set) > 0, " SET\n", "")
if err != nil {
return nil, err
}
args = append(args, setArgs...)
whereArgs, err := bob.ExpressIf(w, d, start+len(args), c.Where,
len(c.Where.Conditions) > 0, "\n", "")
if err != nil {
return nil, err
}
args = append(args, whereArgs...)
return args, nil
}
type ConflictTarget struct {
Constraint string
Columns []any
Where []any
}
func (c ConflictTarget) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, error) {
if c.Constraint != "" {
return bob.ExpressIf(w, d, start, c.Constraint, true, " ON CONSTRAINT ", "")
}
args, err := bob.ExpressSlice(w, d, start, c.Columns, " (", ", ", ")")
if err != nil {
return nil, err
}
whereArgs, err := bob.ExpressSlice(w, d, start+len(args), c.Where, " WHERE ", " AND ", "")
if err != nil {
return nil, err
}
args = append(args, whereArgs...)
return args, nil
}