Function diesel::expression::dsl::sql [] [src]

pub fn sql<ST>(sql: &str) -> SqlLiteral<ST>

Use literal SQL in the query builder

Available for when you truly cannot represent something using the expression DSL. You will need to provide the SQL type of the expression, in addition to the SQL.

Bound parameters

If you need to pass arguments to your query, you should use .bind().

Safety

The compiler will be unable to verify the correctness of the annotated type. If you give the wrong type, it'll either crash at runtime when deserializing the query result or produce invalid values.

Examples

use diesel::expression::sql;
use diesel::types::{Bool, Integer, Text};

#[derive(PartialEq, Debug, Queryable)]
struct User {
    id: i32,
    name: String,
}

let setup = sql::<Bool>("INSERT INTO users(name) VALUES('Ruby')");
setup.execute(&connection).expect("Can't insert in users");

let query = sql::<(Integer, Text)>("SELECT id, name FROM users WHERE name='Ruby';");
let users = query.load::<User>(&connection).expect("Can't query users");
assert_eq!(users, vec![User{id: 3, name: "Ruby".to_owned()}]);

let query = users::table.filter(sql::<Bool>("name='Ruby'")); // Same query as above
let users = query.load::<User>(&connection).expect("Can't query users");
assert_eq!(users, vec![User{id: 3, name: "Ruby".to_owned()}]);