Crates.io | sqlx-conditional-queries-layering |
lib.rs | sqlx-conditional-queries-layering |
version | 0.1.10 |
source | src |
created_at | 2024-05-31 03:54:25.069916 |
updated_at | 2024-07-09 00:56:25.280458 |
description | Query templating for sqlx-conditional-queries. |
homepage | |
repository | https://github.com/o-dasher/sqlx-conditional-queries-layering.git |
max_upload_size | |
id | 1257609 |
size | 9,406 |
sqlx_conditional_queries_layering
This library provides a macro for handling query templates in conjunction with the sqlx_conditional_queries
library. It simplifies the creation of SQL queries with conditional parameters.
let keehee = [Keehee::OwO, Keehee::UmU, Keehee::UwU]
.choose(&mut rand::thread_rng())
.cloned()
.unwrap_or_default();
create_conditional_query_as!(
$keehee_query,
#keehee = match keehee {
Keehee::OwO => "owo",
Keehee::UmU => "umu",
Keehee::UwU => "uwu"
}
);
This will generate an keehee_query
macro.
keehee_query
is a template. You can use it to do an sqlx query:
keehee_query!(BigID, "DO YOUR QUERY", #hey=match {...})
.fetch_one(&pool)
.await;
We can further extend the template with aditional variables using the macro supply_sql_variables_to_query_as
.
supply_sql_variables_to_query_as!(
$keehee_query as some_query,
#name = match Fall::Through {
_ => "{keehee_name}",
}
);
In this example we create some_query
, which is another macro that have the same template variables as keehee_query
with the addition of #name
.
We can merge two queries into one through:
merge_sql_query_as!($(a, b) as argsception);
This will merge all the template variables of a
and b
into a single argsception
query!
This macro relies on other macros from sqlx_conditional_queries
.
You need to enable the macro_metavar_expr
feature to use this library:
#![feature(macro_metavar_expr)]
You will also need to add the following dependencies to Cargo.toml
: sqlx_conditional_queries
, paste
.
You can do so easily, through:
cargo add sqlx_conditional_queries
cargo add paste