diff --git a/examples/sqlx-mysql/src/example_cake.rs b/examples/sqlx-mysql/src/example_cake.rs index e36d5c7d..ce7be6d4 100644 --- a/examples/sqlx-mysql/src/example_cake.rs +++ b/examples/sqlx-mysql/src/example_cake.rs @@ -1,9 +1,14 @@ use sea_orm::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "cake"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "cake" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, @@ -73,3 +78,7 @@ impl Model { Entity::find_related().belongs_to::(self) } } + +impl ActiveModelBehavior for ActiveModel { + type Entity = Entity; +} diff --git a/examples/sqlx-mysql/src/example_cake_filling.rs b/examples/sqlx-mysql/src/example_cake_filling.rs index f7c4f7ee..1922c8ae 100644 --- a/examples/sqlx-mysql/src/example_cake_filling.rs +++ b/examples/sqlx-mysql/src/example_cake_filling.rs @@ -1,9 +1,14 @@ use sea_orm::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "cake_filling"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "cake_filling" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub cake_id: i32, @@ -53,3 +58,7 @@ impl RelationTrait for Relation { } } } + +impl ActiveModelBehavior for ActiveModel { + type Entity = Entity; +} diff --git a/examples/sqlx-mysql/src/example_filling.rs b/examples/sqlx-mysql/src/example_filling.rs index 1b872e98..0acf0bed 100644 --- a/examples/sqlx-mysql/src/example_filling.rs +++ b/examples/sqlx-mysql/src/example_filling.rs @@ -1,9 +1,14 @@ use sea_orm::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "filling"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "filling" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, @@ -56,3 +61,7 @@ impl Model { Entity::find_related().belongs_to::(self) } } + +impl ActiveModelBehavior for ActiveModel { + type Entity = Entity; +} diff --git a/examples/sqlx-mysql/src/example_fruit.rs b/examples/sqlx-mysql/src/example_fruit.rs index 8d0c188d..05176fc4 100644 --- a/examples/sqlx-mysql/src/example_fruit.rs +++ b/examples/sqlx-mysql/src/example_fruit.rs @@ -1,9 +1,14 @@ use sea_orm::entity::prelude::*; #[derive(Copy, Clone, Default, Debug, DeriveEntity)] -#[table = "fruit"] pub struct Entity; +impl EntityName for Entity { + fn table_name(&self) -> &str { + "fruit" + } +} + #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] pub struct Model { pub id: i32, @@ -43,3 +48,7 @@ impl RelationTrait for Relation { panic!() } } + +impl ActiveModelBehavior for ActiveModel { + type Entity = Entity; +} diff --git a/examples/sqlx-mysql/src/main.rs b/examples/sqlx-mysql/src/main.rs index 83501ca7..eb2c79ce 100644 --- a/examples/sqlx-mysql/src/main.rs +++ b/examples/sqlx-mysql/src/main.rs @@ -1,14 +1,16 @@ -use sea_orm::{ColumnTrait, Database, EntityTrait, FromQueryResult, QueryErr, SelectHelper}; +use sea_orm::Database; mod example_cake; mod example_cake_filling; mod example_filling; mod example_fruit; +mod select; use example_cake as cake; use example_cake_filling as cake_filling; use example_filling as filling; use example_fruit as fruit; +use select::*; #[async_std::main] async fn main() { @@ -22,236 +24,5 @@ async fn main() { println!("===== =====\n"); - find_all(&db).await.unwrap(); - - println!("===== =====\n"); - - find_together(&db).await.unwrap(); - - println!("===== =====\n"); - - find_one(&db).await.unwrap(); - - println!("===== =====\n"); - - count_fruits_by_cake(&db).await.unwrap(); - - println!("===== =====\n"); - - find_many_to_many(&db).await.unwrap(); - - if false { - println!("===== =====\n"); - - json_tests(&db).await.unwrap(); - } -} - -async fn find_all(db: &Database) -> Result<(), QueryErr> { - print!("find all cakes: "); - - let cakes = cake::Entity::find().all(db).await?; - - println!(); - for cc in cakes.iter() { - println!("{:?}\n", cc); - } - - print!("find all fruits: "); - - let fruits = fruit::Entity::find().all(db).await?; - - println!(); - for ff in fruits.iter() { - println!("{:?}\n", ff); - } - - Ok(()) -} - -async fn find_together(db: &Database) -> Result<(), QueryErr> { - print!("find cakes and fruits: "); - - let both = cake::Entity::find() - .left_join_and_select(fruit::Entity) - .all(db) - .await?; - - println!(); - for bb in both.iter() { - println!("{:?}\n", bb); - } - - Ok(()) -} - -async fn find_one(db: &Database) -> Result<(), QueryErr> { - print!("find one by primary key: "); - - let cheese = cake::Entity::find_by(1).one(db).await?; - - println!(); - println!("{:?}", cheese); - println!(); - - print!("find one by like: "); - - let chocolate = cake::Entity::find() - .filter(cake::Column::Name.contains("chocolate")) - .one(db) - .await?; - - println!(); - println!("{:?}", chocolate); - println!(); - - print!("find models belong to: "); - - let fruits = cheese.find_fruit().all(db).await?; - - println!(); - for ff in fruits.iter() { - println!("{:?}\n", ff); - } - - Ok(()) -} - -async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> { - #[derive(Debug, FromQueryResult)] - struct SelectResult { - name: String, - num_of_fruits: i32, - } - - print!("count fruits by cake: "); - - let select = cake::Entity::find() - .left_join(fruit::Entity) - .select_only() - .column(cake::Column::Name) - .column_as(fruit::Column::Id.count(), "num_of_fruits") - .group_by(cake::Column::Name); - - let results = select.into_model::().all(db).await?; - - println!(); - for rr in results.iter() { - println!("{:?}\n", rr); - } - - 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); - } - - print!("find fillings for cheese cake: "); - - let cheese = cake::Entity::find_by(1).one(db).await?; - - let fillings: Vec = cheese.find_filling().all(db).await?; - - println!(); - for ff in fillings.iter() { - println!("{:?}\n", ff); - } - - print!("find cakes for lemon: "); - - let lemon = filling::Entity::find_by(2).one(db).await?; - - let cakes: Vec = lemon.find_cake().all(db).await?; - - println!(); - for cc in cakes.iter() { - println!("{:?}\n", cc); - } - - Ok(()) -} - -async fn json_tests(db: &Database) -> Result<(), QueryErr> { - find_all_json(&db).await?; - - println!("===== =====\n"); - - find_together_json(&db).await?; - - println!("===== =====\n"); - - count_fruits_by_cake_json(&db).await?; - - Ok(()) -} - -async fn find_all_json(db: &Database) -> Result<(), QueryErr> { - print!("find all cakes: "); - - let cakes = cake::Entity::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?; - - println!("\n{}\n", serde_json::to_string_pretty(&fruits).unwrap()); - - Ok(()) -} - -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) - .into_json() - .all(db) - .await?; - - println!( - "\n{}\n", - serde_json::to_string_pretty(&cakes_fruits).unwrap() - ); - - print!("find one cake and fruit: "); - - let cake_fruit = cake::Entity::find() - .left_join_and_select(fruit::Entity) - .into_json() - .one(db) - .await?; - - println!("\n{}\n", serde_json::to_string_pretty(&cake_fruit).unwrap()); - - Ok(()) -} - -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) - .select_only() - .column(cake::Column::Name) - .column_as(fruit::Column::Id.count(), "num_of_fruits") - .group_by(cake::Column::Name) - .into_json() - .all(db) - .await?; - - println!("\n{}\n", serde_json::to_string_pretty(&count).unwrap()); - - Ok(()) + all_about_select(&db).await.unwrap(); } diff --git a/examples/sqlx-mysql/src/select.rs b/examples/sqlx-mysql/src/select.rs new file mode 100644 index 00000000..ab7fb074 --- /dev/null +++ b/examples/sqlx-mysql/src/select.rs @@ -0,0 +1,229 @@ +use crate::*; +use sea_orm::{entity::*, query::*, Database, FromQueryResult}; + +pub async fn all_about_select(db: &Database) -> Result<(), QueryErr> { + find_all(db).await?; + + println!("===== =====\n"); + + find_together(db).await?; + + println!("===== =====\n"); + + find_one(db).await?; + + println!("===== =====\n"); + + count_fruits_by_cake(db).await?; + + println!("===== =====\n"); + + find_many_to_many(db).await?; + + if false { + println!("===== =====\n"); + + all_about_select_json(db).await?; + } + + Ok(()) +} + +async fn find_all(db: &Database) -> Result<(), QueryErr> { + print!("find all cakes: "); + + let cakes = cake::Entity::find().all(db).await?; + + println!(); + for cc in cakes.iter() { + println!("{:?}\n", cc); + } + + print!("find all fruits: "); + + let fruits = fruit::Entity::find().all(db).await?; + + println!(); + for ff in fruits.iter() { + println!("{:?}\n", ff); + } + + Ok(()) +} + +async fn find_together(db: &Database) -> Result<(), QueryErr> { + print!("find cakes and fruits: "); + + let both = cake::Entity::find() + .left_join_and_select(fruit::Entity) + .all(db) + .await?; + + println!(); + for bb in both.iter() { + println!("{:?}\n", bb); + } + + Ok(()) +} + +async fn find_one(db: &Database) -> Result<(), QueryErr> { + print!("find one by primary key: "); + + let cheese = cake::Entity::find_by(1).one(db).await?; + + println!(); + println!("{:?}", cheese); + println!(); + + print!("find one by like: "); + + let chocolate = cake::Entity::find() + .filter(cake::Column::Name.contains("chocolate")) + .one(db) + .await?; + + println!(); + println!("{:?}", chocolate); + println!(); + + print!("find models belong to: "); + + let fruits = cheese.find_fruit().all(db).await?; + + println!(); + for ff in fruits.iter() { + println!("{:?}\n", ff); + } + + Ok(()) +} + +async fn count_fruits_by_cake(db: &Database) -> Result<(), QueryErr> { + #[derive(Debug, FromQueryResult)] + struct SelectResult { + name: String, + num_of_fruits: i32, + } + + print!("count fruits by cake: "); + + let select = cake::Entity::find() + .left_join(fruit::Entity) + .select_only() + .column(cake::Column::Name) + .column_as(fruit::Column::Id.count(), "num_of_fruits") + .group_by(cake::Column::Name); + + let results = select.into_model::().all(db).await?; + + println!(); + for rr in results.iter() { + println!("{:?}\n", rr); + } + + 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); + } + + print!("find fillings for cheese cake: "); + + let cheese = cake::Entity::find_by(1).one(db).await?; + + let fillings: Vec = cheese.find_filling().all(db).await?; + + println!(); + for ff in fillings.iter() { + println!("{:?}\n", ff); + } + + print!("find cakes for lemon: "); + + let lemon = filling::Entity::find_by(2).one(db).await?; + + let cakes: Vec = lemon.find_cake().all(db).await?; + + println!(); + for cc in cakes.iter() { + println!("{:?}\n", cc); + } + + Ok(()) +} + +async fn all_about_select_json(db: &Database) -> Result<(), QueryErr> { + find_all_json(&db).await?; + + println!("===== =====\n"); + + find_together_json(&db).await?; + + println!("===== =====\n"); + + count_fruits_by_cake_json(&db).await?; + + Ok(()) +} + +async fn find_all_json(db: &Database) -> Result<(), QueryErr> { + print!("find all cakes: "); + + let cakes = cake::Entity::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?; + + println!("\n{}\n", serde_json::to_string_pretty(&fruits).unwrap()); + + Ok(()) +} + +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) + .into_json() + .all(db) + .await?; + + println!( + "\n{}\n", + serde_json::to_string_pretty(&cakes_fruits).unwrap() + ); + + Ok(()) +} + +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) + .select_only() + .column(cake::Column::Name) + .column_as(fruit::Column::Id.count(), "num_of_fruits") + .group_by(cake::Column::Name) + .into_json() + .all(db) + .await?; + + println!("\n{}\n", serde_json::to_string_pretty(&count).unwrap()); + + Ok(()) +}