@@ -3150,10 +3150,8 @@ pub enum ChildKey<'a> {
31503150#[ derive( Debug , Clone ) ]
31513151pub enum SortKey < ' a > {
31523152 None ,
3153- /// Order by `id asc`
3154- IdAsc ( Option < dsl:: BlockColumn < ' a > > ) ,
3155- /// Order by `id desc`
3156- IdDesc ( Option < dsl:: BlockColumn < ' a > > ) ,
3153+ /// Order by `id <direction>, [block <direction>]`
3154+ Id ( SortDirection , Option < dsl:: BlockColumn < ' a > > ) ,
31573155 /// Order by some other column; `column` will never be `id`
31583156 Key {
31593157 column : dsl:: Column < ' a > ,
@@ -3169,25 +3167,26 @@ impl<'a> fmt::Display for SortKey<'a> {
31693167 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
31703168 match self {
31713169 SortKey :: None => write ! ( f, "none" ) ,
3172- SortKey :: IdAsc ( Option :: None ) => write ! ( f, "{}" , PRIMARY_KEY_COLUMN ) ,
3173- SortKey :: IdAsc ( Some ( br) ) => write ! ( f, "{}, {}" , PRIMARY_KEY_COLUMN , br) ,
3174- SortKey :: IdDesc ( Option :: None ) => write ! ( f, "{} desc" , PRIMARY_KEY_COLUMN ) ,
3175- SortKey :: IdDesc ( Some ( br) ) => {
3176- write ! ( f, "{} desc, {} desc" , PRIMARY_KEY_COLUMN , br)
3170+ SortKey :: Id ( direction, br) => {
3171+ write ! ( f, "{}{}" , PRIMARY_KEY_COLUMN , direction) ?;
3172+ if let Some ( br) = br {
3173+ write ! ( f, ", {} {}" , PRIMARY_KEY_COLUMN , br) ?;
3174+ }
3175+ Ok ( ( ) )
31773176 }
31783177 SortKey :: Key {
31793178 column,
31803179 value : _,
31813180 direction,
31823181 } => write ! (
31833182 f,
3184- "{} {}, {} {}" ,
3183+ "{}{}, {}{}" ,
31853184 column, direction, PRIMARY_KEY_COLUMN , direction
31863185 ) ,
31873186 SortKey :: ChildKey ( child) => match child {
31883187 ChildKey :: Single ( details) => write ! (
31893188 f,
3190- "{} {}, {} {}" ,
3189+ "{}{}, {}{}" ,
31913190 details. sort_by_column,
31923191 details. direction,
31933192 details. child_table. primary_key( ) ,
@@ -3196,7 +3195,7 @@ impl<'a> fmt::Display for SortKey<'a> {
31963195 ChildKey :: Many ( _, details) => details. iter ( ) . try_for_each ( |details| {
31973196 write ! (
31983197 f,
3199- "{} {}, {} {}" ,
3198+ "{}{}, {}{}" ,
32003199 details. sort_by_column,
32013200 details. direction,
32023201 details. child_table. primary_key( ) ,
@@ -3267,17 +3266,20 @@ pub enum SortDirection {
32673266}
32683267
32693268impl SortDirection {
3270- fn as_str ( & self ) -> & ' static str {
3269+ /// Generate either `""` or `" desc"`; convenient for SQL generation
3270+ /// without needing an additional space to separate it from preceding
3271+ /// text
3272+ fn as_sql ( & self ) -> & ' static str {
32713273 match self {
3272- SortDirection :: Asc => "asc " ,
3273- SortDirection :: Desc => "desc" ,
3274+ SortDirection :: Asc => "" ,
3275+ SortDirection :: Desc => " desc" ,
32743276 }
32753277 }
32763278}
32773279
32783280impl std:: fmt:: Display for SortDirection {
32793281 fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3280- write ! ( f, "{}" , self . as_str ( ) )
3282+ write ! ( f, "{}" , self . as_sql ( ) )
32813283 }
32823284}
32833285
@@ -3326,11 +3328,7 @@ impl<'a> SortKey<'a> {
33263328 }
33273329 } else if column. is_primary_key ( ) {
33283330 let block_column = use_block_column. block_column ( table) ;
3329- use SortDirection :: * ;
3330- match direction {
3331- Asc => Ok ( SortKey :: IdAsc ( block_column) ) ,
3332- Desc => Ok ( SortKey :: IdDesc ( block_column) ) ,
3333- }
3331+ Ok ( SortKey :: Id ( direction, block_column) )
33343332 } else {
33353333 Ok ( SortKey :: Key {
33363334 column,
@@ -3619,7 +3617,7 @@ impl<'a> SortKey<'a> {
36193617 EntityOrder :: Descending ( attr, _) => {
36203618 with_key ( table, attr, filter, Desc , use_block_column)
36213619 }
3622- EntityOrder :: Default => Ok ( SortKey :: IdAsc ( use_block_column. block_column ( table) ) ) ,
3620+ EntityOrder :: Default => Ok ( SortKey :: Id ( Asc , use_block_column. block_column ( table) ) ) ,
36233621 EntityOrder :: Unordered => Ok ( SortKey :: None ) ,
36243622 EntityOrder :: ChildAscending ( kind) => match kind {
36253623 EntityOrderByChild :: Object ( child, entity_type) => with_child_object_key (
@@ -3674,7 +3672,7 @@ impl<'a> SortKey<'a> {
36743672 ) -> QueryResult < ( ) > {
36753673 match self {
36763674 SortKey :: None => { }
3677- SortKey :: IdAsc ( br_column ) | SortKey :: IdDesc ( br_column) => {
3675+ SortKey :: Id ( _ , br_column) => {
36783676 if let Some ( br_column) = br_column {
36793677 out. push_sql ( ", " ) ;
36803678
@@ -3774,24 +3772,10 @@ impl<'a> SortKey<'a> {
37743772 use SortDirection :: * ;
37753773 match self {
37763774 SortKey :: None => Ok ( ( ) ) ,
3777- SortKey :: IdAsc ( br_column) => {
3778- out. push_sql ( "order by " ) ;
3779- out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3780- if let Some ( br_column) = br_column {
3781- if use_sort_key_alias {
3782- out. push_sql ( ", " ) ;
3783- out. push_sql ( SORT_KEY_COLUMN ) ;
3784- } else {
3785- out. push_sql ( ", " ) ;
3786- out. push_sql ( br_column. name ( ) ) ;
3787- }
3788- }
3789- Ok ( ( ) )
3790- }
3791- SortKey :: IdDesc ( br_column) => {
3775+ SortKey :: Id ( direction, br_column) => {
37923776 out. push_sql ( "order by " ) ;
37933777 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3794- out. push_sql ( " desc" ) ;
3778+ out. push_sql ( direction . as_sql ( ) ) ;
37953779 if let Some ( br_column) = br_column {
37963780 if use_sort_key_alias {
37973781 out. push_sql ( ", " ) ;
@@ -3800,7 +3784,7 @@ impl<'a> SortKey<'a> {
38003784 out. push_sql ( ", " ) ;
38013785 out. push_sql ( br_column. name ( ) ) ;
38023786 }
3803- out. push_sql ( " desc" ) ;
3787+ out. push_sql ( direction . as_sql ( ) ) ;
38043788 }
38053789 Ok ( ( ) )
38063790 }
@@ -3878,14 +3862,10 @@ impl<'a> SortKey<'a> {
38783862
38793863 match self {
38803864 SortKey :: None => Ok ( ( ) ) ,
3881- SortKey :: IdAsc ( _) => {
3882- order_by_parent_id ( out) ;
3883- out. push_identifier ( PRIMARY_KEY_COLUMN )
3884- }
3885- SortKey :: IdDesc ( _) => {
3865+ SortKey :: Id ( direction, _) => {
38863866 order_by_parent_id ( out) ;
38873867 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3888- out. push_sql ( " desc" ) ;
3868+ out. push_sql ( direction . as_sql ( ) ) ;
38893869 Ok ( ( ) )
38903870 }
38913871 SortKey :: Key {
@@ -3952,15 +3932,13 @@ impl<'a> SortKey<'a> {
39523932 }
39533933 }
39543934 }
3955- out. push_sql ( " " ) ;
3956- out. push_sql ( direction. as_str ( ) ) ;
3935+ out. push_sql ( direction. as_sql ( ) ) ;
39573936 out. push_sql ( ", " ) ;
39583937 if !use_sort_key_alias {
39593938 push_prefix ( rest_prefix, out) ;
39603939 }
39613940 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3962- out. push_sql ( " " ) ;
3963- out. push_sql ( direction. as_str ( ) ) ;
3941+ out. push_sql ( direction. as_sql ( ) ) ;
39643942 Ok ( ( ) )
39653943 }
39663944
@@ -4002,14 +3980,13 @@ impl<'a> SortKey<'a> {
40023980 child. sort_by_column . walk_ast ( out. reborrow ( ) ) ?;
40033981 }
40043982
4005- out. push_sql ( ") " ) ;
3983+ out. push_sql ( ")" ) ;
40063984
4007- out. push_sql ( direction. as_str ( ) ) ;
3985+ out. push_sql ( direction. as_sql ( ) ) ;
40083986 out. push_sql ( ", " ) ;
40093987
40103988 parent_pk. walk_ast ( out. reborrow ( ) ) ?;
4011- out. push_sql ( " " ) ;
4012- out. push_sql ( direction. as_str ( ) ) ;
3989+ out. push_sql ( direction. as_sql ( ) ) ;
40133990 Ok ( ( ) )
40143991 }
40153992
@@ -4032,9 +4009,9 @@ impl<'a> SortKey<'a> {
40324009
40334010 child. child_join_column . walk_ast ( out. reborrow ( ) ) ?;
40344011 }
4035- out. push_sql ( ") " ) ;
4012+ out. push_sql ( ")" ) ;
40364013
4037- out. push_sql ( direction. as_str ( ) ) ;
4014+ out. push_sql ( direction. as_sql ( ) ) ;
40384015
40394016 if UseBlockColumn :: Yes == use_block_column {
40404017 out. push_sql ( ", coalesce(" ) ;
@@ -4048,8 +4025,8 @@ impl<'a> SortKey<'a> {
40484025
40494026 child. child_br . walk_ast ( out. reborrow ( ) ) ?;
40504027 }
4051- out. push_sql ( ") " ) ;
4052- out. push_sql ( direction. as_str ( ) ) ;
4028+ out. push_sql ( ")" ) ;
4029+ out. push_sql ( direction. as_sql ( ) ) ;
40534030 }
40544031
40554032 Ok ( ( ) )
0 commit comments