From 90388a352340a1ecd4b9f63ce3dd43c574850ad3 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Tue, 18 May 2021 01:38:48 +0800 Subject: [PATCH] Example --- examples/sqlx-mysql/Readme.md | 20 ++++-- examples/sqlx-mysql/import.sh | 2 + examples/sqlx-mysql/src/example_cake.rs | 15 +++++ .../sqlx-mysql/src/example_cake_filling.rs | 65 +++++++++++++++++++ examples/sqlx-mysql/src/example_filling.rs | 52 +++++++++++++++ examples/sqlx-mysql/src/main.rs | 24 +++++++ 6 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 examples/sqlx-mysql/src/example_cake_filling.rs create mode 100644 examples/sqlx-mysql/src/example_filling.rs diff --git a/examples/sqlx-mysql/Readme.md b/examples/sqlx-mysql/Readme.md index 4aed9e35..7af36aca 100644 --- a/examples/sqlx-mysql/Readme.md +++ b/examples/sqlx-mysql/Readme.md @@ -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" }) ``` \ No newline at end of file diff --git a/examples/sqlx-mysql/import.sh b/examples/sqlx-mysql/import.sh index dfc5d218..800147d3 100644 --- a/examples/sqlx-mysql/import.sh +++ b/examples/sqlx-mysql/import.sh @@ -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 \ No newline at end of file diff --git a/examples/sqlx-mysql/src/example_cake.rs b/examples/sqlx-mysql/src/example_cake.rs index e69ee46a..bbb5e09f 100644 --- a/examples/sqlx-mysql/src/example_cake.rs +++ b/examples/sqlx-mysql/src/example_cake.rs @@ -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 for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + super::cake_filling::Relation::Filling.def() + } + + fn via() -> Option { + Some(Relation::CakeFilling.def()) + } +} + impl Model { pub fn find_fruit(&self) -> Select { Entity::find_related().belongs_to::(self) diff --git a/examples/sqlx-mysql/src/example_cake_filling.rs b/examples/sqlx-mysql/src/example_cake_filling.rs new file mode 100644 index 00000000..c174f72b --- /dev/null +++ b/examples/sqlx-mysql/src/example_cake_filling.rs @@ -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(), + } + } +} diff --git a/examples/sqlx-mysql/src/example_filling.rs b/examples/sqlx-mysql/src/example_filling.rs new file mode 100644 index 00000000..76fcd066 --- /dev/null +++ b/examples/sqlx-mysql/src/example_filling.rs @@ -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!() + } +} diff --git a/examples/sqlx-mysql/src/main.rs b/examples/sqlx-mysql/src/main.rs index e9784539..4fcc1e1b 100644 --- a/examples/sqlx-mysql/src/main.rs +++ b/examples/sqlx-mysql/src/main.rs @@ -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(()) +}