Skip to main content

sql

Macro sql 

Source
macro_rules! sql {
    ($query:expr) => { ... };
}
Expand description

Convert SQL with ? placeholders to the active backend’s placeholder style.

SQLite: returns the input &str directly — zero allocation, zero runtime cost.

PostgreSQL: rewrites ? to $1, $2, … using rewrite_placeholders. The rewritten string is leaked via Box::leak to obtain &'static str — no caching: each call site leaks one allocation per unique SQL string. The set of unique SQL strings is bounded (call sites are fixed at compile time), so total leaked memory is bounded and acceptable for a long-running process. Do NOT wrap PostgreSQL JSONB queries using ?/?|/?& operators through this macro; use $N placeholders directly for those.

§Example

let rows = sqlx::query(sql!("SELECT id FROM messages WHERE conversation_id = ?"))
    .bind(cid)
    .fetch_all(&pool)
    .await?;