Skip to content

filter_query

FilterQuery

Modifies the select query with given list of where/filter clauses.

With a binder (see templates.SlotBinder) the filter values become named bind parameters, so the built select can be cached as a statement template and run again with other values.

Source code in ormar/queryset/queries/filter_query.py
Python
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class FilterQuery:
    """
    Modifies the select query with given list of where/filter clauses.

    With a ``binder`` (see ``templates.SlotBinder``) the filter values become
    named bind parameters, so the built select can be cached as a statement
    template and run again with other values.
    """

    def __init__(
        self,
        filter_clauses: list[FilterAction],
        exclude: bool = False,
        binder: Optional["SlotBinder"] = None,
    ) -> None:
        self.exclude = exclude
        self.filter_clauses = filter_clauses
        self.binder = binder

    def apply(
        self,
        expr: Select[Any],
    ) -> Select[Any]:
        """
        Applies all filter clauses if set.

        :param expr: query to modify
        :type expr: sqlalchemy.sql.selectable.Select
        :return: modified query
        :rtype: sqlalchemy.sql.selectable.Select
        """
        if self.filter_clauses:
            clauses: list[Union[TextClause, ColumnElement[Any]]] = [
                x.get_text_clause(binder=self.binder) for x in self.filter_clauses
            ]
            if self.exclude:
                # every exclude() call is negated on its own, all of them are ANDed
                clauses = [sqlalchemy.sql.not_(x) for x in clauses]
            clause: Union[TextClause, ColumnElement[Any]] = (
                clauses[0] if len(clauses) == 1 else sqlalchemy.sql.and_(*clauses)
            )
            expr = expr.where(clause)
        return expr

apply(expr)

Applies all filter clauses if set.

Parameters:

Name Type Description Default
expr Select[Any]

query to modify

required

Returns:

Type Description
sqlalchemy.sql.selectable.Select

modified query

Source code in ormar/queryset/queries/filter_query.py
Python
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def apply(
    self,
    expr: Select[Any],
) -> Select[Any]:
    """
    Applies all filter clauses if set.

    :param expr: query to modify
    :type expr: sqlalchemy.sql.selectable.Select
    :return: modified query
    :rtype: sqlalchemy.sql.selectable.Select
    """
    if self.filter_clauses:
        clauses: list[Union[TextClause, ColumnElement[Any]]] = [
            x.get_text_clause(binder=self.binder) for x in self.filter_clauses
        ]
        if self.exclude:
            # every exclude() call is negated on its own, all of them are ANDed
            clauses = [sqlalchemy.sql.not_(x) for x in clauses]
        clause: Union[TextClause, ColumnElement[Any]] = (
            clauses[0] if len(clauses) == 1 else sqlalchemy.sql.and_(*clauses)
        )
        expr = expr.where(clause)
    return expr