Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .insert_mut() method #41

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions turbosql-impl/src/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ pub(super) fn insert(table: &Table) -> proc_macro2::TokenStream {
})
}

fn insert_mut(&mut self) -> Result<i64, ::turbosql::Error> {
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<T: AsRef<#table>>(rows: &[T]) -> Result<(), ::turbosql::Error> {
for row in rows {
row.as_ref().insert()?;
Expand Down
3 changes: 2 additions & 1 deletion turbosql-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64, ::turbosql::Error> { unimplemented!() }
fn insert_mut(&mut self) -> Result<i64, ::turbosql::Error> { unimplemented!() }
fn insert_batch<T: AsRef<Self>>(rows: &[T]) -> Result<(), ::turbosql::Error> { unimplemented!() }
fn update(&self) -> Result<usize, ::turbosql::Error> { unimplemented!() }
fn update_batch<T: AsRef<Self>>(rows: &[T]) -> Result<(), ::turbosql::Error> { unimplemented!() }
Expand Down Expand Up @@ -942,7 +943,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));
Expand Down
6 changes: 4 additions & 2 deletions turbosql/src/lib_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ pub type Blob = Vec<u8>;

/// `#[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<i64, Error>;
/// Inserts all rows in the slice into the database. All `rowid`s must be `None`. On success, returns `Ok(())`.
/// 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<i64, Error>;
/// Insert all rows in the slice into the database. All `rowid`s must be `None`. On success, returns `Ok(())`.
fn insert_batch<T: AsRef<Self>>(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<usize, Error>;
Expand Down
6 changes: 6 additions & 0 deletions turbosql/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,18 @@ 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));
execute!("DELETE FROM personintegrationtest WHERE rowid=3").unwrap();

assert_eq!(select!(i64 "1").unwrap(), 1);
// assert_eq!(select!(Vec<i64> "1").unwrap(), vec![1]);
// assert_eq!(select!(Option<i64> "1").unwrap(), Some(1));
Expand Down