Skip to main content

pypaginate.adapters.sqlalchemy.keyset

Keyset (cursor) WHERE-clause builder.

Renders the lexicographic comparison for keyset pagination directly from a query's ORDER BY columns. The core owns the structure of the predicate (OR-of-AND terms via :func:keyset_terms); this adapter only renders each column OP value comparison and combines them with SQLAlchemy and_/or_.

OrderColumn Objects

class OrderColumn()

A parsed ORDER BY column with its sort direction.

reversed

@property
def reversed() -> OrderColumn

Return a copy with the sort direction flipped.

order_clause

@property
def order_clause() -> Any

Return the .asc() / .desc() expression for ORDER BY.

extract_order_columns

def extract_order_columns(query: Select[Any]) -> list[OrderColumn]

Parse a Select's ORDER BY clause into OrderColumn objects.

Unwraps asc() / desc() wrappers; bare columns default to ascending.

Arguments:

  • query - A SQLAlchemy Select carrying an ORDER BY clause.

Returns:

The ordered list of parsed ORDER BY columns.

Raises:

  • ConfigurationError - If the query has no ORDER BY clause.

build_keyset_condition

def build_keyset_condition(columns: list[OrderColumn],
values: tuple[Any, ...]) -> Any

Build the keyset WHERE expression for columns past values.

For ORDER BY (a ASC, b DESC) with cursor (v1, v2) this renders (a > v1) OR (a = v1 AND b < v2).

Arguments:

  • ``0 - The parsed (direction-aware) ORDER BY columns.
  • ``1 - The cursor values, one per column.

Returns:

A SQLAlchemy boolean expression.

Raises:

  • ``2 - If the column / value counts disagree or are empty.