From 5f9498babc185929623250756caa75d5622b1a3b Mon Sep 17 00:00:00 2001 From: Dan Hansen Date: Sat, 9 Mar 2024 06:53:03 -0800 Subject: [PATCH] [String] `LIKE` properly escapes regexp-characters (#156) * [String] `LIKE` properly escapes regexp-characters * remove unused import --- internal/function.go | 2 +- query_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/function.go b/internal/function.go index e936c28..34306ef 100644 --- a/internal/function.go +++ b/internal/function.go @@ -214,7 +214,7 @@ func LIKE(a, b Value) (Value, error) { if err != nil { return nil, err } - wildcard := strings.Replace(vb, "%", ".*", -1) + wildcard := strings.Replace(regexp.QuoteMeta(vb), "%", ".*", -1) matchLimits := fmt.Sprintf("^%s$", wildcard) re, err := regexp.Compile(matchLimits) if err != nil { diff --git a/query_test.go b/query_test.go index 3a39a5a..d0bdf18 100644 --- a/query_test.go +++ b/query_test.go @@ -158,6 +158,11 @@ func TestQuery(t *testing.T) { query: `SELECT "abcd" LIKE "a%d"`, expectedRows: [][]interface{}{{true}}, }, + { + name: "like operator - special regex characters", + query: `SELECT 'my*string' LIKE '%%*%%', 'my*string' LIKE '%%([][%';`, + expectedRows: [][]interface{}{{true, false}}, + }, { name: "like operator2", query: `SELECT "abcd" LIKE "%a%"`,