Skip to content

Commit 85bbe5a

Browse files
committed
Add query_unchecked and query_file_unchecked macros
1 parent c6e9b27 commit 85bbe5a

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

sqlx-macros/src/lib.rs

+16-2
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,28 @@ macro_rules! async_macro (
127127
#[allow(unused_variables)]
128128
pub fn query(input: TokenStream) -> TokenStream {
129129
#[allow(unused_variables)]
130-
async_macro!(db, input: QueryMacroInput => expand_query(input, db))
130+
async_macro!(db, input: QueryMacroInput => expand_query(input, db, true))
131+
}
132+
133+
#[proc_macro]
134+
#[allow(unused_variables)]
135+
pub fn query_unchecked(input: TokenStream) -> TokenStream {
136+
#[allow(unused_variables)]
137+
async_macro!(db, input: QueryMacroInput => expand_query(input, db, false))
131138
}
132139

133140
#[proc_macro]
134141
#[allow(unused_variables)]
135142
pub fn query_file(input: TokenStream) -> TokenStream {
136143
#[allow(unused_variables)]
137-
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db))
144+
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db, true))
145+
}
146+
147+
#[proc_macro]
148+
#[allow(unused_variables)]
149+
pub fn query_file_unchecked(input: TokenStream) -> TokenStream {
150+
#[allow(unused_variables)]
151+
async_macro!(db, input: QueryMacroInput => expand_query_file(input, db, false))
138152
}
139153

140154
#[proc_macro]

sqlx-macros/src/query_macros/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ mod query;
1919
pub async fn expand_query_file<C: Connection>(
2020
input: QueryMacroInput,
2121
conn: C,
22+
checked: bool,
2223
) -> crate::Result<TokenStream>
2324
where
2425
C::Database: DatabaseExt + Sized,
2526
<C::Database as Database>::TypeInfo: Display,
2627
{
27-
expand_query(input.expand_file_src().await?, conn).await
28+
expand_query(input.expand_file_src().await?, conn, checked).await
2829
}
2930

3031
pub async fn expand_query_as<C: Connection>(

sqlx-macros/src/query_macros/query.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::database::DatabaseExt;
1515
pub async fn expand_query<C: Connection>(
1616
input: QueryMacroInput,
1717
mut conn: C,
18+
checked: bool,
1819
) -> crate::Result<TokenStream>
1920
where
2021
C::Database: DatabaseExt + Sized,
@@ -23,7 +24,7 @@ where
2324
let describe = input.describe_validate(&mut conn).await?;
2425
let sql = &input.source;
2526

26-
let args = args::quote_args(&input, &describe, true)?;
27+
let args = args::quote_args(&input, &describe, checked)?;
2728

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

5960
let query_args = format_ident!("query_args");
60-
let output =
61-
output::quote_query_as::<C::Database>(sql, &record_type, &query_args, &columns, true);
61+
let output = output::quote_query_as::<C::Database>(
62+
sql,
63+
&record_type,
64+
&query_args,
65+
if checked { &columns } else { &[] },
66+
checked,
67+
);
6268

6369
Ok(quote! {
6470
macro_rules! macro_result {

src/macros.rs

+44
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,29 @@ macro_rules! query (
135135
})
136136
);
137137

138+
/// A variant of [query!] which does not check the input or output types. This still does parse
139+
/// the query to ensure it's syntactically and semantically valid for the current database.
140+
#[macro_export]
141+
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
142+
macro_rules! query_unchecked (
143+
// by emitting a macro definition from our proc-macro containing the result tokens,
144+
// we no longer have a need for `proc-macro-hack`
145+
($query:literal) => ({
146+
#[macro_use]
147+
mod _macro_result {
148+
$crate::sqlx_macros::query_unchecked!($query);
149+
}
150+
macro_result!()
151+
});
152+
($query:literal, $($args:expr),*$(,)?) => ({
153+
#[macro_use]
154+
mod _macro_result {
155+
$crate::sqlx_macros::query_unchecked!($query, $($args),*);
156+
}
157+
macro_result!($($args),*)
158+
})
159+
);
160+
138161
/// A variant of [query!] where the SQL query is stored in a separate file.
139162
///
140163
/// Useful for large queries and potentially cleaner than multiline strings.
@@ -196,6 +219,27 @@ macro_rules! query_file (
196219
})
197220
);
198221

222+
/// A variant of [query_file!] which does not check the input or output types. This still does parse
223+
/// the query to ensure it's syntactically and semantically valid for the current database.
224+
#[macro_export]
225+
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
226+
macro_rules! query_file_unchecked (
227+
($query:literal) => (#[allow(dead_code)]{
228+
#[macro_use]
229+
mod _macro_result {
230+
$crate::sqlx_macros::query_file_unchecked!($query);
231+
}
232+
macro_result!()
233+
});
234+
($query:literal, $($args:expr),*$(,)?) => (#[allow(dead_code)]{
235+
#[macro_use]
236+
mod _macro_result {
237+
$crate::sqlx_macros::query_file_unchecked!($query, $($args),*);
238+
}
239+
macro_result!($($args),*)
240+
})
241+
);
242+
199243
/// A variant of [query!] which takes a path to an explicitly defined struct as the output type.
200244
///
201245
/// This lets you return the struct from a function or add your own trait implementations.

0 commit comments

Comments
 (0)