1个不稳定版本
0.5.0 | 2022年7月15日 |
---|
#24 在 #pagination
15KB
136 行
SQLx-Page
此库为SQLx提供基于游标的分页。
它基于sqlx::QueryBuilder
构建,因此与sqlx::QueryBuilder
具有相同的安全级别。
目前仅支持Postgres。在不进行太多工作的情况下,可以添加其他数据库。
安装
版本遵循sqlx
,即sqlx-page
0.5与sqlx
0.5兼容
sqlx-page = { version = "~0.5", features = ["select your sqlx runtime here", "postgres"]
示例
use sqlx::QueryBuilder;
use sqlx_page::Page;
async fn some_func() {
// ... Some business logic
let page = Page::new(
// Direction to select rows.
// If true, will select rows in the "smaller" direction,
// e.g. if your want to sort by the time,
// `true` means select rows from the current cursor to the past
true,
// Size of the page
100,
// Use these columns for sorting.
// The combination of the columns should uniquely identify a row
vec![String::from("row_id")]
);
// Create a `sqlx::QueryBuilder` as usual
let mut builder = QueryBuilder::new(r#"
select row_id, user_name
from users
where true and
"#);
// ... Maybe other search conditions
// Push and bind the pagination condition: `(row_id < $_)`.
// `push_where2..5` are also defined, they are used if your cursor consists of multiple columns
page.push_where1(&mut builder, Some(11));
// Push the order by clause: `order by row_id desc`
page.push_order_by(&mut builder);
// Push the limit clause: `limit 100`
page.push_limit(&mut builder);
// ... Use the builder as usual
}
依赖
~7–22MB
~341K SLoC