Update tests
This commit is contained in:
parent
3cf8866620
commit
0db9021da0
@ -128,7 +128,7 @@ where
|
|||||||
E: EntityTrait,
|
E: EntityTrait,
|
||||||
F: EntityTrait,
|
F: EntityTrait,
|
||||||
{
|
{
|
||||||
fn into_model<M, N>(self) -> Selector<SelectTwoModel<M, N>>
|
pub fn into_model<M, N>(self) -> Selector<SelectTwoModel<M, N>>
|
||||||
where
|
where
|
||||||
M: FromQueryResult,
|
M: FromQueryResult,
|
||||||
N: FromQueryResult,
|
N: FromQueryResult,
|
||||||
|
@ -3,7 +3,7 @@ use crate::{
|
|||||||
RelationDef,
|
RelationDef,
|
||||||
};
|
};
|
||||||
use sea_query::{
|
use sea_query::{
|
||||||
Alias, Expr, IntoCondition, SeaRc, SelectExpr, SelectStatement, SimpleExpr, TableRef,
|
Alias, Expr, Iden, IntoCondition, SeaRc, SelectExpr, SelectStatement, SimpleExpr, TableRef,
|
||||||
};
|
};
|
||||||
pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement};
|
pub use sea_query::{Condition, ConditionalStatement, DynIden, JoinType, Order, OrderedStatement};
|
||||||
|
|
||||||
@ -66,6 +66,33 @@ pub trait QuerySelect: Sized {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a select column with prefixed alias
|
||||||
|
/// ```
|
||||||
|
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
|
||||||
|
///
|
||||||
|
/// assert_eq!(
|
||||||
|
/// cake::Entity::find()
|
||||||
|
/// .select_only()
|
||||||
|
/// .column_as_prefixed(cake::Column::Id.count(), "A_", cake::Column::Id)
|
||||||
|
/// .build(DbBackend::Postgres)
|
||||||
|
/// .to_string(),
|
||||||
|
/// r#"SELECT COUNT("cake"."id") AS "A_id" FROM "cake""#
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
fn column_as_prefixed<C, I>(mut self, col: C, prefix: &str, alias: I) -> Self
|
||||||
|
where
|
||||||
|
C: IntoSimpleExpr,
|
||||||
|
I: Iden,
|
||||||
|
{
|
||||||
|
self.query().expr(SelectExpr {
|
||||||
|
expr: col.into_simple_expr(),
|
||||||
|
alias: Some(SeaRc::new(Alias::new(
|
||||||
|
vec![prefix, alias.to_string().as_str()].join("").as_str(),
|
||||||
|
))),
|
||||||
|
});
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Add a group by column
|
/// Add a group by column
|
||||||
/// ```
|
/// ```
|
||||||
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
|
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
|
||||||
|
@ -652,23 +652,60 @@ pub async fn linked() -> Result<(), DbErr> {
|
|||||||
.save(&ctx.db)
|
.save(&ctx.db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
/*
|
#[derive(Debug, FromQueryResult, PartialEq)]
|
||||||
SELECT `baker`.`id` AS `A_id`, `baker`.`name` AS `A_name`,
|
struct BakerLite {
|
||||||
`baker`.`contact_details` AS `A_contact_details`,
|
name: String,
|
||||||
`baker`.`bakery_id` AS `A_bakery_id`, `customer`.`id` AS `B_id`,
|
}
|
||||||
`customer`.`name` AS `B_name`, `customer`.`notes` AS `B_notes`
|
|
||||||
FROM `baker`
|
#[derive(Debug, FromQueryResult, PartialEq)]
|
||||||
LEFT JOIN `cakes_bakers` ON `baker`.`id` = `cakes_bakers`.`baker_id`
|
struct CustomerLite {
|
||||||
LEFT JOIN `cake` ON `cakes_bakers`.`cake_id` = `cake`.`id`
|
name: String,
|
||||||
LEFT JOIN `lineitem` ON `cake`.`id` = `lineitem`.`cake_id`
|
}
|
||||||
LEFT JOIN `order` ON `lineitem`.`order_id` = `order`.`id`
|
|
||||||
LEFT JOIN `customer` ON `order`.`customer_id` = `customer`.`id`
|
|
||||||
*/
|
|
||||||
let baked_for_customers = Baker::find()
|
let baked_for_customers = Baker::find()
|
||||||
.find_also_linked(baker::BakedForCustomer)
|
.find_also_linked(baker::BakedForCustomer)
|
||||||
|
.select_only()
|
||||||
|
.column_as_prefixed(baker::Column::Name, "A_", baker::Column::Name)
|
||||||
|
.column_as_prefixed(customer::Column::Name, "B_", customer::Column::Name)
|
||||||
|
.group_by(baker::Column::Id)
|
||||||
|
.group_by(customer::Column::Id)
|
||||||
|
.group_by(baker::Column::Name)
|
||||||
|
.group_by(customer::Column::Name)
|
||||||
|
.order_by_asc(baker::Column::Id)
|
||||||
|
.order_by_asc(customer::Column::Id)
|
||||||
|
.into_model::<BakerLite, CustomerLite>()
|
||||||
.all(&ctx.db)
|
.all(&ctx.db)
|
||||||
.await?;
|
.await?;
|
||||||
println!("{:#?}", baked_for_customers);
|
|
||||||
|
assert_eq!(
|
||||||
|
baked_for_customers,
|
||||||
|
vec![
|
||||||
|
(
|
||||||
|
BakerLite {
|
||||||
|
name: "Baker Bob".to_owned(),
|
||||||
|
},
|
||||||
|
Some(CustomerLite {
|
||||||
|
name: "Kara".to_owned(),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
(
|
||||||
|
BakerLite {
|
||||||
|
name: "Baker Bobby".to_owned(),
|
||||||
|
},
|
||||||
|
Some(CustomerLite {
|
||||||
|
name: "Kate".to_owned(),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
(
|
||||||
|
BakerLite {
|
||||||
|
name: "Baker Bobby".to_owned(),
|
||||||
|
},
|
||||||
|
Some(CustomerLite {
|
||||||
|
name: "Kara".to_owned(),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
ctx.delete().await;
|
ctx.delete().await;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user