[][src]Macro postgres_query::query

macro_rules! query {
    #[proc_macro_hack] => { ... };
}

Constructs a new query.

Usage

The first parameter is the SQL query and is always given as a string literal (this might be relaxed in the future). 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 {
    sql: "INSERT INTO people VALUES ($1, $2)",
    parameters: vec![&age, &"John Wick"],
};