macros pls work

This commit is contained in:
2023-08-29 21:34:01 +02:00
parent 8ea074e872
commit 45a25a49cc
3 changed files with 88 additions and 13 deletions

8
rust/Cargo.lock generated
View File

@@ -2340,9 +2340,9 @@ dependencies = [
[[package]] [[package]]
name = "time" name = "time"
version = "0.3.27" version = "0.3.28"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bb39ee79a6d8de55f48f2293a830e040392f1c5f16e336bdd1788cd0aadce07" checksum = "17f6bb557fd245c28e6411aa56b6403c689ad95061f50e4be16c274e70a17e48"
dependencies = [ dependencies = [
"deranged", "deranged",
"itoa", "itoa",
@@ -2359,9 +2359,9 @@ checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb"
[[package]] [[package]]
name = "time-macros" name = "time-macros"
version = "0.2.13" version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "733d258752e9303d392b94b75230d07b0b9c489350c69b851fc6c065fde3e8f9" checksum = "1a942f44339478ef67935ab2bbaec2fb0322496cf3cbe84b261e06ac3814c572"
dependencies = [ dependencies = [
"time-core", "time-core",
] ]

View File

@@ -13,11 +13,29 @@ use serde::{Deserialize, Serialize};
use time; use time;
use uuid::Uuid; use uuid::Uuid;
macro_rules! build_state_query { // #[macro_use]
() => { // mod macros {
println!("hi") // 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)] #[derive(sqlite::Type, PartialEq, PartialOrd, Deserialize, Debug)]
pub enum TripState { pub enum TripState {
@@ -384,11 +402,11 @@ impl TripItem {
crate::execute!( crate::execute!(
&sqlite::QueryClassification { &sqlite::QueryClassification {
query_type: sqlite::QueryType::Update, query_type: sqlite::QueryType::Update,
component: sqlite::Component::Inventory, component: sqlite::Component::Trips,
}, },
pool, pool,
"UPDATE trips_items "UPDATE trips_items
SET pick = ? SET " => "pick" => "= ?
WHERE trip_id = ? WHERE trip_id = ?
AND item_id = ? AND item_id = ?
AND user_id = ?", AND user_id = ?",
@@ -403,7 +421,7 @@ impl TripItem {
crate::execute!( crate::execute!(
&sqlite::QueryClassification { &sqlite::QueryClassification {
query_type: sqlite::QueryType::Update, query_type: sqlite::QueryType::Update,
component: sqlite::Component::Inventory, component: sqlite::Component::Trips,
}, },
pool, pool,
"UPDATE trips_items "UPDATE trips_items
@@ -422,7 +440,7 @@ impl TripItem {
crate::execute!( crate::execute!(
&sqlite::QueryClassification { &sqlite::QueryClassification {
query_type: sqlite::QueryType::Update, query_type: sqlite::QueryType::Update,
component: sqlite::Component::Inventory, component: sqlite::Component::Trips,
}, },
pool, pool,
"UPDATE trips_items "UPDATE trips_items

View File

@@ -138,6 +138,30 @@ pub fn sqlx_query(
metrics::counter!("packager_database_queries_total", 1, &labels); 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<Row, Out>(
// classification: &QueryClassification,
// pool: &Pool,
// query: &'static str,
// args: &[&str],
// ) {
// async {
// sqlx_query(classification, query, &[]);
// let result: Result<Vec<Out>, Error> = sqlx::query_as!(Row, query, args)
// .fetch(pool)
// .map_ok(|row: Row| row.try_into())
// .try_collect::<Vec<Result<Out, Error>>>()
// .await?
// .into_iter()
// .collect::<Result<Vec<Out>, Error>>();
// result
// }
// .instrument(tracing::info_span!("packager::sql::query", "query"))
// }
#[macro_export] #[macro_export]
macro_rules! query_all { macro_rules! query_all {
( $class:expr, $pool:expr, $struct_row:path, $struct_into:path, $query:expr, $( $args:tt )* ) => { ( $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_export]
macro_rules! execute { macro_rules! execute {
( $class:expr, $pool:expr, $query:expr, $( $args:tt )*) => { ( $class:expr, $pool:expr, $query:expr, $( $args:tt )*) => {
@@ -229,6 +260,32 @@ macro_rules! execute {
}.instrument(tracing::info_span!("packager::sql::query", "query")) }.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::sqlite::SqliteQueryResult, Error> = 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] #[macro_export]