#sql-query #pagination #relational #order #table

rdb-pagination

关系数据库中处理分页和排序的SQL查询扩展库

3个不稳定版本

0.2.1 2024年7月5日
0.2.0 2024年7月5日
0.1.1 2024年4月8日

#456 in 数据库接口

Download history 59/week @ 2024-05-06 44/week @ 2024-05-13 79/week @ 2024-05-20 78/week @ 2024-05-27 24/week @ 2024-06-03 5/week @ 2024-06-10 40/week @ 2024-06-17 32/week @ 2024-06-24 253/week @ 2024-07-01 35/week @ 2024-07-08 40/week @ 2024-07-15 27/week @ 2024-07-29 15/week @ 2024-08-05 32/week @ 2024-08-12

每月 74 次下载

MIT 许可证

44KB
830

RDB Pagination

CI

关系数据库中处理分页和排序的SQL查询扩展库。

用法

使用 #[derive(OrderByOptions)] 使结构体实现 OrderByOptions 特性。

use educe::Educe;
use rdb_pagination::{prelude::*, PaginationOptions, SqlJoin, SqlOrderByComponent};

#[derive(Debug, Clone, Educe, OrderByOptions)]
#[educe(Default)]
#[orderByOptions(name = component)]
#[orderByOptions(
    join((component, component_type_id), (component_type, id)),
    join((component_type, component_general_type_id), (component_general_type, id)),
    join((component_type, component_vendor_id), (component_vendor, id)),
)]
pub struct ComponentOrderBy {
    #[educe(Default = 103)]
    #[orderByOptions(("component_general_type", "id"), unique)]
    pub component_general_type_id:    OrderMethod,
    #[orderByOptions(("component_general_type", "name"), unique)]
    pub component_general_type_name:  OrderMethod,
    #[educe(Default = 102)]
    #[orderByOptions(("component_general_type", "order"))]
    pub component_general_type_order: OrderMethod,
    #[educe(Default = 105)]
    #[orderByOptions(("component_vendor", "id"), unique)]
    pub vendor_id:                    OrderMethod,
    #[orderByOptions(("component_vendor", "name"), unique)]
    pub vendor_name:                  OrderMethod,
    #[educe(Default = 104)]
    #[orderByOptions(("component_vendor", "order"))]
    pub vendor_order:                 OrderMethod,
    #[educe(Default = 106)]
    #[orderByOptions(("component_type", "id"), unique)]
    pub component_type_id:            OrderMethod,
    #[educe(Default = 101)]
    #[orderByOptions(("component_type", "order"))]
    pub component_type_order:         OrderMethod,
    #[educe(Default = 107)]
    #[orderByOptions(("component", "id"), unique)]
    pub id:                           OrderMethod,
}

let order_by = ComponentOrderBy {
    component_general_type_id: OrderMethod::from(-1i8),
    ..ComponentOrderBy::default()
};

let pagination_options = PaginationOptions::default().page(3).items_per_page(20).order_by(order_by);

let mut buffer = String::new();

# #[cfg(feature = "mysql")]
assert_eq!("LIMIT 20 OFFSET 40", pagination_options.to_mysql_limit_offset(&mut buffer));

buffer.clear();

let (joins, order_by_components) = pagination_options.order_by.to_sql();

# #[cfg(feature = "mysql")]
assert_eq!(
    "LEFT JOIN `component_type` ON `component_type`.`id` = `component`.`component_type_id`\nLEFT JOIN `component_vendor` ON `component_vendor`.`id` = `component_type`.`component_vendor_id`",
    SqlJoin::format_mysql_join_clauses(&joins, &mut buffer)
);

buffer.clear();

# #[cfg(feature = "mysql")]
assert_eq!(
    "ORDER BY `component_type`.`component_general_type_id` DESC, `component_type`.`order` ASC, `component_vendor`.`order` ASC, `component_type`.`component_vendor_id` ASC, `component`.`component_type_id` ASC, `component`.`id` ASC",
    SqlOrderByComponent::format_mysql_order_by_components(&order_by_components, &mut buffer)
);

Crates.io

https://crates.io/crates/rdb-pagination

文档

https://docs.rs/rdb-pagination

许可证

MIT

依赖

~145KB