Skip to content

fix: match LIKE wildcards in search, $ilike and $sw literally - #1176

Open
ravitejas-tech wants to merge 1 commit into
ppetzold:masterfrom
ravitejas-tech:fix/escape-like-wildcards
Open

ravitejas-tech wants to merge 1 commit into
ppetzold:masterfrom
ravitejas-tech:fix/escape-like-wildcards

Conversation

@ravitejas-tech

Copy link
Copy Markdown

Fixes #1175

search, $ilike and $sw put the term into a LIKE pattern as it was, so % and _ acted as wildcards: searching for % matched every row, and $sw:a_ matched axb.

The term is now escaped (%, _ and the escape character) and the LIKE condition gets an ESCAPE '!' clause, also inside NOT(...) for $not:$ilike. I used ! rather than \ because it needs no quoting in SQL string literals, so the same clause works on Postgres, MySQL/MariaDB and SQLite, which has no default LIKE escape character. A backslash would need '\\' on MySQL/MariaDB and '\' everywhere else.

This changes behaviour for anyone who passed % or _ through these operators on purpose: they now match literally.

The new tests add a row named 50%_off! and cover search (default and multiWordSearch), $ilike, $sw and $not:$ilike. The full suite passes locally on Postgres, MariaDB and SQLite.

The search term and the $ilike and $sw filter values were put into LIKE
patterns as they were, so `%` and `_` acted as wildcards: searching for
`%` matched every row and `$sw:a_` matched `axb`.

Escape `%`, `_` and the escape character in the term and add an
ESCAPE '!' clause to the condition. `!` needs no quoting in SQL string
literals, so the same clause works on Postgres, MySQL/MariaDB and
SQLite, which has no default LIKE escape character.

Fixes ppetzold#1175
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$ilike, $sw, and search do not escape LIKE wildcards (% and _) in the query term

1 participant