-
Notifications
You must be signed in to change notification settings - Fork 39
/
where.go
111 lines (89 loc) · 2.43 KB
/
where.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
package psql
import (
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/expr"
"github.com/stephenafamo/bob/mods"
)
type Filterable interface {
AppendWhere(...any)
}
func Where[Q Filterable, C any](name Expression) WhereMod[Q, C] {
return WhereMod[Q, C]{
name: name,
}
}
func WhereOr[Q Filterable](whereMods ...mods.Where[Q]) mods.Where[Q] {
exprs := make([]bob.Expression, len(whereMods))
for i, mod := range whereMods {
exprs[i] = mod.E
}
return mods.Where[Q]{
E: Or(exprs...),
}
}
func WhereAnd[Q Filterable](whereMods ...mods.Where[Q]) mods.Where[Q] {
exprs := make([]bob.Expression, len(whereMods))
for i, mod := range whereMods {
exprs[i] = mod.E
}
return mods.Where[Q]{
E: And(exprs...),
}
}
type WhereMod[Q Filterable, C any] struct {
name Expression
}
func (w WhereMod[Q, C]) EQ(val C) mods.Where[Q] {
return mods.Where[Q]{E: w.name.EQ(Arg(val))}
}
func (w WhereMod[Q, C]) NE(val C) mods.Where[Q] {
return mods.Where[Q]{E: w.name.NE(Arg(val))}
}
func (w WhereMod[Q, C]) LT(val C) mods.Where[Q] {
return mods.Where[Q]{E: w.name.LT(Arg(val))}
}
func (w WhereMod[Q, C]) LTE(val C) mods.Where[Q] {
return mods.Where[Q]{E: w.name.LTE(Arg(val))}
}
func (w WhereMod[Q, C]) GT(val C) mods.Where[Q] {
return mods.Where[Q]{E: w.name.GT(Arg(val))}
}
func (w WhereMod[Q, C]) GTE(val C) mods.Where[Q] {
return mods.Where[Q]{E: w.name.GTE(Arg(val))}
}
func (w WhereMod[Q, C]) In(slice ...C) mods.Where[Q] {
values := make([]any, 0, len(slice))
for _, value := range slice {
values = append(values, value)
}
return mods.Where[Q]{E: w.name.In(Arg(values...))}
}
func (w WhereMod[Q, C]) NotIn(slice ...C) mods.Where[Q] {
values := make([]any, 0, len(slice))
for _, value := range slice {
values = append(values, value)
}
return mods.Where[Q]{E: w.name.NotIn(Arg(values...))}
}
func (w WhereMod[Q, C]) Like(val C) mods.Where[Q] {
return mods.Where[Q]{E: w.name.Like(Arg(val))}
}
func (w WhereMod[Q, C]) ILike(val C) mods.Where[Q] {
return mods.Where[Q]{E: expr.OP("ILIKE", w.name, Arg(val))}
}
func WhereNull[Q Filterable, C any](name Expression) WhereNullMod[Q, C] {
return WhereNullMod[Q, C]{
WhereMod: Where[Q, C](name),
}
}
type WhereNullMod[Q interface {
AppendWhere(e ...any)
}, C any] struct {
WhereMod[Q, C]
}
func (w WhereNullMod[Q, C]) IsNull() mods.Where[Q] {
return mods.Where[Q]{E: w.name.IsNull()}
}
func (w WhereNullMod[Q, C]) IsNotNull() mods.Where[Q] {
return mods.Where[Q]{E: w.name.IsNotNull()}
}