-
Notifications
You must be signed in to change notification settings - Fork 25
/
url_filter.go
168 lines (151 loc) · 4.89 KB
/
url_filter.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
package urlvalues
import (
"sort"
"strings"
"github.com/corestoreio/pkg/sql/ddl"
"github.com/corestoreio/pkg/sql/dml"
)
//'regexp' => "{{fieldName}} REGEXP ?",
// URLFilter is used with Query.Apply to add WHERE clauses from the URL values:
// - ?foo=bar - Where(`"foo" = 'bar'`)
// - ?foo=hello&foo=world - Where(`"foo" IN ('hello','world')`)
// - ?foo__eq=bar - Where(`"foo" = 'bar'`)
// - ?foo__neq=bar - Where(`"foo" != 'bar'`)
// - ?foo__exclude=bar - Where(`"foo" != 'bar'`)
// - ?foo__gt=42 - Where(`"foo" > 42`)
// - ?foo__gte=42 - Where(`"foo" >= 42`)
// - ?foo__from=42&foo__to=100 - Where(`"foo" >= 42` AND `foo` <= 100)
// - ?foo__lt=42 - Where(`"foo" < 42`)
// - ?foo__lte=42 - Where(`"foo" <= 42`)
// - ?foo__null= - Where(`"foo" IS NULL`)
// - ?foo__notnull= - Where(`"foo" IS NOT NULL`)
// - ?foo__lte=42&foo__sort=desc - WHERE `foo` <= 42 ORDER BY `foo` DESC
// - ?foo__lte=42&foo__sort=asc - WHERE `foo` <= 42 ORDER BY `foo` ASC
// - ?foo__like=bar - Where(`"foo" LIKE 'bar'`)
// - ?foo__nlike=bar - Where(`"foo" NOT LIKE 'bar'`)
// - ?foo__match=bar - Where(`"foo" SIMILAR TO 'bar'`)
// - ?foo__ss=bar - Where(`"foo" <=> 'bar'`) // space ship operator
type Filter struct {
Deterministic bool // if true internal map gets printed in its order, otherwise flaky tests.
Values Values
allowed map[string]struct{}
}
func NewFilter(values Values) *Filter {
return &Filter{
Values: values,
}
}
// Allow only columnName__operators are allowed
func (f *Filter) Allow(filters ...string) {
if f.allowed == nil {
f.allowed = make(map[string]struct{})
}
for _, filter := range filters {
f.allowed[filter] = struct{}{}
}
}
func (f *Filter) isAllowed(filter string) bool {
if len(f.allowed) == 0 {
return true
}
_, ok := f.allowed[filter]
return ok
}
// Filters builds the SQL conditions by appending to slice `cond` and returning it.
func (f *Filter) Filters(tbl *ddl.Table, cond dml.Conditions) (_ dml.Conditions, sortOrder []string) {
if f == nil {
return cond, sortOrder
}
if f.Deterministic {
keys := make([]string, 0, len(f.Values))
for filter := range f.Values {
keys = append(keys, filter)
}
sort.Strings(keys)
for _, filter := range keys {
cond, sortOrder = f.iterate(tbl, cond, sortOrder, filter, f.Values[filter])
}
return cond, sortOrder
}
for filter, values := range f.Values {
cond, sortOrder = f.iterate(tbl, cond, sortOrder, filter, values)
}
return cond, sortOrder
}
func (f *Filter) iterate(tbl *ddl.Table, cond dml.Conditions, sortOrder []string, filter string, values []string) (_ dml.Conditions, _sortOrder []string) {
if strings.HasSuffix(filter, "[]") {
filter = filter[:len(filter)-2]
}
if !f.isAllowed(filter) {
return cond, sortOrder
}
var operation string
if ind := strings.Index(filter, "__"); ind != -1 {
filter, operation = filter[:ind], filter[ind+2:]
}
if tbl.HasColumn(filter) {
// TODO AND or OR
cond, sortOrder = addOperator(cond, sortOrder, filter, operation, values)
}
return cond, sortOrder
}
func addOperator(b dml.Conditions, sortOrder []string, field, op string, values []string) (_ dml.Conditions, _sortOrder []string) {
switch op {
case "", "eq", "include", "in":
b = forAllValues(b, field, values, dml.Equal, dml.In)
case "exclude", "neq", "nin":
b = forAllValues(b, field, values, dml.NotEqual, dml.NotIn)
case "gt":
b = forEachValue(b, field, values, dml.Greater)
case "gte", "from":
b = forEachValue(b, field, values, dml.GreaterOrEqual)
case "lt":
b = forEachValue(b, field, values, dml.Less)
case "lte", "to":
b = forEachValue(b, field, values, dml.LessOrEqual)
case "like":
b = forEachValue(b, field, values, dml.Like)
case "nlike":
b = forEachValue(b, field, values, dml.NotLike)
case "null":
b = forEachValue(b, field, values, dml.Null)
case "notnull":
b = forEachValue(b, field, values, dml.NotNull)
case "ss":
b = forEachValue(b, field, values, dml.SpaceShip)
case "bw":
b = forAllValues(b, field, values, dml.Equal, dml.Between)
case "nbw":
b = forAllValues(b, field, values, dml.NotEqual, dml.NotBetween)
// case "match":
// b = forEachValue(b, field, values, " SIMILAR TO ")
case "sort":
var ascDesc string
if len(values) == 1 {
switch vRaw := strings.ToUpper(values[0]); vRaw {
case "":
// do nothing
case "ASC":
ascDesc = " ASC"
case "DESC":
ascDesc = " DESC"
}
}
sortOrder = append(sortOrder, field+ascDesc)
}
return b, sortOrder
}
func forEachValue(b dml.Conditions, field string, values []string, opValue dml.Op) dml.Conditions {
for _, value := range values {
b = append(b, dml.Column(field).Op(opValue).Str(value))
}
return b
}
func forAllValues(
b dml.Conditions, field string, values []string, singleOpValue, multiOpValue dml.Op,
) dml.Conditions {
if len(values) <= 1 {
return forEachValue(b, field, values, singleOpValue)
}
return append(b, dml.Column(field).Op(multiOpValue).Strs(values...))
}