-
Notifications
You must be signed in to change notification settings - Fork 0
/
ILike.go
67 lines (55 loc) · 1.94 KB
/
ILike.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
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package dfl
// ILike is a BinaryOperator that evaluates the SQL standard like expression.
// It is similar to the Like BinaryOperator but is case insensitive.
// The parameters must be of type string.
// The right parameter may have "%" characters that are interpreted as (.*) in a regular expression test.
import (
"fmt"
"regexp"
"strings"
"github.com/pkg/errors"
)
type ILike struct {
*BinaryOperator
}
func (i ILike) Dfl(quotes []string, pretty bool, tabs int) string {
return i.BinaryOperator.Dfl("ilike", quotes, pretty, tabs)
}
// Sql returns the SQL representation of this node as a string
func (i ILike) Sql(pretty bool, tabs int) string {
return i.BinaryOperator.Sql("ILIKE", pretty, tabs)
}
func (i ILike) Map() map[string]interface{} {
return i.BinaryOperator.Map("ilike", i.Left, i.Right)
}
func (i ILike) Compile() Node {
left := i.Left.Compile()
right := i.Right.Compile()
return ILike{&BinaryOperator{Left: left, Right: right}}
}
func (i ILike) Evaluate(vars map[string]interface{}, ctx interface{}, funcs FunctionMap, quotes []string) (map[string]interface{}, interface{}, error) {
vars, lv, err := i.Left.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, false, err
}
lvs := strings.ToLower(fmt.Sprint(lv))
vars, rv, err := i.Right.Evaluate(vars, ctx, funcs, quotes)
if err != nil {
return vars, false, err
}
rvs := strings.ToLower(fmt.Sprint(rv))
if len(rvs) == 0 {
return vars, len(lvs) == 0, nil
}
pattern, err := regexp.Compile("^" + strings.Replace(rvs, "%", ".*", -1) + "$")
if err != nil {
return vars, false, errors.Wrap(err, ErrorEvaluate{Node: i, Quotes: quotes}.Error())
}
return vars, pattern.MatchString(lvs), nil
}