forked from tobgu/qframe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
118 lines (101 loc) · 2.6 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
package scolumn
import (
"github.com/tobgu/qframe/errors"
"github.com/tobgu/qframe/filter"
"github.com/tobgu/qframe/internal/index"
qfstrings "github.com/tobgu/qframe/internal/strings"
)
var filterFuncs0 = map[string]func(index.Int, Column, index.Bool) error{
filter.IsNull: isNull,
filter.IsNotNull: isNotNull,
}
var filterFuncs1 = map[string]func(index.Int, Column, string, index.Bool) error{
filter.Gt: gt,
filter.Gte: gte,
filter.Lt: lt,
filter.Lte: lte,
filter.Eq: eq,
filter.Neq: neq,
"like": like,
"ilike": ilike,
}
var multiInputFilterFuncs = map[string]func(index.Int, Column, qfstrings.StringSet, index.Bool) error{
filter.In: in,
}
var filterFuncs2 = map[string]func(index.Int, Column, Column, index.Bool) error{
filter.Gt: gt2,
filter.Gte: gte2,
filter.Lt: lt2,
filter.Lte: lte2,
filter.Eq: eq2,
filter.Neq: neq2,
}
func neq(index index.Int, s Column, comparatee string, bIndex index.Bool) error {
for i, x := range bIndex {
if !x {
s, isNull := s.stringAt(index[i])
bIndex[i] = isNull || s != comparatee
}
}
return nil
}
func like(index index.Int, s Column, comparatee string, bIndex index.Bool) error {
return regexFilter(index, s, comparatee, bIndex, true)
}
func ilike(index index.Int, s Column, comparatee string, bIndex index.Bool) error {
return regexFilter(index, s, comparatee, bIndex, false)
}
func in(index index.Int, s Column, comparatee qfstrings.StringSet, bIndex index.Bool) error {
for i, x := range bIndex {
if !x {
s, isNull := s.stringAt(index[i])
if !isNull {
bIndex[i] = comparatee.Contains(s)
}
}
}
return nil
}
func regexFilter(index index.Int, s Column, comparatee string, bIndex index.Bool, caseSensitive bool) error {
matcher, err := qfstrings.NewMatcher(comparatee, caseSensitive)
if err != nil {
return errors.Propagate("Regex filter", err)
}
for i, x := range bIndex {
if !x {
s, isNull := s.stringAt(index[i])
if !isNull {
bIndex[i] = matcher.Matches(s)
}
}
}
return nil
}
func neq2(index index.Int, col, col2 Column, bIndex index.Bool) error {
for i, x := range bIndex {
if !x {
s, isNull := col.stringAt(index[i])
s2, isNull2 := col2.stringAt(index[i])
bIndex[i] = isNull || isNull2 || s != s2
}
}
return nil
}
func isNull(index index.Int, col Column, bIndex index.Bool) error {
for i, x := range bIndex {
if !x {
_, isNull := col.stringAt(index[i])
bIndex[i] = isNull
}
}
return nil
}
func isNotNull(index index.Int, col Column, bIndex index.Bool) error {
for i, x := range bIndex {
if !x {
_, isNull := col.stringAt(index[i])
bIndex[i] = !isNull
}
}
return nil
}