Refactor example

This commit is contained in:
Chris Tsang 2021-06-05 19:54:36 +08:00
parent eafb4a33d0
commit f433872c0a
6 changed files with 273 additions and 237 deletions

View File

@ -1,9 +1,14 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "cake"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"cake"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
@ -73,3 +78,7 @@ impl Model {
Entity::find_related().belongs_to::<Entity>(self) Entity::find_related().belongs_to::<Entity>(self)
} }
} }
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}

View File

@ -1,9 +1,14 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "cake_filling"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"cake_filling"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub cake_id: i32, pub cake_id: i32,
@ -53,3 +58,7 @@ impl RelationTrait for Relation {
} }
} }
} }
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}

View File

@ -1,9 +1,14 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "filling"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"filling"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
@ -56,3 +61,7 @@ impl Model {
Entity::find_related().belongs_to::<Entity>(self) Entity::find_related().belongs_to::<Entity>(self)
} }
} }
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}

View File

@ -1,9 +1,14 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
#[table = "fruit"]
pub struct Entity; pub struct Entity;
impl EntityName for Entity {
fn table_name(&self) -> &str {
"fruit"
}
}
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
pub id: i32, pub id: i32,
@ -43,3 +48,7 @@ impl RelationTrait for Relation {
panic!() panic!()
} }
} }
impl ActiveModelBehavior for ActiveModel {
type Entity = Entity;
}

View File

@ -1,14 +1,16 @@
use sea_orm::{ColumnTrait, Database, EntityTrait, FromQueryResult, QueryErr, SelectHelper}; use sea_orm::Database;
mod example_cake; mod example_cake;
mod example_cake_filling; mod example_cake_filling;
mod example_filling; mod example_filling;
mod example_fruit; mod example_fruit;
mod select;
use example_cake as cake; use example_cake as cake;
use example_cake_filling as cake_filling; use example_cake_filling as cake_filling;
use example_filling as filling; use example_filling as filling;
use example_fruit as fruit; use example_fruit as fruit;
use select::*;
#[async_std::main] #[async_std::main]
async fn main() { async fn main() {
@ -22,236 +24,5 @@ async fn main() {
println!("===== =====\n"); println!("===== =====\n");
find_all(&db).await.unwrap(); all_about_select(&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::<SelectResult>().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<filling::Model> = 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<cake::Model> = 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(())
} }

View File

@ -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::<SelectResult>().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<filling::Model> = 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<cake::Model> = 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(())
}