Skip to main content

pypaginate.adapters.sqlalchemy.cursor_page

Keyset page assembly for the SQLAlchemy cursor backends.

Holds the decode -> keyset-filter -> re-order -> over-fetch -> trim -> encode mechanics, kept separate from the (async / sync) backend classes in :mod:pypaginate.adapters.sqlalchemy.cursor. prepare_query and finalize_page are the two entry points the backends call.

prepare_query

def prepare_query(
query: Select[Any],
params: CursorParams) -> tuple[Select[Any], list[OrderColumn]]

Build the keyset-filtered SELECT over-fetching limit + 1 rows.

Arguments:

  • query - A SQLAlchemy Select carrying an ORDER BY clause.
  • params - The cursor request (limit + optional after / before).

Returns:

The prepared statement and the original ORDER BY columns.

finalize_page

def finalize_page(rows: list[Any], order_cols: list[OrderColumn],
params: CursorParams) -> CursorPage[Any]

Trim the over-fetched rows, reverse if backward, and compute cursors.

Arguments:

  • rows - The (up to limit + 1) fetched rows.
  • order_cols - The original ORDER BY columns.
  • params - The cursor request that produced rows.

Returns:

A :class:CursorPage with next / previous cursors.