From a6c82574cd493187ba6876c90f4b7f8e580f9e75 Mon Sep 17 00:00:00 2001 From: PedroTurik Date: Mon, 20 Nov 2023 21:28:57 -0300 Subject: [PATCH 1/4] small changes --- .vscode/settings.json | 22 +++++++++++++++++++++- turbosql-impl/src/delete.rs | 33 +++++++++++++++++++++++++++++++++ turbosql-impl/src/insert.rs | 1 + turbosql-impl/src/lib.rs | 4 ++++ turbosql/src/lib.rs | 4 ++-- turbosql/src/lib_inner.rs | 1 + 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 turbosql-impl/src/delete.rs diff --git a/.vscode/settings.json b/.vscode/settings.json index 7cdca85..58861f7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,25 @@ "rust-analyzer.cargo.features": ["test"], "rust-analyzer.checkOnSave.command": "clippy", "editor.detectIndentation": false, - "editor.insertSpaces": false +"editor.insertSpaces": false, +"files.exclude": { + "**/.git": true, + "**/.svn": true, + "**/.hg": true, + "**/CVS": true, + "**/.DS_Store": true, + "**/Thumbs.db": true, + ".github": true, + ".vscode": true, + "target": true, + "rustfmt.toml": true, + "README.md": true +}, +"hide-files.files": [ + ".github", + ".vscode", + "target", + "rustfmt.toml", + "README.md" +] } diff --git a/turbosql-impl/src/delete.rs b/turbosql-impl/src/delete.rs new file mode 100644 index 0000000..4fd9a92 --- /dev/null +++ b/turbosql-impl/src/delete.rs @@ -0,0 +1,33 @@ +use quote::quote_spanned; + +use crate::Table; + +pub(super) fn delete(table: &Table) -> proc_macro2::TokenStream { + let sql = makesql_delete(table); + super::validate_sql_or_abort(&sql); + let columns = table.columns.iter().filter(|c| c.name == "rowid").map(|c| { + let ident = &c.ident; + if c.sql_type == "TEXT" && c.rust_type != "Option < String >" { + quote_spanned!(c.span => &::turbosql::serde_json::to_string(&self.#ident)? as &dyn ::turbosql::ToSql) + } else { + quote_spanned!(c.span => &self.#ident as &dyn ::turbosql::ToSql) + } + }) + .collect::>(); + + + quote_spanned! { table.span => + fn delete(&self) -> Result { + assert!(self.rowid.is_some()); + ::turbosql::__TURBOSQL_DB.with(|db| { + let db = db.borrow_mut(); + let mut stmt = db.prepare_cached(#sql)?; + Ok(stmt.execute(&[#( #columns ),*] as &[&dyn ::turbosql::ToSql])?) + }) + } + } +} + +fn makesql_delete(table: &Table) -> String { + format!("DELETE FROM {} WHERE rowid = ?", table.name) +} diff --git a/turbosql-impl/src/insert.rs b/turbosql-impl/src/insert.rs index dcf7fa6..a7fb989 100644 --- a/turbosql-impl/src/insert.rs +++ b/turbosql-impl/src/insert.rs @@ -8,6 +8,7 @@ pub(super) fn insert(table: &Table) -> proc_macro2::TokenStream { super::validate_sql_or_abort(&sql); let columns = table.columns.iter().map(|c| { + println!("c: {:#?}", c); let ident = &c.ident; if c.sql_type == "TEXT" && c.rust_type != "Option < String >" { quote_spanned!(c.span => &::turbosql::serde_json::to_string(&self.#ident)? as &dyn ::turbosql::ToSql) diff --git a/turbosql-impl/src/lib.rs b/turbosql-impl/src/lib.rs index 0e26023..ee5d6f2 100644 --- a/turbosql-impl/src/lib.rs +++ b/turbosql-impl/src/lib.rs @@ -28,6 +28,7 @@ const MIGRATIONS_FILENAME: &str = "test.migrations.toml"; mod insert; mod update; +mod delete; #[derive(Debug, Clone)] struct Table { @@ -722,8 +723,10 @@ pub fn turbosql_derive_macro(input: proc_macro::TokenStream) -> proc_macro::Toke // create trait functions + println!("{:#?}", table); let fn_insert = insert::insert(&table); let fn_update = update::update(&table); + let fn_delete = delete::delete(&table); // output tokenstream @@ -732,6 +735,7 @@ pub fn turbosql_derive_macro(input: proc_macro::TokenStream) -> proc_macro::Toke impl ::turbosql::Turbosql for #table { #fn_insert #fn_update + #fn_delete } } .into() diff --git a/turbosql/src/lib.rs b/turbosql/src/lib.rs index d6b002b..9118d30 100644 --- a/turbosql/src/lib.rs +++ b/turbosql/src/lib.rs @@ -1,8 +1,8 @@ #![forbid(unsafe_code)] #![doc = include_str!("../README.md")] -#[cfg(all(not(feature = "test"), any(test, doctest)))] -compile_error!("turbosql must be tested with '--features test -- --test-threads=1'"); +// #[cfg(all(not(feature = "test"), any(test, doctest)))] +// compile_error!("turbosql must be tested with '--features test -- --test-threads=1'"); #[cfg(not(target_arch = "wasm32"))] include!("lib_inner.rs"); diff --git a/turbosql/src/lib_inner.rs b/turbosql/src/lib_inner.rs index ebd6f09..4645ea6 100644 --- a/turbosql/src/lib_inner.rs +++ b/turbosql/src/lib_inner.rs @@ -34,6 +34,7 @@ pub trait Turbosql { /// 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; fn update_batch>(rows: &[T]) -> Result<(), Error>; + fn delete(&self) -> Result; } #[derive(thiserror::Error, Debug)] From dbb3587e7a8529efd6f10f39bade46e00d6499d9 Mon Sep 17 00:00:00 2001 From: PedroTurik Date: Mon, 20 Nov 2023 21:52:35 -0300 Subject: [PATCH 2/4] small changes --- migrations.toml | 35 ++++++++++++++++++++++++++++++ turbosql-impl/src/delete.rs | 2 +- turbosql-impl/src/insert.rs | 2 +- turbosql-impl/src/lib.rs | 2 +- turbosql/src/lib.rs | 4 ++-- turbosql/src/lib_inner.rs | 2 +- turbosql/tests/integration_test.rs | 7 ++++++ 7 files changed, 48 insertions(+), 6 deletions(-) diff --git a/migrations.toml b/migrations.toml index 8b13789..aa85f29 100644 --- a/migrations.toml +++ b/migrations.toml @@ -1 +1,36 @@ +# This file is auto-generated by Turbosql. +# It is used to create and apply automatic schema migrations. +# It should be checked into source control. +# Modifying it by hand may be dangerous; see the docs. +migrations_append_only = [ + "CREATE TABLE person (rowid INTEGER PRIMARY KEY) STRICT", + "ALTER TABLE person ADD COLUMN name TEXT", + "ALTER TABLE person ADD COLUMN age INTEGER", + "ALTER TABLE person ADD COLUMN image_jpg BLOB", +] +output_generated_schema_for_your_information_do_not_edit = """ + CREATE TABLE _turbosql_migrations ( + rowid INTEGER PRIMARY KEY, + migration TEXT NOT NULL + ) STRICT + CREATE TABLE person ( + rowid INTEGER PRIMARY KEY, + name TEXT, + age INTEGER, + image_jpg BLOB + ) STRICT +""" + +[output_generated_tables_do_not_edit.person] +name = "person" + +[[output_generated_tables_do_not_edit.person.columns]] +name = "rowid" +rust_type = "Option < i64 >" +sql_type = "INTEGER PRIMARY KEY" + +[[output_generated_tables_do_not_edit.person.columns]] +name = "name" +rust_type = "Option < String >" +sql_type = "TEXT" diff --git a/turbosql-impl/src/delete.rs b/turbosql-impl/src/delete.rs index 4fd9a92..2d33045 100644 --- a/turbosql-impl/src/delete.rs +++ b/turbosql-impl/src/delete.rs @@ -17,7 +17,7 @@ pub(super) fn delete(table: &Table) -> proc_macro2::TokenStream { quote_spanned! { table.span => - fn delete(&self) -> Result { + fn delete(&self) -> Result { assert!(self.rowid.is_some()); ::turbosql::__TURBOSQL_DB.with(|db| { let db = db.borrow_mut(); diff --git a/turbosql-impl/src/insert.rs b/turbosql-impl/src/insert.rs index a7fb989..1e93ee8 100644 --- a/turbosql-impl/src/insert.rs +++ b/turbosql-impl/src/insert.rs @@ -8,7 +8,7 @@ pub(super) fn insert(table: &Table) -> proc_macro2::TokenStream { super::validate_sql_or_abort(&sql); let columns = table.columns.iter().map(|c| { - println!("c: {:#?}", c); + // println!("c: {:#?}", c); let ident = &c.ident; if c.sql_type == "TEXT" && c.rust_type != "Option < String >" { quote_spanned!(c.span => &::turbosql::serde_json::to_string(&self.#ident)? as &dyn ::turbosql::ToSql) diff --git a/turbosql-impl/src/lib.rs b/turbosql-impl/src/lib.rs index ee5d6f2..8aafb1b 100644 --- a/turbosql-impl/src/lib.rs +++ b/turbosql-impl/src/lib.rs @@ -723,7 +723,7 @@ pub fn turbosql_derive_macro(input: proc_macro::TokenStream) -> proc_macro::Toke // create trait functions - println!("{:#?}", table); + // println!("{:#?}", table); let fn_insert = insert::insert(&table); let fn_update = update::update(&table); let fn_delete = delete::delete(&table); diff --git a/turbosql/src/lib.rs b/turbosql/src/lib.rs index 9118d30..d6b002b 100644 --- a/turbosql/src/lib.rs +++ b/turbosql/src/lib.rs @@ -1,8 +1,8 @@ #![forbid(unsafe_code)] #![doc = include_str!("../README.md")] -// #[cfg(all(not(feature = "test"), any(test, doctest)))] -// compile_error!("turbosql must be tested with '--features test -- --test-threads=1'"); +#[cfg(all(not(feature = "test"), any(test, doctest)))] +compile_error!("turbosql must be tested with '--features test -- --test-threads=1'"); #[cfg(not(target_arch = "wasm32"))] include!("lib_inner.rs"); diff --git a/turbosql/src/lib_inner.rs b/turbosql/src/lib_inner.rs index 4645ea6..5351817 100644 --- a/turbosql/src/lib_inner.rs +++ b/turbosql/src/lib_inner.rs @@ -34,7 +34,7 @@ pub trait Turbosql { /// 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; fn update_batch>(rows: &[T]) -> Result<(), Error>; - fn delete(&self) -> Result; + fn delete(&self) -> Result; } #[derive(thiserror::Error, Debug)] diff --git a/turbosql/tests/integration_test.rs b/turbosql/tests/integration_test.rs index e648ff4..2a49251 100644 --- a/turbosql/tests/integration_test.rs +++ b/turbosql/tests/integration_test.rs @@ -58,6 +58,13 @@ fn integration_test() { select!(Vec "field_string FROM personintegrationtest").unwrap(), vec!["Bob", "Bob"] ); + let mut r = PersonIntegrationTest { + ..Default::default() + }; + let id = r.insert().unwrap(); + r.rowid = Some(id); + r.delete().unwrap(); + assert!(select!(PersonIntegrationTest "WHERE rowid = ?", id).is_err()); execute!("DELETE FROM personintegrationtest WHERE rowid = 2").unwrap(); assert_eq!(select!(i64 "SELECT 1").unwrap(), 1); assert_eq!(select!(bool "SELECT 1 > ? AS val", 0).unwrap(), true); From 34ed2821846401de98bfd91bf4c8de11ac9a1b76 Mon Sep 17 00:00:00 2001 From: PedroTurik Date: Mon, 20 Nov 2023 22:00:44 -0300 Subject: [PATCH 3/4] small changes --- .vscode/settings.json | 27 --------------------------- turbosql-impl/src/insert.rs | 1 - turbosql-impl/src/lib.rs | 1 - 3 files changed, 29 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 58861f7..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "rust-analyzer.checkOnSave.features": ["test"], - "rust-analyzer.cargo.features": ["test"], - "rust-analyzer.checkOnSave.command": "clippy", - "editor.detectIndentation": false, -"editor.insertSpaces": false, -"files.exclude": { - "**/.git": true, - "**/.svn": true, - "**/.hg": true, - "**/CVS": true, - "**/.DS_Store": true, - "**/Thumbs.db": true, - ".github": true, - ".vscode": true, - "target": true, - "rustfmt.toml": true, - "README.md": true -}, -"hide-files.files": [ - ".github", - ".vscode", - "target", - "rustfmt.toml", - "README.md" -] -} diff --git a/turbosql-impl/src/insert.rs b/turbosql-impl/src/insert.rs index 1e93ee8..dcf7fa6 100644 --- a/turbosql-impl/src/insert.rs +++ b/turbosql-impl/src/insert.rs @@ -8,7 +8,6 @@ pub(super) fn insert(table: &Table) -> proc_macro2::TokenStream { super::validate_sql_or_abort(&sql); let columns = table.columns.iter().map(|c| { - // println!("c: {:#?}", c); let ident = &c.ident; if c.sql_type == "TEXT" && c.rust_type != "Option < String >" { quote_spanned!(c.span => &::turbosql::serde_json::to_string(&self.#ident)? as &dyn ::turbosql::ToSql) diff --git a/turbosql-impl/src/lib.rs b/turbosql-impl/src/lib.rs index 8aafb1b..b9562ec 100644 --- a/turbosql-impl/src/lib.rs +++ b/turbosql-impl/src/lib.rs @@ -723,7 +723,6 @@ pub fn turbosql_derive_macro(input: proc_macro::TokenStream) -> proc_macro::Toke // create trait functions - // println!("{:#?}", table); let fn_insert = insert::insert(&table); let fn_update = update::update(&table); let fn_delete = delete::delete(&table); From 7ae232fa7c6c17df599f671e169f8e46f7ffdb61 Mon Sep 17 00:00:00 2001 From: PedroTurik Date: Mon, 20 Nov 2023 22:02:23 -0300 Subject: [PATCH 4/4] small changes --- .vscode/settings.json | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7cdca85 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "rust-analyzer.checkOnSave.features": ["test"], + "rust-analyzer.cargo.features": ["test"], + "rust-analyzer.checkOnSave.command": "clippy", + "editor.detectIndentation": false, + "editor.insertSpaces": false +}