Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __like and __ilike filters (#421) #954

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ Changelog
0.17
====

0.17.9
------

Added
^^^^^
- Add `__like` and `__ilike` filters. (#421)
Fixed
^^^^^
Changed
^^^^^^^

0.17.8
------

Expand Down
2 changes: 2 additions & 0 deletions docs/query.rst
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ When using ``.filter()`` method you can use number of modifiers to field names t
- ``not_isnull`` - field is not null
- ``contains`` - field contains specified substring
- ``icontains`` - case insensitive ``contains``
- ``like`` - field matches the specified pattern (may contain the SQL wildcards ``%`` and ``_``)
- ``ilike`` - case insensitive ``like``
- ``startswith`` - if field starts with value
- ``istartswith`` - case insensitive ``startswith``
- ``endswith`` - if field ends with value
Expand Down
58 changes: 58 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,64 @@ async def test_iendswith(self):
{"moo"},
)

async def test_like(self):
self.assertSetEqual(
set(await CharFields.filter(char__like="moo").values_list("char", flat=True)),
{"moo"},
)
self.assertSetEqual(
set(await CharFields.filter(char__like="%moo%").values_list("char", flat=True)),
{"moo"},
)
self.assertSetEqual(
set(await CharFields.filter(char__like="mo_").values_list("char", flat=True)),
{"moo"},
)
self.assertSetEqual(
set(await CharFields.filter(char__like="moo%_").values_list("char", flat=True)),
set(),
)
self.assertSetEqual(
set(await CharFields.filter(char__like="%o%").values_list("char", flat=True)),
{"moo", "oink"},
)

async def test_like_escapes_backslash_that_does_not_escape_sql_wildcard(self):
await CharFields.create(char="o\\ink")
await CharFields.create(char="o\\\\ink")
await CharFields.create(char="oink\\")
self.assertSetEqual(
set(await CharFields.filter(char__like=r"o\i%").values_list("char", flat=True)),
{"o\\ink"},
)
self.assertSetEqual(
set(await CharFields.filter(char__like=r"o\\i%").values_list("char", flat=True)),
{"o\\\\ink"},
)
self.assertSetEqual(
set(await CharFields.filter(char__like="%\\").values_list("char", flat=True)),
{"oink\\"},
)

async def test_like_does_not_escape_backslash_that_escapes_sql_wildcard(self):
await CharFields.create(char=r"o%nk")
await CharFields.create(char=r"o_nk")
self.assertSetEqual(
set(await CharFields.filter(char__like=r"o\_nk").values_list("char", flat=True)),
{r"o_nk"},
)
self.assertSetEqual(
set(await CharFields.filter(char__like=r"o\%nk").values_list("char", flat=True)),
{r"o%nk"},
)

async def test_ilike_is_case_insensitive(self):
await CharFields.create(char=r"OinK")
self.assertSetEqual(
set(await CharFields.filter(char__ilike=r"o%k").values_list("char", flat=True)),
{r"OinK", r"oink"},
)

async def test_sorting(self):
self.assertEqual(
await CharFields.all().order_by("char").values_list("char", flat=True),
Expand Down
27 changes: 27 additions & 0 deletions tortoise/backends/mysql/executor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from pypika import Parameter, functions
from pypika.enums import SqlTypes
from pypika.terms import Criterion
Expand All @@ -18,13 +20,15 @@
contains,
ends_with,
format_quotes,
ilike,
insensitive_contains,
insensitive_ends_with,
insensitive_exact,
insensitive_starts_with,
json_contained_by,
json_contains,
json_filter,
like,
search,
starts_with,
)
Expand All @@ -45,12 +49,33 @@ def escape_like(val: str) -> str:
return val.replace("\\", "\\\\\\\\").replace("%", "\\%").replace("_", "\\_")


def escape_like_except_wildcards(val: str) -> str:
# Replace \ with \\\\ if the backslash is not followed by % or _
return re.sub(r"\\(?![%_])", r"\\\\\\\\", val)


def mysql_contains(field: Term, value: str) -> Criterion:
return Like(
functions.Cast(field, SqlTypes.CHAR), StrWrapper(f"%{escape_like(value)}%"), escape=""
)


def mysql_like(field: Term, value: str) -> Criterion:
return Like(
functions.Cast(field, SqlTypes.CHAR),
StrWrapper(escape_like_except_wildcards(value)),
escape="",
)


def mysql_ilike(field: Term, value: str) -> Criterion:
return Like(
functions.Upper(functions.Cast(field, SqlTypes.CHAR)),
functions.Upper(StrWrapper(escape_like_except_wildcards(value))),
escape="",
)


def mysql_starts_with(field: Term, value: str) -> Criterion:
return Like(
functions.Cast(field, SqlTypes.CHAR), StrWrapper(f"{escape_like(value)}%"), escape=""
Expand Down Expand Up @@ -98,6 +123,8 @@ def mysql_search(field: Term, value: str):
class MySQLExecutor(BaseExecutor):
FILTER_FUNC_OVERRIDE = {
contains: mysql_contains,
like: mysql_like,
ilike: mysql_ilike,
starts_with: mysql_starts_with,
ends_with: mysql_ends_with,
insensitive_exact: mysql_insensitive_exact,
Expand Down
31 changes: 31 additions & 0 deletions tortoise/filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import operator
import re
from functools import partial
from typing import TYPE_CHECKING, Any, Dict, Iterable, Optional, Tuple

Expand Down Expand Up @@ -83,6 +84,11 @@ def escape_like(val: str) -> str:
return val.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")


def escape_like_except_wildcards(val: str) -> str:
# Replace \ with \\ if the backslash is not followed by % or _
return re.sub(r"\\(?![%_])", r"\\\\", val)


##############################################################################
# Encoders
# Should be type: (Any, instance: "Model", field: Field) -> type:
Expand Down Expand Up @@ -157,6 +163,19 @@ def contains(field: Term, value: str) -> Criterion:
return Like(Cast(field, SqlTypes.VARCHAR), field.wrap_constant(f"%{escape_like(value)}%"))


def like(field: Term, value: str) -> Criterion:
return Like(
Cast(field, SqlTypes.VARCHAR), field.wrap_constant(escape_like_except_wildcards(value))
)


def ilike(field: Term, value: str) -> Criterion:
return Like(
Upper(Cast(field, SqlTypes.VARCHAR)),
field.wrap_constant(Upper(escape_like_except_wildcards(value))),
)


def search(field: Term, value: str):
# will be override in each executor
pass
Expand Down Expand Up @@ -446,6 +465,18 @@ def get_filters_for_field(
"operator": between_and,
"value_encoder": list_encoder,
},
f"{field_name}__like": {
"field": actual_field_name,
"source_field": source_field,
"operator": like,
"value_encoder": string_encoder,
},
f"{field_name}__ilike": {
"field": actual_field_name,
"source_field": source_field,
"operator": ilike,
"value_encoder": string_encoder,
},
f"{field_name}__contains": {
"field": actual_field_name,
"source_field": source_field,
Expand Down