Skip to main content

sqlx_data_params/
params.rs

1use crate::filter::FilterParams;
2use crate::pagination::LimitParam;
3use crate::pagination::OffsetParam;
4use crate::pagination::Pagination;
5use crate::search::SearchParams;
6use crate::sort::SortingParams;
7
8#[derive(Clone, Debug, Default)]
9pub struct Params {
10    pub filters: Option<FilterParams>,
11    pub search: Option<SearchParams>,
12    pub sort_by: Option<SortingParams>,
13    pub limit: Option<LimitParam>,
14    pub offset: Option<OffsetParam>,
15    pub pagination: Option<Pagination>,
16}
17
18impl Params {
19    /// Checks if the params have no modifications (empty state)
20    pub fn is_empty(&self) -> bool {
21        self.filters.is_none()
22            && self.search.is_none()
23            && self.sort_by.is_none()
24            && self.pagination.is_none()
25            && self.limit.is_none()
26            && self.offset.is_none()
27    }
28
29    /// Checks if the params have any modifications (opposite of is_empty)
30    pub fn has_modifications(&self) -> bool {
31        !self.is_empty()
32    }
33
34    pub fn is_disable_total_count(&self) -> bool {
35        match &self.pagination {
36            Some(Pagination::Slice(slice)) => slice.disable_total_count,
37            _ => false, // Cursor and Serial does not have disable_total_count
38        }
39    }
40
41    /// Returns 1 if pagination type requires LIMIT+1 for has_next detection, 0 otherwise
42    pub fn limit_plus_one(&self) -> u32 {
43        match &self.pagination {
44            Some(Pagination::Slice(_)) => 1,  // Slice needs +1 for has_next
45            Some(Pagination::Cursor(_)) => 1, // Cursor needs +1 for has_next
46            Some(Pagination::Serial(_)) => 0, // Serial doesn't need +1 (uses total count)
47            None => 0,                        // No pagination, no +1
48        }
49    }
50}