Skip to content

Commit

Permalink
Merge pull request #17 from 88labs/feature/sql-escape
Browse files Browse the repository at this point in the history
fix: エスケープ文字に'あ'などを指定するとEscapeLikeWithCharが正常に動作しない
  • Loading branch information
KamikazeZirou authored Dec 24, 2021
2 parents 3b54d2f + a3d0d90 commit 82a74d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sql-escape/escape.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sql_escape

import "strings"
import (
"strings"
"unicode/utf8"
)

// EscapeLike escapes the special characters in the SQL Like statement.
// The escape character is regarded as '\\'.
Expand All @@ -9,7 +12,12 @@ func EscapeLike(s string) string {
}

// EscapeLikeWithChar escapes the special characters in the SQL Like statement.
// Set 'c' to a character with a length of 1 as rune.
func EscapeLikeWithChar(s string, c rune) string {
if utf8.RuneLen(c) != 1 {
panic("set 'c' to a character with a length of 1 as rune.")
}

var b strings.Builder
b.Grow(2 * (len(s)))

Expand Down
2 changes: 2 additions & 0 deletions sql-escape/escape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ func TestEscapeLikeWithChar(t *testing.T) {
name: "no-escape",
args: args{
s: "t",
c: '!',
},
want: "t",
},
{
name: "empty",
args: args{
s: "",
c: '!',
},
want: "",
},
Expand Down

0 comments on commit 82a74d5

Please sign in to comment.