Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query_unchecked and query_file_unchecked macros #256

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions sqlx-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,28 @@ macro_rules! async_macro (
#[allow(unused_variables)]
pub fn query(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
async_macro!(db, input: QueryMacroInput => expand_query(input, db))
async_macro!(db, input: QueryMacroInput => expand_query(input, db, true))
}

#[proc_macro]
#[allow(unused_variables)]
pub fn query_unchecked(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
async_macro!(db, input: QueryMacroInput => expand_query(input, db, false))
}

#[proc_macro]
#[allow(unused_variables)]
pub fn query_file(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db))
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db, true))
}

#[proc_macro]
#[allow(unused_variables)]
pub fn query_file_unchecked(input: TokenStream) -> TokenStream {
#[allow(unused_variables)]
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db, false))
}

#[proc_macro]
Expand Down
3 changes: 2 additions & 1 deletion sqlx-macros/src/query_macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ mod query;
pub async fn expand_query_file<C: Connection>(
input: QueryMacroInput,
conn: C,
checked: bool,
) -> crate::Result<TokenStream>
where
C::Database: DatabaseExt + Sized,
<C::Database as Database>::TypeInfo: Display,
{
expand_query(input.expand_file_src().await?, conn).await
expand_query(input.expand_file_src().await?, conn, checked).await
}

pub async fn expand_query_as<C: Connection>(
Expand Down
12 changes: 9 additions & 3 deletions sqlx-macros/src/query_macros/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::database::DatabaseExt;
pub async fn expand_query<C: Connection>(
input: QueryMacroInput,
mut conn: C,
checked: bool,
) -> crate::Result<TokenStream>
where
C::Database: DatabaseExt + Sized,
Expand All @@ -23,7 +24,7 @@ where
let describe = input.describe_validate(&mut conn).await?;
let sql = &input.source;

let args = args::quote_args(&input, &describe, true)?;
let args = args::quote_args(&input, &describe, checked)?;

let arg_names = &input.arg_names;
let db_path = <C::Database as DatabaseExt>::db_path();
Expand Down Expand Up @@ -57,8 +58,13 @@ where
.collect::<TokenStream>();

let query_args = format_ident!("query_args");
let output =
output::quote_query_as::<C::Database>(sql, &record_type, &query_args, &columns, true);
let output = output::quote_query_as::<C::Database>(
sql,
&record_type,
&query_args,
if checked { &columns } else { &[] },
checked,
);

Ok(quote! {
macro_rules! macro_result {
Expand Down
44 changes: 44 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down