Skip to content

Commit a77d600

Browse files
author
timsolov
committed
added two comparison methods: NLIKE, NILIKE
1 parent 68fde18 commit a77d600

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

filter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func (f *Filter) Where() (string, error) {
201201
var exp string
202202

203203
switch f.Method {
204-
case EQ, NE, GT, LT, GTE, LTE, LIKE, ILIKE:
204+
case EQ, NE, GT, LT, GTE, LTE, LIKE, ILIKE, NLIKE, NILIKE:
205205
exp = fmt.Sprintf("%s %s ?", f.Name, translateMethods[f.Method])
206206
return exp, nil
207207
case IS, NOT:
@@ -234,7 +234,7 @@ func (f *Filter) Args() ([]interface{}, error) {
234234
return args, nil
235235
}
236236
return nil, ErrUnknownMethod
237-
case LIKE, ILIKE:
237+
case LIKE, ILIKE, NLIKE, NILIKE:
238238
value := f.Value.(string)
239239
if len(value) >= 2 && strings.HasPrefix(value, "*") {
240240
value = "%" + value[1:]
@@ -302,7 +302,7 @@ func (f *Filter) setBool(list []string) error {
302302
func (f *Filter) setString(list []string) error {
303303
if len(list) == 1 {
304304
switch f.Method {
305-
case EQ, NE, GT, LT, GTE, LTE, LIKE, ILIKE, IN:
305+
case EQ, NE, GT, LT, GTE, LTE, LIKE, ILIKE, NLIKE, NILIKE, IN:
306306
f.Value = list[0]
307307
return nil
308308
case IS, NOT:

main.go

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,39 @@ type Method string
3232

3333
// Compare methods:
3434
var (
35-
EQ Method = "EQ"
36-
NE Method = "NE"
37-
GT Method = "GT"
38-
LT Method = "LT"
39-
GTE Method = "GTE"
40-
LTE Method = "LTE"
41-
LIKE Method = "LIKE"
42-
ILIKE Method = "ILIKE"
43-
IS Method = "IS"
44-
NOT Method = "NOT"
45-
IN Method = "IN"
35+
EQ Method = "EQ"
36+
NE Method = "NE"
37+
GT Method = "GT"
38+
LT Method = "LT"
39+
GTE Method = "GTE"
40+
LTE Method = "LTE"
41+
LIKE Method = "LIKE"
42+
ILIKE Method = "ILIKE"
43+
NLIKE Method = "NLIKE"
44+
NILIKE Method = "NILIKE"
45+
IS Method = "IS"
46+
NOT Method = "NOT"
47+
IN Method = "IN"
4648
)
4749

4850
// NULL constant
4951
const NULL = "NULL"
5052

5153
var (
5254
translateMethods map[Method]string = map[Method]string{
53-
EQ: "=",
54-
NE: "!=",
55-
GT: ">",
56-
LT: "<",
57-
GTE: ">=",
58-
LTE: "<=",
59-
LIKE: "LIKE",
60-
ILIKE: "ILIKE",
61-
IS: "IS",
62-
NOT: "IS NOT",
63-
IN: "IN",
55+
EQ: "=",
56+
NE: "!=",
57+
GT: ">",
58+
LT: "<",
59+
GTE: ">=",
60+
LTE: "<=",
61+
LIKE: "LIKE",
62+
ILIKE: "ILIKE",
63+
NLIKE: "NOT LIKE",
64+
NILIKE: "NOT ILIKE",
65+
IS: "IS",
66+
NOT: "IS NOT",
67+
IN: "IN",
6468
}
6569
)
6670

main_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ func TestWhere(t *testing.T) {
220220
{url: "?id=100", err: "id: can't be greater then 10"},
221221
{url: "?id[in]=100,200", err: "id[in]: can't be greater then 10"},
222222

223+
// not like, not ilike:
224+
{url: "?u[nlike]=superman", expected: " WHERE u NOT LIKE ?"},
225+
{url: "?u[nilike]=superman", expected: " WHERE u NOT ILIKE ?"},
226+
223227
{url: "?id=1&name=superman", expected: " WHERE id = ?", ignore: true},
224228
{url: "?id=1&name=superman&s[like]=super", expected: " WHERE id = ? AND s LIKE ?", expected2: " WHERE s LIKE ? AND id = ?", ignore: true},
225229
{url: "?s=super", expected: " WHERE s = ?"},
@@ -228,11 +232,12 @@ func TestWhere(t *testing.T) {
228232
{url: "?s=puper", expected: "", err: "s: puper: not in scope"},
229233
{url: "?u=puper", expected: " WHERE u = ?"},
230234
{url: "?u[eq]=1,2", expected: "", err: "u[eq]: method are not allowed"},
231-
{url: "?u[gt]=1", expected: "", err: "u[gt]: method are not allowed"},
235+
{url: "?u[gt]=1", expected: " WHERE u > ?"},
232236
{url: "?id[in]=1,2", expected: " WHERE id IN (?, ?)"},
233237
{url: "?id[eq]=1&id[eq]=4", expected: " WHERE id = ? AND id = ?"},
234238
{url: "?id[gte]=1&id[lte]=4", expected: " WHERE id >= ? AND id <= ?", expected2: " WHERE id <= ? AND id >= ?"},
235239
{url: "?id[gte]=1|id[lte]=4", expected: " WHERE (id >= ? OR id <= ?)", expected2: " WHERE (id <= ? OR id >= ?)"},
240+
// null:
236241
{url: "?u[not]=NULL", expected: " WHERE u IS NOT NULL"},
237242
{url: "?u[is]=NULL", expected: " WHERE u IS NULL"},
238243
// bool:

0 commit comments

Comments
 (0)