paginate(rel, {<sort attr>}, page_size, {<attr list>})
paginate(rel, {<sort attr>}, page_size, {<attr list>}, prev, next)
paginate(rel, {<sort attr>}, page_size, {<attr list>}, prev, next, options)
Converts rel into an ordered table with a specified left-right attr list, starting from an optional prev/next key and in blocks of a given page_size.
Returns a tuple{data:table{...}, prev_page:str, next_page:str}
where data is the next page of data and prev_page and next_page are key positions for adjacent pages that can be passed into subsequent paginate calls.
The {sort attr} should be unique and in the attr list, and will be used to order the tuples.
Use -attr to reverse the sort.
The default sort attribute name is '_sort', if left empty.
page_size will be used to limit the number of tuples in the table (after the ordering and any prev/next jump).
prev/next are each {v:str}. If they are passed in, they indicate where the data should start from. Generally you should pass in values returned from earlier calls of paginate.
An example prev/next value is:
{v:str}{["SNO:str=S2"]}
Values can arrive via http.hh form, via URL query/post: form[k="p"], form[k="n"]
A table is an array of tuples and can no longer be used in relational expressions.
Examples
Note: the data attribute of these results are no longer relations. They are tables, i.e. ordered sets of tuples.
first page
print(paginate($P, {PNO}, 3, {PNO,PNAME,COLOR,WEIGHT,CITY}))
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
data prev_page next_page
######################################### PNO:str=P3
PNO PNAME COLOR WEIGHT CITY
P1 Nut Red 12 London
P2 Bolt Green 17 Paris
P3 Screw Blue 17 Rome
#########################################
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
Note: the next_page can be used to fetch the next page by passing it to paginate as the next parameter, e.g. when a user clicks a "next page" button.
There is no prev_page here which means there is no earlier page available.
page after first page
Here we pass a next_page value (from the previous example) which specifies where the page should start from, i.e. the next page after the previous example, in this case
print(paginate($P, {PNO}, 3, {PNO,PNAME,COLOR,WEIGHT,CITY}, {v:str}{}, {v:str}{["PNO:str=P3"]}))
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
data prev_page next_page
######################################### PNO:str=P4
PNO PNAME COLOR WEIGHT CITY
P4 Screw Red 14 London
P5 Cam Blue 12 Paris
P6 Cog Red 19 London
#########################################
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
Note: the prev_page can be used to fetch the previous page by passing it to paginate as the prev parameter, e.g. when a user clicks a "previous page" button.
There is no next_page here which means there is no later page available.