Skip to content

Commit 7583d62

Browse files
committed
[update] refactoring
1 parent d1a978b commit 7583d62

File tree

2 files changed

+86
-22
lines changed

2 files changed

+86
-22
lines changed

postgresql.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,28 @@ func (m *PostgreSQL) Mark() string {
2121

2222
func (m *PostgreSQL) IsJSON(v string) (string, bool) {
2323
//table.json:field.name:type
24-
dot := strings.Index(v, ".")
25-
if (dot == -1 && v[:5] != "json:") || v[dot+1:dot+6] != "json:" {
26-
return v, false
24+
fieldOnly := strings.HasPrefix(v, "json:")
25+
var dot int
26+
if !fieldOnly {
27+
dot = strings.Index(v, ".")
28+
if dot == -1 || !strings.HasPrefix(v[dot+1:], "json:") {
29+
return v, false
30+
}
2731
}
2832

2933
// separate table and field
3034
table := ""
3135
field := v
32-
if dot != -1 {
36+
if !fieldOnly {
3337
table = v[:dot]
3438
field = v[dot+1:]
3539
}
3640

3741
// separate field name and meta info
3842
meta := strings.Split(field, ":")
39-
name := meta[1]
40-
tp := "text"
43+
name := strings.Split(meta[1], ".")
44+
45+
var tp string
4146
if len(meta) == 3 {
4247
tp = meta[2]
4348
}
@@ -52,11 +57,9 @@ func (m *PostgreSQL) IsJSON(v string) (string, bool) {
5257
}
5358

5459
if table != "" {
55-
name = fmt.Sprintf("%s(\"%s\".\"%s\"->'%s')::%s%s", s, table, field, name, tp, e)
56-
} else {
57-
name = fmt.Sprintf("%s(\"%s\"->'%s')::%s%s", s, field, name, tp, e)
60+
return fmt.Sprintf("%s(\"%s\".\"%s\"->'%s')::%s%s", s, table, name[0], name[1], tp, e), true
5861
}
59-
return name, true
62+
return fmt.Sprintf("%s(\"%s\"->'%s')::%s%s", s, name[0], name[1], tp, e), true
6063
}
6164

6265
func (m *PostgreSQL) Contains(v string, isJSON bool) string {
@@ -77,39 +80,39 @@ func (m *PostgreSQL) NotContains(v string, isJSON bool) string {
7780
func (m *PostgreSQL) BeginsWith(v string, isJSON bool) string {
7881
var search string
7982
if isJSON {
80-
search = "'\"' || " + m.Mark() + " || '%'"
83+
search = "'\"' || " + m.Mark() + " || '%'"
8184
} else {
82-
search = m.Mark() + " || '%'"
85+
search = m.Mark() + " || '%'"
8386
}
8487
return fmt.Sprintf("%s LIKE %s", v, search)
8588
}
8689

8790
func (m *PostgreSQL) NotBeginsWith(v string, isJSON bool) string {
8891
var search string
8992
if isJSON {
90-
search = "'\"' || " + m.Mark() + " || '%'"
93+
search = "'\"' || " + m.Mark() + " || '%'"
9194
} else {
92-
search = m.Mark() + " || '%'"
95+
search = m.Mark() + " || '%'"
9396
}
9497
return fmt.Sprintf("%s NOT LIKE %s", v, search)
9598
}
9699

97100
func (m *PostgreSQL) EndsWith(v string, isJSON bool) string {
98101
var search string
99102
if isJSON {
100-
search = "'%' || " + m.Mark() + " || '\"'"
103+
search = "'%' || " + m.Mark() + " || '\"'"
101104
} else {
102-
search = "'%' || " + m.Mark() + " "
105+
search = "'%' || " + m.Mark()
103106
}
104107
return fmt.Sprintf("%s LIKE %s", v, search)
105108
}
106109

107110
func (m *PostgreSQL) NotEndsWith(v string, isJSON bool) string {
108111
var search string
109112
if isJSON {
110-
search = "'%' || " + m.Mark() + " || '\"'"
113+
search = "'%' || " + m.Mark() + " || '\"'"
111114
} else {
112-
search = "'%' || " + m.Mark() + " "
115+
search = "'%' || " + m.Mark()
113116
}
114117
return fmt.Sprintf("%s NOT LIKE %s", v, search)
115118
}

sql_test.go

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,120 +13,143 @@ var cOrC = `{ "glue":"or", "rules":[{ "field": "a", "condition":{ "type":"is nul
1313
var JSONaAndB = `{ "glue":"and", "rules":[{ "field": "json:cfg.a", "condition":{ "type":"less", "filter":1}}, { "field": "json:cfg.b", "condition":{ "type":"greater", "filter":"abc" }}]}`
1414

1515
var cases = [][]string{
16-
{`{}`, "", ""},
16+
{`{}`, "", "", ""},
1717
{
1818
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"equal", "filter":1 }}]}`,
1919
"a = ?",
20+
"a = $1",
2021
"1",
2122
},
2223
{
2324
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notEqual", "filter":1 }}]}`,
2425
"a <> ?",
26+
"a <> $1",
2527
"1",
2628
},
2729
{
2830
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"less", "filter":1 }}]}`,
2931
"a < ?",
32+
"a < $1",
3033
"1",
3134
},
3235
{
3336
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"lessOrEqual", "filter":1 }}]}`,
3437
"a <= ?",
38+
"a <= $1",
3539
"1",
3640
},
3741
{
3842
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"greater", "filter":1 }}]}`,
3943
"a > ?",
44+
"a > $1",
4045
"1",
4146
},
4247
{
4348
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"greaterOrEqual", "filter":1 }}]}`,
4449
"a >= ?",
50+
"a >= $1",
4551
"1",
4652
},
4753
{
4854
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"contains", "filter":1 }}]}`,
4955
"INSTR(a, ?) > 0",
56+
"a LIKE '%' || $1 || '%'",
5057
"1",
5158
},
5259
{
5360
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notContains", "filter":1 }}]}`,
5461
"INSTR(a, ?) = 0",
62+
"a NOT LIKE '%' || $1 || '%'",
5563
"1",
5664
},
5765
{
5866
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"beginsWith", "filter":"1" }}]}`,
5967
"a LIKE CONCAT(?, '%')",
68+
"a LIKE $1 || '%'",
6069
"1",
6170
},
6271
{
6372
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notBeginsWith", "filter":"1" }}]}`,
6473
"a NOT LIKE CONCAT(?, '%')",
74+
"a NOT LIKE $1 || '%'",
6575
"1",
6676
},
6777
{
6878
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"endsWith", "filter":"1" }}]}`,
6979
"a LIKE CONCAT('%', ?)",
80+
"a LIKE '%' || $1",
7081
"1",
7182
},
7283
{
7384
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notEndsWith", "filter":"1" }}]}`,
7485
"a NOT LIKE CONCAT('%', ?)",
86+
"a NOT LIKE '%' || $1",
7587
"1",
7688
},
7789
{
7890
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"between", "filter":{ "start":1, "end":2 } }}]}`,
7991
"( a > ? AND a < ? )",
92+
"( a > $1 AND a < $2 )",
8093
"1,2",
8194
},
8295
{
8396
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"between", "filter":{ "start":1 } }}]}`,
8497
"a > ?",
98+
"a > $1",
8599
"1",
86100
},
87101
{
88102
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"between", "filter":{ "end":2 } }}]}`,
89103
"a < ?",
104+
"a < $1",
90105
"2",
91106
},
92107
{
93108
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notBetween", "filter":{ "start":1, "end":2 } }}]}`,
94109
"( a < ? OR a > ? )",
110+
"( a < $1 OR a > $2 )",
95111
"1,2",
96112
},
97113
{
98114
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notBetween", "filter":{ "start":1 } }}]}`,
99115
"a < ?",
116+
"a < $1",
100117
"1",
101118
},
102119
{
103120
`{ "glue":"and", "rules":[{ "field": "a", "condition":{ "type":"notBetween", "filter":{ "end":2 } }}]}`,
104121
"a > ?",
122+
"a > $1",
105123
"2",
106124
},
107125
{
108126
aAndB,
109127
"( a < ? AND b > ? )",
128+
"( a < $1 AND b > $2 )",
110129
"1,abc",
111130
},
112131
{
113132
aOrB,
114133
"( a < ? OR b > ? )",
134+
"( a < $1 OR b > $2 )",
115135
"1,abc",
116136
},
117137
{
118138
`{ "glue":"AND", "rules":[` + aAndB + `,` + aOrB + `,{ "field":"c", "condition": { "type":"equal", "filter":3 } }]}`,
119139
"( ( a < ? AND b > ? ) AND ( a < ? OR b > ? ) AND c = ? )",
140+
"( ( a < $1 AND b > $2 ) AND ( a < $3 OR b > $4 ) AND c = $5 )",
120141
"1,abc,1,abc,3",
121142
},
122143
{
123144
`{ "glue":"and", "rules":[{ "field": "a", "includes":[1,2,3]}]}`,
124145
"a IN(?,?,?)",
146+
"a IN($1,$2,$3)",
125147
"1,2,3",
126148
},
127149
{
128150
`{ "glue":"and", "rules":[{ "field": "a", "includes":["a","b","c"]}]}`,
129151
"a IN(?,?,?)",
152+
"a IN($1,$2,$3)",
130153
"a,b,c",
131154
},
132155
}
@@ -234,14 +257,52 @@ func TestSQL(t *testing.T) {
234257
continue
235258
}
236259

237-
if valsStr != line[2] {
238-
t.Errorf("wrong sql generated\nj: %s\ns: %s\nr: %s", line[0], line[2], valsStr)
260+
if valsStr != line[3] {
261+
t.Errorf("wrong sql generated (values)\nj: %s\ns: %s\nr: %s", line[0], line[3], valsStr)
262+
continue
263+
}
264+
}
265+
}
266+
267+
func TestPostgre(t *testing.T) {
268+
queryConfig := SQLConfig{
269+
// Whitelist: map[string]bool{
270+
// "a": true,
271+
// "b": true,
272+
// "c": true,
273+
// }
274+
}
275+
for _, line := range cases {
276+
format, err := FromJSON([]byte(line[0]))
277+
if err != nil {
278+
t.Errorf("can't parse json\nj: %s\n%f", line[0], err)
279+
continue
280+
}
281+
282+
sql, vals, err := GetSQL(format, &queryConfig, &PostgreSQL{})
283+
if err != nil {
284+
t.Errorf("can't generate sql\nj: %s\n%f", line[0], err)
285+
continue
286+
}
287+
if sql != line[2] {
288+
t.Errorf("wrong sql generated\nj: %s\ns: %s\nr: %s", line[0], line[2], sql)
289+
continue
290+
}
291+
292+
valsStr, err := anyToStringArray(vals)
293+
if err != nil {
294+
t.Errorf("can't convert parameters\nj: %s\n%f", line[0], err)
295+
continue
296+
}
297+
298+
if valsStr != line[3] {
299+
t.Errorf("wrong sql generated\nj: %s\ns: %s\nr: %s", line[0], line[3], valsStr)
239300
continue
240301
}
241302
}
242303
}
243304

244-
func TestPSQL(t *testing.T) {
305+
func TestPostgreJSON(t *testing.T) {
245306
queryConfig := SQLConfig{
246307
// Whitelist: map[string]bool{
247308
// "a": true,

0 commit comments

Comments
 (0)