Crate rdb_pagination
source ·Expand description
§RDB Pagination
SQL query extension library for handling pagination and sorting in relational databases.
§Usage
Use #[derive(OrderByOptions)]
to have a struct implement the OrderByOptions
trait.
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();
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();
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();
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)
);
Modules§
- A convenience module appropriate for glob imports.
Structs§
- An integer value for ordering.
- Struct representing pagination information.
- Struct representing pagination options.
- Struct for generating the
JOIN
clause. - Struct for generating the
ORDER BY
clause.
Enums§
- Enum representing the name of a table or a column.
- Enum representing the order type.
Traits§
- Options for the
ORDER BY
clause. - Value for
OrderMethod
. This should bei8
ori16
. - Operators for
SqlJoin
s.
Type Aliases§
- The name of a column.
- The name of a table and the name of a column inside that table.
- The name of a table.
Derive Macros§
- Order
ByOptions derive