-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqld.go
315 lines (259 loc) · 7.86 KB
/
sqld.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package sqld
import (
"fmt"
"strconv"
"strings"
)
// Op is a boolean operator
type Op string
const (
OR Op = "OR"
AND Op = "AND"
)
// Sect is a filtering section of the query
type Sect string
const (
WHERE Sect = "WHERE"
HAVING Sect = "HAVING"
)
// Sorting is the direction in which you want to sort a query
type Sorting string
const (
ASC Sorting = "ASC"
DESC Sorting = "DESC"
)
// Section builds a filtering section.
// If the condition is empty, the returned string is also empty.
func Section(sect Sect, cond string) string {
if cond == "" {
return ""
}
return "\n" + string(sect) + " " + cond
}
// Where builds a WHERE filtering section.
// If the condition is empty, the returned string is also empty.
func Where(pred string) string {
return Section(WHERE, pred)
}
// Having builds an HAVING filtering section.
// If the condition is empty, the returned string is also empty.
func Having(pred string) string {
return Section(HAVING, pred)
}
// Cond builds a condition concatenating the filters with the given operator.
// If the filters are all empty, the returned string is also empty.
func Cond(op Op, filters ...string) string {
bldr := strings.Builder{}
for _, filter := range filters {
if filter == "" {
continue
}
if bldr.Len() != 0 {
bldr.WriteString(" " + string(op) + "\n\t")
}
bldr.WriteString(filter)
}
if bldr.Len() == 0 {
return ""
}
return "(\n\t" + bldr.String() + "\n)"
}
// And builds a condition concatenating the filters with the AND operator.
// If the filters are all empty, the returned string is also empty.
func And(filters ...string) string {
return Cond(AND, filters...)
}
// Or builds a condition concatenating the filters with the OR operator.
// If the filters are all empty, the returned string is also empty.
func Or(filters ...string) string {
return Cond(OR, filters...)
}
// Not negates the given string.
// If the filter is empty, the returned string is also empty.
func Not(filter string) string {
if filter == "" {
return ""
}
return "NOT(" + filter + ")"
}
// Sort produces a sorting statement on the column, with the given direction
func Sort(column string, sorting Sorting) string {
return column + " " + string(sorting)
}
// Asc produces an ASC sorting statement on the column, with the given direction
func Asc(column string) string {
return Sort(column, ASC)
}
// Desc produces a DESC sorting statement on the column, with the given direction
func Desc(column string) string {
return Sort(column, DESC)
}
// OrderBy builds an ORDER BY section.
// If the sortings are all empty, the returned string is also empty.
func OrderBy(sorts ...string) string {
bldr := strings.Builder{}
for _, sort := range sorts {
if sort == "" {
continue
}
if bldr.Len() > 0 {
bldr.WriteString(",\n\t")
}
bldr.WriteString(sort)
}
if bldr.Len() == 0 {
return ""
}
return "\nORDER BY " + bldr.String()
}
// Null produces a filter that checks if the target is NULL
func Null(target string) string {
return target + " IS NULL"
}
// PrinterFn is a callback that applies a parameter to the given statement (usually a filter)
type PrinterFn func(string) string
// Eq produces a PrinterFn that equates the target with the given parameter
func Eq(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s = :%s", target, param)
}
}
// Like produces a PrinterFn that checks if the target text respects the given pattern
func Like(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s LIKE :%s", target, param)
}
}
// ILike produces a PrinterFn that checks if the target text respects the given pattern, ignoring the casing
func ILike(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("LOWER(%s) LIKE LOWER(:%s)", target, param)
}
}
// ILike produces a PrinterFn that checks if the target text respects the given pattern, ignoring the casing
//
// Instead of lowering the two strings, it uses Postgres ILIKE operand
func PgILike(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s ILIKE :%s", target, param)
}
}
// In produces a PrinterFn that checks if the target is contained in the given parameter slice
func In(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s IN(:%s)", target, param)
}
}
// Gt produces a PrinterFn that checks if the target is greater than the given parameter
func Gt(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s > :%s", target, param)
}
}
// Gte produces a PrinterFn that checks if the target is greater or equal the given parameter
func Gte(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s >= :%s", target, param)
}
}
// Lt produces a PrinterFn that checks if the target is smaller than the given parameter
func Lt(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s < :%s", target, param)
}
}
// Lte produces a PrinterFn that checks if the target is smaller or equal the given parameter
func Lte(target string) PrinterFn {
return func(param string) string {
return fmt.Sprintf("%s <= :%s", target, param)
}
}
// FmtStartsWith maps the parameter with the desired pattern.
// Skips the mapping if the value is empty or nil
func FmtStartsWith[S string | *string](val S) S {
if cast, ok := any(val).(string); ok {
if cast == "" {
return val
}
return any(cast + "%").(S)
} else if cast, ok := any(val).(*string); ok {
if cast == nil {
return val
}
str := *cast + "%"
return any(&str).(S)
} else {
panic("unreachable")
}
}
// FmtEndsWith maps the parameter with the desired pattern.
// Skips the mapping if the value is empty or nil
func FmtEndsWith[S string | *string](val S) S {
if cast, ok := any(val).(string); ok {
if cast == "" {
return val
}
return any("%" + cast).(S)
} else if cast, ok := any(val).(*string); ok {
if cast == nil {
return val
}
str := "%" + *cast
return any(&str).(S)
} else {
panic("unreachable")
}
}
// FmtContains maps the parameter with the desired pattern.
// Skips the mapping if the value is empty or nil
func FmtContains[S string | *string](val S) S {
if cast, ok := any(val).(string); ok {
if cast == "" {
return val
}
return any("%" + cast + "%").(S)
} else if cast, ok := any(val).(*string); ok {
if cast == nil {
return val
}
str := "%" + *cast + "%"
return any(&str).(S)
} else {
panic("unreachable")
}
}
// Params is just an alias for a map containing the query parameters
type Params map[string]any
// Predicate is a callback that validates a condition on a value
type PredicateFn[T any] func(T) bool
// If is used to build the query dynamically, based on runtime conditions.
//
// If the predicate is true, the value is pushed in the parameter map and the printed filter is returned.
// If the predicate is false, the parameter map is untouched, and an empty string is returned.
func If[T any](pred PredicateFn[T], val T, params *Params, printer PrinterFn) string {
if !pred(val) {
return ""
}
argName := "arg" + strconv.Itoa(len(*params))
(*params)[argName] = val
return printer(argName)
}
// IfNotNil is a proxy for If with a predicate that checks if the pointer is not nil
func IfNotNil[T any](val *T, params *Params, printer PrinterFn) string {
return If(func(t *T) bool {
return t != nil
}, val, params, printer)
}
// IfNotZero is a proxy for If with a predicate that checks if the value is not equal to the zero value of its type
func IfNotZero[T comparable](val T, params *Params, printer PrinterFn) string {
return If(func(t T) bool {
var zero T
return t != zero
}, val, params, printer)
}
// IfNotEmpty is a proxy for If with a predicate that checks if the slice is not empty
func IfNotEmpty[T any](val []T, params *Params, printer PrinterFn) string {
return If(func(t []T) bool {
return len(t) > 0
}, val, params, printer)
}