-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwhere.go
More file actions
71 lines (65 loc) · 1.62 KB
/
where.go
File metadata and controls
71 lines (65 loc) · 1.62 KB
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
package bartlett
import (
"encoding/csv"
"fmt"
"strings"
)
func parseSimpleWhereCond(rawCond string) (cond, val string) {
parts := strings.Split(rawCond, `.`)
if parts[0] == `not` {
cond = fmt.Sprintf(`%s.%s`, parts[0], parts[1])
} else {
cond = parts[0]
}
val = strings.Replace(rawCond, cond+`.`, ``, 1)
return cond, val
}
func rectifyArg(cond, val string) (string, string) {
if strings.Contains(cond, `LIKE ?`) {
val = strings.Replace(val, `*`, `%`, -1)
}
return cond, val
}
func urlToWhereCond(column, condition string) string {
switch condition {
case `eq`:
return fmt.Sprintf(`%s = ?`, column)
case `not.eq`:
return fmt.Sprintf(`%s != ?`, column)
case `neq`:
return fmt.Sprintf(`%s != ?`, column)
case `not.neq`:
return fmt.Sprintf(`%s = ?`, column)
case `gt`:
return fmt.Sprintf(`%s > ?`, column)
case `not.gt`:
return fmt.Sprintf(`%s <= ?`, column)
case `gte`:
return fmt.Sprintf(`%s >= ?`, column)
case `not.gte`:
return fmt.Sprintf(`%s < ?`, column)
case `lt`:
return fmt.Sprintf(`%s < ?`, column)
case `not.lt`:
return fmt.Sprintf(`%s >= ?`, column)
case `lte`:
return fmt.Sprintf(`%s <= ?`, column)
case `not.lte`:
return fmt.Sprintf(`%s > ?`, column)
case `like`:
return fmt.Sprintf(`%s LIKE ?`, column)
case `not.like`:
return fmt.Sprintf(`%s NOT LIKE ?`, column)
case `is`:
return fmt.Sprintf(`%s IS ?`, column)
case `not.is`:
return fmt.Sprintf(`%s IS NOT ?`, column)
default:
return ``
}
}
func whereIn(rawVal string) []string {
r := csv.NewReader(strings.NewReader(strings.TrimPrefix(strings.TrimSuffix(rawVal, `)`), `(`)))
vals, _ := r.Read()
return vals
}