Skip to main content

cratestack_sqlx/
order.rs

1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct OrderClause {
3    pub(crate) target: OrderTarget,
4    pub(crate) direction: SortDirection,
5}
6
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub(crate) enum OrderTarget {
9    Column(&'static str),
10    RelationScalar {
11        parent_table: &'static str,
12        parent_column: &'static str,
13        related_table: &'static str,
14        related_column: &'static str,
15        value_sql: &'static str,
16    },
17}
18
19impl OrderClause {
20    pub const fn column(column: &'static str, direction: SortDirection) -> Self {
21        Self {
22            target: OrderTarget::Column(column),
23            direction,
24        }
25    }
26
27    pub const fn relation_scalar(
28        parent_table: &'static str,
29        parent_column: &'static str,
30        related_table: &'static str,
31        related_column: &'static str,
32        value_sql: &'static str,
33        direction: SortDirection,
34    ) -> Self {
35        Self {
36            target: OrderTarget::RelationScalar {
37                parent_table,
38                parent_column,
39                related_table,
40                related_column,
41                value_sql,
42            },
43            direction,
44        }
45    }
46
47    pub(crate) fn is_relation_scalar(&self) -> bool {
48        matches!(self.target, OrderTarget::RelationScalar { .. })
49    }
50
51    pub(crate) fn targets_column(&self, column: &str) -> bool {
52        matches!(self.target, OrderTarget::Column(candidate) if candidate == column)
53    }
54
55    pub(crate) fn direction(&self) -> SortDirection {
56        self.direction
57    }
58}
59
60#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum SortDirection {
62    Asc,
63    Desc,
64}