-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisitor.go
58 lines (49 loc) · 1.22 KB
/
visitor.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
package filter
import (
"fmt"
"strings"
)
type Visitor interface {
Visit(node Node)
}
var sqlOperators = map[uint32]string{
compareEq: "=",
compareNotEq: "<>",
compareGt: ">",
compareGte: ">=",
compareLt: "<",
compareLte: "<=",
compareIn: "IN",
compareNotIn: "NOT IN",
logicAnd: "AND",
logicOr: "OR",
logicNot: "NOT",
}
type SQLVisitor struct {
sql string
}
func (v *SQLVisitor) Visit(node Node) {
if logicNode, ok := node.(*LogicNode); ok {
v.visitLogic(logicNode)
} else {
v.visitCompare(node.(*CompareNode))
}
}
func (v *SQLVisitor) visitLogic(node *LogicNode) {
v.sql += " " + sqlOperators[node.Operator] + " "
}
func (v *SQLVisitor) visitCompare(node *CompareNode) {
param := strings.Replace(node.Field, ".", "", -1)
if node.Operator == compareIn || node.Operator == compareNotIn {
var params []string
for index := range node.Value.([]interface{}) {
params = append(params, fmt.Sprintf(":%s_%d", param, index))
}
v.sql += fmt.Sprintf("%s %s(%s)", node.Field, sqlOperators[node.Operator], strings.Join(params, ","))
} else {
v.sql += fmt.Sprintf("%s %s :%s", node.Field, sqlOperators[node.Operator], param)
}
}
func (v *SQLVisitor) Sql() string {
return v.sql
}