This commit is contained in:
Chris Tsang 2021-05-18 01:38:48 +08:00
parent d5cdeb1fe0
commit 90388a3523
6 changed files with 174 additions and 4 deletions

View File

@ -22,7 +22,7 @@ find all cakes: SELECT `cake`.`id`, `cake`.`name` FROM `cake`
Model { id: 1, name: "New York Cheese" }
Model { id: 2, name: "Chocolate Fudge" }
Model { id: 2, name: "Chocolate Forest" }
find all fruits: SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`
@ -40,7 +40,7 @@ find cakes and fruits: SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,
(Model { id: 1, name: "New York Cheese" }, Model { id: 1, name: "Blueberry", cake_id: Some(1) })
(Model { id: 2, name: "Chocolate Fudge" }, Model { id: 3, name: "Strawberry", cake_id: Some(2) })
(Model { id: 2, name: "Chocolate Forest" }, Model { id: 3, name: "Strawberry", cake_id: Some(2) })
===== =====
@ -50,7 +50,7 @@ Model { id: 1, name: "New York Cheese" }
find one by like: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%chocolate%' LIMIT 1
Model { id: 2, name: "Chocolate Fudge" }
Model { id: 2, name: "Chocolate Forest" }
find models belong to: SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id` WHERE `cake`.`id` = 1
@ -64,6 +64,18 @@ count fruits by cake: SELECT `cake`.`name`, COUNT(`fruit`.`id`) AS `num_of_fruit
SelectResult { name: "New York Cheese", num_of_fruits: 2 }
SelectResult { name: "Chocolate Fudge", num_of_fruits: 1 }
SelectResult { name: "Chocolate Forest", num_of_fruits: 1 }
===== =====
find cakes and fillings: SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`, `filling`.`id` AS `B_id`, `filling`.`name` AS `B_name` FROM `cake` LEFT JOIN `cake_filling` ON `cake`.`id` = `cake_filling`.`cake_id` LEFT JOIN `filling` ON `cake_filling`.`filling_id` = `filling`.`id`
(Model { id: 1, name: "New York Cheese" }, Model { id: 1, name: "Vanilla" })
(Model { id: 1, name: "New York Cheese" }, Model { id: 2, name: "Lemon" })
(Model { id: 2, name: "Chocolate Forest" }, Model { id: 2, name: "Lemon" })
(Model { id: 2, name: "Chocolate Forest" }, Model { id: 3, name: "Mango" })
```

View File

@ -1,4 +1,6 @@
cp ../../src/tests_cfg/cake.rs src/example_cake.rs
cp ../../src/tests_cfg/fruit.rs src/example_fruit.rs
cp ../../src/tests_cfg/filling.rs src/example_filling.rs
cp ../../src/tests_cfg/cake_filling.rs src/example_cake_filling.rs
sed -i 's/^use crate::/use sea_orm::/g' src/*.rs

View File

@ -24,6 +24,7 @@ pub enum PrimaryKey {
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Fruit,
CakeFilling,
}
impl EntityTrait for Entity {
@ -54,6 +55,10 @@ impl RelationTrait for Relation {
.from(Column::Id)
.to(super::fruit::Column::CakeId)
.into(),
Self::CakeFilling => Entity::has_many(super::cake_filling::Entity)
.from(Column::Id)
.to(super::cake_filling::Column::CakeId)
.into(),
}
}
}
@ -64,6 +69,16 @@ impl Related<super::fruit::Entity> for Entity {
}
}
impl Related<super::filling::Entity> for Entity {
fn to() -> RelationDef {
super::cake_filling::Relation::Filling.def()
}
fn via() -> Option<RelationDef> {
Some(Relation::CakeFilling.def())
}
}
impl Model {
pub fn find_fruit(&self) -> Select<super::fruit::Entity> {
Entity::find_related().belongs_to::<Entity>(self)

View File

@ -0,0 +1,65 @@
use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "cake_filling"]
pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
pub struct Model {
pub cake_id: i32,
pub filling_id: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
CakeId,
FillingId,
}
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
CakeId,
FillingId,
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {
Cake,
Filling,
}
impl EntityTrait for Entity {
type Model = Model;
type Column = Column;
type PrimaryKey = PrimaryKey;
type Relation = Relation;
}
impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnType {
match self {
Self::CakeId => ColumnType::Integer(None),
Self::FillingId => ColumnType::Integer(None),
}
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
match self {
Self::Cake => Entity::has_many(super::cake::Entity)
.from(Column::CakeId)
.to(super::cake::Column::Id)
.into(),
Self::Filling => Entity::has_many(super::filling::Entity)
.from(Column::FillingId)
.to(super::filling::Column::Id)
.into(),
}
}
}

View File

@ -0,0 +1,52 @@
use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "filling"]
pub struct Entity;
#[derive(Clone, Debug, Default, PartialEq, DeriveModel)]
pub struct Model {
pub id: i32,
pub name: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Name,
}
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey {
Id,
}
#[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {}
impl EntityTrait for Entity {
type Model = Model;
type Column = Column;
type PrimaryKey = PrimaryKey;
type Relation = Relation;
}
impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnType {
match self {
Self::Id => ColumnType::Integer(None),
Self::Name => ColumnType::String(None),
}
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!()
}
}

View File

@ -1,10 +1,14 @@
use sea_orm::{ColumnTrait, Database, EntityTrait, FromQueryResult, QueryErr, QueryHelper};
mod example_cake;
mod example_cake_filling;
mod example_fruit;
mod example_filling;
use example_cake as cake;
use example_cake_filling as cake_filling;
use example_fruit as fruit;
use example_filling as filling;
#[async_std::main]
async fn main() {
@ -31,6 +35,10 @@ async fn main() {
println!("===== =====\n");
count_fruits_by_cake(&db).await.unwrap();
println!("===== =====\n");
find_many_to_many(&db).await.unwrap();
}
async fn find_all(db: &Database) -> Result<(), QueryErr> {
@ -128,3 +136,19 @@ async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> {
Ok(())
}
async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> {
print!("find cakes and fillings: ");
let both = cake::Entity::find()
.left_join_and_select(filling::Entity)
.all(db)
.await?;
println!();
for bb in both.iter() {
println!("{:?}\n", bb);
}
Ok(())
}