Ejemplo n.º 1
0
class SearchQueryExpression(SearchQueryCombinable, Expression):
    def __init__(self, value: LexemeCombinable, using=None, **extra):
        super().__init__(output_field=SearchQueryField())
        self.using = using
        self.extra = extra
        if isinstance(
                value,
                str):  # If the value is a string, we assume it's a phrase
            self.value = Value(
                '"%s"' % value
            )  # We wrap it in quotes to make sure it's parsed as a phrase
        else:  # Otherwise, we assume it's a lexeme
            self.value = value

    def as_sql(
        self,
        compiler: SQLCompiler,
        connection: BaseDatabaseWrapper,
        **extra_context: Any,
    ) -> Tuple[str, List[Any]]:
        sql, params = compiler.compile(self.value)
        return (sql, params)

    def __repr__(self) -> str:
        return self.value.__repr__()
Ejemplo n.º 2
0
class SearchQuery(SearchQueryCombinable, Expression):
    def __init__(self,
                 value: Union[LexemeCombinable, str],
                 search_type: str = "lexeme",
                 **extra):
        super().__init__(output_field=SearchQueryField())
        self.extra = extra
        if (isinstance(value, str) or search_type == "phrase"
            ):  # If the value is a string, we assume it's a phrase
            safe_string = re.sub(
                r"\W+", " ", value
            )  # Remove non-word characters. This is done to disallow the usage of full text search operators in the MATCH clause, because MySQL doesn't include these kinds of characters in FULLTEXT indexes.
            self.value = Value(
                '"%s"' % safe_string
            )  # We wrap it in quotes to make sure it's parsed as a phrase
        else:  # Otherwise, we assume it's a lexeme
            self.value = value

    def as_sql(
        self,
        compiler: SQLCompiler,
        connection: BaseDatabaseWrapper,
        **extra_context: Any,
    ) -> Tuple[str, List[Any]]:
        sql, params = compiler.compile(self.value)
        return (sql, params)

    def __repr__(self) -> str:
        return self.value.__repr__()