Skip to content

offset_query

OffsetQuery

Modifies the select query with offset if set

With a binder (see templates.SlotBinder) the offset is bound as :ormar_offset, so one cached statement serves every non-zero offset.

Source code in ormar/queryset/queries/offset_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
class OffsetQuery:
    """
    Modifies the select query with offset if set

    With a ``binder`` (see ``templates.SlotBinder``) the offset is bound as
    ``:ormar_offset``, so one cached statement serves every non-zero offset.
    """

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

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

        :param expr: query to modify
        :type expr: sqlalchemy.sql.selectable.Select
        :return: modified query
        :rtype: sqlalchemy.sql.selectable.Select
        """
        if self.query_offset:
            value = (
                self.binder.offset(value=self.query_offset)
                if self.binder is not None
                else self.query_offset
            )
            expr = expr.offset(offset=value)
        return expr

apply(expr)

Applies the offset 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/offset_query.py
Python
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def apply(self, expr: sqlalchemy.sql.Select) -> sqlalchemy.sql.Select:
    """
    Applies the offset clause.

    :param expr: query to modify
    :type expr: sqlalchemy.sql.selectable.Select
    :return: modified query
    :rtype: sqlalchemy.sql.selectable.Select
    """
    if self.query_offset:
        value = (
            self.binder.offset(value=self.query_offset)
            if self.binder is not None
            else self.query_offset
        )
        expr = expr.offset(offset=value)
    return expr