Skip to content

limit_query

LimitQuery

Modifies the select query with limit clause.

With a binder (see templates.SlotBinder) the limit is bound as :ormar_limit, so one cached statement serves every limit value.

Source code in ormar/queryset/queries/limit_query.py
Python
 9
10
11
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
class LimitQuery:
    """
    Modifies the select query with limit clause.

    With a ``binder`` (see ``templates.SlotBinder``) the limit is bound as
    ``:ormar_limit``, so one cached statement serves every limit value.
    """

    def __init__(
        self, limit_count: Optional[int], binder: Optional["SlotBinder"] = None
    ) -> None:
        self.limit_count = limit_count
        self.binder = binder

    def apply(self, expr: sqlalchemy.sql.Select) -> sqlalchemy.sql.Select:
        """
        Applies the limit clause.

        :param expr: query to modify
        :type expr: sqlalchemy.sql.selectable.Select
        :return: modified query
        :rtype: sqlalchemy.sql.selectable.Select
        """

        if self.limit_count is not None:
            value = (
                self.binder.limit(value=self.limit_count)
                if self.binder is not None
                else self.limit_count
            )
            expr = expr.limit(limit=value)

        return expr

apply(expr)

Applies the limit clause.

Parameters:

Name Type Description Default
expr Select

query to modify

required

Returns:

Type Description
sqlalchemy.sql.selectable.Select

modified query

Source code in ormar/queryset/queries/limit_query.py
Python
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def apply(self, expr: sqlalchemy.sql.Select) -> sqlalchemy.sql.Select:
    """
    Applies the limit clause.

    :param expr: query to modify
    :type expr: sqlalchemy.sql.selectable.Select
    :return: modified query
    :rtype: sqlalchemy.sql.selectable.Select
    """

    if self.limit_count is not None:
        value = (
            self.binder.limit(value=self.limit_count)
            if self.binder is not None
            else self.limit_count
        )
        expr = expr.limit(limit=value)

    return expr