-
Notifications
You must be signed in to change notification settings - Fork 37
/
mods.go
167 lines (117 loc) · 3.27 KB
/
mods.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package mods
import (
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/clause"
"github.com/stephenafamo/bob/expr"
)
type QueryMods[T any] []bob.Mod[T]
func (q QueryMods[T]) Apply(query T) {
for _, v := range q {
v.Apply(query)
}
}
type QueryModFunc[T any] func(T)
func (q QueryModFunc[T]) Apply(query T) {
q(query)
}
type With[Q interface{ AppendWith(clause.CTE) }] clause.CTE
func (f With[Q]) Apply(q Q) {
q.AppendWith(clause.CTE(f))
}
type Recursive[Q interface{ SetRecursive(bool) }] bool
func (r Recursive[Q]) Apply(q Q) {
q.SetRecursive(bool(r))
}
type Select[Q interface{ AppendSelect(columns ...any) }] []any
func (s Select[Q]) Apply(q Q) {
q.AppendSelect(s...)
}
type Preload[Q interface{ AppendPreloadSelect(columns ...any) }] []any
func (s Preload[Q]) Apply(q Q) {
q.AppendPreloadSelect(s...)
}
type Join[Q interface{ AppendJoin(clause.Join) }] clause.Join
func (j Join[Q]) Apply(q Q) {
q.AppendJoin(clause.Join(j))
}
type Where[Q interface{ AppendWhere(e ...any) }] struct {
E bob.Expression
}
func (w Where[Q]) Apply(q Q) {
q.AppendWhere(w.E)
}
type GroupBy[Q interface{ AppendGroup(any) }] struct {
E any
}
func (f GroupBy[Q]) Apply(q Q) {
q.AppendGroup(f.E)
}
type GroupWith[Q interface{ SetGroupWith(string) }] string
func (f GroupWith[Q]) Apply(q Q) {
q.SetGroupWith(string(f))
}
type GroupByDistinct[Q interface{ SetGroupByDistinct(bool) }] bool
func (f GroupByDistinct[Q]) Apply(q Q) {
q.SetGroupByDistinct(bool(f))
}
type Having[Q interface{ AppendHaving(e ...any) }] []any
func (d Having[Q]) Apply(q Q) {
q.AppendHaving(d...)
}
type Window[Q interface{ AppendWindow(clause.NamedWindow) }] clause.NamedWindow
func (f Window[Q]) Apply(q Q) {
q.AppendWindow(clause.NamedWindow(f))
}
type OrderBy[Q interface{ AppendOrder(clause.OrderDef) }] clause.OrderDef
func (f OrderBy[Q]) Apply(q Q) {
q.AppendOrder(clause.OrderDef(f))
}
type Limit[Q interface{ SetLimit(limit any) }] struct {
Count any
}
func (f Limit[Q]) Apply(q Q) {
q.SetLimit(f.Count)
}
type Offset[Q interface{ SetOffset(offset any) }] struct {
Count any
}
func (f Offset[Q]) Apply(q Q) {
q.SetOffset(f.Count)
}
type Fetch[Q interface{ SetFetch(clause.Fetch) }] clause.Fetch
func (f Fetch[Q]) Apply(q Q) {
q.SetFetch(clause.Fetch(f))
}
type Combine[Q interface{ SetCombine(clause.Combine) }] clause.Combine
func (f Combine[Q]) Apply(q Q) {
q.SetCombine(clause.Combine(f))
}
type For[Q interface{ SetFor(clause.For) }] clause.For
func (f For[Q]) Apply(q Q) {
q.SetFor(clause.For(f))
}
type Values[Q interface{ AppendValues(vals ...bob.Expression) }] []bob.Expression
func (s Values[Q]) Apply(q Q) {
q.AppendValues(s...)
}
type Rows[Q interface{ AppendValues(vals ...bob.Expression) }] [][]bob.Expression
func (r Rows[Q]) Apply(q Q) {
for _, row := range r {
q.AppendValues(row...)
}
}
type Returning[Q interface{ AppendReturning(vals ...any) }] []any
func (s Returning[Q]) Apply(q Q) {
q.AppendReturning(s...)
}
type Set[Q interface{ AppendSet(clauses ...any) }] []string
func (s Set[Q]) To(to any) bob.Mod[Q] {
return set[Q]{expr.OP("=", expr.Quote(s...), to)}
}
func (s Set[Q]) ToArg(to any) bob.Mod[Q] {
return set[Q]{expr.OP("=", expr.Quote(s...), expr.Arg(to))}
}
type set[Q interface{ AppendSet(clauses ...any) }] []any
func (s set[Q]) Apply(q Q) {
q.AppendSet(s...)
}