1
1
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 ,
4
4
} ;
5
5
pub use sea_query:: JoinType ;
6
- use sea_query:: { Alias , Condition , Expr , IntoIden , SeaRc , SelectExpr } ;
6
+ use sea_query:: { Alias , Expr , IntoIden , SeaRc , SelectExpr } ;
7
7
8
8
impl < E > Select < E >
9
9
where
68
68
L : Linked < FromEntity = E , ToEntity = T > ,
69
69
T : EntityTrait ,
70
70
{
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 ) ;
96
73
for col in <T :: Column as Iterable >:: iter ( ) {
97
74
let alias = format ! ( "{}{}" , SelectB . as_str( ) , col. as_str( ) ) ;
98
75
let expr = Expr :: col ( (
@@ -114,31 +91,8 @@ where
114
91
L : Linked < FromEntity = E , ToEntity = T > ,
115
92
T : EntityTrait ,
116
93
{
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 ) ;
142
96
for col in <T :: Column as Iterable >:: iter ( ) {
143
97
let alias = format ! ( "{}{}" , SelectB . as_str( ) , col. as_str( ) ) ;
144
98
let expr = Expr :: col ( (
@@ -380,8 +334,8 @@ mod tests {
380
334
r#"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,"# ,
381
335
r#"`r1`.`id` AS `B_id`, `r1`.`name` AS `B_name`, `r1`.`vendor_id` AS `B_vendor_id`"# ,
382
336
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 `"# ,
385
339
]
386
340
. join( " " )
387
341
) ;
@@ -398,9 +352,9 @@ mod tests {
398
352
r#"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,"# ,
399
353
r#"`r2`.`id` AS `B_id`, `r2`.`name` AS `B_name`"# ,
400
354
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 `"# ,
404
358
]
405
359
. join( " " )
406
360
) ;
@@ -480,9 +434,9 @@ mod tests {
480
434
r#"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,"# ,
481
435
r#"`r2`.`id` AS `B_id`, `r2`.`name` AS `B_name`"# ,
482
436
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 `"# ,
486
440
]
487
441
. join( " " )
488
442
) ;
0 commit comments