-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
where.go
90 lines (75 loc) · 1.47 KB
/
where.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
package mogi
import (
"fmt"
"strings"
)
type whereCond struct {
col string
v []interface{}
}
func newWhereCond(col string, v []interface{}) whereCond {
return whereCond{
col: strings.ToLower(col),
v: unifyInterfaces(v),
}
}
func (wc whereCond) matches(in input) bool {
vals := in.where()
v, ok := vals[wc.col]
if !ok {
return false
}
// compare slices
if slice, ok := v.([]interface{}); ok {
for i, src := range slice {
if !equals(src, wc.v[i]) {
return false
}
}
return true
}
// compare single value
return equals(v, wc.v[0])
}
func (wc whereCond) priority() int {
return 1
}
func (wc whereCond) String() string {
return fmt.Sprintf("WHERE %s ≈ %v", wc.col, wc.v)
}
type whereOpCond struct {
col string
op string
v []interface{}
}
func newWhereOpCond(col string, v []interface{}, op string) whereOpCond {
return whereOpCond{
col: strings.ToLower(col),
v: unifyInterfaces(v),
op: strings.ToLower(op),
}
}
func (wc whereOpCond) matches(in input) bool {
vals := in.whereOp()
v, ok := vals[colop{wc.col, wc.op}]
if !ok {
return false
}
// compare slices
if slice, ok := v.([]interface{}); ok {
for i, src := range slice {
if !equals(src, wc.v[i]) {
return false
}
}
return true
}
// compare single value
return equals(v, wc.v[0])
}
func (wc whereOpCond) priority() int {
return 2
}
func (wc whereOpCond) String() string {
return fmt.Sprintf("WHERE %s %s %v", wc.col, strings.ToUpper(wc.op), wc.v)
}