Skip to content

Commit 4d39ad8

Browse files
committed
store: Combine ChildKey::IdAsc and ChildKey::IdDesc
1 parent dec0152 commit 4d39ad8

File tree

1 file changed

+25
-60
lines changed

1 file changed

+25
-60
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3139,8 +3139,7 @@ pub enum ChildKey<'a> {
31393139
Single(ChildKeyDetails<'a>),
31403140
/// First column is the primary key of the parent
31413141
Many(dsl::Column<'a>, Vec<ChildKeyDetails<'a>>),
3142-
IdAsc(ChildIdDetails<'a>, UseBlockColumn),
3143-
IdDesc(ChildIdDetails<'a>, UseBlockColumn),
3142+
Id(SortDirection, ChildIdDetails<'a>, UseBlockColumn),
31443143
ManyIdAsc(Vec<ChildIdDetails<'a>>, UseBlockColumn),
31453144
ManyIdDesc(Vec<ChildIdDetails<'a>>, UseBlockColumn),
31463145
}
@@ -3232,24 +3231,13 @@ impl<'a> fmt::Display for SortKey<'a> {
32323231
})
32333232
}
32343233

3235-
ChildKey::IdAsc(details, UseBlockColumn::No) => {
3236-
write!(f, "{}", details.child_table.primary_key())
3234+
ChildKey::Id(direction, details, UseBlockColumn::No) => {
3235+
write!(f, "{}{}", details.child_table.primary_key(), direction)
32373236
}
3238-
ChildKey::IdAsc(details, UseBlockColumn::Yes) => {
3237+
ChildKey::Id(direction, details, UseBlockColumn::Yes) => {
32393238
write!(
32403239
f,
3241-
"{}, {}",
3242-
details.child_table.primary_key(),
3243-
details.child_br
3244-
)
3245-
}
3246-
ChildKey::IdDesc(details, UseBlockColumn::No) => {
3247-
write!(f, "{} desc", details.child_table.primary_key(),)
3248-
}
3249-
ChildKey::IdDesc(details, UseBlockColumn::Yes) => {
3250-
write!(
3251-
f,
3252-
"{} desc, {} desc",
3240+
"{}{direction}, {}{direction}",
32533241
details.child_table.primary_key(),
32543242
details.child_br
32553243
)
@@ -3385,33 +3373,20 @@ impl<'a> SortKey<'a> {
33853373
let child_pk = child_table.primary_key();
33863374
let child_br = child_table.block_column();
33873375
let child_at_block = child_table.at_block(block);
3388-
use SortDirection::*;
3389-
return match direction {
3390-
Asc => Ok(SortKey::ChildKey(ChildKey::IdAsc(
3391-
ChildIdDetails {
3392-
child_table,
3393-
child_from,
3394-
parent_join_column: parent_column,
3395-
child_join_column: child_column,
3396-
child_pk,
3397-
child_br,
3398-
child_at_block,
3399-
},
3400-
use_block_column,
3401-
))),
3402-
Desc => Ok(SortKey::ChildKey(ChildKey::IdDesc(
3403-
ChildIdDetails {
3404-
child_table,
3405-
child_from,
3406-
parent_join_column: parent_column,
3407-
child_join_column: child_column,
3408-
child_pk,
3409-
child_br,
3410-
child_at_block,
3411-
},
3412-
use_block_column,
3413-
))),
3414-
};
3376+
3377+
return Ok(SortKey::ChildKey(ChildKey::Id(
3378+
direction,
3379+
ChildIdDetails {
3380+
child_table,
3381+
child_from,
3382+
parent_join_column: parent_column,
3383+
child_join_column: child_column,
3384+
child_pk,
3385+
child_br,
3386+
child_at_block,
3387+
},
3388+
use_block_column,
3389+
)));
34153390
}
34163391

34173392
let child_table = child_table.child(1);
@@ -3744,13 +3719,11 @@ impl<'a> SortKey<'a> {
37443719
}
37453720
ChildKey::ManyIdAsc(_, UseBlockColumn::No)
37463721
| ChildKey::ManyIdDesc(_, UseBlockColumn::No) => { /* nothing to do */ }
3747-
ChildKey::IdAsc(child, UseBlockColumn::Yes)
3748-
| ChildKey::IdDesc(child, UseBlockColumn::Yes) => {
3722+
ChildKey::Id(_, child, UseBlockColumn::Yes) => {
37493723
out.push_sql(", ");
37503724
child.child_br.walk_ast(out.reborrow())?;
37513725
}
3752-
ChildKey::IdAsc(_, UseBlockColumn::No)
3753-
| ChildKey::IdDesc(_, UseBlockColumn::No) => { /* nothing to do */ }
3726+
ChildKey::Id(_, _, UseBlockColumn::No) => { /* nothing to do */ }
37543727
}
37553728

37563729
if let SelectStatementLevel::InnerStatement = select_statement_level {
@@ -3821,21 +3794,13 @@ impl<'a> SortKey<'a> {
38213794
SortKey::multi_sort_id_expr(children, Desc, *use_block_column, out)
38223795
}
38233796

3824-
ChildKey::IdAsc(child, use_block_column) => {
3825-
child.child_pk.walk_ast(out.reborrow())?;
3826-
if UseBlockColumn::Yes == *use_block_column {
3827-
out.push_sql(", ");
3828-
child.child_br.walk_ast(out.reborrow())?;
3829-
}
3830-
Ok(())
3831-
}
3832-
ChildKey::IdDesc(child, use_block_column) => {
3797+
ChildKey::Id(direction, child, use_block_column) => {
38333798
child.child_pk.walk_ast(out.reborrow())?;
3834-
out.push_sql(" desc");
3799+
out.push_sql(direction.as_sql());
38353800
if UseBlockColumn::Yes == *use_block_column {
38363801
out.push_sql(", ");
38373802
child.child_br.walk_ast(out.reborrow())?;
3838-
out.push_sql(" desc");
3803+
out.push_sql(direction.as_sql());
38393804
}
38403805
Ok(())
38413806
}
@@ -4103,7 +4068,7 @@ impl<'a> SortKey<'a> {
41034068
)?;
41044069
}
41054070
}
4106-
ChildKey::IdAsc(child, _) | ChildKey::IdDesc(child, _) => {
4071+
ChildKey::Id(_, child, _) => {
41074072
add(
41084073
&child.child_from,
41094074
&child.child_join_column,

0 commit comments

Comments
 (0)