toasty_core/stmt/
order_by_expr.rs

1use super::{Direction, Expr};
2
3/// A single expression within an [`OrderBy`](super::OrderBy) clause, with an
4/// optional sort direction.
5///
6/// When `order` is `None`, the database default direction is used (typically
7/// ascending).
8///
9/// # Examples
10///
11/// ```ignore
12/// use toasty_core::stmt::{OrderByExpr, Direction, Expr};
13///
14/// let expr = OrderByExpr {
15///     expr: Expr::null(),
16///     order: Some(Direction::Desc),
17/// };
18/// ```
19#[derive(Debug, Clone, PartialEq)]
20pub struct OrderByExpr {
21    /// The expression to order by.
22    pub expr: Expr,
23
24    /// The sort direction, or `None` for the database default.
25    pub order: Option<Direction>,
26}
27
28impl OrderByExpr {
29    /// Flips the sort direction. `Desc` becomes default (ascending); default
30    /// and `Asc` become `Asc` (explicit ascending, since the default changed).
31    pub fn reverse(&mut self) {
32        self.order = match self.order {
33            Some(Direction::Desc) => None,
34            _ => Some(Direction::Asc),
35        }
36    }
37}