From 45a25a49cc5adaa43c2e6e7539763de31e551b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Tue, 29 Aug 2023 21:34:01 +0200 Subject: [PATCH] macros pls work --- rust/Cargo.lock | 8 +++--- rust/src/models/trips.rs | 36 ++++++++++++++++++------- rust/src/sqlite.rs | 57 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 13 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 66fe5a8..503918a 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2340,9 +2340,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bb39ee79a6d8de55f48f2293a830e040392f1c5f16e336bdd1788cd0aadce07" +checksum = "17f6bb557fd245c28e6411aa56b6403c689ad95061f50e4be16c274e70a17e48" dependencies = [ "deranged", "itoa", @@ -2359,9 +2359,9 @@ checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" [[package]] name = "time-macros" -version = "0.2.13" +version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "733d258752e9303d392b94b75230d07b0b9c489350c69b851fc6c065fde3e8f9" +checksum = "1a942f44339478ef67935ab2bbaec2fb0322496cf3cbe84b261e06ac3814c572" dependencies = [ "time-core", ] diff --git a/rust/src/models/trips.rs b/rust/src/models/trips.rs index a9a196e..7be9c0d 100644 --- a/rust/src/models/trips.rs +++ b/rust/src/models/trips.rs @@ -13,11 +13,29 @@ use serde::{Deserialize, Serialize}; use time; use uuid::Uuid; -macro_rules! build_state_query { - () => { - println!("hi") - }; -} +// #[macro_use] +// mod macros { +// macro_rules! build_state_query { +// ($pool:expr, $state_name:literal, $value:expr, $trip_id:expr, $item_id:expr, $user_id:expr) => { +// crate::execute!( +// &sqlite::QueryClassification { +// query_type: sqlite::QueryType::Update, +// component: sqlite::Component::Trips, +// }, +// $pool, +// ["UPDATE trips_items SET pick = ? +// WHERE trip_id = ? +// AND item_id = ? +// AND user_id = " +// ,"?"] +// $value, +// $trip_id, +// $item_id, +// $user_id +// ) +// }; +// } +// } #[derive(sqlite::Type, PartialEq, PartialOrd, Deserialize, Debug)] pub enum TripState { @@ -384,11 +402,11 @@ impl TripItem { crate::execute!( &sqlite::QueryClassification { query_type: sqlite::QueryType::Update, - component: sqlite::Component::Inventory, + component: sqlite::Component::Trips, }, pool, "UPDATE trips_items - SET pick = ? + SET " => "pick" => "= ? WHERE trip_id = ? AND item_id = ? AND user_id = ?", @@ -403,7 +421,7 @@ impl TripItem { crate::execute!( &sqlite::QueryClassification { query_type: sqlite::QueryType::Update, - component: sqlite::Component::Inventory, + component: sqlite::Component::Trips, }, pool, "UPDATE trips_items @@ -422,7 +440,7 @@ impl TripItem { crate::execute!( &sqlite::QueryClassification { query_type: sqlite::QueryType::Update, - component: sqlite::Component::Inventory, + component: sqlite::Component::Trips, }, pool, "UPDATE trips_items diff --git a/rust/src/sqlite.rs b/rust/src/sqlite.rs index 8405e95..e7e80a6 100644 --- a/rust/src/sqlite.rs +++ b/rust/src/sqlite.rs @@ -138,6 +138,30 @@ pub fn sqlx_query( metrics::counter!("packager_database_queries_total", 1, &labels); } +// This does not work, as the query*! macros expect a string literal for the query, so +// it has to be there at compile time +// +// fn query_all( +// classification: &QueryClassification, +// pool: &Pool, +// query: &'static str, +// args: &[&str], +// ) { +// async { +// sqlx_query(classification, query, &[]); +// let result: Result, Error> = sqlx::query_as!(Row, query, args) +// .fetch(pool) +// .map_ok(|row: Row| row.try_into()) +// .try_collect::>>() +// .await? +// .into_iter() +// .collect::, Error>>(); + +// result +// } +// .instrument(tracing::info_span!("packager::sql::query", "query")) +// } + #[macro_export] macro_rules! query_all { ( $class:expr, $pool:expr, $struct_row:path, $struct_into:path, $query:expr, $( $args:tt )* ) => { @@ -210,6 +234,13 @@ macro_rules! query_exists { }; } +#[macro_export] +macro_rules! strip_plus { + (+ $($rest:tt)*) => { + $($rest)* + } +} + #[macro_export] macro_rules! execute { ( $class:expr, $pool:expr, $query:expr, $( $args:tt )*) => { @@ -229,6 +260,32 @@ macro_rules! execute { }.instrument(tracing::info_span!("packager::sql::query", "query")) } }; + + ( $class:expr, $pool:expr, $( $query:expr )=>+, $( $args:tt )*) => { + { + use tracing::Instrument as _; + async { + // $crate::sqlite::sqlx_query($class, $( $query )+ , &[]); + // println!("haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaay: {}", $crate::strip_plus!($(+ $query )+)); + let result: Result = sqlx::query!( + // "x" + "y", + $crate::strip_plus!($(+ $query )+), + // "UPDATE trips_items + // SET " + "pick" + + // "= ? + // WHERE trip_id = ? + // AND item_id = ? + // AND user_id = ?", + $( $args )* + ) + .execute($pool) + .await + .map_err(|e| e.into()); + + result + }.instrument(tracing::info_span!("packager::sql::query", "query")) + } + }; } #[macro_export]