tacker.db.sqlalchemyutils module

tacker.db.sqlalchemyutils.paginate_query(query, model, limit, sorts, marker_obj=None)

Returns a query with sorting / pagination criteria added.

Pagination works by requiring a unique sort key, specified by sorts. (If sort keys is not unique, then we risk looping through values.) We use the last row in the previous page as the ‘marker’ for pagination. So we must return values that follow the passed marker in the order. With a single-valued sort key, this would be easy: sort_key > X. With a compound-values sort key, (k1, k2, k3) we must do this to repeat the lexicographical ordering: (k1 > X1) or (k1 == X1 && k2 > X2) or (k1 == X1 && k2 == X2 && k3 > X3) The reason of didn’t use OFFSET clause was it don’t scale, please refer discussion at https://lists.launchpad.net/openstack/msg02547.html

We also have to cope with different sort directions.

Typically, the id of the last row is used as the client-facing pagination marker, then the actual marker object must be fetched from the db and passed in to us as marker.

  • query – the query object to which we should add paging/sorting

  • model – the ORM model class

  • limit – maximum number of items to return

  • sorts – array of attributes and direction by which results should be sorted

  • marker – the last item of the previous page; we returns the next results after this value.

Return type:



The query with sorting/pagination added.