Skip to content

Commit

Permalink
fix: database.WithQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
forbearing committed Mar 19, 2024
1 parent 1d75a9e commit 4df3c0d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -258,9 +259,15 @@ func (db *database[M]) WithQuery(query M, fuzzyMatch ...bool) types.Database[M]
for k, v := range q {
items := strings.Split(v, ",")
// TODO: should we skip if items length is 0?
// skip the string slice which all element is empty.
if len(strings.Join(items, "")) == 0 {
continue
}
var regexpVal string
for _, item := range items {
regexpVal = regexpVal + "|.*" + item + ".*"
// WARN: not forget to escape the regexp value using regexp.QuoteMeta.
// eg: localhost\hello.world -> localhost\\hello\.world
regexpVal = regexpVal + "|.*" + regexp.QuoteMeta(item) + ".*"
}
regexpVal = strings.TrimPrefix(regexpVal, "|")
db.db = db.db.Where(fmt.Sprintf("`%s` REGEXP ?", k), regexpVal)
Expand Down

0 comments on commit 4df3c0d

Please sign in to comment.