mirror of https://github.com/mickael-menu/zk
Add --exact-match option (#30)
parent
6ba92a03b7
commit
083c0dae73
@ -0,0 +1,14 @@
|
|||||||
|
package sqlite
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// escapeLikeTerm returns the given term after escaping any LIKE-significant
|
||||||
|
// characters with the given escapeChar.
|
||||||
|
// This is meant to be used with the ESCAPE keyword:
|
||||||
|
// https://www.sqlite.org/lang_expr.html
|
||||||
|
func escapeLikeTerm(term string, escapeChar rune) string {
|
||||||
|
escape := func(term string, char string) string {
|
||||||
|
return strings.ReplaceAll(term, char, string(escapeChar)+char)
|
||||||
|
}
|
||||||
|
return escape(escape(escape(term, string(escapeChar)), "%"), "_")
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package sqlite
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mickael-menu/zk/internal/util/test/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestEscapeLikeTerm(t *testing.T) {
|
||||||
|
test := func(term string, escapeChar rune, expected string) {
|
||||||
|
assert.Equal(t, escapeLikeTerm(term, escapeChar), expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
test("foo bar", '@', "foo bar")
|
||||||
|
test("foo%bar_with@", '@', "foo@%bar@_with@@")
|
||||||
|
test(`foo%bar_with\`, '\\', `foo\%bar\_with\\`)
|
||||||
|
}
|
Loading…
Reference in New Issue