From 5e0176ef1a26361e8bd24932776ec01599d526a7 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 6 Jun 2021 18:30:59 +0800 Subject: [PATCH] Simplify example --- examples/sqlx-mysql/Readme.md | 2 +- examples/sqlx-mysql/src/entities.rs | 4 ++ examples/sqlx-mysql/src/example_cake.rs | 4 +- .../sqlx-mysql/src/example_cake_filling.rs | 4 +- examples/sqlx-mysql/src/example_filling.rs | 4 +- examples/sqlx-mysql/src/example_fruit.rs | 4 +- examples/sqlx-mysql/src/main.rs | 2 + examples/sqlx-mysql/src/operation.rs | 8 ++-- examples/sqlx-mysql/src/select.rs | 38 +++++++++---------- 9 files changed, 34 insertions(+), 36 deletions(-) create mode 100644 examples/sqlx-mysql/src/entities.rs diff --git a/examples/sqlx-mysql/Readme.md b/examples/sqlx-mysql/Readme.md index 37ad0807..2f479722 100644 --- a/examples/sqlx-mysql/Readme.md +++ b/examples/sqlx-mysql/Readme.md @@ -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 Forest" } +Some(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 diff --git a/examples/sqlx-mysql/src/entities.rs b/examples/sqlx-mysql/src/entities.rs new file mode 100644 index 00000000..a2a4988a --- /dev/null +++ b/examples/sqlx-mysql/src/entities.rs @@ -0,0 +1,4 @@ +pub use super::cake::Entity as Cake; +pub use super::cake_filling::Entity as CakeFilling; +pub use super::filling::Entity as Filling; +pub use super::fruit::Entity as Fruit; \ 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 ce7be6d4..c420ab34 100644 --- a/examples/sqlx-mysql/src/example_cake.rs +++ b/examples/sqlx-mysql/src/example_cake.rs @@ -79,6 +79,4 @@ impl Model { } } -impl ActiveModelBehavior for ActiveModel { - type Entity = Entity; -} +impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/example_cake_filling.rs b/examples/sqlx-mysql/src/example_cake_filling.rs index 1922c8ae..f23d9d9d 100644 --- a/examples/sqlx-mysql/src/example_cake_filling.rs +++ b/examples/sqlx-mysql/src/example_cake_filling.rs @@ -59,6 +59,4 @@ impl RelationTrait for Relation { } } -impl ActiveModelBehavior for ActiveModel { - type Entity = Entity; -} +impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/example_filling.rs b/examples/sqlx-mysql/src/example_filling.rs index 0acf0bed..442a0701 100644 --- a/examples/sqlx-mysql/src/example_filling.rs +++ b/examples/sqlx-mysql/src/example_filling.rs @@ -62,6 +62,4 @@ impl Model { } } -impl ActiveModelBehavior for ActiveModel { - type Entity = Entity; -} +impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/example_fruit.rs b/examples/sqlx-mysql/src/example_fruit.rs index 05176fc4..9d35b709 100644 --- a/examples/sqlx-mysql/src/example_fruit.rs +++ b/examples/sqlx-mysql/src/example_fruit.rs @@ -49,6 +49,4 @@ impl RelationTrait for Relation { } } -impl ActiveModelBehavior for ActiveModel { - type Entity = Entity; -} +impl ActiveModelBehavior for ActiveModel {} diff --git a/examples/sqlx-mysql/src/main.rs b/examples/sqlx-mysql/src/main.rs index fe2c93ea..44c1d97f 100644 --- a/examples/sqlx-mysql/src/main.rs +++ b/examples/sqlx-mysql/src/main.rs @@ -1,5 +1,6 @@ use sea_orm::Database; +mod entities; mod example_cake; mod example_cake_filling; mod example_filling; @@ -7,6 +8,7 @@ mod example_fruit; mod operation; mod select; +use entities::*; use example_cake as cake; use example_cake_filling as cake_filling; use example_filling as filling; diff --git a/examples/sqlx-mysql/src/operation.rs b/examples/sqlx-mysql/src/operation.rs index 05031513..7bf89859 100644 --- a/examples/sqlx-mysql/src/operation.rs +++ b/examples/sqlx-mysql/src/operation.rs @@ -1,5 +1,5 @@ -use crate::*; use sea_orm::{entity::*, query::*, Database}; +use super::*; pub async fn all_about_operation(db: &Database) -> Result<(), ExecErr> { insert_and_update(db).await?; @@ -20,12 +20,12 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> { name: Val::set("pear".to_owned()), ..Default::default() }; - let res = fruit::Entity::insert(pear).exec(db).await?; + let res = Fruit::insert(pear).exec(db).await?; println!(); println!("Inserted: {:?}\n", res); - let pear = fruit::Entity::find_by(res.last_insert_id) + let pear = Fruit::find_by(res.last_insert_id) .one(db) .await .map_err(|_| ExecErr)?; @@ -36,7 +36,7 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> { let mut pear: fruit::ActiveModel = pear.unwrap().into(); pear.name = Val::set("Sweet pear".to_owned()); - let res = fruit::Entity::update(pear).exec(db).await?; + let res = Fruit::update(pear).exec(db).await?; println!(); println!("Updated: {:?}\n", res); diff --git a/examples/sqlx-mysql/src/select.rs b/examples/sqlx-mysql/src/select.rs index 08c1dccf..627b266f 100644 --- a/examples/sqlx-mysql/src/select.rs +++ b/examples/sqlx-mysql/src/select.rs @@ -1,5 +1,5 @@ -use crate::*; use sea_orm::{entity::*, query::*, Database, FromQueryResult}; +use super::*; pub async fn all_about_select(db: &Database) -> Result<(), QueryErr> { find_all(db).await?; @@ -32,7 +32,7 @@ pub async fn all_about_select(db: &Database) -> Result<(), QueryErr> { async fn find_all(db: &Database) -> Result<(), QueryErr> { print!("find all cakes: "); - let cakes = cake::Entity::find().all(db).await?; + let cakes = Cake::find().all(db).await?; println!(); for cc in cakes.iter() { @@ -41,7 +41,7 @@ async fn find_all(db: &Database) -> Result<(), QueryErr> { print!("find all fruits: "); - let fruits = fruit::Entity::find().all(db).await?; + let fruits = Fruit::find().all(db).await?; println!(); for ff in fruits.iter() { @@ -54,8 +54,8 @@ async fn find_all(db: &Database) -> Result<(), QueryErr> { async fn find_together(db: &Database) -> Result<(), QueryErr> { print!("find cakes and fruits: "); - let both = cake::Entity::find() - .left_join_and_select(fruit::Entity) + let both = Cake::find() + .left_join_and_select(Fruit) .all(db) .await?; @@ -70,7 +70,7 @@ async fn find_together(db: &Database) -> Result<(), QueryErr> { async fn find_one(db: &Database) -> Result<(), QueryErr> { print!("find one by primary key: "); - let cheese: Option = cake::Entity::find_by(1).one(db).await?; + let cheese: Option = Cake::find_by(1).one(db).await?; let cheese = cheese.unwrap(); println!(); @@ -79,7 +79,7 @@ async fn find_one(db: &Database) -> Result<(), QueryErr> { print!("find one by like: "); - let chocolate = cake::Entity::find() + let chocolate = Cake::find() .filter(cake::Column::Name.contains("chocolate")) .one(db) .await?; @@ -109,8 +109,8 @@ async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> { print!("count fruits by cake: "); - let select = cake::Entity::find() - .left_join(fruit::Entity) + let select = Cake::find() + .left_join(Fruit) .select_only() .column(cake::Column::Name) .column_as(fruit::Column::Id.count(), "num_of_fruits") @@ -129,8 +129,8 @@ async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> { 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) + let both = Cake::find() + .left_join_and_select(Filling) .all(db) .await?; @@ -141,7 +141,7 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> { print!("find fillings for cheese cake: "); - let cheese = cake::Entity::find_by(1).one(db).await?; + let cheese = Cake::find_by(1).one(db).await?; if let Some(cheese) = cheese { let fillings: Vec = cheese.find_filling().all(db).await?; @@ -154,7 +154,7 @@ async fn find_many_to_many(db: &Database) -> Result<(), QueryErr> { print!("find cakes for lemon: "); - let lemon = filling::Entity::find_by(2).one(db).await?; + let lemon = Filling::find_by(2).one(db).await?; if let Some(lemon) = lemon { let cakes: Vec = lemon.find_cake().all(db).await?; @@ -185,13 +185,13 @@ async fn all_about_select_json(db: &Database) -> Result<(), QueryErr> { async fn find_all_json(db: &Database) -> Result<(), QueryErr> { print!("find all cakes: "); - let cakes = cake::Entity::find().into_json().all(db).await?; + let cakes = Cake::find().into_json().all(db).await?; println!("\n{}\n", serde_json::to_string_pretty(&cakes).unwrap()); print!("find all fruits: "); - let fruits = fruit::Entity::find().into_json().all(db).await?; + let fruits = Fruit::find().into_json().all(db).await?; println!("\n{}\n", serde_json::to_string_pretty(&fruits).unwrap()); @@ -201,8 +201,8 @@ async fn find_all_json(db: &Database) -> Result<(), QueryErr> { async fn find_together_json(db: &Database) -> Result<(), QueryErr> { print!("find cakes and fruits: "); - let cakes_fruits = cake::Entity::find() - .left_join_and_select(fruit::Entity) + let cakes_fruits = Cake::find() + .left_join_and_select(Fruit) .into_json() .all(db) .await?; @@ -218,8 +218,8 @@ async fn find_together_json(db: &Database) -> Result<(), QueryErr> { async fn count_fruits_by_cake_json(db: &Database) -> Result<(), QueryErr> { print!("count fruits by cake: "); - let count = cake::Entity::find() - .left_join(fruit::Entity) + let count = Cake::find() + .left_join(Fruit) .select_only() .column(cake::Column::Name) .column_as(fruit::Column::Id.count(), "num_of_fruits")