MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / find_with_linked

Method find_with_linked

src/query/join.rs:113–156  ·  view source on GitHub ↗

Left Join with a Linked Entity and select Entity as a `Vec`.

(self, l: L)

Source from the content-addressed store, hash-verified

111
112 /// Left Join with a Linked Entity and select Entity as a `Vec`.
113 pub fn find_with_linked<L, T>(self, l: L) -> SelectTwoMany<E, T>
114 where
115 L: Linked<FromEntity = E, ToEntity = T>,
116 T: EntityTrait,
117 {
118 let mut slf = self;
119 for (i, mut rel) in l.link().into_iter().enumerate() {
120 let to_tbl = Alias::new(format!("r{i}")).into_iden();
121 let from_tbl = if i > 0 {
122 Alias::new(format!("r{}", i - 1)).into_iden()
123 } else {
124 unpack_table_ref(&rel.from_tbl)
125 };
126 let table_ref = rel.to_tbl;
127
128 let mut condition = Condition::all().add(join_tbl_on_condition(
129 SeaRc::clone(&from_tbl),
130 SeaRc::clone(&to_tbl),
131 rel.from_col,
132 rel.to_col,
133 ));
134 if let Some(f) = rel.on_condition.take() {
135 condition = condition.add(f(SeaRc::clone(&from_tbl), SeaRc::clone(&to_tbl)));
136 }
137
138 slf.query()
139 .join_as(JoinType::LeftJoin, table_ref, to_tbl, condition);
140 }
141 slf = slf.apply_alias(SelectA.as_str());
142 let mut select_two_many = SelectTwoMany::new_without_prepare(slf.query);
143 for col in <T::Column as Iterable>::iter() {
144 let alias = format!("{}{}", SelectB.as_str(), col.as_str());
145 let expr = Expr::col((
146 Alias::new(format!("r{}", l.link().len() - 1)).into_iden(),
147 col.into_iden(),
148 ));
149 select_two_many.query().expr(SelectExpr {
150 expr: col.select_as(expr),
151 alias: Some(SeaRc::new(Alias::new(alias))),
152 window: None,
153 });
154 }
155 select_two_many
156 }
157
158 #[doc(hidden)]
159 /// Recursive self-join with CTE

Callers 1

linkedFunction · 0.80

Calls 13

join_tbl_on_conditionFunction · 0.85
into_iterMethod · 0.80
addMethod · 0.80
takeMethod · 0.80
join_asMethod · 0.80
apply_aliasMethod · 0.80
exprMethod · 0.80
unpack_table_refFunction · 0.70
newFunction · 0.50
linkMethod · 0.45
queryMethod · 0.45
as_strMethod · 0.45

Tested by 1

linkedFunction · 0.64