1use crate::AliasedEntityColumn;
2use sql_orm_core::{Entity, EntityColumn};
3use sql_orm_query::{OrderBy, SortDirection};
4
5pub trait EntityColumnOrderExt<E: Entity> {
6 fn asc(self) -> OrderBy;
7
8 fn desc(self) -> OrderBy;
9}
10
11impl<E: Entity> EntityColumnOrderExt<E> for EntityColumn<E> {
12 fn asc(self) -> OrderBy {
13 OrderBy::asc(self)
14 }
15
16 fn desc(self) -> OrderBy {
17 OrderBy::desc(self)
18 }
19}
20
21impl<E: Entity> EntityColumnOrderExt<E> for AliasedEntityColumn<E> {
22 fn asc(self) -> OrderBy {
23 let column = self.column_ref();
24 OrderBy::new(column.table, column.column_name, SortDirection::Asc)
25 }
26
27 fn desc(self) -> OrderBy {
28 let column = self.column_ref();
29 OrderBy::new(column.table, column.column_name, SortDirection::Desc)
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::EntityColumnOrderExt;
36 use crate::EntityColumnAliasExt;
37 use sql_orm_core::{
38 ColumnMetadata, Entity, EntityColumn, EntityMetadata, PrimaryKeyMetadata, SqlServerType,
39 };
40 use sql_orm_query::{OrderBy, SortDirection, TableRef};
41
42 struct TestEntity;
43
44 static TEST_ENTITY_COLUMNS: [ColumnMetadata; 2] = [
45 ColumnMetadata {
46 rust_field: "id",
47 column_name: "id",
48 renamed_from: None,
49 sql_type: SqlServerType::BigInt,
50 nullable: false,
51 primary_key: true,
52 identity: None,
53 default_sql: None,
54 computed_sql: None,
55 rowversion: false,
56 insertable: false,
57 updatable: false,
58 max_length: None,
59 precision: None,
60 scale: None,
61 },
62 ColumnMetadata {
63 rust_field: "created_at",
64 column_name: "created_at",
65 renamed_from: None,
66 sql_type: SqlServerType::DateTime2,
67 nullable: false,
68 primary_key: false,
69 identity: None,
70 default_sql: None,
71 computed_sql: None,
72 rowversion: false,
73 insertable: true,
74 updatable: true,
75 max_length: None,
76 precision: None,
77 scale: None,
78 },
79 ];
80
81 static TEST_ENTITY_METADATA: EntityMetadata = EntityMetadata {
82 rust_name: "TestEntity",
83 schema: "dbo",
84 table: "test_entities",
85 renamed_from: None,
86 columns: &TEST_ENTITY_COLUMNS,
87 primary_key: PrimaryKeyMetadata {
88 name: None,
89 columns: &["id"],
90 },
91 indexes: &[],
92 foreign_keys: &[],
93 navigations: &[],
94 };
95
96 impl Entity for TestEntity {
97 fn metadata() -> &'static EntityMetadata {
98 &TEST_ENTITY_METADATA
99 }
100 }
101
102 #[allow(non_upper_case_globals)]
103 impl TestEntity {
104 const id: EntityColumn<TestEntity> = EntityColumn::new("id", "id");
105 const created_at: EntityColumn<TestEntity> = EntityColumn::new("created_at", "created_at");
106 }
107
108 #[test]
109 fn ordering_methods_build_expected_order_by_values() {
110 assert_eq!(
111 TestEntity::id.asc(),
112 OrderBy::new(
113 TableRef::new("dbo", "test_entities"),
114 "id",
115 SortDirection::Asc
116 )
117 );
118 assert_eq!(
119 TestEntity::created_at.desc(),
120 OrderBy::new(
121 TableRef::new("dbo", "test_entities"),
122 "created_at",
123 SortDirection::Desc
124 )
125 );
126 }
127
128 #[test]
129 fn aliased_columns_build_order_by_against_table_alias() {
130 assert_eq!(
131 TestEntity::created_at.aliased("t").desc(),
132 OrderBy::new(
133 TableRef::with_alias("dbo", "test_entities", "t"),
134 "created_at",
135 SortDirection::Desc
136 )
137 );
138 }
139}