From 433084b0e53aa037db0f169767a5e705be3b6783 Mon Sep 17 00:00:00 2001 From: Diogo Date: Tue, 21 Feb 2023 12:30:32 +0100 Subject: [PATCH 1/8] Add `PlainToTsQuery` postgres function --- tortoise/contrib/postgres/functions.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tortoise/contrib/postgres/functions.py b/tortoise/contrib/postgres/functions.py index 5f6b6b361..5e2953cbd 100644 --- a/tortoise/contrib/postgres/functions.py +++ b/tortoise/contrib/postgres/functions.py @@ -19,6 +19,15 @@ def __init__(self, field: Term): super(ToTsQuery, self).__init__("TO_TSQUERY", field) +class PlainToTsQuery(Function): # type: ignore + """ + plainto_tsquery function + """ + + def __init__(self, field: Term): + super(PlainToTsQuery, self).__init__("PLAINTO_TSQUERY", field) + + class Random(Function): # type: ignore """ Generate random number. From b7ab20553d2e9a1601becd41f4407e881ff49aa3 Mon Sep 17 00:00:00 2001 From: Diogo Date: Tue, 21 Feb 2023 12:32:00 +0100 Subject: [PATCH 2/8] Allow pass a postgres function to SearchCriterion --- tortoise/contrib/postgres/search.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tortoise/contrib/postgres/search.py b/tortoise/contrib/postgres/search.py index 9a050b548..616f0e7cd 100644 --- a/tortoise/contrib/postgres/search.py +++ b/tortoise/contrib/postgres/search.py @@ -1,6 +1,6 @@ +from typing import Union from pypika.enums import Comparator -from pypika.terms import BasicCriterion, Term - +from pypika.terms import BasicCriterion, Term, Function from tortoise.contrib.postgres.functions import ToTsQuery, ToTsVector @@ -9,5 +9,9 @@ class Comp(Comparator): # type: ignore class SearchCriterion(BasicCriterion): # type: ignore - def __init__(self, field: Term, expr: Term): - super().__init__(Comp.search, ToTsVector(field), ToTsQuery(expr)) + def __init__(self, field: Term, expr: Union[Term, Function]): + if isinstance(expr, Function): + _expr = expr + else: + _expr = ToTsQuery(expr) + super().__init__(Comp.search, ToTsVector(field), _expr) From 26b0924b2e9d5c3cf3a0c551a96953cc4613afef Mon Sep 17 00:00:00 2001 From: Diogo Date: Tue, 21 Feb 2023 15:28:26 +0100 Subject: [PATCH 3/8] Update docs --- docs/contrib/postgres.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/contrib/postgres.rst b/docs/contrib/postgres.rst index 57be9735c..768a5c4ef 100644 --- a/docs/contrib/postgres.rst +++ b/docs/contrib/postgres.rst @@ -28,6 +28,7 @@ Functions .. autoclass:: tortoise.contrib.postgres.functions.ToTsVector .. autoclass:: tortoise.contrib.postgres.functions.ToTsQuery +.. autoclass:: tortoise.contrib.postgres.functions.PlainToTsQuery Search ====== From af3216ae8b8be51b3286fb2299ffbeee1d79eeda Mon Sep 17 00:00:00 2001 From: Diogo Date: Wed, 22 Feb 2023 09:47:16 +0100 Subject: [PATCH 4/8] Allow pass the config name for search functions --- tortoise/contrib/postgres/functions.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tortoise/contrib/postgres/functions.py b/tortoise/contrib/postgres/functions.py index 5e2953cbd..b2690e46a 100644 --- a/tortoise/contrib/postgres/functions.py +++ b/tortoise/contrib/postgres/functions.py @@ -1,13 +1,15 @@ from pypika.terms import Function, Term +DEFAULT_TEXT_SEARCH_CONFIG = "pg_catalog.simple" + class ToTsVector(Function): # type: ignore """ to to_tsvector function """ - def __init__(self, field: Term): - super(ToTsVector, self).__init__("TO_TSVECTOR", field) + def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): + super(ToTsVector, self).__init__("TO_TSVECTOR", config_name, field) class ToTsQuery(Function): # type: ignore @@ -15,8 +17,8 @@ class ToTsQuery(Function): # type: ignore to_tsquery function """ - def __init__(self, field: Term): - super(ToTsQuery, self).__init__("TO_TSQUERY", field) + def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): + super(ToTsQuery, self).__init__("TO_TSQUERY", config_name, field) class PlainToTsQuery(Function): # type: ignore @@ -24,8 +26,8 @@ class PlainToTsQuery(Function): # type: ignore plainto_tsquery function """ - def __init__(self, field: Term): - super(PlainToTsQuery, self).__init__("PLAINTO_TSQUERY", field) + def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): + super(PlainToTsQuery, self).__init__("PLAINTO_TSQUERY", config_name, field) class Random(Function): # type: ignore From 185809e6ceca5bb603e3e26abdb758ff80afb7a6 Mon Sep 17 00:00:00 2001 From: Diogo Date: Wed, 22 Feb 2023 10:30:32 +0100 Subject: [PATCH 5/8] `SearchCriterion` extracts config name from `expr` --- tortoise/contrib/postgres/functions.py | 12 +++++++----- tortoise/contrib/postgres/search.py | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tortoise/contrib/postgres/functions.py b/tortoise/contrib/postgres/functions.py index b2690e46a..4c26b2269 100644 --- a/tortoise/contrib/postgres/functions.py +++ b/tortoise/contrib/postgres/functions.py @@ -1,14 +1,14 @@ +from typing import Optional from pypika.terms import Function, Term -DEFAULT_TEXT_SEARCH_CONFIG = "pg_catalog.simple" - class ToTsVector(Function): # type: ignore """ to to_tsvector function """ - def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): + def __init__(self, field: Term, config_name: str = "pg_catalog.simple"): + self.config_name = config_name super(ToTsVector, self).__init__("TO_TSVECTOR", config_name, field) @@ -17,7 +17,8 @@ class ToTsQuery(Function): # type: ignore to_tsquery function """ - def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): + def __init__(self, field: Term, config_name: str = "pg_catalog.simple"): + self.config_name = config_name super(ToTsQuery, self).__init__("TO_TSQUERY", config_name, field) @@ -26,7 +27,8 @@ class PlainToTsQuery(Function): # type: ignore plainto_tsquery function """ - def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): + def __init__(self, field: Term, config_name: str = "pg_catalog.simple"): + self.config_name = config_name super(PlainToTsQuery, self).__init__("PLAINTO_TSQUERY", config_name, field) diff --git a/tortoise/contrib/postgres/search.py b/tortoise/contrib/postgres/search.py index 616f0e7cd..aaf372c3e 100644 --- a/tortoise/contrib/postgres/search.py +++ b/tortoise/contrib/postgres/search.py @@ -14,4 +14,4 @@ def __init__(self, field: Term, expr: Union[Term, Function]): _expr = expr else: _expr = ToTsQuery(expr) - super().__init__(Comp.search, ToTsVector(field), _expr) + super().__init__(Comp.search, ToTsVector(config_name=_expr.config_name, field=field), _expr) From b131dd304f5eb119a4601d0a2e637bd09719819e Mon Sep 17 00:00:00 2001 From: Diogo Date: Wed, 22 Feb 2023 12:03:58 +0100 Subject: [PATCH 6/8] Add `DEFAULT_TEXT_SEARCH_CONFIG` --- tortoise/contrib/postgres/functions.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tortoise/contrib/postgres/functions.py b/tortoise/contrib/postgres/functions.py index 4c26b2269..b2690e46a 100644 --- a/tortoise/contrib/postgres/functions.py +++ b/tortoise/contrib/postgres/functions.py @@ -1,14 +1,14 @@ -from typing import Optional from pypika.terms import Function, Term +DEFAULT_TEXT_SEARCH_CONFIG = "pg_catalog.simple" + class ToTsVector(Function): # type: ignore """ to to_tsvector function """ - def __init__(self, field: Term, config_name: str = "pg_catalog.simple"): - self.config_name = config_name + def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): super(ToTsVector, self).__init__("TO_TSVECTOR", config_name, field) @@ -17,8 +17,7 @@ class ToTsQuery(Function): # type: ignore to_tsquery function """ - def __init__(self, field: Term, config_name: str = "pg_catalog.simple"): - self.config_name = config_name + def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): super(ToTsQuery, self).__init__("TO_TSQUERY", config_name, field) @@ -27,8 +26,7 @@ class PlainToTsQuery(Function): # type: ignore plainto_tsquery function """ - def __init__(self, field: Term, config_name: str = "pg_catalog.simple"): - self.config_name = config_name + def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): super(PlainToTsQuery, self).__init__("PLAINTO_TSQUERY", config_name, field) From 1336bf05c33691951734816c7f7cca6133d30fc4 Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 20 Apr 2023 18:14:03 +0100 Subject: [PATCH 7/8] Get `config_name` `expr` args --- tortoise/contrib/postgres/search.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tortoise/contrib/postgres/search.py b/tortoise/contrib/postgres/search.py index aaf372c3e..382a73be8 100644 --- a/tortoise/contrib/postgres/search.py +++ b/tortoise/contrib/postgres/search.py @@ -10,8 +10,6 @@ class Comp(Comparator): # type: ignore class SearchCriterion(BasicCriterion): # type: ignore def __init__(self, field: Term, expr: Union[Term, Function]): - if isinstance(expr, Function): - _expr = expr - else: - _expr = ToTsQuery(expr) - super().__init__(Comp.search, ToTsVector(config_name=_expr.config_name, field=field), _expr) + if not isinstance(expr, Function): + expr = ToTsQuery(expr) + super().__init__(Comp.search, ToTsVector(config_name=expr.args[0].value, field=field), expr) From 01bf325e3a52ea697ba45bd8fb6d9e17967f6895 Mon Sep 17 00:00:00 2001 From: dbf Date: Thu, 6 Jun 2024 11:22:43 +0200 Subject: [PATCH 8/8] Remove old implementation of `PlainToTsQuery` --- tortoise/contrib/postgres/functions.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tortoise/contrib/postgres/functions.py b/tortoise/contrib/postgres/functions.py index d10d5845f..b2690e46a 100644 --- a/tortoise/contrib/postgres/functions.py +++ b/tortoise/contrib/postgres/functions.py @@ -30,15 +30,6 @@ def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG): super(PlainToTsQuery, self).__init__("PLAINTO_TSQUERY", config_name, field) -class PlainToTsQuery(Function): # type: ignore - """ - plainto_tsquery function - """ - - def __init__(self, field: Term): - super(PlainToTsQuery, self).__init__("PLAINTO_TSQUERY", field) - - class Random(Function): # type: ignore """ Generate random number.