Skip to content

Commit 47abc5a

Browse files
authored
Add delete feature (#36)
1 parent 7b09fb1 commit 47abc5a

File tree

5 files changed

+79
-0
lines changed

5 files changed

+79
-0
lines changed

migrations.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,36 @@
1+
# This file is auto-generated by Turbosql.
2+
# It is used to create and apply automatic schema migrations.
3+
# It should be checked into source control.
4+
# Modifying it by hand may be dangerous; see the docs.
15

6+
migrations_append_only = [
7+
"CREATE TABLE person (rowid INTEGER PRIMARY KEY) STRICT",
8+
"ALTER TABLE person ADD COLUMN name TEXT",
9+
"ALTER TABLE person ADD COLUMN age INTEGER",
10+
"ALTER TABLE person ADD COLUMN image_jpg BLOB",
11+
]
12+
output_generated_schema_for_your_information_do_not_edit = """
13+
CREATE TABLE _turbosql_migrations (
14+
rowid INTEGER PRIMARY KEY,
15+
migration TEXT NOT NULL
16+
) STRICT
17+
CREATE TABLE person (
18+
rowid INTEGER PRIMARY KEY,
19+
name TEXT,
20+
age INTEGER,
21+
image_jpg BLOB
22+
) STRICT
23+
"""
24+
25+
[output_generated_tables_do_not_edit.person]
26+
name = "person"
27+
28+
[[output_generated_tables_do_not_edit.person.columns]]
29+
name = "rowid"
30+
rust_type = "Option < i64 >"
31+
sql_type = "INTEGER PRIMARY KEY"
32+
33+
[[output_generated_tables_do_not_edit.person.columns]]
34+
name = "name"
35+
rust_type = "Option < String >"
36+
sql_type = "TEXT"

turbosql-impl/src/delete.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use quote::quote_spanned;
2+
3+
use crate::Table;
4+
5+
pub(super) fn delete(table: &Table) -> proc_macro2::TokenStream {
6+
let sql = makesql_delete(table);
7+
super::validate_sql_or_abort(&sql);
8+
let columns = table.columns.iter().filter(|c| c.name == "rowid").map(|c| {
9+
let ident = &c.ident;
10+
if c.sql_type == "TEXT" && c.rust_type != "Option < String >" {
11+
quote_spanned!(c.span => &::turbosql::serde_json::to_string(&self.#ident)? as &dyn ::turbosql::ToSql)
12+
} else {
13+
quote_spanned!(c.span => &self.#ident as &dyn ::turbosql::ToSql)
14+
}
15+
})
16+
.collect::<Vec<_>>();
17+
18+
19+
quote_spanned! { table.span =>
20+
fn delete(&self) -> Result<usize, ::turbosql::Error> {
21+
assert!(self.rowid.is_some());
22+
::turbosql::__TURBOSQL_DB.with(|db| {
23+
let db = db.borrow_mut();
24+
let mut stmt = db.prepare_cached(#sql)?;
25+
Ok(stmt.execute(&[#( #columns ),*] as &[&dyn ::turbosql::ToSql])?)
26+
})
27+
}
28+
}
29+
}
30+
31+
fn makesql_delete(table: &Table) -> String {
32+
format!("DELETE FROM {} WHERE rowid = ?", table.name)
33+
}

turbosql-impl/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const MIGRATIONS_FILENAME: &str = "test.migrations.toml";
2828

2929
mod insert;
3030
mod update;
31+
mod delete;
3132

3233
#[derive(Debug, Clone)]
3334
struct Table {
@@ -724,6 +725,7 @@ pub fn turbosql_derive_macro(input: proc_macro::TokenStream) -> proc_macro::Toke
724725

725726
let fn_insert = insert::insert(&table);
726727
let fn_update = update::update(&table);
728+
let fn_delete = delete::delete(&table);
727729

728730
// output tokenstream
729731

@@ -732,6 +734,7 @@ pub fn turbosql_derive_macro(input: proc_macro::TokenStream) -> proc_macro::Toke
732734
impl ::turbosql::Turbosql for #table {
733735
#fn_insert
734736
#fn_update
737+
#fn_delete
735738
}
736739
}
737740
.into()

turbosql/src/lib_inner.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub trait Turbosql {
3434
/// 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.
3535
fn update(&self) -> Result<usize, Error>;
3636
fn update_batch<T: AsRef<Self>>(rows: &[T]) -> Result<(), Error>;
37+
fn delete(&self) -> Result<usize, Error>;
3738
}
3839

3940
#[derive(thiserror::Error, Debug)]

turbosql/tests/integration_test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ fn integration_test() {
5858
select!(Vec<String> "field_string FROM personintegrationtest").unwrap(),
5959
vec!["Bob", "Bob"]
6060
);
61+
let mut r = PersonIntegrationTest {
62+
..Default::default()
63+
};
64+
let id = r.insert().unwrap();
65+
r.rowid = Some(id);
66+
r.delete().unwrap();
67+
assert!(select!(PersonIntegrationTest "WHERE rowid = ?", id).is_err());
6168
execute!("DELETE FROM personintegrationtest WHERE rowid = 2").unwrap();
6269
assert_eq!(select!(i64 "SELECT 1").unwrap(), 1);
6370
assert_eq!(select!(bool "SELECT 1 > ? AS val", 0).unwrap(), true);

0 commit comments

Comments
 (0)