Skip to content

Commit 932dbcf

Browse files
committed
store: Combine SortKey::IdAsc and SortKey::IdDesc into one variant
1 parent 6678d9c commit 932dbcf

File tree

1 file changed

+35
-58
lines changed

1 file changed

+35
-58
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 35 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3157,10 +3157,8 @@ pub enum ChildKey<'a> {
31573157
#[derive(Debug, Clone)]
31583158
pub enum SortKey<'a> {
31593159
None,
3160-
/// Order by `id asc`
3161-
IdAsc(Option<dsl::BlockColumn<'a>>),
3162-
/// Order by `id desc`
3163-
IdDesc(Option<dsl::BlockColumn<'a>>),
3160+
/// Order by `id <direction>, [block <direction>]`
3161+
Id(SortDirection, Option<dsl::BlockColumn<'a>>),
31643162
/// Order by some other column; `column` will never be `id`
31653163
Key {
31663164
column: dsl::Column<'a>,
@@ -3176,25 +3174,26 @@ impl<'a> fmt::Display for SortKey<'a> {
31763174
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31773175
match self {
31783176
SortKey::None => write!(f, "none"),
3179-
SortKey::IdAsc(Option::None) => write!(f, "{}", PRIMARY_KEY_COLUMN),
3180-
SortKey::IdAsc(Some(br)) => write!(f, "{}, {}", PRIMARY_KEY_COLUMN, br),
3181-
SortKey::IdDesc(Option::None) => write!(f, "{} desc", PRIMARY_KEY_COLUMN),
3182-
SortKey::IdDesc(Some(br)) => {
3183-
write!(f, "{} desc, {} desc", PRIMARY_KEY_COLUMN, br)
3177+
SortKey::Id(direction, br) => {
3178+
write!(f, "{}{}", PRIMARY_KEY_COLUMN, direction)?;
3179+
if let Some(br) = br {
3180+
write!(f, ", {} {}", PRIMARY_KEY_COLUMN, br)?;
3181+
}
3182+
Ok(())
31843183
}
31853184
SortKey::Key {
31863185
column,
31873186
value: _,
31883187
direction,
31893188
} => write!(
31903189
f,
3191-
"{} {}, {} {}",
3190+
"{}{}, {}{}",
31923191
column, direction, PRIMARY_KEY_COLUMN, direction
31933192
),
31943193
SortKey::ChildKey(child) => match child {
31953194
ChildKey::Single(details) => write!(
31963195
f,
3197-
"{} {}, {} {}",
3196+
"{}{}, {}{}",
31983197
details.sort_by_column,
31993198
details.direction,
32003199
details.child_table.primary_key(),
@@ -3203,7 +3202,7 @@ impl<'a> fmt::Display for SortKey<'a> {
32033202
ChildKey::Many(_, details) => details.iter().try_for_each(|details| {
32043203
write!(
32053204
f,
3206-
"{} {}, {} {}",
3205+
"{}{}, {}{}",
32073206
details.sort_by_column,
32083207
details.direction,
32093208
details.child_table.primary_key(),
@@ -3274,17 +3273,20 @@ pub enum SortDirection {
32743273
}
32753274

32763275
impl SortDirection {
3277-
fn as_str(&self) -> &'static str {
3276+
/// Generate either `""` or `" desc"`; convenient for SQL generation
3277+
/// without needing an additional space to separate it from preceding
3278+
/// text
3279+
fn as_sql(&self) -> &'static str {
32783280
match self {
3279-
SortDirection::Asc => "asc",
3280-
SortDirection::Desc => "desc",
3281+
SortDirection::Asc => "",
3282+
SortDirection::Desc => " desc",
32813283
}
32823284
}
32833285
}
32843286

32853287
impl std::fmt::Display for SortDirection {
32863288
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
3287-
write!(f, "{}", self.as_str())
3289+
write!(f, "{}", self.as_sql())
32883290
}
32893291
}
32903292

@@ -3333,11 +3335,7 @@ impl<'a> SortKey<'a> {
33333335
}
33343336
} else if column.is_primary_key() {
33353337
let block_column = use_block_column.block_column(table);
3336-
use SortDirection::*;
3337-
match direction {
3338-
Asc => Ok(SortKey::IdAsc(block_column)),
3339-
Desc => Ok(SortKey::IdDesc(block_column)),
3340-
}
3338+
Ok(SortKey::Id(direction, block_column))
33413339
} else {
33423340
Ok(SortKey::Key {
33433341
column,
@@ -3626,7 +3624,7 @@ impl<'a> SortKey<'a> {
36263624
EntityOrder::Descending(attr, _) => {
36273625
with_key(table, attr, filter, Desc, use_block_column)
36283626
}
3629-
EntityOrder::Default => Ok(SortKey::IdAsc(use_block_column.block_column(table))),
3627+
EntityOrder::Default => Ok(SortKey::Id(Asc, use_block_column.block_column(table))),
36303628
EntityOrder::Unordered => Ok(SortKey::None),
36313629
EntityOrder::ChildAscending(kind) => match kind {
36323630
EntityOrderByChild::Object(child, entity_type) => with_child_object_key(
@@ -3681,7 +3679,7 @@ impl<'a> SortKey<'a> {
36813679
) -> QueryResult<()> {
36823680
match self {
36833681
SortKey::None => {}
3684-
SortKey::IdAsc(br_column) | SortKey::IdDesc(br_column) => {
3682+
SortKey::Id(_, br_column) => {
36853683
if let Some(br_column) = br_column {
36863684
out.push_sql(", ");
36873685

@@ -3781,24 +3779,10 @@ impl<'a> SortKey<'a> {
37813779
use SortDirection::*;
37823780
match self {
37833781
SortKey::None => Ok(()),
3784-
SortKey::IdAsc(br_column) => {
3785-
out.push_sql("order by ");
3786-
out.push_identifier(PRIMARY_KEY_COLUMN)?;
3787-
if let Some(br_column) = br_column {
3788-
if use_sort_key_alias {
3789-
out.push_sql(", ");
3790-
out.push_sql(SORT_KEY_COLUMN);
3791-
} else {
3792-
out.push_sql(", ");
3793-
out.push_sql(br_column.name());
3794-
}
3795-
}
3796-
Ok(())
3797-
}
3798-
SortKey::IdDesc(br_column) => {
3782+
SortKey::Id(direction, br_column) => {
37993783
out.push_sql("order by ");
38003784
out.push_identifier(PRIMARY_KEY_COLUMN)?;
3801-
out.push_sql(" desc");
3785+
out.push_sql(direction.as_sql());
38023786
if let Some(br_column) = br_column {
38033787
if use_sort_key_alias {
38043788
out.push_sql(", ");
@@ -3807,7 +3791,7 @@ impl<'a> SortKey<'a> {
38073791
out.push_sql(", ");
38083792
out.push_sql(br_column.name());
38093793
}
3810-
out.push_sql(" desc");
3794+
out.push_sql(direction.as_sql());
38113795
}
38123796
Ok(())
38133797
}
@@ -3885,14 +3869,10 @@ impl<'a> SortKey<'a> {
38853869

38863870
match self {
38873871
SortKey::None => Ok(()),
3888-
SortKey::IdAsc(_) => {
3889-
order_by_parent_id(out);
3890-
out.push_identifier(PRIMARY_KEY_COLUMN)
3891-
}
3892-
SortKey::IdDesc(_) => {
3872+
SortKey::Id(direction, _) => {
38933873
order_by_parent_id(out);
38943874
out.push_identifier(PRIMARY_KEY_COLUMN)?;
3895-
out.push_sql(" desc");
3875+
out.push_sql(direction.as_sql());
38963876
Ok(())
38973877
}
38983878
SortKey::Key {
@@ -3959,15 +3939,13 @@ impl<'a> SortKey<'a> {
39593939
}
39603940
}
39613941
}
3962-
out.push_sql(" ");
3963-
out.push_sql(direction.as_str());
3942+
out.push_sql(direction.as_sql());
39643943
out.push_sql(", ");
39653944
if !use_sort_key_alias {
39663945
push_prefix(rest_prefix, out);
39673946
}
39683947
out.push_identifier(PRIMARY_KEY_COLUMN)?;
3969-
out.push_sql(" ");
3970-
out.push_sql(direction.as_str());
3948+
out.push_sql(direction.as_sql());
39713949
Ok(())
39723950
}
39733951

@@ -4009,14 +3987,13 @@ impl<'a> SortKey<'a> {
40093987
child.sort_by_column.walk_ast(out.reborrow())?;
40103988
}
40113989

4012-
out.push_sql(") ");
3990+
out.push_sql(")");
40133991

4014-
out.push_sql(direction.as_str());
3992+
out.push_sql(direction.as_sql());
40153993
out.push_sql(", ");
40163994

40173995
parent_pk.walk_ast(out.reborrow())?;
4018-
out.push_sql(" ");
4019-
out.push_sql(direction.as_str());
3996+
out.push_sql(direction.as_sql());
40203997
Ok(())
40213998
}
40223999

@@ -4039,9 +4016,9 @@ impl<'a> SortKey<'a> {
40394016

40404017
child.child_join_column.walk_ast(out.reborrow())?;
40414018
}
4042-
out.push_sql(") ");
4019+
out.push_sql(")");
40434020

4044-
out.push_sql(direction.as_str());
4021+
out.push_sql(direction.as_sql());
40454022

40464023
if UseBlockColumn::Yes == use_block_column {
40474024
out.push_sql(", coalesce(");
@@ -4055,8 +4032,8 @@ impl<'a> SortKey<'a> {
40554032

40564033
child.child_br.walk_ast(out.reborrow())?;
40574034
}
4058-
out.push_sql(") ");
4059-
out.push_sql(direction.as_str());
4035+
out.push_sql(")");
4036+
out.push_sql(direction.as_sql());
40604037
}
40614038

40624039
Ok(())

0 commit comments

Comments
 (0)