seitime-frappe/frappe/query_builder/custom.py
Suraj Shetty c0c5b2ebdd
style: format all python files using black (#16453)
Co-authored-by: Frappe Bot <developers@frappe.io>
2022-04-12 10:59:25 +05:30

104 lines
3.4 KiB
Python

from typing import Any, Optional
from pypika.functions import DistinctOptionFunction
from pypika.terms import Term
from pypika.utils import builder, format_alias_sql, format_quotes
import frappe
class GROUP_CONCAT(DistinctOptionFunction):
def __init__(self, column: str, alias: Optional[str] = None):
"""[ Implements the group concat function read more about it at https://www.geeksforgeeks.org/mysql-group_concat-function ]
Args:
column (str): [ name of the column you want to concat]
alias (Optional[str], optional): [ is this an alias? ]. Defaults to None.
"""
super(GROUP_CONCAT, self).__init__("GROUP_CONCAT", column, alias=alias)
class STRING_AGG(DistinctOptionFunction):
def __init__(self, column: str, separator: str = ",", alias: Optional[str] = None):
"""[ Implements the group concat function read more about it at https://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-ver15 ]
Args:
column (str): [ name of the column you want to concat ]
separator (str, optional): [separator to be used]. Defaults to ",".
alias (Optional[str], optional): [description]. Defaults to None.
"""
super(STRING_AGG, self).__init__("STRING_AGG", column, separator, alias=alias)
class MATCH(DistinctOptionFunction):
def __init__(self, column: str, *args, **kwargs):
"""[ Implementation of Match Against read more about it https://dev.mysql.com/doc/refman/8.0/en/fulltext-search.html#function_match ]
Args:
column (str):[ column to search in ]
"""
alias = kwargs.get("alias")
super(MATCH, self).__init__(" MATCH", column, *args, alias=alias)
self._Against = False
def get_function_sql(self, **kwargs):
s = super(DistinctOptionFunction, self).get_function_sql(**kwargs)
if self._Against:
return f"{s} AGAINST ({frappe.db.escape(f'+{self._Against}*')} IN BOOLEAN MODE)"
return s
@builder
def Against(self, text: str):
"""[ Text that has to be searched against ]
Args:
text (str): [ the text string that we match it against ]
"""
self._Against = text
class TO_TSVECTOR(DistinctOptionFunction):
def __init__(self, column: str, *args, **kwargs):
"""[ Implementation of TO_TSVECTOR read more about it https://www.postgresql.org/docs/9.1/textsearch-controls.html]
Args:
column (str): [ column to search in ]
"""
alias = kwargs.get("alias")
super(TO_TSVECTOR, self).__init__("TO_TSVECTOR", column, *args, alias=alias)
self._PLAINTO_TSQUERY = False
def get_function_sql(self, **kwargs):
s = super(DistinctOptionFunction, self).get_function_sql(**kwargs)
if self._PLAINTO_TSQUERY:
return f"{s} @@ PLAINTO_TSQUERY({frappe.db.escape(self._PLAINTO_TSQUERY)})"
return s
@builder
def Against(self, text: str):
"""[ Text that has to be searched against ]
Args:
text (str): [ the text string that we match it against ]
"""
self._PLAINTO_TSQUERY = text
class ConstantColumn(Term):
alias = None
def __init__(self, value: str) -> None:
"""[ Returns a pseudo column with a constant value in all the rows]
Args:
value (str): [ Value of the column ]
"""
self.value = value
def get_sql(self, quote_char: Optional[str] = None, **kwargs: Any) -> str:
return format_alias_sql(
format_quotes(self.value, kwargs.get("secondary_quote_char") or ""),
self.alias or self.value,
quote_char=quote_char,
**kwargs,
)