Skip to content

Commit 6c7da7c

Browse files
committed
fix search query to focus on strings only
1 parent cd99d6d commit 6c7da7c

File tree

2 files changed

+11
-52
lines changed

2 files changed

+11
-52
lines changed

things/api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ def tasks(uuid=None, include_items=False, **kwargs): # noqa: C901
9090
If the argument is `True`, only include tasks _with_ a due date.
9191
If the argument is `None` (default), then include all tasks.
9292
93-
search_query : str, bool, or None, optional
93+
search_query : str, optional
9494
The string value is passed to the SQL LIKE operator. It can thus
95-
include placeholders such as '%' and '_'.
95+
include placeholders such as '%' and '_'. Per default, it is
96+
wrapped in '% ... %'.
9697
9798
Currently titles and notes of to-dos, projects, headings, and areas
9899
are taken into account.
@@ -332,8 +333,8 @@ def search(query: str, **kwargs) -> List[Dict]:
332333
"""
333334
Search tasks in the database.
334335
335-
Currently titles and notes of to-dos, projects, headings, and areas
336-
are taken into account.
336+
Currently any part of a title and note of a to-do, project,
337+
heading, or area is matched.
337338
338339
See the `search_query` parameter of `api.tasks` for details.
339340
@@ -348,6 +349,7 @@ def search(query: str, **kwargs) -> List[Dict]:
348349
'type': 'to-do',
349350
'title': 'Book flights',
350351
...}]
352+
351353
>>> things.search('book%room')
352354
[{'uuid': 'YrOmUnEXASmpq8ch6RsyPt',
353355
'type': 'to-do',

things/database.py

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -676,59 +676,16 @@ def make_search_filter(query: str) -> str:
676676
OR TASK.notes LIKE "%dinner%"
677677
OR AREA.title LIKE "%dinner%"
678678
)'
679-
680-
>>> make_search_filter(False)
681-
'AND (
682-
TASK.title LIKE "%%"
683-
OR TASK.notes LIKE "%%"
684-
OR AREA.title LIKE "%%"
685-
)'
686-
687-
>>> make_search_filter('')
688-
'AND (
689-
TASK.title IS NULL
690-
OR TASK.notes IS NULL
691-
OR AREA.title IS NULL
692-
)'
693-
694-
>>> make_search_filter(None)
695-
''
696679
"""
697-
result = ""
680+
if not query:
681+
return ""
698682

699683
# noqa todo 'TMChecklistItem.title'
700-
sources = ["TASK.title", "TASK.notes", "AREA.title"]
701-
sub_searches = (make_sub_search_filter(source, query) for source in sources)
702-
result = " OR ".join(filter(None, sub_searches))
684+
columns = ["TASK.title", "TASK.notes", "AREA.title"]
703685

704-
if result:
705-
result = f"AND ({result})"
706-
707-
return result
686+
sub_searches = (f'{column} LIKE "%{query}%"' for column in columns)
708687

709-
710-
def make_sub_search_filter(column, query):
711-
"""
712-
Examples
713-
--------
714-
>>> make_sub_search_filter('TASK.title', 'dinner')
715-
'TASK.title LIKE "%dinner%"'
716-
717-
>>> make_sub_search_filter('TASK.title', None)
718-
''
719-
720-
>>> make_sub_search_filter('TASK.title', False)
721-
'TASK.title IS NULL'
722-
723-
>>> make_sub_search_filter('TASK.title', True)
724-
'TASK.title IS NOT NULL'
725-
"""
726-
default = f'{column} LIKE "%{query}%"'
727-
return {
728-
None: "",
729-
False: f"{column} IS NULL",
730-
True: f"{column} IS NOT NULL",
731-
}.get(query, default)
688+
return f"AND ({' OR '.join(sub_searches)})"
732689

733690

734691
def validate(parameter, argument, valid_arguments):

0 commit comments

Comments
 (0)