Skip to content

Commit 4809cc6

Browse files
committed
Refactor Select.find_also_linked and find_with_linked to use Linked trait method
1 parent e44a963 commit 4809cc6

File tree

5 files changed

+72
-104
lines changed

5 files changed

+72
-104
lines changed

src/entity/link.rs

+38-24
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,44 @@ pub trait Linked {
1919

2020
/// Find all the Entities that are linked to the Entity
2121
fn find_linked(&self) -> Select<Self::ToEntity> {
22-
let mut select = Select::new();
23-
for (i, mut rel) in self.link().into_iter().rev().enumerate() {
24-
let from_tbl = Alias::new(format!("r{i}")).into_iden();
25-
let to_tbl = if i > 0 {
26-
Alias::new(format!("r{}", i - 1)).into_iden()
27-
} else {
28-
unpack_table_ref(&rel.to_tbl)
29-
};
30-
let table_ref = rel.from_tbl;
31-
32-
let mut condition = Condition::all().add(join_tbl_on_condition(
33-
SeaRc::clone(&from_tbl),
34-
SeaRc::clone(&to_tbl),
35-
rel.from_col,
36-
rel.to_col,
37-
));
38-
if let Some(f) = rel.on_condition.take() {
39-
condition = condition.add(f(SeaRc::clone(&from_tbl), SeaRc::clone(&to_tbl)));
40-
}
41-
42-
select
43-
.query()
44-
.join_as(JoinType::InnerJoin, table_ref, from_tbl, condition);
22+
find_linked(self.link().into_iter().rev(), JoinType::InnerJoin)
23+
}
24+
25+
/// Find all the Entities that are linked to the Entity, in reverse
26+
fn find_linked_rev(&self) -> Select<Self::FromEntity> {
27+
find_linked(
28+
self.link().into_iter().map(LinkDef::rev),
29+
JoinType::LeftJoin,
30+
)
31+
}
32+
}
33+
34+
fn find_linked<I, E>(links: I, join: JoinType) -> Select<E>
35+
where
36+
I: Iterator<Item = LinkDef>,
37+
E: EntityTrait,
38+
{
39+
let mut select = Select::new();
40+
for (i, mut rel) in links.enumerate() {
41+
let from_tbl = Alias::new(format!("r{i}")).into_iden();
42+
let to_tbl = if i > 0 {
43+
Alias::new(format!("r{}", i - 1)).into_iden()
44+
} else {
45+
unpack_table_ref(&rel.to_tbl)
46+
};
47+
let table_ref = rel.from_tbl;
48+
49+
let mut condition = Condition::all().add(join_tbl_on_condition(
50+
SeaRc::clone(&from_tbl),
51+
SeaRc::clone(&to_tbl),
52+
rel.from_col,
53+
rel.to_col,
54+
));
55+
if let Some(f) = rel.on_condition.take() {
56+
condition = condition.add(f(SeaRc::clone(&from_tbl), SeaRc::clone(&to_tbl)));
4557
}
46-
select
58+
59+
select.query().join_as(join, table_ref, from_tbl, condition);
4760
}
61+
select
4862
}

src/entity/model.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
Statement,
55
};
66
use async_trait::async_trait;
7-
pub use sea_query::Value;
7+
pub use sea_query::{Alias, CommonTableExpression, IntoTableRef, JoinType, UnionType, Value};
88
use std::fmt::Debug;
99

1010
/// A Trait for a Model

src/executor/cursor.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -851,9 +851,9 @@ mod tests {
851851
r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name","#,
852852
r#""r2"."id" AS "B_id", "r2"."name" AS "B_name""#,
853853
r#"FROM "cake""#,
854-
r#"LEFT JOIN "cake_filling" AS "r0" ON "cake"."id" = "r0"."cake_id""#,
855-
r#"LEFT JOIN "filling" AS "r1" ON "r0"."filling_id" = "r1"."id""#,
856-
r#"LEFT JOIN "vendor" AS "r2" ON "r1"."vendor_id" = "r2"."id""#,
854+
r#"LEFT JOIN "cake_filling" AS "r0" ON "r0"."cake_id" = "cake"."id""#,
855+
r#"LEFT JOIN "filling" AS "r1" ON "r1"."id" = "r0"."filling_id""#,
856+
r#"LEFT JOIN "vendor" AS "r2" ON "r2"."id" = "r1"."vendor_id""#,
857857
r#"WHERE "cake"."id" < $1 ORDER BY "cake"."id" ASC, "vendor"."id" ASC LIMIT $2"#,
858858
]
859859
.join(" ")
@@ -916,9 +916,9 @@ mod tests {
916916
r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name","#,
917917
r#""r2"."id" AS "B_id", "r2"."name" AS "B_name""#,
918918
r#"FROM "cake""#,
919-
r#"LEFT JOIN "cake_filling" AS "r0" ON "cake"."id" = "r0"."cake_id""#,
920-
r#"LEFT JOIN "filling" AS "r1" ON "r0"."filling_id" = "r1"."id""#,
921-
r#"LEFT JOIN "vendor" AS "r2" ON "r1"."vendor_id" = "r2"."id""#,
919+
r#"LEFT JOIN "cake_filling" AS "r0" ON "r0"."cake_id" = "cake"."id""#,
920+
r#"LEFT JOIN "filling" AS "r1" ON "r1"."id" = "r0"."filling_id""#,
921+
r#"LEFT JOIN "vendor" AS "r2" ON "r2"."id" = "r1"."vendor_id""#,
922922
r#"WHERE "cake"."id" < $1 ORDER BY "cake"."id" ASC, "vendor"."id" ASC LIMIT $2"#,
923923
]
924924
.join(" ")
@@ -966,9 +966,9 @@ mod tests {
966966
r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name","#,
967967
r#""r2"."id" AS "B_id", "r2"."name" AS "B_name""#,
968968
r#"FROM "cake""#,
969-
r#"LEFT JOIN "cake_filling" AS "r0" ON "cake"."id" = "r0"."cake_id""#,
970-
r#"LEFT JOIN "filling" AS "r1" ON "r0"."filling_id" = "r1"."id""#,
971-
r#"LEFT JOIN "vendor" AS "r2" ON "r1"."vendor_id" = "r2"."id""#,
969+
r#"LEFT JOIN "cake_filling" AS "r0" ON "r0"."cake_id" = "cake"."id""#,
970+
r#"LEFT JOIN "filling" AS "r1" ON "r1"."id" = "r0"."filling_id""#,
971+
r#"LEFT JOIN "vendor" AS "r2" ON "r2"."id" = "r1"."vendor_id""#,
972972
r#"WHERE "vendor"."id" < $1 ORDER BY "vendor"."id" ASC, "cake"."id" ASC LIMIT $2"#,
973973
]
974974
.join(" ")
@@ -1019,9 +1019,9 @@ mod tests {
10191019
r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name","#,
10201020
r#""r2"."id" AS "B_id", "r2"."name" AS "B_name""#,
10211021
r#"FROM "cake""#,
1022-
r#"LEFT JOIN "cake_filling" AS "r0" ON "cake"."id" = "r0"."cake_id""#,
1023-
r#"LEFT JOIN "filling" AS "r1" ON "r0"."filling_id" = "r1"."id""#,
1024-
r#"LEFT JOIN "vendor" AS "r2" ON "r1"."vendor_id" = "r2"."id""#,
1022+
r#"LEFT JOIN "cake_filling" AS "r0" ON "r0"."cake_id" = "cake"."id""#,
1023+
r#"LEFT JOIN "filling" AS "r1" ON "r1"."id" = "r0"."filling_id""#,
1024+
r#"LEFT JOIN "vendor" AS "r2" ON "r2"."id" = "r1"."vendor_id""#,
10251025
r#"WHERE "vendor"."id" < $1 ORDER BY "vendor"."id" ASC, "cake"."id" ASC LIMIT $2"#,
10261026
]
10271027
.join(" ")

src/executor/select.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1558,9 +1558,9 @@ mod tests {
15581558
r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name","#,
15591559
r#""r2"."id" AS "B_id", "r2"."name" AS "B_name""#,
15601560
r#"FROM "cake""#,
1561-
r#"LEFT JOIN "cake_filling" AS "r0" ON "cake"."id" = "r0"."cake_id""#,
1562-
r#"LEFT JOIN "filling" AS "r1" ON "r0"."filling_id" = "r1"."id""#,
1563-
r#"LEFT JOIN "vendor" AS "r2" ON "r1"."vendor_id" = "r2"."id""#,
1561+
r#"LEFT JOIN "cake_filling" AS "r0" ON "r0"."cake_id" = "cake"."id""#,
1562+
r#"LEFT JOIN "filling" AS "r1" ON "r1"."id" = "r0"."filling_id""#,
1563+
r#"LEFT JOIN "vendor" AS "r2" ON "r2"."id" = "r1"."vendor_id""#,
15641564
]
15651565
.join(" ")
15661566
.as_str(),
@@ -1720,9 +1720,9 @@ mod tests {
17201720
[
17211721
r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name","#,
17221722
r#""r2"."id" AS "B_id", "r2"."name" AS "B_name" FROM "cake""#,
1723-
r#"LEFT JOIN "cake_filling" AS "r0" ON "cake"."id" = "r0"."cake_id""#,
1724-
r#"LEFT JOIN "filling" AS "r1" ON "r0"."filling_id" = "r1"."id""#,
1725-
r#"LEFT JOIN "vendor" AS "r2" ON "r1"."vendor_id" = "r2"."id""#,
1723+
r#"LEFT JOIN "cake_filling" AS "r0" ON "r0"."cake_id" = "cake"."id""#,
1724+
r#"LEFT JOIN "filling" AS "r1" ON "r1"."id" = "r0"."filling_id""#,
1725+
r#"LEFT JOIN "vendor" AS "r2" ON "r2"."id" = "r1"."vendor_id""#,
17261726
]
17271727
.join(" ")
17281728
.as_str(),

src/query/join.rs

+15-61
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{
2-
join_tbl_on_condition, unpack_table_ref, ColumnTrait, EntityTrait, IdenStatic, Iterable,
3-
Linked, QuerySelect, Related, Select, SelectA, SelectB, SelectTwo, SelectTwoMany,
2+
ColumnTrait, EntityTrait, IdenStatic, Iterable, Linked, QuerySelect, Related, Select, SelectA,
3+
SelectB, SelectTwo, SelectTwoMany,
44
};
55
pub use sea_query::JoinType;
6-
use sea_query::{Alias, Condition, Expr, IntoIden, SeaRc, SelectExpr};
6+
use sea_query::{Alias, Expr, IntoIden, SeaRc, SelectExpr};
77

88
impl<E> Select<E>
99
where
@@ -68,31 +68,8 @@ where
6868
L: Linked<FromEntity = E, ToEntity = T>,
6969
T: EntityTrait,
7070
{
71-
let mut slf = self;
72-
for (i, mut rel) in l.link().into_iter().enumerate() {
73-
let to_tbl = Alias::new(format!("r{i}")).into_iden();
74-
let from_tbl = if i > 0 {
75-
Alias::new(format!("r{}", i - 1)).into_iden()
76-
} else {
77-
unpack_table_ref(&rel.from_tbl)
78-
};
79-
let table_ref = rel.to_tbl;
80-
81-
let mut condition = Condition::all().add(join_tbl_on_condition(
82-
SeaRc::clone(&from_tbl),
83-
SeaRc::clone(&to_tbl),
84-
rel.from_col,
85-
rel.to_col,
86-
));
87-
if let Some(f) = rel.on_condition.take() {
88-
condition = condition.add(f(SeaRc::clone(&from_tbl), SeaRc::clone(&to_tbl)));
89-
}
90-
91-
slf.query()
92-
.join_as(JoinType::LeftJoin, table_ref, to_tbl, condition);
93-
}
94-
slf = slf.apply_alias(SelectA.as_str());
95-
let mut select_two = SelectTwo::new_without_prepare(slf.query);
71+
let select = l.find_linked_rev().apply_alias(SelectA.as_str());
72+
let mut select_two = SelectTwo::new_without_prepare(select.query);
9673
for col in <T::Column as Iterable>::iter() {
9774
let alias = format!("{}{}", SelectB.as_str(), col.as_str());
9875
let expr = Expr::col((
@@ -114,31 +91,8 @@ where
11491
L: Linked<FromEntity = E, ToEntity = T>,
11592
T: EntityTrait,
11693
{
117-
let mut slf = self;
118-
for (i, mut rel) in l.link().into_iter().enumerate() {
119-
let to_tbl = Alias::new(format!("r{i}")).into_iden();
120-
let from_tbl = if i > 0 {
121-
Alias::new(format!("r{}", i - 1)).into_iden()
122-
} else {
123-
unpack_table_ref(&rel.from_tbl)
124-
};
125-
let table_ref = rel.to_tbl;
126-
127-
let mut condition = Condition::all().add(join_tbl_on_condition(
128-
SeaRc::clone(&from_tbl),
129-
SeaRc::clone(&to_tbl),
130-
rel.from_col,
131-
rel.to_col,
132-
));
133-
if let Some(f) = rel.on_condition.take() {
134-
condition = condition.add(f(SeaRc::clone(&from_tbl), SeaRc::clone(&to_tbl)));
135-
}
136-
137-
slf.query()
138-
.join_as(JoinType::LeftJoin, table_ref, to_tbl, condition);
139-
}
140-
slf = slf.apply_alias(SelectA.as_str());
141-
let mut select_two_many = SelectTwoMany::new_without_prepare(slf.query);
94+
let select = l.find_linked_rev().apply_alias(SelectA.as_str());
95+
let mut select_two_many = SelectTwoMany::new_without_prepare(select.query);
14296
for col in <T::Column as Iterable>::iter() {
14397
let alias = format!("{}{}", SelectB.as_str(), col.as_str());
14498
let expr = Expr::col((
@@ -380,8 +334,8 @@ mod tests {
380334
r#"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,"#,
381335
r#"`r1`.`id` AS `B_id`, `r1`.`name` AS `B_name`, `r1`.`vendor_id` AS `B_vendor_id`"#,
382336
r#"FROM `cake`"#,
383-
r#"LEFT JOIN `cake_filling` AS `r0` ON `cake`.`id` = `r0`.`cake_id`"#,
384-
r#"LEFT JOIN `filling` AS `r1` ON `r0`.`filling_id` = `r1`.`id`"#,
337+
r#"LEFT JOIN `cake_filling` AS `r0` ON `r0`.`cake_id` = `cake`.`id`"#,
338+
r#"LEFT JOIN `filling` AS `r1` ON `r1`.`id` = `r0`.`filling_id`"#,
385339
]
386340
.join(" ")
387341
);
@@ -398,9 +352,9 @@ mod tests {
398352
r#"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,"#,
399353
r#"`r2`.`id` AS `B_id`, `r2`.`name` AS `B_name`"#,
400354
r#"FROM `cake`"#,
401-
r#"LEFT JOIN `cake_filling` AS `r0` ON `cake`.`id` = `r0`.`cake_id`"#,
402-
r#"LEFT JOIN `filling` AS `r1` ON `r0`.`filling_id` = `r1`.`id`"#,
403-
r#"LEFT JOIN `vendor` AS `r2` ON `r1`.`vendor_id` = `r2`.`id`"#,
355+
r#"LEFT JOIN `cake_filling` AS `r0` ON `r0`.`cake_id` = `cake`.`id`"#,
356+
r#"LEFT JOIN `filling` AS `r1` ON `r1`.`id` = `r0`.`filling_id`"#,
357+
r#"LEFT JOIN `vendor` AS `r2` ON `r2`.`id` = `r1`.`vendor_id`"#,
404358
]
405359
.join(" ")
406360
);
@@ -480,9 +434,9 @@ mod tests {
480434
r#"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,"#,
481435
r#"`r2`.`id` AS `B_id`, `r2`.`name` AS `B_name`"#,
482436
r#"FROM `cake`"#,
483-
r#"LEFT JOIN `cake_filling` AS `r0` ON `cake`.`id` = `r0`.`cake_id` AND `cake`.`name` LIKE '%cheese%'"#,
484-
r#"LEFT JOIN `filling` AS `r1` ON `r0`.`filling_id` = `r1`.`id`"#,
485-
r#"LEFT JOIN `vendor` AS `r2` ON `r1`.`vendor_id` = `r2`.`id`"#,
437+
r#"LEFT JOIN `cake_filling` AS `r0` ON `r0`.`cake_id` = `cake`.`id` AND `cake`.`name` LIKE '%cheese%'"#,
438+
r#"LEFT JOIN `filling` AS `r1` ON `r1`.`id` = `r0`.`filling_id`"#,
439+
r#"LEFT JOIN `vendor` AS `r2` ON `r2`.`id` = `r1`.`vendor_id`"#,
486440
]
487441
.join(" ")
488442
);

0 commit comments

Comments
 (0)