forked from xbsoftware/querysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.go
114 lines (94 loc) · 2.92 KB
/
sql.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
package querysql
import (
"encoding/json"
"fmt"
"strings"
)
type Filter struct {
Glue string `json:"glue"`
Field string `json:"field"`
Condition Condition `json:"condition"`
Includes []interface{} `json:"includes"`
Kids []Filter `json:"rules"`
}
type Condition struct {
Rule string `json:"type"`
Value interface{} `json:"filter"`
}
type CustomOperation func(string, Condition) (string, []interface{}, error)
type SQLConfig struct {
Whitelist map[string]bool
Operations map[string]CustomOperation
}
func FromJSON(text []byte) (Filter, error) {
f := Filter{}
err := json.Unmarshal(text, &f)
return f, err
}
var NoValues = make([]interface{}, 0)
func inSQL(field string, data []interface{}) (string, []interface{}, error) {
marks := make([]string, len(data))
for i := range marks {
marks[i] = "?"
}
sql := fmt.Sprintf("%s IN(%s)", field, strings.Join(marks, ","))
return sql, data, nil
}
func GetSQL(data Filter, config *SQLConfig) (string, []interface{}, error) {
if data.Kids == nil {
if config != nil && config.Whitelist != nil && !config.Whitelist[data.Field] {
return "", nil, fmt.Errorf("field name is not in whitelist: %s", data.Field)
}
if len(data.Includes) > 0 {
return inSQL(data.Field, data.Includes)
}
switch data.Condition.Rule {
case "":
return "", NoValues, nil
case "equal":
return fmt.Sprintf("%s = ?", data.Field), []interface{}{data.Condition.Value}, nil
case "notEqual":
return fmt.Sprintf("%s <> ?", data.Field), []interface{}{data.Condition.Value}, nil
case "contains":
return fmt.Sprintf("INSTR(%s, ?) > 0", data.Field), []interface{}{data.Condition.Value}, nil
case "notContains":
return fmt.Sprintf("INSTR(%s, ?) < 0", data.Field), []interface{}{data.Condition.Value}, nil
case "lessOrEqual":
return fmt.Sprintf("%s <= ?", data.Field), []interface{}{data.Condition.Value}, nil
case "greaterOrEqual":
return fmt.Sprintf( "%s >= ?", data.Field), []interface{}{data.Condition.Value}, nil
case "less":
return fmt.Sprintf("%s < ?", data.Field), []interface{}{data.Condition.Value}, nil
case "greater":
return fmt.Sprintf("%s > ?", data.Field), []interface{}{data.Condition.Value}, nil
}
if config != nil && config.Operations != nil {
op, opOk := config.Operations[data.Condition.Rule]
if opOk {
return op(data.Field, data.Condition)
}
}
return "", NoValues, fmt.Errorf("unknown operation: %s", data.Condition.Rule)
}
out := make([]string, 0, len(data.Kids))
values := make([]interface{}, 0)
for _, r := range data.Kids {
subSql, subValues, err := GetSQL(r, config)
if err != nil {
return "", nil, err
}
out = append(out, subSql)
values = append(values, subValues...)
}
var glue string
if data.Glue == "or" {
glue = " OR "
} else {
glue = " AND "
}
outStr := strings.Join(out, glue)
if len(data.Kids) > 1 {
outStr = "( " + outStr + " )"
}
return outStr, values, nil
}