Skip to content

Commit 791157f

Browse files
committed
store: Combine ChildKey::IdAsc and ChildKey::IdDesc
1 parent 2280537 commit 791157f

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
@@ -3146,8 +3146,7 @@ pub enum ChildKey<'a> {
31463146
Single(ChildKeyDetails<'a>),
31473147
/// First column is the primary key of the parent
31483148
Many(dsl::Column<'a>, Vec<ChildKeyDetails<'a>>),
3149-
IdAsc(ChildIdDetails<'a>, UseBlockColumn),
3150-
IdDesc(ChildIdDetails<'a>, UseBlockColumn),
3149+
Id(SortDirection, ChildIdDetails<'a>, UseBlockColumn),
31513150
ManyIdAsc(Vec<ChildIdDetails<'a>>, UseBlockColumn),
31523151
ManyIdDesc(Vec<ChildIdDetails<'a>>, UseBlockColumn),
31533152
}
@@ -3239,24 +3238,13 @@ impl<'a> fmt::Display for SortKey<'a> {
32393238
})
32403239
}
32413240

3242-
ChildKey::IdAsc(details, UseBlockColumn::No) => {
3243-
write!(f, "{}", details.child_table.primary_key())
3241+
ChildKey::Id(direction, details, UseBlockColumn::No) => {
3242+
write!(f, "{}{}", details.child_table.primary_key(), direction)
32443243
}
3245-
ChildKey::IdAsc(details, UseBlockColumn::Yes) => {
3244+
ChildKey::Id(direction, details, UseBlockColumn::Yes) => {
32463245
write!(
32473246
f,
3248-
"{}, {}",
3249-
details.child_table.primary_key(),
3250-
details.child_br
3251-
)
3252-
}
3253-
ChildKey::IdDesc(details, UseBlockColumn::No) => {
3254-
write!(f, "{} desc", details.child_table.primary_key(),)
3255-
}
3256-
ChildKey::IdDesc(details, UseBlockColumn::Yes) => {
3257-
write!(
3258-
f,
3259-
"{} desc, {} desc",
3247+
"{}{direction}, {}{direction}",
32603248
details.child_table.primary_key(),
32613249
details.child_br
32623250
)
@@ -3392,33 +3380,20 @@ impl<'a> SortKey<'a> {
33923380
let child_pk = child_table.primary_key();
33933381
let child_br = child_table.block_column();
33943382
let child_at_block = child_table.at_block(block);
3395-
use SortDirection::*;
3396-
return match direction {
3397-
Asc => Ok(SortKey::ChildKey(ChildKey::IdAsc(
3398-
ChildIdDetails {
3399-
child_table,
3400-
child_from,
3401-
parent_join_column: parent_column,
3402-
child_join_column: child_column,
3403-
child_pk,
3404-
child_br,
3405-
child_at_block,
3406-
},
3407-
use_block_column,
3408-
))),
3409-
Desc => Ok(SortKey::ChildKey(ChildKey::IdDesc(
3410-
ChildIdDetails {
3411-
child_table,
3412-
child_from,
3413-
parent_join_column: parent_column,
3414-
child_join_column: child_column,
3415-
child_pk,
3416-
child_br,
3417-
child_at_block,
3418-
},
3419-
use_block_column,
3420-
))),
3421-
};
3383+
3384+
return Ok(SortKey::ChildKey(ChildKey::Id(
3385+
direction,
3386+
ChildIdDetails {
3387+
child_table,
3388+
child_from,
3389+
parent_join_column: parent_column,
3390+
child_join_column: child_column,
3391+
child_pk,
3392+
child_br,
3393+
child_at_block,
3394+
},
3395+
use_block_column,
3396+
)));
34223397
}
34233398

34243399
let child_table = child_table.child(1);
@@ -3751,13 +3726,11 @@ impl<'a> SortKey<'a> {
37513726
}
37523727
ChildKey::ManyIdAsc(_, UseBlockColumn::No)
37533728
| ChildKey::ManyIdDesc(_, UseBlockColumn::No) => { /* nothing to do */ }
3754-
ChildKey::IdAsc(child, UseBlockColumn::Yes)
3755-
| ChildKey::IdDesc(child, UseBlockColumn::Yes) => {
3729+
ChildKey::Id(_, child, UseBlockColumn::Yes) => {
37563730
out.push_sql(", ");
37573731
child.child_br.walk_ast(out.reborrow())?;
37583732
}
3759-
ChildKey::IdAsc(_, UseBlockColumn::No)
3760-
| ChildKey::IdDesc(_, UseBlockColumn::No) => { /* nothing to do */ }
3733+
ChildKey::Id(_, _, UseBlockColumn::No) => { /* nothing to do */ }
37613734
}
37623735

37633736
if let SelectStatementLevel::InnerStatement = select_statement_level {
@@ -3828,21 +3801,13 @@ impl<'a> SortKey<'a> {
38283801
SortKey::multi_sort_id_expr(children, Desc, *use_block_column, out)
38293802
}
38303803

3831-
ChildKey::IdAsc(child, use_block_column) => {
3832-
child.child_pk.walk_ast(out.reborrow())?;
3833-
if UseBlockColumn::Yes == *use_block_column {
3834-
out.push_sql(", ");
3835-
child.child_br.walk_ast(out.reborrow())?;
3836-
}
3837-
Ok(())
3838-
}
3839-
ChildKey::IdDesc(child, use_block_column) => {
3804+
ChildKey::Id(direction, child, use_block_column) => {
38403805
child.child_pk.walk_ast(out.reborrow())?;
3841-
out.push_sql(" desc");
3806+
out.push_sql(direction.as_sql());
38423807
if UseBlockColumn::Yes == *use_block_column {
38433808
out.push_sql(", ");
38443809
child.child_br.walk_ast(out.reborrow())?;
3845-
out.push_sql(" desc");
3810+
out.push_sql(direction.as_sql());
38463811
}
38473812
Ok(())
38483813
}
@@ -4110,7 +4075,7 @@ impl<'a> SortKey<'a> {
41104075
)?;
41114076
}
41124077
}
4113-
ChildKey::IdAsc(child, _) | ChildKey::IdDesc(child, _) => {
4078+
ChildKey::Id(_, child, _) => {
41144079
add(
41154080
&child.child_from,
41164081
&child.child_join_column,

0 commit comments

Comments
 (0)