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

Adding find_with_linked #1728

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/query/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ where
F: EntityTrait,
{
pub(crate) fn new(query: SelectStatement) -> Self {
Self::new_without_prepare(query)
.prepare_select()
.prepare_order_by()
}

pub(crate) fn new_without_prepare(query: SelectStatement) -> Self {
Self {
query,
entity: PhantomData,
}
.prepare_select()
.prepare_order_by()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we invoke prepare_order_by() method twice? When we call SelectTwoMany::new() it will invoke prepare_order_by() method twice.


Expand Down
46 changes: 46 additions & 0 deletions src/query/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,52 @@ where
}
select_two
}

/// Left Join with a Linked Entity and select Entity as a `Vec`.
pub fn find_with_linked<L, T>(self, l: L) -> SelectTwoMany<E, T>
where
L: Linked<FromEntity = E, ToEntity = T>,
T: EntityTrait,
{
let mut slf = self;
for (i, mut rel) in l.link().into_iter().enumerate() {
let to_tbl = Alias::new(format!("r{i}")).into_iden();
let from_tbl = if i > 0 {
Alias::new(format!("r{}", i - 1)).into_iden()
} else {
unpack_table_ref(&rel.from_tbl)
};
let table_ref = rel.to_tbl;

let mut condition = Condition::all().add(join_tbl_on_condition(
SeaRc::clone(&from_tbl),
SeaRc::clone(&to_tbl),
rel.from_col,
rel.to_col,
));
if let Some(f) = rel.on_condition.take() {
condition = condition.add(f(SeaRc::clone(&from_tbl), SeaRc::clone(&to_tbl)));
}

slf.query()
.join_as(JoinType::LeftJoin, table_ref, to_tbl, condition);
}
slf = slf.apply_alias(SelectA.as_str());
let mut select_two_many = SelectTwoMany::new_without_prepare(slf.query);
for col in <T::Column as Iterable>::iter() {
let alias = format!("{}{}", SelectB.as_str(), col.as_str());
let expr = Expr::col((
Alias::new(format!("r{}", l.link().len() - 1)).into_iden(),
col.into_iden(),
));
select_two_many.query().expr(SelectExpr {
expr: col.select_as(expr),
alias: Some(SeaRc::new(Alias::new(alias))),
window: None,
});
}
select_two_many
}
}

#[cfg(test)]
Expand Down
77 changes: 77 additions & 0 deletions tests/relational_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod common;

pub use chrono::offset::Utc;
pub use common::{bakery_chain::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
pub use rust_decimal::prelude::*;
pub use rust_decimal_macros::dec;
use sea_orm::{entity::*, query::*, DbErr, DerivePartialModel, FromQueryResult};
Expand Down Expand Up @@ -747,6 +748,82 @@ pub async fn linked() -> Result<(), DbErr> {
}]
);

let select_baker_with_customer = Baker::find().find_with_linked(baker::BakedForCustomer);

assert_eq!(
select_baker_with_customer
.build(sea_orm::DatabaseBackend::MySql)
.to_string(),
[
// FIXME: This might be faulty!
"SELECT `baker`.`id` AS `A_id`,",
"`baker`.`name` AS `A_name`,",
"`baker`.`contact_details` AS `A_contact_details`,",
"`baker`.`bakery_id` AS `A_bakery_id`,",
"`r4`.`id` AS `B_id`,",
"`r4`.`name` AS `B_name`,",
"`r4`.`notes` AS `B_notes`",
"FROM `baker`",
"LEFT JOIN `cakes_bakers` AS `r0` ON `baker`.`id` = `r0`.`baker_id`",
"LEFT JOIN `cake` AS `r1` ON `r0`.`cake_id` = `r1`.`id`",
"LEFT JOIN `lineitem` AS `r2` ON `r1`.`id` = `r2`.`cake_id`",
"LEFT JOIN `order` AS `r3` ON `r2`.`order_id` = `r3`.`id`",
"LEFT JOIN `customer` AS `r4` ON `r3`.`customer_id` = `r4`.`id`",
"ORDER BY `baker`.`id` ASC",
]
.join(" ")
);

assert_eq!(
select_baker_with_customer.all(&ctx.db).await?,
[
(
baker::Model {
id: 1,
name: "Baker Bob".into(),
contact_details: serde_json::json!({
"mobile": "+61424000000",
"home": "0395555555",
"address": "12 Test St, Testville, Vic, Australia",
}),
bakery_id: Some(1),
},
vec![customer::Model {
id: 2,
name: "Kara".into(),
notes: Some("Loves all cakes".into()),
}]
),
(
baker::Model {
id: 2,
name: "Baker Bobby".into(),
contact_details: serde_json::json!({
"mobile": "+85212345678",
}),
bakery_id: Some(1),
},
vec![
customer::Model {
id: 2,
name: "Kara".into(),
notes: Some("Loves all cakes".into()),
},
customer::Model {
id: 1,
name: "Kate".into(),
notes: Some("Loves cheese cake".into()),
},
customer::Model {
id: 1,
name: "Kate".into(),
notes: Some("Loves cheese cake".into()),
},
]
),
]
);

ctx.delete().await;

Ok(())
Expand Down