left_join_and_select_also

This commit is contained in:
Chris Tsang 2021-06-27 00:03:16 +08:00
parent 394dfc07cc
commit df7bb5c195
6 changed files with 24 additions and 18 deletions

View File

@ -51,10 +51,7 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Fruit => Entity::has_many(super::fruit::Entity)
.from(Column::Id)
.to(super::fruit::Column::CakeId)
.into(),
Self::Fruit => Entity::has_many(super::fruit::Entity).into(),
}
}
}

View File

@ -53,11 +53,11 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Cake => Entity::has_one(super::cake::Entity)
Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::CakeId)
.to(super::cake::Column::Id)
.into(),
Self::Filling => Entity::has_one(super::filling::Entity)
Self::Filling => Entity::belongs_to(super::filling::Entity)
.from(Column::FillingId)
.to(super::filling::Column::Id)
.into(),

View File

@ -35,7 +35,9 @@ impl PrimaryKeyTrait for PrimaryKey {
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
pub enum Relation {
Cake,
}
impl ColumnTrait for Column {
type EntityName = Entity;
@ -51,7 +53,18 @@ impl ColumnTrait for Column {
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!()
match self {
Self::Cake => Entity::belongs_to(super::cake::Entity)
.from(Column::CakeId)
.to(super::cake::Column::Id)
.into(),
}
}
}
impl Related<super::cake::Entity> for Entity {
fn to() -> RelationDef {
Relation::Cake.def()
}
}

View File

@ -66,7 +66,7 @@ async fn find_all(db: &DbConn) -> Result<(), QueryErr> {
async fn find_together(db: &DbConn) -> Result<(), QueryErr> {
print!("find cakes and fruits: ");
let both = Cake::find().left_join_and_select(Fruit).all(db).await?;
let both = Cake::find().left_join_and_select_also(Fruit).all(db).await?;
println!();
for bb in both.iter() {
@ -141,7 +141,7 @@ async fn count_fruits_by_cake(db: &DbConn) -> Result<(), QueryErr> {
async fn find_many_to_many(db: &DbConn) -> Result<(), QueryErr> {
print!("find cakes and fillings: ");
let both = Cake::find().left_join_and_select(Filling).all(db).await?;
let both = Cake::find().left_join_and_select_also(Filling).all(db).await?;
println!();
for bb in both.iter() {
@ -211,7 +211,7 @@ async fn find_together_json(db: &DbConn) -> Result<(), QueryErr> {
print!("find cakes and fruits: ");
let cakes_fruits = Cake::find()
.left_join_and_select(Fruit)
.left_join_and_select_also(Fruit)
.into_json()
.all(db)
.await?;

View File

@ -132,12 +132,8 @@ where
self.into_model::<E::Model, F::Model>().one(db).await
}
pub async fn all(
self,
db: &DatabaseConnection,
) -> Result<Vec<(E::Model, Vec<F::Model>)>, QueryErr> {
let rows = self.into_model::<E::Model, F::Model>().all(db).await?;
Ok(parse_query_result::<E, _>(rows))
pub async fn all(self, db: &DatabaseConnection) -> Result<Vec<(E::Model, Option<F::Model>)>, QueryErr> {
self.into_model::<E::Model, F::Model>().all(db).await
}
}

View File

@ -41,7 +41,7 @@ where
}
/// Left Join with a Related Entity and select both Entity.
pub fn left_join_and_select<R>(self, r: R) -> SelectTwo<E, R>
pub fn left_join_and_select_also<R>(self, r: R) -> SelectTwo<E, R>
where
R: EntityTrait,
E: Related<R>,