-
Notifications
You must be signed in to change notification settings - Fork 37
/
qm.go
127 lines (108 loc) · 3.38 KB
/
qm.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package im
import (
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/clause"
"github.com/stephenafamo/bob/dialect/psql/dialect"
"github.com/stephenafamo/bob/expr"
"github.com/stephenafamo/bob/internal"
"github.com/stephenafamo/bob/mods"
)
func With(name string, columns ...string) dialect.CTEChain[*dialect.InsertQuery] {
return dialect.With[*dialect.InsertQuery](name, columns...)
}
func Recursive(r bool) bob.Mod[*dialect.InsertQuery] {
return mods.Recursive[*dialect.InsertQuery](r)
}
func Into(name any, columns ...string) bob.Mod[*dialect.InsertQuery] {
return mods.QueryModFunc[*dialect.InsertQuery](func(i *dialect.InsertQuery) {
i.Table = clause.Table{
Expression: name,
Columns: columns,
}
})
}
func IntoAs(name any, alias string, columns ...string) bob.Mod[*dialect.InsertQuery] {
return mods.QueryModFunc[*dialect.InsertQuery](func(i *dialect.InsertQuery) {
i.Table = clause.Table{
Expression: name,
Alias: alias,
Columns: columns,
}
})
}
func OverridingSystem() bob.Mod[*dialect.InsertQuery] {
return mods.QueryModFunc[*dialect.InsertQuery](func(i *dialect.InsertQuery) {
i.Overriding = "SYSTEM"
})
}
func OverridingUser() bob.Mod[*dialect.InsertQuery] {
return mods.QueryModFunc[*dialect.InsertQuery](func(i *dialect.InsertQuery) {
i.Overriding = "USER"
})
}
func Values(clauses ...bob.Expression) bob.Mod[*dialect.InsertQuery] {
return mods.Values[*dialect.InsertQuery](clauses)
}
func Rows(rows ...[]bob.Expression) bob.Mod[*dialect.InsertQuery] {
return mods.Rows[*dialect.InsertQuery](rows)
}
// Insert from a query
func Query(q bob.Query) bob.Mod[*dialect.InsertQuery] {
return mods.QueryModFunc[*dialect.InsertQuery](func(i *dialect.InsertQuery) {
i.Query = q
})
}
// The column to target. Will auto add brackets
func OnConflict(columns ...any) mods.Conflict[*dialect.InsertQuery] {
return mods.Conflict[*dialect.InsertQuery](func() clause.Conflict {
return clause.Conflict{
Target: clause.ConflictTarget{
Columns: columns,
},
}
})
}
func OnConflictOnConstraint(constraint string) mods.Conflict[*dialect.InsertQuery] {
return mods.Conflict[*dialect.InsertQuery](func() clause.Conflict {
return clause.Conflict{
Target: clause.ConflictTarget{
Constraint: constraint,
},
}
})
}
func Returning(clauses ...any) bob.Mod[*dialect.InsertQuery] {
return mods.Returning[*dialect.InsertQuery](clauses)
}
//========================================
// For use in ON CONFLICT DO UPDATE SET
//========================================
func Set(sets ...bob.Expression) bob.Mod[*clause.Conflict] {
return mods.QueryModFunc[*clause.Conflict](func(c *clause.Conflict) {
c.Set.Set = append(c.Set.Set, internal.ToAnySlice(sets)...)
})
}
func SetCol(from string) mods.Set[*clause.Conflict] {
return mods.Set[*clause.Conflict]{from}
}
func SetExcluded(cols ...string) bob.Mod[*clause.Conflict] {
exprs := make([]any, 0, len(cols))
for _, col := range cols {
if col == "" {
continue
}
exprs = append(exprs,
expr.Join{Exprs: []bob.Expression{
expr.Quote(col), expr.Raw("= EXCLUDED."), expr.Quote(col),
}},
)
}
return mods.QueryModFunc[*clause.Conflict](func(c *clause.Conflict) {
c.Set.Set = append(c.Set.Set, exprs...)
})
}
func Where(e bob.Expression) bob.Mod[*clause.Conflict] {
return mods.QueryModFunc[*clause.Conflict](func(c *clause.Conflict) {
c.Where.Conditions = append(c.Where.Conditions, e)
})
}