-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfilterapply.go
243 lines (225 loc) · 5.91 KB
/
filterapply.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
package yaorm
import (
"fmt"
"reflect"
"strings"
"github.com/geoffreybauduin/yaorm/_vendor/github.com/lann/squirrel"
"github.com/geoffreybauduin/yaorm/tools"
"github.com/geoffreybauduin/yaorm/yaormfilter"
)
type filterApplier struct {
statement squirrel.SelectBuilder
filter yaormfilter.Filter
tableName string
dbp DBProvider
}
type filterFieldApplier struct {
statement squirrel.SelectBuilder
field reflect.Value
tableName string
tagData []string
dbFieldName string
isJoining bool
leftJoin bool
dbp DBProvider
filter yaormfilter.Filter
}
func getTableNameFromFilter(f yaormfilter.Filter) string {
table, err := GetTableByFilter(f)
if err != nil {
return ""
}
return table.Name()
}
func getTableFromFilter(f yaormfilter.Filter) *Table {
table, err := GetTableByFilter(f)
if err != nil {
return nil
}
return table
}
func apply(statement squirrel.SelectBuilder, f yaormfilter.Filter, dbp DBProvider) squirrel.SelectBuilder {
applier := &filterApplier{
statement: statement,
filter: f,
tableName: getTableNameFromFilter(f),
dbp: dbp,
}
applier.Apply()
statement = applier.statement
for _, option := range f.GetSelectOptions() {
switch option {
case yaormfilter.RequestOptions.SelectForUpdate:
if dbp.CanSelectForUpdate() {
statement = statement.Suffix(fmt.Sprintf(`FOR UPDATE OF %s`, dbp.EscapeValue(applier.tableName)))
}
case yaormfilter.RequestOptions.SelectDistinct:
statement = statement.Distinct()
}
}
for _, orderBy := range f.GetOrderBy() {
statement = statement.OrderBy(
fmt.Sprintf("%s.%s %s", dbp.EscapeValue(applier.tableName), dbp.EscapeValue(orderBy.Field), orderBy.Way),
)
}
if shouldLimit, limit := f.GetLimit(); shouldLimit {
statement = statement.Limit(limit)
}
if shouldOffset, offset := f.GetOffset(); shouldOffset {
statement = statement.Offset(offset)
}
return statement
}
func (a *filterApplier) Apply() {
// We may have a pointer as parameter, we need to make sure we work with raw values
underlyingFilter := tools.GetNonPtrValue(a.filter)
st := underlyingFilter.Type()
for i := 0; i < st.NumField(); i++ {
field := st.Field(i)
dbFieldData, ok := field.Tag.Lookup("filter")
if !ok || dbFieldData == "-" {
// Skip
continue
}
tagData := strings.Split(dbFieldData, ",")
applier := &filterFieldApplier{
field: underlyingFilter.Field(i),
tagData: tagData,
statement: a.statement,
tableName: a.tableName,
dbp: a.dbp,
filter: a.filter,
}
applier.Apply()
a.statement = applier.statement
}
}
func (a *filterFieldApplier) Apply() {
a.setupFromTag()
// If field is not nil, then apply the filter
if a.fieldIsValid() {
switch a.field.Kind() {
case reflect.Ptr, reflect.Interface:
a.applyPtr()
break
case reflect.Slice:
a.applySlice()
break
default:
panic(a.field.Kind())
}
}
}
func (a *filterFieldApplier) fieldIsValid() bool {
return a.field.IsValid() && !a.field.IsNil()
}
func (a *filterFieldApplier) setupFromTag() {
a.dbFieldName = a.tagData[0]
a.isJoining = false
a.leftJoin = false
// Set up LEFT JOIN from tag
if len(a.tagData) == 4 && strings.Contains(a.tagData[1], "join") {
a.isJoining = true
if strings.Contains(a.tagData[1], "left") {
a.leftJoin = true
}
}
// Set up LEFT JOIN from filter option
if !a.leftJoin {
for _, option := range a.filter.GetSelectOptions() {
if option == yaormfilter.RequestOptions.LeftJoin {
a.leftJoin = true
}
}
}
}
func (a *filterFieldApplier) applyPtr() {
valueFilter, ok := a.field.Interface().(yaormfilter.ValueFilter)
if ok {
a.statement = valueFilter.Apply(a.statement, a.dbp.EscapeValue(a.tableName), a.dbp.EscapeValue(a.dbFieldName))
} else {
structFilter, ok := a.field.Interface().(yaormfilter.Filter)
if ok {
tableName := getTableNameFromFilter(structFilter)
if a.isJoining {
tableName = fmt.Sprintf("%s_%s", a.tableName, tableName)
} else {
panic("what the fuck man, you're not joining")
}
a.applyFilter(structFilter, tableName)
} else {
// maybe it's another kind of filter ?
panic(fmt.Errorf("I DONT KNOW HOW TO HANDLE THAT KIND OF FILTER %+v", a.field.Interface()))
}
}
}
func (a *filterFieldApplier) applySlice() {
filterSlice := reflect.ValueOf(a.field.Interface())
for idx := 0; idx < filterSlice.Len(); idx++ {
f := filterSlice.Index(idx).Interface().(yaormfilter.Filter)
tableName := getTableNameFromFilter(f)
if a.isJoining {
tableName = fmt.Sprintf("%s%d", tableName, idx)
}
a.applyFilter(f, tableName)
}
}
func (a *filterFieldApplier) applyFilter(f yaormfilter.Filter, tableName string) {
if !hasAnyFilter(f) {
return
}
if a.isJoining {
a.join(f, tableName)
}
filterApplier := &filterApplier{
statement: a.statement,
tableName: tableName,
filter: f,
dbp: a.dbp,
}
filterApplier.Apply()
a.statement = filterApplier.statement
}
func (a *filterFieldApplier) join(f yaormfilter.Filter, tableAlias string) {
joinCondition := fmt.Sprintf(
`%s as %s on %s.%s = %s.%s`,
getTableFromFilter(f).NameForQuery(a.dbp),
a.dbp.EscapeValue(tableAlias),
a.dbp.EscapeValue(tableAlias),
a.dbp.EscapeValue(a.tagData[2]),
a.dbp.EscapeValue(a.tableName),
a.dbp.EscapeValue(a.tagData[3]),
)
if !a.leftJoin {
a.statement = a.statement.Join(joinCondition)
} else {
a.statement = a.statement.LeftJoin(joinCondition)
}
}
func hasAnyFilter(f yaormfilter.Filter) bool {
valueF := tools.GetNonPtrValue(f)
if !valueF.IsValid() {
return false
}
st := valueF.Type()
for i := 0; i < st.NumField(); i++ {
val, ok := st.Field(i).Tag.Lookup("filter")
if !ok || val == "-" {
continue
}
field := valueF.Field(i)
if field.CanInterface() {
_, ok := field.Interface().(yaormfilter.ValueFilter)
if ok {
return true
}
_, ok = field.Interface().(yaormfilter.Filter)
if ok && hasAnyFilter(field.Interface().(yaormfilter.Filter)) {
return true
}
} else {
panic(field)
}
}
return false
}