Skip to content

Commit ddd19a9

Browse files
committed
[update] json syntax, extra rules
1 parent 6eea0b4 commit ddd19a9

File tree

2 files changed

+55
-8
lines changed

2 files changed

+55
-8
lines changed

sql.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ type Filter struct {
1515
}
1616

1717
type Condition struct {
18-
Rule string `json:"rule"`
19-
Value interface{} `json:"value"`
18+
Rule string `json:"type"`
19+
Value interface{} `json:"filter"`
2020
}
2121

2222
type CustomOperation func(string, Condition) (string, []interface{}, error)
@@ -59,13 +59,24 @@ func GetSQL(data Filter, config *SQLConfig) (string, []interface{}, error) {
5959
return "", NoValues, nil
6060
case "equal":
6161
return fmt.Sprintf("%s = ?", data.Field), []interface{}{data.Condition.Value}, nil
62+
case "notEqual":
63+
return fmt.Sprintf("%s <> ?", data.Field), []interface{}{data.Condition.Value}, nil
64+
case "contains":
65+
return fmt.Sprintf("INSTR(%s, ?) > 0", data.Field), []interface{}{data.Condition.Value}, nil
66+
case "notContains":
67+
return fmt.Sprintf("INSTR(%s, ?) < 0", data.Field), []interface{}{data.Condition.Value}, nil
68+
case "lessOrEqual":
69+
return fmt.Sprintf("%s <= ?", data.Field), []interface{}{data.Condition.Value}, nil
70+
case "greaterOrEqual":
71+
return fmt.Sprintf( "%s >= ?", data.Field), []interface{}{data.Condition.Value}, nil
6272
case "less":
6373
return fmt.Sprintf("%s < ?", data.Field), []interface{}{data.Condition.Value}, nil
6474
case "greater":
6575
return fmt.Sprintf("%s > ?", data.Field), []interface{}{data.Condition.Value}, nil
76+
6677
}
6778

68-
if config.Operations != nil {
79+
if config != nil && config.Operations != nil {
6980
op, opOk := config.Operations[data.Condition.Rule]
7081
if opOk {
7182
return op(data.Field, data.Condition)

sql_test.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,52 @@ import (
77
"testing"
88
)
99

10-
var aAndB = `{ "glue":"and", "rules":[{ "field": "a", "condition":{ "rule":"less", "value":1}}, { "field": "b", "condition":{ "rule":"greater", "value":"abc" }}]}`
11-
var aOrB = `{ "glue":"or", "rules":[{ "field": "a", "condition":{ "rule":"less", "value":1}}, { "field": "b", "condition":{ "rule":"greater", "value":"abc" }}]}`
12-
var cOrC = `{ "glue":"or", "rules":[{ "field": "a", "condition":{ "rule":"is null" }}, { "field": "b", "condition":{ "rule":"range100", "value":500 }}]}`
10+
var aAndB = `{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"less", "filter":1}}, { "field": "b", "condition":{ "type":"greater", "filter":"abc" }}]}`
11+
var aOrB = `{ "glue":"or", "rules":[{ "field": "a", "condition":{ "type":"less", "filter":1}}, { "field": "b", "condition":{ "type":"greater", "filter":"abc" }}]}`
12+
var cOrC = `{ "glue":"or", "rules":[{ "field": "a", "condition":{ "type":"is null" }}, { "field": "b", "condition":{ "type":"range100", "filter":500 }}]}`
1313

1414
var cases = [][]string{
1515
[]string{`{}`, "", ""},
1616
[]string{
17-
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "rule":"equal", "value":1 }}]}`,
17+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"equal", "filter":1 }}]}`,
1818
"a = ?",
1919
"1",
2020
},
21+
[]string{
22+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notEqual", "filter":1 }}]}`,
23+
"a <> ?",
24+
"1",
25+
},
26+
[]string{
27+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"less", "filter":1 }}]}`,
28+
"a < ?",
29+
"1",
30+
},
31+
[]string{
32+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"lessOrEqual", "filter":1 }}]}`,
33+
"a <= ?",
34+
"1",
35+
},
36+
[]string{
37+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"greater", "filter":1 }}]}`,
38+
"a > ?",
39+
"1",
40+
},
41+
[]string{
42+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"greaterOrEqual", "filter":1 }}]}`,
43+
"a >= ?",
44+
"1",
45+
},
46+
[]string{
47+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"contains", "filter":1 }}]}`,
48+
"INSTR(a, ?) > 0",
49+
"1",
50+
},
51+
[]string{
52+
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notContains", "filter":1 }}]}`,
53+
"INSTR(a, ?) < 0",
54+
"1",
55+
},
2156
[]string{
2257
aAndB,
2358
"( a < ? AND b > ? )",
@@ -29,7 +64,7 @@ var cases = [][]string{
2964
"1,abc",
3065
},
3166
[]string{
32-
`{ "glue":"AND", "rules":[` + aAndB + `,` + aOrB + `,{ "field":"c", "condition": { "rule":"equal", "value":3 } }]}`,
67+
`{ "glue":"AND", "rules":[` + aAndB + `,` + aOrB + `,{ "field":"c", "condition": { "type":"equal", "filter":3 } }]}`,
3368
"( ( a < ? AND b > ? ) AND ( a < ? OR b > ? ) AND c = ? )",
3469
"1,abc,1,abc,3",
3570
},
@@ -45,6 +80,7 @@ var cases = [][]string{
4580
},
4681
}
4782

83+
4884
func anyToStringArray(some []interface{}) (string, error) {
4985
out := make([]string, 0, len(some))
5086
for _, x := range some {

0 commit comments

Comments
 (0)