[][src]Macro postgres_query::query

macro_rules! query {
    ($($tt:tt)*) => { ... };
}

Constructs a new query at compile-time. See also query_dyn!.

Usage

This macro expands to an expression with the type Query.

The first parameter is the SQL query and is always given as a string literal. This string literal may contain parameter bindings on the form $ident where ident is any valid Rust identifier ($abc, $value_123, etc.). The order of the parameters does not matter.

let age = 42;
let insert_person = query!(
    "INSERT INTO people VALUES ($age, $name)",
    name = "John Wick", // Binds "$name" to "John Wick"
    age,                // Binds "$age" to the value of `age`
);

During compilation the query is converted into the format expected by PostgreSQL: parameter bindings are converted to using numbers ($1, $2, etc.) and the actual parameter values are put into a 1-indexed array. The code snippet above would be expanded into the following:

let age = 42;
let insert_person = Query::new_static(
    "INSERT INTO people VALUES ($1, $2)",
    vec![&age, &"John Wick"],
);