Skip to content

Commit

Permalink
Add query_unchecked and query_file_unchecked macros
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Apr 15, 2020
1 parent c6e9b27 commit 85bbe5a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 6 deletions.
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

0 comments on commit 85bbe5a

Please sign in to comment.