Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate find_with_* result with HashMap #1733

Closed
billy1624 opened this issue Jun 30, 2023 · 1 comment
Closed

Consolidate find_with_* result with HashMap #1733

billy1624 opened this issue Jun 30, 2023 · 1 comment
Milestone

Comments

@billy1624
Copy link
Member

Motivation

Currently, find_with_* method will produce a query like below, where ORDER BY is required to make sure it's in proper order to be consolidated.

SELECT `container`.`db_id` AS `A_db_id`, `content`.`id` AS `B_id`, `content`.`container_id` AS `B_container_id`
FROM `container`
LEFT JOIN `content` ON `container`.`db_id` = `content`.`container_id`
ORDER BY `container`.`db_id` ASC

The consolidation logic is defined at

fn consolidate_query_result<L, R>(
rows: Vec<(L::Model, Option<R::Model>)>,
) -> Vec<(L::Model, Vec<R::Model>)>
where
L: EntityTrait,
R: EntityTrait,
{
let mut acc: Vec<(L::Model, Vec<R::Model>)> = Vec::new();
for (l, r) in rows {
if let Some((last_l, last_r)) = acc.last_mut() {
let mut same_l = true;
for pk_col in <L::PrimaryKey as Iterable>::iter() {
let col = pk_col.into_column();
let val = l.get(col);
let last_val = last_l.get(col);
if !val.eq(&last_val) {
same_l = false;
break;
}
}
if same_l {
if let Some(r) = r {
last_r.push(r);
continue;
}
}
}
let rows = match r {
Some(r) => vec![r],
None => vec![],
};
acc.push((l, rows));
}
acc
}

@billy1624 billy1624 added this to the 0.12.x milestone Jun 30, 2023
@billy1624 billy1624 linked a pull request Jul 10, 2023 that will close this issue
4 tasks
@billy1624
Copy link
Member Author

Closed by #1743

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant