Add query_unchecked and query_file_unchecked macros

This commit is contained in:
meh
2020-04-15 20:23:48 +02:00
committed by Ryan Leckey
parent c3cc146e7c
commit bfc52ca2f4
4 changed files with 71 additions and 6 deletions

View File

@@ -135,6 +135,29 @@ macro_rules! query (
})
);
/// A variant of [query!] which does not check the input or output types. This still does parse
/// the query to ensure it's syntactically and semantically valid for the current database.
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
macro_rules! query_unchecked (
// by emitting a macro definition from our proc-macro containing the result tokens,
// we no longer have a need for `proc-macro-hack`
($query:literal) => ({
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query_unchecked!($query);
}
macro_result!()
});
($query:literal, $($args:expr),*$(,)?) => ({
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query_unchecked!($query, $($args),*);
}
macro_result!($($args),*)
})
);
/// A variant of [query!] where the SQL query is stored in a separate file.
///
/// Useful for large queries and potentially cleaner than multiline strings.
@@ -196,6 +219,27 @@ macro_rules! query_file (
})
);
/// A variant of [query_file!] which does not check the input or output types. This still does parse
/// the query to ensure it's syntactically and semantically valid for the current database.
#[macro_export]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
macro_rules! query_file_unchecked (
($query:literal) => (#[allow(dead_code)]{
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query_file_unchecked!($query);
}
macro_result!()
});
($query:literal, $($args:expr),*$(,)?) => (#[allow(dead_code)]{
#[macro_use]
mod _macro_result {
$crate::sqlx_macros::query_file_unchecked!($query, $($args),*);
}
macro_result!($($args),*)
})
);
/// A variant of [query!] which takes a path to an explicitly defined struct as the output type.
///
/// This lets you return the struct from a function or add your own trait implementations.