diff --git a/examples/proxy_gluesql_example/Cargo.toml b/examples/proxy_gluesql_example/Cargo.toml index 9529d69a6..dcd29c928 100644 --- a/examples/proxy_gluesql_example/Cargo.toml +++ b/examples/proxy_gluesql_example/Cargo.toml @@ -8,6 +8,7 @@ publish = false [workspace] [dependencies] +anyhow = "^1" async-std = { version = "1.12", features = ["attributes", "tokio1"] } serde_json = { version = "1" } serde = { version = "1" } diff --git a/examples/proxy_gluesql_example/src/entity/post.rs b/examples/proxy_gluesql_example/src/entity/post.rs index 868846046..56e83982e 100644 --- a/examples/proxy_gluesql_example/src/entity/post.rs +++ b/examples/proxy_gluesql_example/src/entity/post.rs @@ -1,11 +1,11 @@ -use sea_orm::entity::prelude::*; +use sea_orm::{entity::prelude::*, ActiveValue::*}; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] #[sea_orm(table_name = "posts")] pub struct Model { #[sea_orm(primary_key)] - pub id: i64, + pub id: Uuid, pub title: String, pub text: String, @@ -14,4 +14,15 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl ActiveModelBehavior for ActiveModel {} +#[async_trait::async_trait] +impl ActiveModelBehavior for ActiveModel { + async fn before_save(self, _db: &C, _insert: bool) -> Result + where + C: ConnectionTrait, + { + println!("Before save"); + let mut ret = self.clone(); + ret.id = Set(Uuid::new_v4()); + Ok(ret) + } +} diff --git a/examples/proxy_gluesql_example/src/main.rs b/examples/proxy_gluesql_example/src/main.rs index b17e33a95..56b1f0128 100644 --- a/examples/proxy_gluesql_example/src/main.rs +++ b/examples/proxy_gluesql_example/src/main.rs @@ -4,6 +4,7 @@ mod entity; +use anyhow::{anyhow, Result}; use std::{ collections::BTreeMap, sync::{Arc, Mutex}, @@ -11,8 +12,8 @@ use std::{ use gluesql::{memory_storage::MemoryStorage, prelude::Glue}; use sea_orm::{ - ActiveValue::Set, Database, DbBackend, DbErr, EntityTrait, ProxyDatabaseTrait, ProxyExecResult, - ProxyRow, Statement, + ActiveModelTrait, ActiveValue::Set, Database, DbBackend, DbErr, EntityTrait, + ProxyDatabaseTrait, ProxyExecResult, ProxyRow, Statement, }; use entity::post::{ActiveModel, Entity}; @@ -99,6 +100,12 @@ impl ProxyDatabaseTrait for ProxyDb { val.unwrap_or(0).to_string(), false, ), + sea_orm::Value::Uuid(val) => { + Value::SingleQuotedString(match val { + Some(val) => val.to_string(), + None => "".to_string(), + }) + } _ => todo!(), }; } @@ -132,14 +139,14 @@ impl ProxyDatabaseTrait for ProxyDb { } #[async_std::main] -async fn main() { +async fn main() -> Result<()> { let mem = MemoryStorage::default(); let mut glue = Glue::new(mem); glue.execute( r#" CREATE TABLE IF NOT EXISTS posts ( - id INTEGER PRIMARY KEY, + id UUID PRIMARY KEY, title TEXT NOT NULL, text TEXT NOT NULL ) @@ -160,32 +167,41 @@ async fn main() { println!("Initialized"); let data = ActiveModel { - id: Set(11), title: Set("Homo".to_owned()), text: Set("いいよ、来いよ".to_owned()), + ..Default::default() }; - Entity::insert(data).exec(&db).await.unwrap(); + + println!("data: {:?}", data); + let ret = data.insert(&db).await.map_err(|err| anyhow!("{:?}", err))?; + println!("ret: {:?}", ret); + let data = ActiveModel { - id: Set(45), title: Set("Homo".to_owned()), text: Set("そうだよ".to_owned()), + ..Default::default() }; - Entity::insert(data).exec(&db).await.unwrap(); + let ret = data.insert(&db).await.map_err(|err| anyhow!("{:?}", err))?; + println!("ret: {:?}", ret); + let data = ActiveModel { - id: Set(14), title: Set("Homo".to_owned()), text: Set("悔い改めて".to_owned()), + ..Default::default() }; - Entity::insert(data).exec(&db).await.unwrap(); + let ret = data.insert(&db).await.map_err(|err| anyhow!("{:?}", err))?; + println!("ret: {:?}", ret); let list = Entity::find().all(&db).await.unwrap().to_vec(); println!("Result: {:?}", list); + + Ok(()) } #[cfg(test)] mod tests { #[smol_potat::test] async fn try_run() { - crate::main() + crate::main().unwrap(); } } diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 6cf9aff59..686f01a70 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -256,10 +256,9 @@ where if db_backend == DbBackend::MySql && last_insert_id == 0 { return Err(DbErr::RecordNotInserted); } - Some( - ValueTypeOf::::try_from_u64(last_insert_id) - .map_err(|_| DbErr::UnpackInsertId)?, - ) + ValueTypeOf::::try_from_u64(last_insert_id) + .map(|val| Some(val)) + .unwrap_or(None) } else { None }