Skip to main content

sql_orm_query/
order.rs

1use crate::expr::{ColumnRef, TableRef};
2use sql_orm_core::{Entity, EntityColumn};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum SortDirection {
6    Asc,
7    Desc,
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct OrderBy {
12    pub table: TableRef,
13    pub column_name: &'static str,
14    pub direction: SortDirection,
15}
16
17impl OrderBy {
18    pub const fn new(table: TableRef, column_name: &'static str, direction: SortDirection) -> Self {
19        Self {
20            table,
21            column_name,
22            direction,
23        }
24    }
25
26    pub fn asc<E: Entity>(column: EntityColumn<E>) -> Self {
27        let column_ref = ColumnRef::for_entity_column(column);
28        Self::new(column_ref.table, column_ref.column_name, SortDirection::Asc)
29    }
30
31    pub fn desc<E: Entity>(column: EntityColumn<E>) -> Self {
32        let column_ref = ColumnRef::for_entity_column(column);
33        Self::new(
34            column_ref.table,
35            column_ref.column_name,
36            SortDirection::Desc,
37        )
38    }
39}