-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
executable file
·146 lines (132 loc) · 3.87 KB
/
filters.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
package templates
var Filters = `package gen
import (
"context"
"fmt"
"strings"
"gorm.io/gorm"
)
{{range $obj := .Model.ObjectEntities}}
{{if not $obj.IsExtended}}
func (f *{{$obj.Name}}FilterType) IsEmpty(ctx context.Context) bool {
wheres := []string{}
values := []interface{}{}
joins := []string{}
err := f.ApplyWithAlias(ctx, "companies", &wheres, &values, &joins)
if err != nil {
panic(err)
}
return len(wheres) == 0
}
func (f *{{$obj.Name}}FilterType) Apply(ctx context.Context, wheres *[]string, values *[]interface{}, joins *[]string) error {
return f.ApplyWithAlias(ctx, TableName("{{$obj.TableName}}"), wheres, values, joins)
}
func (f *{{$obj.Name}}FilterType) ApplyWithAlias(ctx context.Context, alias string, wheres *[]string, values *[]interface{}, joins *[]string) error {
if f == nil {
return nil
}
aliasPrefix := alias + "."
_where, _values := f.WhereContent(aliasPrefix)
*wheres = append(*wheres, _where...)
*values = append(*values, _values...)
if f.Or != nil {
cs := []string{}
vs := []interface{}{}
js := []string{}
for _, or := range f.Or {
_cs := []string{}
err := or.ApplyWithAlias(ctx, alias, &_cs, &vs, &js)
if err != nil {
return err
}
cs = append(cs, strings.Join(_cs, " AND "))
}
if len(cs) > 0 {
*wheres = append(*wheres, "("+strings.Join(cs, " OR ")+")")
}
*values = append(*values, vs...)
*joins = append(*joins, js...)
}
if f.And != nil {
cs := []string{}
vs := []interface{}{}
js := []string{}
for _, and := range f.And {
err := and.ApplyWithAlias(ctx, alias, &cs, &vs, &js)
if err != nil {
return err
}
}
if len(cs) > 0 {
*wheres = append(*wheres, strings.Join(cs, " AND "))
}
*values = append(*values, vs...)
*joins = append(*joins, js...)
}
{{range $rel := $obj.Relationships}}
{{if not $rel.Target.IsExtended}}
{{$varName := (printf "f.%s" $rel.MethodName)}}
if {{$varName}} != nil {
_alias := alias + "_{{$rel.Name}}"
*joins = append(*joins, {{$rel.JoinString}})
err := {{$varName}}.ApplyWithAlias(ctx, _alias, wheres, values, joins)
if err != nil {
return err
}
}{{end}}{{end}}
return nil
}
func (f *{{$obj.Name}}FilterType) WhereContent(aliasPrefix string) (conditions []string, values []interface{}) {
conditions = []string{}
values = []interface{}{}
{{range $col := $obj.Columns}}{{if $col.IsWritableType}}
{{range $fm := $col.FilterMapping}} {{$varName := (printf "f.%s%s" $col.MethodName $fm.SuffixCamel)}}
if {{$varName}} != nil {
conditions = append(conditions, aliasPrefix + SnakeString("{{$col.Name}}")+" {{$fm.Operator}}")
{{if $fm.IsLike}}values = append(values, "%"+{{$fm.WrapValueVariable $varName}}+"%"){{else}}values = append(values, {{$fm.WrapValueVariable $varName}}){{end}}
}
{{end}}
if f.{{$col.MethodName}}Null != nil {
if *f.{{$col.MethodName}}Null {
conditions = append(conditions, aliasPrefix+SnakeString("{{$col.Name}}")+" IS NULL" + " OR " + aliasPrefix+SnakeString("{{$col.Name}}")+" =''")
} else {
conditions = append(conditions, aliasPrefix+SnakeString("{{$col.Name}}")+" IS NOT NULL" + " OR " + aliasPrefix+SnakeString("{{$col.Name}}")+" <> ''")
}
}
{{end}}
{{end}}
return
}
// AndWith convenience method for combining two or more filters with AND statement
func (f *{{$obj.Name}}FilterType) AndWith(f2 ...*{{$obj.Name}}FilterType) *{{$obj.Name}}FilterType {
_f2 := f2[:0]
for _, x := range f2 {
if x != nil {
_f2 = append(_f2, x)
}
}
if len(_f2) == 0 {
return f
}
return &{{$obj.Name}}FilterType{
And: append(_f2,f),
}
}
// OrWith convenience method for combining two or more filters with OR statement
func (f *{{$obj.Name}}FilterType) OrWith(f2 ...*{{$obj.Name}}FilterType) *{{$obj.Name}}FilterType {
_f2 := f2[:0]
for _, x := range f2 {
if x != nil {
_f2 = append(_f2, x)
}
}
if len(_f2) == 0 {
return f
}
return &{{$obj.Name}}FilterType{
Or: append(_f2,f),
}
}
{{end}}
{{end}}
`