-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilters.go
310 lines (263 loc) · 9.76 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
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
package scope
import (
"context"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop/v5"
"github.com/pkg/errors"
"github.com/alphaflow/scope/util"
)
const FailQuery = "1=0"
/*************** Generic Filtering and Sorting ****************/
// Filter operands that can be used within ForFiltersFromParams
var filterTypes = map[string]string{
"EQ": "=",
"NE": "!=",
"LT": "<",
"GT": ">",
"LTE": "<=",
"GTE": ">=",
"NU": "is null",
"NN": "is not null",
"LK": "like",
"ILK": "ilike",
"NLK": "not like",
"NILK": "not ilike",
"DF": "is distinct from",
"NDF": "is not distinct from",
"IN": "in",
"NIN": "not in",
}
// Filter logics that can be used within ForFiltersFromParams
var filterLogics = map[string]string{
"AND": "AND",
"OR": "OR",
}
// Sort directions that can be used within ForSortFromParams
var sortDirections = map[string]string{
"ASC": "ASC",
"DESC": "DESC",
}
// ForFiltersFromParams filters a model based on the provided filter params.
func ForFiltersFromParams(ctx context.Context, model interface{}, params buffalo.ParamValues) (pop.ScopeFunc, error) {
v := reflect.ValueOf(model)
if v.Kind() != reflect.Struct {
return nil, errors.New("struct expected")
}
modelPtr := reflect.New(reflect.TypeOf(model)).Interface()
filterSeparator := getFilterSeparator(params)
filterArgsSeparator := getFilterArgsSeparator(params)
columns := make([]string, 0)
if !util.IsBlank(params.Get("filter_columns")) {
columns = strings.Split(params.Get("filter_columns"), filterSeparator)
}
types := make([]string, 0)
if !util.IsBlank(params.Get("filter_types")) {
types = strings.Split(params.Get("filter_types"), filterSeparator)
}
leftParens := make([]string, 0)
if !util.IsBlank(params.Get("filter_left_parens")) {
leftParens = strings.Split(params.Get("filter_left_parens"), filterSeparator)
}
rightParens := make([]string, 0)
if !util.IsBlank(params.Get("filter_right_parens")) {
rightParens = strings.Split(params.Get("filter_right_parens"), filterSeparator)
}
logic := make([]string, 0)
if !util.IsBlank(params.Get("filter_logic")) {
logic = strings.Split(params.Get("filter_logic"), filterSeparator)
}
// filter_values can be empty and still be valid (for example, X = "")
values := strings.Split(params.Get("filter_values"), filterSeparator)
// If nothing is specified, this is a no-op.
if len(columns) == 0 && len(types) == 0 && len(values) == 1 && len(logic) == 0 && len(leftParens) == 0 && len(rightParens) == 0 {
return func(q *pop.Query) *pop.Query {
return q
}, nil
}
if len(columns) != len(types) || len(columns) != len(values) || len(columns) != len(logic)+1 || len(leftParens) != len(rightParens) {
// We must have the same number of all filtering params. We must have 1 more column than logical operators.
return nil, errors.New("missing or mismatched filter parameters")
}
// Check for custom filter fields, and handle appropriately.
columnMap := make(map[string]string, 0)
filterColumns, err := GetAllFilterColumns(ctx, modelPtr)
if err != nil {
return nil, err
}
for _, column := range filterColumns {
columnMap[column.Name] = column.Statement
}
clauses := make([]string, len(columns))
args := make([]interface{}, 0)
argsPerClause := make([]int, len(columns))
for i, col := range columns {
// Find the correct operator for this filter.
op, ok := filterTypes[strings.ToUpper(types[i])]
if !ok {
return nil, errors.Errorf("invalid filter type: %v", types[i])
}
// If this column is filterable, build this clause.
if stmt, ok := columnMap[col]; ok {
clauses[i] = fmt.Sprintf("%s %s", stmt, op)
} else {
return nil, errors.Errorf("invalid filter field: %v", col)
}
// Add the arg to the list if this operator takes args.
if filterOperatorHasOneArg(types[i]) {
args = append(args, values[i])
argsPerClause[i] = 1
} else if filterOperatorHasArgs(types[i]) {
if util.IsBlank(values[i]) {
argsPerClause[i] = 0
} else {
separatedValues := strings.Split(values[i], filterArgsSeparator)
for _, arg := range separatedValues {
args = append(args, arg)
}
argsPerClause[i] = len(separatedValues)
}
}
}
// Convert parenthesis into appropriate strings.
leftParenIndicies := make(map[int]string, 0)
rightParenIndicies := make(map[int]string, 0)
for _, i := range leftParens {
index, err := strconv.Atoi(i)
if index > len(columns)-1 || err != nil {
return nil, errors.Errorf("invalid filter parentheses: %v", i)
}
leftParenIndicies[index] = leftParenIndicies[index] + "("
}
for _, i := range rightParens {
index, err := strconv.Atoi(i)
if index > len(columns)-1 || err != nil {
return nil, errors.Errorf("invalid filter parentheses: %v", i)
}
rightParenIndicies[index] = rightParenIndicies[index] + ")"
}
// Apply Logic, starting with the first clause.
queryString := buildFilterClause(clauses[0], types[0], argsPerClause[0], leftParenIndicies[0], rightParenIndicies[0])
for i, l := range logic {
logic, ok := filterLogics[strings.ToUpper(l)]
if !ok {
return nil, errors.Errorf("invalid filter logic: %v", logic[i])
}
clauseWithArgs := buildFilterClause(clauses[i+1], types[i+1], argsPerClause[i+1], leftParenIndicies[i+1], rightParenIndicies[i+1])
queryString = fmt.Sprintf("%s %s %s", queryString, logic, clauseWithArgs)
}
// Wrap our query string in parens, so its always evaluated as 1 expression and cannot conflict with other scopes.
queryString = fmt.Sprintf("(%s)", queryString)
return func(q *pop.Query) *pop.Query {
return q.Where(queryString, args...)
}, nil
}
func buildFilterClause(clause, operator string, argsPerClause int, leftParen, rightParen string) string {
if filterOperatorHasNoArgs(operator) {
return fmt.Sprintf("%s%s%s", leftParen, clause, rightParen)
} else if filterOperatorHasOneArg(operator) {
return fmt.Sprintf("%s%s ?%s", leftParen, clause, rightParen)
}
if argsPerClause == 0 {
return fmt.Sprintf("%s%s%s", leftParen, FailQuery, rightParen)
}
// We add an extra space to the last argument in the query, to circumvent https://github.com/gobuffalo/pop/issues/610
// Luckily, the pop code only replaces the exact string "(?)", so by returning "( ?)" our args are supplied properly.
return fmt.Sprintf("%s%s (%v ?)%s", leftParen, clause, strings.Repeat("?, ", argsPerClause-1), rightParen)
}
func filterOperatorHasNoArgs(operator string) bool {
return strings.ToUpper(operator) == "NN" || strings.ToUpper(operator) == "NU"
}
func filterOperatorHasOneArg(operator string) bool {
return !(filterOperatorHasNoArgs(operator) || filterOperatorHasArgs(operator))
}
func filterOperatorHasArgs(operator string) bool {
return strings.ToUpper(operator) == "IN" || strings.ToUpper(operator) == "NIN"
}
// ForOrder is a generic scope function for ordering.
func ForOrder(orderClauses ...string) pop.ScopeFunc {
return func(q *pop.Query) *pop.Query {
for _, clause := range orderClauses {
q.Order(clause)
}
return q
}
}
// ForSortFromParams orders a query based on the provided query params.
func ForSortFromParams(ctx context.Context, model interface{}, params buffalo.ParamValues) (pop.ScopeFunc, error) {
v := reflect.ValueOf(model)
if v.Kind() != reflect.Struct {
return nil, errors.New("struct expected")
}
modelPtr := reflect.New(reflect.TypeOf(model)).Interface()
filterSeparator := getFilterSeparator(params)
columns := make([]string, 0)
if !util.IsBlank(params.Get("sort_columns")) {
columns = strings.Split(params.Get("sort_columns"), filterSeparator)
}
directions := make([]string, 0)
if !util.IsBlank(params.Get("sort_directions")) {
directions = strings.Split(params.Get("sort_directions"), filterSeparator)
}
// If nothing is specified, this is a no-op.
if len(columns) == 0 && len(directions) == 0 {
return func(q *pop.Query) *pop.Query {
return q
}, nil
}
if len(columns) != len(directions) {
// We must have the same number of all sorting params.
return nil, errors.New("missing or mismatched sort parameters")
}
// Check for custom sort fields, and handle appropriately.
columnMap := make(map[string]string, 0)
sortColumns, err := GetAllSortColumns(ctx, modelPtr)
if err != nil {
return nil, err
}
for _, column := range sortColumns {
columnMap[column.Name] = column.Statement
}
clauses := make([]string, len(columns))
for i, col := range columns {
// Find the correct operator for this filter.
op, ok := sortDirections[strings.ToUpper(directions[i])]
if !ok {
return nil, errors.New(fmt.Sprintf("invalid sort direction: %v", directions[i]))
}
// If this column is sortable, build this clause.
if stmt, ok := columnMap[col]; ok {
clauses[i] = fmt.Sprintf("%s %s", stmt, op)
} else {
return nil, errors.Errorf("invalid sort field: %v", col)
}
}
return ForOrder(clauses...), nil
}
// ForPaginateFromParams paginates a query based on a list of parameters, generally c.Params()
func ForPaginateFromParams(params buffalo.ParamValues) pop.ScopeFunc {
return func(q *pop.Query) *pop.Query {
return q.PaginateFromParams(params)
}
}
// getFilterSeparator gets the filterSeparator token. The parameter filter_separator can be used to separate the filter
// columns, etc, if | is not suitable.
func getFilterSeparator(params buffalo.ParamValues) string {
filterSeparator := "|"
if !util.IsBlank(params.Get("filter_separator")) {
filterSeparator = params.Get("filter_separator")
}
return filterSeparator
}
// getFilterArgsSeparator gets the filterSeparator token. The parameter filter_args_separator can be used to separate
// the filter args for operators with many args, if , is not suitable.
func getFilterArgsSeparator(params buffalo.ParamValues) string {
filterArgsSeparator := ","
if !util.IsBlank(params.Get("filter_args_separator")) {
filterArgsSeparator = params.Get("filter_args_separator")
}
return filterArgsSeparator
}