Skip to main content

miden_client_sqlite_store/db_management/
utils.rs

1// MACROS
2// ================================================================================================
3
4/// Auxiliary macro which substitutes `$src` token by `$dst` expression.
5#[macro_export]
6macro_rules! subst {
7    ($src:tt, $dst:expr_2021) => {
8        $dst
9    };
10}
11
12/// Generates a simple insert SQL statement with parameters for the provided table name and fields.
13/// Supports optional conflict resolution (adding "| REPLACE" or "| IGNORE" at the end will generate
14/// "OR REPLACE" and "OR IGNORE", correspondingly).
15///
16/// # Usage:
17///
18/// ```ignore
19/// insert_sql!(users { id, first_name, last_name, age } | REPLACE);
20/// ```
21///
22/// which generates:
23/// ```sql
24/// INSERT OR REPLACE INTO `users` (`id`, `first_name`, `last_name`, `age`) VALUES (?, ?, ?, ?)
25/// ```
26#[macro_export]
27macro_rules! insert_sql {
28    ($table:ident { $first_field:ident $(, $($field:ident),+)? $(,)? } $(| $on_conflict:expr)?) => {
29        concat!(
30            stringify!(INSERT $(OR $on_conflict)? INTO ),
31            "`",
32            stringify!($table),
33            "` (`",
34            stringify!($first_field),
35            $($(concat!("`, `", stringify!($field))),+ ,)?
36            "`) VALUES (",
37            subst!($first_field, "?"),
38            $($(subst!($field, ", ?")),+ ,)?
39            ")"
40        )
41    };
42}