@@ -3122,10 +3122,8 @@ pub enum ChildKey<'a> {
31223122#[ derive( Debug , Clone ) ]
31233123pub enum SortKey < ' a > {
31243124 None ,
3125- /// Order by `id asc`
3126- IdAsc ( Option < dsl:: BlockColumn < ' a > > ) ,
3127- /// Order by `id desc`
3128- IdDesc ( Option < dsl:: BlockColumn < ' a > > ) ,
3125+ /// Order by `id <direction>, [block <direction>]`
3126+ Id ( SortDirection , Option < dsl:: BlockColumn < ' a > > ) ,
31293127 /// Order by some other column; `column` will never be `id`
31303128 Key {
31313129 column : dsl:: Column < ' a > ,
@@ -3141,25 +3139,26 @@ impl<'a> fmt::Display for SortKey<'a> {
31413139 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
31423140 match self {
31433141 SortKey :: None => write ! ( f, "none" ) ,
3144- SortKey :: IdAsc ( Option :: None ) => write ! ( f, "{}" , PRIMARY_KEY_COLUMN ) ,
3145- SortKey :: IdAsc ( Some ( br) ) => write ! ( f, "{}, {}" , PRIMARY_KEY_COLUMN , br) ,
3146- SortKey :: IdDesc ( Option :: None ) => write ! ( f, "{} desc" , PRIMARY_KEY_COLUMN ) ,
3147- SortKey :: IdDesc ( Some ( br) ) => {
3148- write ! ( f, "{} desc, {} desc" , PRIMARY_KEY_COLUMN , br)
3142+ SortKey :: Id ( direction, br) => {
3143+ write ! ( f, "{}{}" , PRIMARY_KEY_COLUMN , direction) ?;
3144+ if let Some ( br) = br {
3145+ write ! ( f, ", {} {}" , PRIMARY_KEY_COLUMN , br) ?;
3146+ }
3147+ Ok ( ( ) )
31493148 }
31503149 SortKey :: Key {
31513150 column,
31523151 value : _,
31533152 direction,
31543153 } => write ! (
31553154 f,
3156- "{} {}, {} {}" ,
3155+ "{}{}, {}{}" ,
31573156 column, direction, PRIMARY_KEY_COLUMN , direction
31583157 ) ,
31593158 SortKey :: ChildKey ( child) => match child {
31603159 ChildKey :: Single ( details) => write ! (
31613160 f,
3162- "{} {}, {} {}" ,
3161+ "{}{}, {}{}" ,
31633162 details. sort_by_column,
31643163 details. direction,
31653164 details. child_table. primary_key( ) ,
@@ -3168,7 +3167,7 @@ impl<'a> fmt::Display for SortKey<'a> {
31683167 ChildKey :: Many ( _, details) => details. iter ( ) . try_for_each ( |details| {
31693168 write ! (
31703169 f,
3171- "{} {}, {} {}" ,
3170+ "{}{}, {}{}" ,
31723171 details. sort_by_column,
31733172 details. direction,
31743173 details. child_table. primary_key( ) ,
@@ -3239,17 +3238,20 @@ pub enum SortDirection {
32393238}
32403239
32413240impl SortDirection {
3242- fn as_str ( & self ) -> & ' static str {
3241+ /// Generate either `""` or `" desc"`; convenient for SQL generation
3242+ /// without needing an additional space to separate it from preceding
3243+ /// text
3244+ fn as_sql ( & self ) -> & ' static str {
32433245 match self {
3244- SortDirection :: Asc => "asc " ,
3245- SortDirection :: Desc => "desc" ,
3246+ SortDirection :: Asc => "" ,
3247+ SortDirection :: Desc => " desc" ,
32463248 }
32473249 }
32483250}
32493251
32503252impl std:: fmt:: Display for SortDirection {
32513253 fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3252- write ! ( f, "{}" , self . as_str ( ) )
3254+ write ! ( f, "{}" , self . as_sql ( ) )
32533255 }
32543256}
32553257
@@ -3298,11 +3300,7 @@ impl<'a> SortKey<'a> {
32983300 }
32993301 } else if column. is_primary_key ( ) {
33003302 let block_column = use_block_column. block_column ( table) ;
3301- use SortDirection :: * ;
3302- match direction {
3303- Asc => Ok ( SortKey :: IdAsc ( block_column) ) ,
3304- Desc => Ok ( SortKey :: IdDesc ( block_column) ) ,
3305- }
3303+ Ok ( SortKey :: Id ( direction, block_column) )
33063304 } else {
33073305 Ok ( SortKey :: Key {
33083306 column,
@@ -3591,7 +3589,7 @@ impl<'a> SortKey<'a> {
35913589 EntityOrder :: Descending ( attr, _) => {
35923590 with_key ( table, attr, filter, Desc , use_block_column)
35933591 }
3594- EntityOrder :: Default => Ok ( SortKey :: IdAsc ( use_block_column. block_column ( table) ) ) ,
3592+ EntityOrder :: Default => Ok ( SortKey :: Id ( Asc , use_block_column. block_column ( table) ) ) ,
35953593 EntityOrder :: Unordered => Ok ( SortKey :: None ) ,
35963594 EntityOrder :: ChildAscending ( kind) => match kind {
35973595 EntityOrderByChild :: Object ( child, entity_type) => with_child_object_key (
@@ -3646,7 +3644,7 @@ impl<'a> SortKey<'a> {
36463644 ) -> QueryResult < ( ) > {
36473645 match self {
36483646 SortKey :: None => { }
3649- SortKey :: IdAsc ( br_column ) | SortKey :: IdDesc ( br_column) => {
3647+ SortKey :: Id ( _ , br_column) => {
36503648 if let Some ( br_column) = br_column {
36513649 out. push_sql ( ", " ) ;
36523650
@@ -3746,24 +3744,10 @@ impl<'a> SortKey<'a> {
37463744 use SortDirection :: * ;
37473745 match self {
37483746 SortKey :: None => Ok ( ( ) ) ,
3749- SortKey :: IdAsc ( br_column) => {
3750- out. push_sql ( "order by " ) ;
3751- out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3752- if let Some ( br_column) = br_column {
3753- if use_sort_key_alias {
3754- out. push_sql ( ", " ) ;
3755- out. push_sql ( SORT_KEY_COLUMN ) ;
3756- } else {
3757- out. push_sql ( ", " ) ;
3758- out. push_sql ( br_column. name ( ) ) ;
3759- }
3760- }
3761- Ok ( ( ) )
3762- }
3763- SortKey :: IdDesc ( br_column) => {
3747+ SortKey :: Id ( direction, br_column) => {
37643748 out. push_sql ( "order by " ) ;
37653749 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3766- out. push_sql ( " desc" ) ;
3750+ out. push_sql ( direction . as_sql ( ) ) ;
37673751 if let Some ( br_column) = br_column {
37683752 if use_sort_key_alias {
37693753 out. push_sql ( ", " ) ;
@@ -3772,7 +3756,7 @@ impl<'a> SortKey<'a> {
37723756 out. push_sql ( ", " ) ;
37733757 out. push_sql ( br_column. name ( ) ) ;
37743758 }
3775- out. push_sql ( " desc" ) ;
3759+ out. push_sql ( direction . as_sql ( ) ) ;
37763760 }
37773761 Ok ( ( ) )
37783762 }
@@ -3850,14 +3834,10 @@ impl<'a> SortKey<'a> {
38503834
38513835 match self {
38523836 SortKey :: None => Ok ( ( ) ) ,
3853- SortKey :: IdAsc ( _) => {
3854- order_by_parent_id ( out) ;
3855- out. push_identifier ( PRIMARY_KEY_COLUMN )
3856- }
3857- SortKey :: IdDesc ( _) => {
3837+ SortKey :: Id ( direction, _) => {
38583838 order_by_parent_id ( out) ;
38593839 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3860- out. push_sql ( " desc" ) ;
3840+ out. push_sql ( direction . as_sql ( ) ) ;
38613841 Ok ( ( ) )
38623842 }
38633843 SortKey :: Key {
@@ -3924,15 +3904,13 @@ impl<'a> SortKey<'a> {
39243904 }
39253905 }
39263906 }
3927- out. push_sql ( " " ) ;
3928- out. push_sql ( direction. as_str ( ) ) ;
3907+ out. push_sql ( direction. as_sql ( ) ) ;
39293908 out. push_sql ( ", " ) ;
39303909 if !use_sort_key_alias {
39313910 push_prefix ( rest_prefix, out) ;
39323911 }
39333912 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3934- out. push_sql ( " " ) ;
3935- out. push_sql ( direction. as_str ( ) ) ;
3913+ out. push_sql ( direction. as_sql ( ) ) ;
39363914 Ok ( ( ) )
39373915 }
39383916
@@ -3974,14 +3952,13 @@ impl<'a> SortKey<'a> {
39743952 child. sort_by_column . walk_ast ( out. reborrow ( ) ) ?;
39753953 }
39763954
3977- out. push_sql ( ") " ) ;
3955+ out. push_sql ( ")" ) ;
39783956
3979- out. push_sql ( direction. as_str ( ) ) ;
3957+ out. push_sql ( direction. as_sql ( ) ) ;
39803958 out. push_sql ( ", " ) ;
39813959
39823960 parent_pk. walk_ast ( out. reborrow ( ) ) ?;
3983- out. push_sql ( " " ) ;
3984- out. push_sql ( direction. as_str ( ) ) ;
3961+ out. push_sql ( direction. as_sql ( ) ) ;
39853962 Ok ( ( ) )
39863963 }
39873964
@@ -4004,9 +3981,9 @@ impl<'a> SortKey<'a> {
40043981
40053982 child. child_join_column . walk_ast ( out. reborrow ( ) ) ?;
40063983 }
4007- out. push_sql ( ") " ) ;
3984+ out. push_sql ( ")" ) ;
40083985
4009- out. push_sql ( direction. as_str ( ) ) ;
3986+ out. push_sql ( direction. as_sql ( ) ) ;
40103987
40113988 if UseBlockColumn :: Yes == use_block_column {
40123989 out. push_sql ( ", coalesce(" ) ;
@@ -4020,8 +3997,8 @@ impl<'a> SortKey<'a> {
40203997
40213998 child. child_br . walk_ast ( out. reborrow ( ) ) ?;
40223999 }
4023- out. push_sql ( ") " ) ;
4024- out. push_sql ( direction. as_str ( ) ) ;
4000+ out. push_sql ( ")" ) ;
4001+ out. push_sql ( direction. as_sql ( ) ) ;
40254002 }
40264003
40274004 Ok ( ( ) )
0 commit comments