Skip to content

Commit 989c980

Browse files
committed
store: Combine SortKey::ManyIdAsc and SortKey::ManyIdDesc
1 parent 7848017 commit 989c980

File tree

1 file changed

+34
-79
lines changed

1 file changed

+34
-79
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 34 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,8 +3112,7 @@ pub enum ChildKey<'a> {
31123112
/// First column is the primary key of the parent
31133113
Many(dsl::Column<'a>, Vec<ChildKeyDetails<'a>>),
31143114
Id(SortDirection, ChildIdDetails<'a>, UseBlockColumn),
3115-
ManyIdAsc(Vec<ChildIdDetails<'a>>, UseBlockColumn),
3116-
ManyIdDesc(Vec<ChildIdDetails<'a>>, UseBlockColumn),
3115+
ManyId(SortDirection, Vec<ChildIdDetails<'a>>, UseBlockColumn),
31173116
}
31183117

31193118
/// Convenience to pass the name of the column to order by around. If `name`
@@ -3174,35 +3173,21 @@ impl<'a> fmt::Display for SortKey<'a> {
31743173
)
31753174
}),
31763175

3177-
ChildKey::ManyIdAsc(details, UseBlockColumn::No) => details
3178-
.iter()
3179-
.try_for_each(|details| write!(f, "{}", details.child_table.primary_key())),
3180-
ChildKey::ManyIdAsc(details, UseBlockColumn::Yes) => {
3181-
details.iter().try_for_each(|details| {
3182-
write!(
3183-
f,
3184-
"{}, {}",
3185-
details.child_table.primary_key(),
3186-
details.child_table.block_column()
3187-
)
3188-
})
3189-
}
3190-
ChildKey::ManyIdDesc(details, UseBlockColumn::No) => {
3176+
ChildKey::ManyId(direction, details, UseBlockColumn::No) => {
31913177
details.iter().try_for_each(|details| {
3192-
write!(f, "{} desc", details.child_table.primary_key(),)
3178+
write!(f, "{}{direction}", details.child_table.primary_key())
31933179
})
31943180
}
3195-
ChildKey::ManyIdDesc(details, UseBlockColumn::Yes) => {
3181+
ChildKey::ManyId(direction, details, UseBlockColumn::Yes) => {
31963182
details.iter().try_for_each(|details| {
31973183
write!(
31983184
f,
3199-
"{} desc, {} desc",
3185+
"{}{direction}, {}{direction}",
32003186
details.child_table.primary_key(),
3201-
details.child_br
3187+
details.child_table.block_column()
32023188
)
32033189
})
32043190
}
3205-
32063191
ChildKey::Id(direction, details, UseBlockColumn::No) => {
32073192
write!(f, "{}{}", details.child_table.primary_key(), direction)
32083193
}
@@ -3471,53 +3456,29 @@ impl<'a> SortKey<'a> {
34713456
"Sorting by fulltext fields".to_string(),
34723457
))
34733458
} else if sort_by_column.is_primary_key() {
3474-
use SortDirection::*;
3475-
match direction {
3476-
Asc => Ok(SortKey::ChildKey(ChildKey::ManyIdAsc(
3477-
build_children_vec(
3478-
layout,
3479-
block,
3480-
parent_table,
3481-
entity_types,
3482-
child,
3483-
direction,
3484-
)?
3485-
.iter()
3486-
.map(|details| ChildIdDetails {
3487-
child_table: details.child_table,
3488-
child_from: details.child_from,
3489-
parent_join_column: details.parent_join_column,
3490-
child_join_column: details.child_join_column,
3491-
child_pk: details.child_pk,
3492-
child_br: details.child_br,
3493-
child_at_block: details.child_at_block,
3494-
})
3495-
.collect(),
3496-
use_block_column,
3497-
))),
3498-
Desc => Ok(SortKey::ChildKey(ChildKey::ManyIdDesc(
3499-
build_children_vec(
3500-
layout,
3501-
block,
3502-
parent_table,
3503-
entity_types,
3504-
child,
3505-
direction,
3506-
)?
3507-
.iter()
3508-
.map(|details| ChildIdDetails {
3509-
child_table: details.child_table,
3510-
child_from: details.child_from,
3511-
parent_join_column: details.parent_join_column,
3512-
child_join_column: details.child_join_column,
3513-
child_pk: details.child_pk,
3514-
child_br: details.child_br,
3515-
child_at_block: details.child_at_block,
3516-
})
3517-
.collect(),
3518-
use_block_column,
3519-
))),
3520-
}
3459+
Ok(SortKey::ChildKey(ChildKey::ManyId(
3460+
direction,
3461+
build_children_vec(
3462+
layout,
3463+
block,
3464+
parent_table,
3465+
entity_types,
3466+
child,
3467+
direction,
3468+
)?
3469+
.iter()
3470+
.map(|details| ChildIdDetails {
3471+
child_table: details.child_table,
3472+
child_from: details.child_from,
3473+
parent_join_column: details.parent_join_column,
3474+
child_join_column: details.child_join_column,
3475+
child_pk: details.child_pk,
3476+
child_br: details.child_br,
3477+
child_at_block: details.child_at_block,
3478+
})
3479+
.collect(),
3480+
use_block_column,
3481+
)))
35213482
} else {
35223483
Ok(SortKey::ChildKey(ChildKey::Many(
35233484
parent_table.primary_key(),
@@ -3682,15 +3643,13 @@ impl<'a> SortKey<'a> {
36823643
child.sort_by_column.walk_ast(out.reborrow())?;
36833644
}
36843645
}
3685-
ChildKey::ManyIdAsc(children, UseBlockColumn::Yes)
3686-
| ChildKey::ManyIdDesc(children, UseBlockColumn::Yes) => {
3646+
ChildKey::ManyId(_, children, UseBlockColumn::Yes) => {
36873647
for child in children.iter() {
36883648
out.push_sql(", ");
36893649
child.child_br.walk_ast(out.reborrow())?;
36903650
}
36913651
}
3692-
ChildKey::ManyIdAsc(_, UseBlockColumn::No)
3693-
| ChildKey::ManyIdDesc(_, UseBlockColumn::No) => { /* nothing to do */ }
3652+
ChildKey::ManyId(_, _, UseBlockColumn::No) => { /* nothing to do */ }
36943653
ChildKey::Id(_, child, UseBlockColumn::Yes) => {
36953654
out.push_sql(", ");
36963655
child.child_br.walk_ast(out.reborrow())?;
@@ -3714,7 +3673,6 @@ impl<'a> SortKey<'a> {
37143673
out: &mut AstPass<'_, 'b, Pg>,
37153674
use_sort_key_alias: bool,
37163675
) -> QueryResult<()> {
3717-
use SortDirection::*;
37183676
match self {
37193677
SortKey::None => Ok(()),
37203678
SortKey::Id(direction, br_column) => {
@@ -3759,11 +3717,8 @@ impl<'a> SortKey<'a> {
37593717
out,
37603718
),
37613719

3762-
ChildKey::ManyIdAsc(children, use_block_column) => {
3763-
SortKey::multi_sort_id_expr(children, Asc, *use_block_column, out)
3764-
}
3765-
ChildKey::ManyIdDesc(children, use_block_column) => {
3766-
SortKey::multi_sort_id_expr(children, Desc, *use_block_column, out)
3720+
ChildKey::ManyId(direction, children, use_block_column) => {
3721+
SortKey::multi_sort_id_expr(children, *direction, *use_block_column, out)
37673722
}
37683723

37693724
ChildKey::Id(direction, child, use_block_column) => {
@@ -4029,7 +3984,7 @@ impl<'a> SortKey<'a> {
40293984
)?;
40303985
}
40313986
}
4032-
ChildKey::ManyIdAsc(children, _) | ChildKey::ManyIdDesc(children, _) => {
3987+
ChildKey::ManyId(_, children, _) => {
40333988
for child in children.iter() {
40343989
add(
40353990
&child.child_from,

0 commit comments

Comments
 (0)