From 18eb37955bdd501e53cd95cc492ea29ba18013fe Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Tue, 23 Jul 2024 12:36:43 +0400 Subject: [PATCH 1/4] insert_mut --- turbosql-impl/src/insert.rs | 11 +++++++++++ turbosql-impl/src/lib.rs | 2 +- turbosql/tests/integration_test.rs | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/turbosql-impl/src/insert.rs b/turbosql-impl/src/insert.rs index c2a1cf3..7754cb0 100644 --- a/turbosql-impl/src/insert.rs +++ b/turbosql-impl/src/insert.rs @@ -27,6 +27,17 @@ pub(super) fn insert(table: &Table) -> proc_macro2::TokenStream { }) } + fn insert_mut(&mut self) -> Result { + assert!(self.rowid.is_none()); + ::turbosql::__TURBOSQL_DB.with(|db| { + let db = db.borrow_mut(); + let mut stmt = db.prepare_cached(#sql)?; + let result = stmt.insert(&[#( #columns ),*] as &[&dyn ::turbosql::ToSql])?; + self.rowid = Some(result); + Ok(result) + }) + } + fn insert_batch>(rows: &[T]) -> Result<(), ::turbosql::Error> { for row in rows { row.as_ref().insert()?; diff --git a/turbosql-impl/src/lib.rs b/turbosql-impl/src/lib.rs index eb0faec..d5fe14c 100644 --- a/turbosql-impl/src/lib.rs +++ b/turbosql-impl/src/lib.rs @@ -942,7 +942,7 @@ fn create(table: &Table, minitable: &MiniTable) { if old_toml_str.replace("\r\n", "\n") != new_toml_str { #[cfg(not(feature = "test"))] if std::env::var("CI").is_ok() || std::env::var("TURBOSQL_LOCKED_MODE").is_ok() { - abort_call_site!("Change in `{}` detected with CI or TURBOSQL_LOCKED_MODE environment variable set. Make sure your `migrations.toml` file is up-to-date.", migrations_toml_path_lossy); + abort_call_site!("Change in `{}` detected with CI or TURBOSQL_LOCKED_MODE environment variable set. Make sure your `migrations.toml` file is committed and up-to-date.", migrations_toml_path_lossy); }; fs::write(&migrations_toml_path, new_toml_str) .unwrap_or_else(|e| abort_call_site!("Unable to write {}: {:?}", migrations_toml_path_lossy, e)); diff --git a/turbosql/tests/integration_test.rs b/turbosql/tests/integration_test.rs index ba3d966..6d09f18 100644 --- a/turbosql/tests/integration_test.rs +++ b/turbosql/tests/integration_test.rs @@ -74,12 +74,17 @@ fn integration_test() { ..Default::default() }; + let mut row2 = row.clone(); + assert_eq!(row.insert().unwrap(), 1); assert_eq!(row.insert().unwrap(), 2); row.rowid = Some(1); row.field_u8 = Some(84); assert_eq!(row.update().unwrap(), 1); + row2.insert_mut().unwrap(); + assert_eq!(row2.rowid, Some(3)); + assert_eq!(select!(i64 "1").unwrap(), 1); // assert_eq!(select!(Vec "1").unwrap(), vec![1]); // assert_eq!(select!(Option "1").unwrap(), Some(1)); From 879f0a6afa25316cdb7c68c61c49162fadcb4fd4 Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Tue, 23 Jul 2024 12:48:49 +0400 Subject: [PATCH 2/4] fix stuff --- turbosql/src/lib_inner.rs | 2 ++ turbosql/tests/integration_test.rs | 1 + 2 files changed, 3 insertions(+) diff --git a/turbosql/src/lib_inner.rs b/turbosql/src/lib_inner.rs index b918bd7..539e62b 100644 --- a/turbosql/src/lib_inner.rs +++ b/turbosql/src/lib_inner.rs @@ -26,6 +26,8 @@ pub type Blob = Vec; pub trait Turbosql { /// Inserts this row into the database. `rowid` must be `None`. On success, returns the new `rowid`. fn insert(&self) -> Result; + /// Inserts this row into the database, and updates the `rowid` of the struct to match the rowid newly inserted into the database. `rowid` must be `None`. On success, returns the new `rowid`. + fn insert_mut(&mut self) -> Result; /// Inserts all rows in the slice into the database. All `rowid`s must be `None`. On success, returns `Ok(())`. fn insert_batch>(rows: &[T]) -> Result<(), Error>; /// Updates this existing row in the database, based on `rowid`, which must be `Some`. All fields are overwritten in the database. On success, returns the number of rows updated, which should be 1. diff --git a/turbosql/tests/integration_test.rs b/turbosql/tests/integration_test.rs index 6d09f18..1951cff 100644 --- a/turbosql/tests/integration_test.rs +++ b/turbosql/tests/integration_test.rs @@ -84,6 +84,7 @@ fn integration_test() { row2.insert_mut().unwrap(); assert_eq!(row2.rowid, Some(3)); + execute!("DELETE FROM personintegrationtest WHERE rowid=3").unwrap(); assert_eq!(select!(i64 "1").unwrap(), 1); // assert_eq!(select!(Vec "1").unwrap(), vec![1]); From fff2e5918f793446f7e931d762a9c3d9fd55ab76 Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Tue, 23 Jul 2024 12:55:00 +0400 Subject: [PATCH 3/4] add insert_mut to dummy impl --- turbosql-impl/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/turbosql-impl/src/lib.rs b/turbosql-impl/src/lib.rs index d5fe14c..b733e07 100644 --- a/turbosql-impl/src/lib.rs +++ b/turbosql-impl/src/lib.rs @@ -674,6 +674,7 @@ pub fn turbosql_derive_macro(input: proc_macro::TokenStream) -> proc_macro::Toke let dummy_impl = quote! { impl ::turbosql::Turbosql for #table_ident { fn insert(&self) -> Result { unimplemented!() } + fn insert_mut(&mut self) -> Result { unimplemented!() } fn insert_batch>(rows: &[T]) -> Result<(), ::turbosql::Error> { unimplemented!() } fn update(&self) -> Result { unimplemented!() } fn update_batch>(rows: &[T]) -> Result<(), ::turbosql::Error> { unimplemented!() } From 8f19e8d17011dd45aa56dbc96cd5a8415ad700cf Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Tue, 23 Jul 2024 12:59:32 +0400 Subject: [PATCH 4/4] Update lib_inner.rs --- turbosql/src/lib_inner.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/turbosql/src/lib_inner.rs b/turbosql/src/lib_inner.rs index 539e62b..e4656c6 100644 --- a/turbosql/src/lib_inner.rs +++ b/turbosql/src/lib_inner.rs @@ -24,11 +24,11 @@ pub type Blob = Vec; /// `#[derive(Turbosql)]` generates impls for this trait. pub trait Turbosql { - /// Inserts this row into the database. `rowid` must be `None`. On success, returns the new `rowid`. + /// Insert this row into the database. `rowid` must be `None`. On success, the new `rowid` is returned. fn insert(&self) -> Result; - /// Inserts this row into the database, and updates the `rowid` of the struct to match the rowid newly inserted into the database. `rowid` must be `None`. On success, returns the new `rowid`. + /// Insert this row into the database, and update the `rowid` of the struct to match the new rowid in the database. `rowid` must be `None` on call. On success, the new `rowid` is returned. fn insert_mut(&mut self) -> Result; - /// Inserts all rows in the slice into the database. All `rowid`s must be `None`. On success, returns `Ok(())`. + /// Insert all rows in the slice into the database. All `rowid`s must be `None`. On success, returns `Ok(())`. fn insert_batch>(rows: &[T]) -> Result<(), Error>; /// Updates this existing row in the database, based on `rowid`, which must be `Some`. All fields are overwritten in the database. On success, returns the number of rows updated, which should be 1. fn update(&self) -> Result;