From 16e1342e9a74eea5b93989363c7c7ed8bfd9adeb Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Mon, 10 May 2021 03:16:39 +0800 Subject: [PATCH] More example --- examples/sqlx-mysql/Readme.md | 11 +++++++++++ examples/sqlx-mysql/src/main.rs | 13 ++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/examples/sqlx-mysql/Readme.md b/examples/sqlx-mysql/Readme.md index 7371280e..7d272d7e 100644 --- a/examples/sqlx-mysql/Readme.md +++ b/examples/sqlx-mysql/Readme.md @@ -1,11 +1,18 @@ # SeaORM SQLx MySql example +Prepare: + +Setup a test database and configure the connection string in `main.rs`. +Run `bakery.sql` to setup the test table and data. + Running: + ```sh cargo run ``` Example output: + ```sh Database { connection: SqlxMySqlPoolConnection } @@ -27,6 +34,10 @@ find one by primary key: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `ca 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" } + 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 Model { id: 1, name: "Blueberry", cake_id: Some(1) } diff --git a/examples/sqlx-mysql/src/main.rs b/examples/sqlx-mysql/src/main.rs index 1a31c18b..8e1d859c 100644 --- a/examples/sqlx-mysql/src/main.rs +++ b/examples/sqlx-mysql/src/main.rs @@ -1,4 +1,4 @@ -use sea_orm::{Database, EntityTrait, QueryErr}; +use sea_orm::{ColumnTrait, Database, EntityTrait, QueryErr}; mod example_cake; mod example_fruit; @@ -57,6 +57,17 @@ async fn find_one(db: &Database) -> Result<(), QueryErr> { 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?;