@@ -1953,7 +1953,7 @@ impl<'a> ParentLimit<'a> {
1953
1953
fn restrict ( & self , out : & mut AstPass < Pg > ) -> QueryResult < ( ) > {
1954
1954
if let ParentLimit :: Ranked ( sort_key, range) = self {
1955
1955
out. push_sql ( " " ) ;
1956
- sort_key. order_by ( out) ?;
1956
+ sort_key. order_by ( out, false ) ?;
1957
1957
range. walk_ast ( out. reborrow ( ) ) ?;
1958
1958
}
1959
1959
Ok ( ( ) )
@@ -2319,7 +2319,7 @@ impl<'a> FilterWindow<'a> {
2319
2319
out. push_sql ( "select '" ) ;
2320
2320
out. push_sql ( self . table . object . as_str ( ) ) ;
2321
2321
out. push_sql ( "' as entity, c.id, c.vid, p.id::text as g$parent_id" ) ;
2322
- sort_key. select ( & mut out) ?;
2322
+ sort_key. select ( & mut out, false ) ?;
2323
2323
self . children ( ParentLimit :: Outer , block, out)
2324
2324
}
2325
2325
@@ -2810,15 +2810,20 @@ impl<'a> SortKey<'a> {
2810
2810
}
2811
2811
2812
2812
/// Generate selecting the sort key if it is needed
2813
- fn select ( & self , out : & mut AstPass < Pg > ) -> QueryResult < ( ) > {
2813
+ fn select ( & self , out : & mut AstPass < Pg > , select_sort_key_directly : bool ) -> QueryResult < ( ) > {
2814
2814
match self {
2815
- SortKey :: None => Ok ( ( ) ) ,
2815
+ SortKey :: None => { }
2816
2816
SortKey :: IdAsc ( br_column) | SortKey :: IdDesc ( br_column) => {
2817
2817
if let Some ( br_column) = br_column {
2818
2818
out. push_sql ( ", " ) ;
2819
- br_column. name ( out) ;
2819
+
2820
+ if select_sort_key_directly {
2821
+ out. push_sql ( "sort_key$" ) ;
2822
+ } else {
2823
+ br_column. name ( out) ;
2824
+ out. push_sql ( " as sort_key$" ) ;
2825
+ }
2820
2826
}
2821
- Ok ( ( ) )
2822
2827
}
2823
2828
SortKey :: Key {
2824
2829
column,
@@ -2828,46 +2833,62 @@ impl<'a> SortKey<'a> {
2828
2833
if column. is_primary_key ( ) {
2829
2834
return Err ( constraint_violation ! ( "SortKey::Key never uses 'id'" ) ) ;
2830
2835
}
2831
- out. push_sql ( ", c." ) ;
2832
- out. push_identifier ( column. name . as_str ( ) ) ?;
2833
- Ok ( ( ) )
2836
+ if select_sort_key_directly {
2837
+ out. push_sql ( ", sort_key$" ) ;
2838
+ } else {
2839
+ out. push_sql ( ", c." ) ;
2840
+ out. push_identifier ( column. name . as_str ( ) ) ?;
2841
+ out. push_sql ( " as sort_key$" ) ;
2842
+ }
2834
2843
}
2835
- SortKey :: ChildKey ( nested) => {
2836
- match nested {
2837
- ChildKey :: Single ( child) => {
2844
+ SortKey :: ChildKey ( nested) => match nested {
2845
+ ChildKey :: Single ( child) => {
2846
+ if child. column . is_primary_key ( ) {
2847
+ return Err ( constraint_violation ! ( "SortKey::Key never uses 'id'" ) ) ;
2848
+ }
2849
+ if select_sort_key_directly {
2850
+ out. push_sql ( ", sort_key$" ) ;
2851
+ } else {
2852
+ out. push_sql ( format ! ( ", {}." , child. prefix) . as_str ( ) ) ;
2853
+ out. push_identifier ( child. column . name . as_str ( ) ) ?;
2854
+ out. push_sql ( " as sort_key$" ) ;
2855
+ }
2856
+ }
2857
+ ChildKey :: Many ( children) => {
2858
+ if select_sort_key_directly {
2859
+ return Err ( constraint_violation ! ( "Kamil, please fix me :(" ) ) ;
2860
+ }
2861
+
2862
+ for child in children. iter ( ) {
2838
2863
if child. column . is_primary_key ( ) {
2839
2864
return Err ( constraint_violation ! ( "SortKey::Key never uses 'id'" ) ) ;
2840
2865
}
2841
2866
out. push_sql ( format ! ( ", {}." , child. prefix) . as_str ( ) ) ;
2842
2867
out. push_identifier ( child. column . name . as_str ( ) ) ?;
2843
2868
}
2844
- ChildKey :: Many ( children) => {
2845
- for child in children. iter ( ) {
2846
- if child. column . is_primary_key ( ) {
2847
- return Err ( constraint_violation ! ( "SortKey::Key never uses 'id'" ) ) ;
2848
- }
2849
- out. push_sql ( format ! ( ", {}." , child. prefix) . as_str ( ) ) ;
2850
- out. push_identifier ( child. column . name . as_str ( ) ) ?;
2851
- }
2852
- }
2853
- }
2854
2869
2855
- Ok ( ( ) )
2856
- }
2870
+ out. push_sql ( " as sort_key$" ) ;
2871
+ }
2872
+ } ,
2857
2873
}
2874
+ Ok ( ( ) )
2858
2875
}
2859
2876
2860
2877
/// Generate
2861
2878
/// order by [name direction], id
2862
- fn order_by ( & self , out : & mut AstPass < Pg > ) -> QueryResult < ( ) > {
2879
+ fn order_by ( & self , out : & mut AstPass < Pg > , use_sort_key_alias : bool ) -> QueryResult < ( ) > {
2863
2880
match self {
2864
2881
SortKey :: None => Ok ( ( ) ) ,
2865
2882
SortKey :: IdAsc ( br_column) => {
2866
2883
out. push_sql ( "order by " ) ;
2867
2884
out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
2868
2885
if let Some ( br_column) = br_column {
2869
- out. push_sql ( ", " ) ;
2870
- br_column. bare_name ( out) ;
2886
+ if use_sort_key_alias {
2887
+ out. push_sql ( ", sort_key$" ) ;
2888
+ } else {
2889
+ out. push_sql ( ", " ) ;
2890
+ br_column. bare_name ( out) ;
2891
+ }
2871
2892
}
2872
2893
Ok ( ( ) )
2873
2894
}
@@ -2876,8 +2897,12 @@ impl<'a> SortKey<'a> {
2876
2897
out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
2877
2898
out. push_sql ( " desc" ) ;
2878
2899
if let Some ( br_column) = br_column {
2879
- out. push_sql ( ", " ) ;
2880
- br_column. bare_name ( out) ;
2900
+ if use_sort_key_alias {
2901
+ out. push_sql ( ", sort_key$" ) ;
2902
+ } else {
2903
+ out. push_sql ( ", " ) ;
2904
+ br_column. bare_name ( out) ;
2905
+ }
2881
2906
out. push_sql ( " desc" ) ;
2882
2907
}
2883
2908
Ok ( ( ) )
@@ -2888,7 +2913,15 @@ impl<'a> SortKey<'a> {
2888
2913
direction,
2889
2914
} => {
2890
2915
out. push_sql ( "order by " ) ;
2891
- SortKey :: sort_expr ( column, value, direction, None , None , out)
2916
+ SortKey :: sort_expr (
2917
+ column,
2918
+ value,
2919
+ direction,
2920
+ None ,
2921
+ None ,
2922
+ use_sort_key_alias,
2923
+ out,
2924
+ )
2892
2925
}
2893
2926
SortKey :: ChildKey ( child) => {
2894
2927
out. push_sql ( "order by " ) ;
@@ -2899,6 +2932,7 @@ impl<'a> SortKey<'a> {
2899
2932
child. direction ,
2900
2933
Some ( & child. prefix ) ,
2901
2934
Some ( "c" ) ,
2935
+ use_sort_key_alias,
2902
2936
out,
2903
2937
) ,
2904
2938
ChildKey :: Many ( children) => {
@@ -2939,7 +2973,7 @@ impl<'a> SortKey<'a> {
2939
2973
direction,
2940
2974
} => {
2941
2975
out. push_sql ( "order by g$parent_id, " ) ;
2942
- SortKey :: sort_expr ( column, value, direction, None , None , out)
2976
+ SortKey :: sort_expr ( column, value, direction, None , None , false , out)
2943
2977
}
2944
2978
SortKey :: ChildKey ( _) => {
2945
2979
return Err ( diesel:: result:: Error :: QueryBuilderError (
@@ -2957,6 +2991,7 @@ impl<'a> SortKey<'a> {
2957
2991
direction : & str ,
2958
2992
column_prefix : Option < & str > ,
2959
2993
rest_prefix : Option < & str > ,
2994
+ use_sort_key_alias : bool ,
2960
2995
out : & mut AstPass < Pg > ,
2961
2996
) -> QueryResult < ( ) > {
2962
2997
if column. is_primary_key ( ) {
@@ -2980,18 +3015,27 @@ impl<'a> SortKey<'a> {
2980
3015
FulltextAlgorithm :: ProximityRank => "ts_rank_cd(" ,
2981
3016
} ;
2982
3017
out. push_sql ( algorithm) ;
2983
- let name = column. name . as_str ( ) ;
2984
- push_prefix ( column_prefix, out) ;
2985
- out. push_identifier ( name) ?;
3018
+ if use_sort_key_alias {
3019
+ out. push_sql ( "sort_key$" ) ;
3020
+ } else {
3021
+ let name = column. name . as_str ( ) ;
3022
+ push_prefix ( column_prefix, out) ;
3023
+ out. push_identifier ( name) ?;
3024
+ }
3025
+
2986
3026
out. push_sql ( ", to_tsquery(" ) ;
2987
3027
2988
3028
out. push_bind_param :: < Text , _ > ( & value. unwrap ( ) ) ?;
2989
3029
out. push_sql ( "))" ) ;
2990
3030
}
2991
3031
_ => {
2992
- let name = column. name . as_str ( ) ;
2993
- push_prefix ( column_prefix, out) ;
2994
- out. push_identifier ( name) ?;
3032
+ if use_sort_key_alias {
3033
+ out. push_sql ( "sort_key$" ) ;
3034
+ } else {
3035
+ let name = column. name . as_str ( ) ;
3036
+ push_prefix ( column_prefix, out) ;
3037
+ out. push_identifier ( name) ?;
3038
+ }
2995
3039
}
2996
3040
}
2997
3041
if ENV_VARS . store . reversible_order_by_off {
@@ -3000,13 +3044,17 @@ impl<'a> SortKey<'a> {
3000
3044
out. push_sql ( direction) ;
3001
3045
out. push_sql ( " nulls last" ) ;
3002
3046
out. push_sql ( ", " ) ;
3003
- push_prefix ( rest_prefix, out) ;
3047
+ if !use_sort_key_alias {
3048
+ push_prefix ( rest_prefix, out) ;
3049
+ }
3004
3050
out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3005
3051
} else {
3006
3052
out. push_sql ( " " ) ;
3007
3053
out. push_sql ( direction) ;
3008
3054
out. push_sql ( ", " ) ;
3009
- push_prefix ( rest_prefix, out) ;
3055
+ if !use_sort_key_alias {
3056
+ push_prefix ( rest_prefix, out) ;
3057
+ }
3010
3058
out. push_identifier ( PRIMARY_KEY_COLUMN ) ?;
3011
3059
out. push_sql ( " " ) ;
3012
3060
out. push_sql ( direction) ;
@@ -3314,7 +3362,7 @@ impl<'a> FilterQuery<'a> {
3314
3362
write_column_names ( column_names, table, Some ( "c" ) , & mut out) ?;
3315
3363
self . filtered_rows ( table, filter, out. reborrow ( ) ) ?;
3316
3364
out. push_sql ( "\n " ) ;
3317
- self . sort_key . order_by ( & mut out) ?;
3365
+ self . sort_key . order_by ( & mut out, false ) ?;
3318
3366
self . range . walk_ast ( out. reborrow ( ) ) ?;
3319
3367
out. push_sql ( ") c" ) ;
3320
3368
Ok ( ( ) )
@@ -3389,11 +3437,11 @@ impl<'a> FilterQuery<'a> {
3389
3437
out. push_sql ( "select '" ) ;
3390
3438
out. push_sql ( table. object . as_str ( ) ) ;
3391
3439
out. push_sql ( "' as entity, c.id, c.vid" ) ;
3392
- self . sort_key . select ( & mut out) ?;
3440
+ self . sort_key . select ( & mut out, false ) ?;
3393
3441
self . filtered_rows ( table, filter, out. reborrow ( ) ) ?;
3394
3442
}
3395
3443
out. push_sql ( "\n " ) ;
3396
- self . sort_key . order_by ( & mut out) ?;
3444
+ self . sort_key . order_by ( & mut out, true ) ?;
3397
3445
self . range . walk_ast ( out. reborrow ( ) ) ?;
3398
3446
3399
3447
out. push_sql ( ")\n " ) ;
@@ -3406,7 +3454,7 @@ impl<'a> FilterQuery<'a> {
3406
3454
out. push_sql ( "select m.entity, " ) ;
3407
3455
jsonb_build_object ( column_names, "c" , table, & mut out) ?;
3408
3456
out. push_sql ( " as data, c.id" ) ;
3409
- self . sort_key . select ( & mut out) ?;
3457
+ self . sort_key . select ( & mut out, true ) ?;
3410
3458
out. push_sql ( "\n from " ) ;
3411
3459
out. push_sql ( table. qualified_name . as_str ( ) ) ;
3412
3460
out. push_sql ( " c," ) ;
@@ -3415,7 +3463,7 @@ impl<'a> FilterQuery<'a> {
3415
3463
out. push_bind_param :: < Text , _ > ( & table. object . as_str ( ) ) ?;
3416
3464
}
3417
3465
out. push_sql ( "\n " ) ;
3418
- self . sort_key . order_by ( & mut out) ?;
3466
+ self . sort_key . order_by ( & mut out, true ) ?;
3419
3467
Ok ( ( ) )
3420
3468
}
3421
3469
@@ -3466,7 +3514,7 @@ impl<'a> FilterQuery<'a> {
3466
3514
window. children_uniform ( & self . sort_key , self . block , out. reborrow ( ) ) ?;
3467
3515
}
3468
3516
out. push_sql ( "\n " ) ;
3469
- self . sort_key . order_by ( & mut out) ?;
3517
+ self . sort_key . order_by ( & mut out, true ) ?;
3470
3518
self . range . walk_ast ( out. reborrow ( ) ) ?;
3471
3519
out. push_sql ( ") c)\n " ) ;
3472
3520
0 commit comments