@@ -3156,10 +3156,8 @@ pub enum ChildKey<'a> {
31563156#[ derive( Debug , Clone ) ]
31573157pub enum SortKey < ' a > {
31583158 None ,
3159- /// Order by `id asc`
3160- IdAsc ( Option < dsl:: BlockColumn < ' a > > ) ,
3161- /// Order by `id desc`
3162- IdDesc ( Option < dsl:: BlockColumn < ' a > > ) ,
3159+ /// Order by `id <direction>, [block <direction>]`
3160+ Id ( SortDirection , Option < dsl:: BlockColumn < ' a > > ) ,
31633161 /// Order by some other column; `column` will never be `id`
31643162 Key {
31653163 column : dsl:: Column < ' a > ,
@@ -3175,25 +3173,26 @@ impl<'a> fmt::Display for SortKey<'a> {
31753173 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
31763174 match self {
31773175 SortKey :: None => write ! ( f, "none" ) ,
3178- SortKey :: IdAsc ( Option :: None ) => write ! ( f, "{}" , PRIMARY_KEY_COLUMN ) ,
3179- SortKey :: IdAsc ( Some ( br) ) => write ! ( f, "{}, {}" , PRIMARY_KEY_COLUMN , br) ,
3180- SortKey :: IdDesc ( Option :: None ) => write ! ( f, "{} desc" , PRIMARY_KEY_COLUMN ) ,
3181- SortKey :: IdDesc ( Some ( br) ) => {
3182- write ! ( f, "{} desc, {} desc" , PRIMARY_KEY_COLUMN , br)
3176+ SortKey :: Id ( direction, br) => {
3177+ write ! ( f, "{}{}" , PRIMARY_KEY_COLUMN , direction) ?;
3178+ if let Some ( br) = br {
3179+ write ! ( f, ", {} {}" , PRIMARY_KEY_COLUMN , br) ?;
3180+ }
3181+ Ok ( ( ) )
31833182 }
31843183 SortKey :: Key {
31853184 column,
31863185 value : _,
31873186 direction,
31883187 } => write ! (
31893188 f,
3190- "{} {}, {} {}" ,
3189+ "{}{}, {}{}" ,
31913190 column, direction, PRIMARY_KEY_COLUMN , direction
31923191 ) ,
31933192 SortKey :: ChildKey ( child) => match child {
31943193 ChildKey :: Single ( details) => write ! (
31953194 f,
3196- "{} {}, {} {}" ,
3195+ "{}{}, {}{}" ,
31973196 details. sort_by_column,
31983197 details. direction,
31993198 details. child_table. primary_key( ) ,
@@ -3202,7 +3201,7 @@ impl<'a> fmt::Display for SortKey<'a> {
32023201 ChildKey :: Many ( _, details) => details. iter ( ) . try_for_each ( |details| {
32033202 write ! (
32043203 f,
3205- "{} {}, {} {}" ,
3204+ "{}{}, {}{}" ,
32063205 details. sort_by_column,
32073206 details. direction,
32083207 details. child_table. primary_key( ) ,
@@ -3273,17 +3272,20 @@ pub enum SortDirection {
32733272}
32743273
32753274impl SortDirection {
3276- fn as_str ( & self ) -> & ' static str {
3275+ /// Generate either `""` or `" desc"`; convenient for SQL generation
3276+ /// without needing an additional space to separate it from preceding
3277+ /// text
3278+ fn as_sql ( & self ) -> & ' static str {
32773279 match self {
3278- SortDirection :: Asc => "asc " ,
3279- SortDirection :: Desc => "desc" ,
3280+ SortDirection :: Asc => "" ,
3281+ SortDirection :: Desc => " desc" ,
32803282 }
32813283 }
32823284}
32833285
32843286impl std:: fmt:: Display for SortDirection {
32853287 fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3286- write ! ( f, "{}" , self . as_str ( ) )
3288+ write ! ( f, "{}" , self . as_sql ( ) )
32873289 }
32883290}
32893291
@@ -3332,11 +3334,7 @@ impl<'a> SortKey<'a> {
33323334 }
33333335 } else if column. is_primary_key ( ) {
33343336 let block_column = use_block_column. block_column ( table) ;
3335- use SortDirection :: * ;
3336- match direction {
3337- Asc => Ok ( SortKey :: IdAsc ( block_column) ) ,
3338- Desc => Ok ( SortKey :: IdDesc ( block_column) ) ,
3339- }
3337+ Ok ( SortKey :: Id ( direction, block_column) )
33403338 } else {
33413339 Ok ( SortKey :: Key {
33423340 column,
@@ -3625,7 +3623,7 @@ impl<'a> SortKey<'a> {
36253623 EntityOrder :: Descending ( attr, _) => {
36263624 with_key ( table, attr, filter, Desc , use_block_column)
36273625 }
3628- EntityOrder :: Default => Ok ( SortKey :: IdAsc ( use_block_column. block_column ( table) ) ) ,
3626+ EntityOrder :: Default => Ok ( SortKey :: Id ( Asc , use_block_column. block_column ( table) ) ) ,
36293627 EntityOrder :: Unordered => Ok ( SortKey :: None ) ,
36303628 EntityOrder :: ChildAscending ( kind) => match kind {
36313629 EntityOrderByChild :: Object ( child, entity_type) => with_child_object_key (
@@ -3680,7 +3678,7 @@ impl<'a> SortKey<'a> {
36803678 ) -> QueryResult < ( ) > {
36813679 match self {
36823680 SortKey :: None => { }
3683- SortKey :: IdAsc ( br_column ) | SortKey :: IdDesc ( br_column) => {
3681+ SortKey :: Id ( _ , br_column) => {
36843682 if let Some ( br_column) = br_column {
36853683 out. push_sql ( ", " ) ;
36863684
@@ -3780,24 +3778,10 @@ impl<'a> SortKey<'a> {
37803778 use SortDirection :: * ;
37813779 match self {
37823780 SortKey :: None => Ok ( ( ) ) ,
3783- SortKey :: IdAsc ( br_column) => {
3784- out. push_sql ( "order by " ) ;
3785- out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3786- if let Some ( br_column) = br_column {
3787- if use_sort_key_alias {
3788- out. push_sql ( ", " ) ;
3789- out. push_sql ( SORT_KEY_COLUMN ) ;
3790- } else {
3791- out. push_sql ( ", " ) ;
3792- out. push_sql ( br_column. name ( ) ) ;
3793- }
3794- }
3795- Ok ( ( ) )
3796- }
3797- SortKey :: IdDesc ( br_column) => {
3781+ SortKey :: Id ( direction, br_column) => {
37983782 out. push_sql ( "order by " ) ;
37993783 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3800- out. push_sql ( " desc" ) ;
3784+ out. push_sql ( direction . as_sql ( ) ) ;
38013785 if let Some ( br_column) = br_column {
38023786 if use_sort_key_alias {
38033787 out. push_sql ( ", " ) ;
@@ -3806,7 +3790,7 @@ impl<'a> SortKey<'a> {
38063790 out. push_sql ( ", " ) ;
38073791 out. push_sql ( br_column. name ( ) ) ;
38083792 }
3809- out. push_sql ( " desc" ) ;
3793+ out. push_sql ( direction . as_sql ( ) ) ;
38103794 }
38113795 Ok ( ( ) )
38123796 }
@@ -3884,14 +3868,10 @@ impl<'a> SortKey<'a> {
38843868
38853869 match self {
38863870 SortKey :: None => Ok ( ( ) ) ,
3887- SortKey :: IdAsc ( _) => {
3888- order_by_parent_id ( out) ;
3889- out. push_identifier ( PRIMARY_KEY_COLUMN )
3890- }
3891- SortKey :: IdDesc ( _) => {
3871+ SortKey :: Id ( direction, _) => {
38923872 order_by_parent_id ( out) ;
38933873 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3894- out. push_sql ( " desc" ) ;
3874+ out. push_sql ( direction . as_sql ( ) ) ;
38953875 Ok ( ( ) )
38963876 }
38973877 SortKey :: Key {
@@ -3958,15 +3938,13 @@ impl<'a> SortKey<'a> {
39583938 }
39593939 }
39603940 }
3961- out. push_sql ( " " ) ;
3962- out. push_sql ( direction. as_str ( ) ) ;
3941+ out. push_sql ( direction. as_sql ( ) ) ;
39633942 out. push_sql ( ", " ) ;
39643943 if !use_sort_key_alias {
39653944 push_prefix ( rest_prefix, out) ;
39663945 }
39673946 out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3968- out. push_sql ( " " ) ;
3969- out. push_sql ( direction. as_str ( ) ) ;
3947+ out. push_sql ( direction. as_sql ( ) ) ;
39703948 Ok ( ( ) )
39713949 }
39723950
@@ -4008,14 +3986,13 @@ impl<'a> SortKey<'a> {
40083986 child. sort_by_column . walk_ast ( out. reborrow ( ) ) ?;
40093987 }
40103988
4011- out. push_sql ( ") " ) ;
3989+ out. push_sql ( ")" ) ;
40123990
4013- out. push_sql ( direction. as_str ( ) ) ;
3991+ out. push_sql ( direction. as_sql ( ) ) ;
40143992 out. push_sql ( ", " ) ;
40153993
40163994 parent_pk. walk_ast ( out. reborrow ( ) ) ?;
4017- out. push_sql ( " " ) ;
4018- out. push_sql ( direction. as_str ( ) ) ;
3995+ out. push_sql ( direction. as_sql ( ) ) ;
40193996 Ok ( ( ) )
40203997 }
40213998
@@ -4038,9 +4015,9 @@ impl<'a> SortKey<'a> {
40384015
40394016 child. child_join_column . walk_ast ( out. reborrow ( ) ) ?;
40404017 }
4041- out. push_sql ( ") " ) ;
4018+ out. push_sql ( ")" ) ;
40424019
4043- out. push_sql ( direction. as_str ( ) ) ;
4020+ out. push_sql ( direction. as_sql ( ) ) ;
40444021
40454022 if UseBlockColumn :: Yes == use_block_column {
40464023 out. push_sql ( ", coalesce(" ) ;
@@ -4054,8 +4031,8 @@ impl<'a> SortKey<'a> {
40544031
40554032 child. child_br . walk_ast ( out. reborrow ( ) ) ?;
40564033 }
4057- out. push_sql ( ") " ) ;
4058- out. push_sql ( direction. as_str ( ) ) ;
4034+ out. push_sql ( ")" ) ;
4035+ out. push_sql ( direction. as_sql ( ) ) ;
40594036 }
40604037
40614038 Ok ( ( ) )
0 commit comments