Macro ormx::conditional_query_as[][src]

macro_rules! conditional_query_as {
    ($($t : tt) *) => { ... };
}
Expand description

An improved version of sqlx::query_as!.

Syntax

The syntax of conditional_query_as! differs from the original sqlx::query_as!. Formally, it accepts this syntax:

conditional_query_as!(
    PATH,
    (
        LITERAL |
        ?(EXPRESSION) |
        PATTERN = EXPRESSION => { (LITERAL | ?(EXPRESSION))* }
    )*
)

Arguments are now provided inline with the query like this:

let user_id = Some(2);
conditional_query_as!(
    User,
    "SELECT * FROM users WHERE user_id =" ?(user_id.unwrap())
);

Also, conditional_query_as! can parse multiple string literals like this:

conditional_query_as!(
    User,
    "SELECT * FROM users"
    "WHERE user_id =" ?(user_id)
    "AND first_name =" ?(first_name)
);

Conditions

conditional_query_as! can be used to have queries depend on a condition during runtime. This is achieved by checking the correctness of all possible queries at compile time. The syntax for a condition is

PATTERN = EXPRESSION => { (LITERAL | ?(EXPRESSION))* }

Please note that conditions can’t be nested right now. Also, the number of conditions per query is currently limited to 5.

Example:

let limit = Some(10);
conditional_query_as!(
    User,
    "SELECT * FROM users"
    Some(l) = limit => {
        "LIMIT" ?(l)
    }
);