Macro sqlx::query_file
source · macro_rules! query_file { ($path:literal) => { ... }; ($path:literal, $($args:tt)*) => { ... }; }
Available on crate feature
macros
only.Expand description
A variant of query!
where the SQL query is stored in a separate file.
Useful for large queries and potentially cleaner than multiline strings.
The syntax and requirements (see query!
) are the same except the SQL
string is replaced by a file path.
The file must be relative to the project root (the directory containing Cargo.toml
),
unlike include_str!()
which uses compiler internals to get the path of the file where it
was invoked.
examples/queries/account-by-id.sql
:
select * from (select (1) as id, 'Herp Derpinson' as name) accounts
where id = ?
src/my_query.rs
:
ⓘ
let account = sqlx::query_file!("tests/test-query-account-by-id.sql", 1i32)
.fetch_one(&mut conn)
.await?;
println!("{account:?}");
println!("{}: {}", account.id, account.name);