diff options
Diffstat (limited to 'internal/db/bundb/util.go')
-rw-r--r-- | internal/db/bundb/util.go | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/internal/db/bundb/util.go b/internal/db/bundb/util.go index bdd45d1e7..3c3249daf 100644 --- a/internal/db/bundb/util.go +++ b/internal/db/bundb/util.go @@ -34,9 +34,10 @@ var likeEscaper = strings.NewReplacer( `_`, `\_`, // Exactly one char. ) -// whereSubqueryLike appends a WHERE clause to the -// given SelectQuery, which searches for matches -// of `search` in the given subQuery using LIKE. +// whereLike appends a WHERE clause to the +// given SelectQuery, which searches for +// matches of `search` in the given subQuery +// using LIKE. func whereLike( query *bun.SelectQuery, subject interface{}, @@ -58,6 +59,30 @@ func whereLike( ) } +// whereStartsLike is like whereLike, +// but only searches for strings that +// START WITH `search`. +func whereStartsLike( + query *bun.SelectQuery, + subject interface{}, + search string, +) *bun.SelectQuery { + // Escape existing wildcard + escape + // chars in the search query string. + search = likeEscaper.Replace(search) + + // Add our own wildcards back in; search + // zero or more chars after the query. + search += `%` + + // Append resulting WHERE + // clause to the main query. + return query.Where( + "(?) LIKE ? ESCAPE ?", + subject, search, `\`, + ) +} + // updateWhere parses []db.Where and adds it to the given update query. func updateWhere(q *bun.UpdateQuery, where []db.Where) { for _, w := range where { |