Skip to content

Commit

Permalink
insert_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
trevyn committed Jul 23, 2024
1 parent fb36a7a commit 18eb379
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
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
2 changes: 1 addition & 1 deletion turbosql-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
5 changes: 5 additions & 0 deletions turbosql/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i64> "1").unwrap(), vec![1]);
// assert_eq!(select!(Option<i64> "1").unwrap(), Some(1));
Expand Down

0 comments on commit 18eb379

Please sign in to comment.