diesel::dsl

Function sql

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

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.

This function is intended for use when you need a small bit of raw SQL in your query. If you want to write the entire query using raw SQL, use sql_query instead.

Query parameters can be bound into the literal SQL using SqlLiteral::bind().

§Safety

The compiler will be unable to verify the correctness of the annotated type. If you give the wrong type, it’ll either return an error when deserializing the query result or produce unexpected values.

§Examples

use diesel::dsl::sql;
let user = users.filter(sql::<Bool>("name = 'Sean'")).first(connection)?;
let expected = (1, String::from("Sean"));
assert_eq!(expected, user);
let query = users
    .select(name)
    .filter(
        sql::<Bool>("id > ")
        .bind::<Integer,_>(1)
        .sql(" AND name <> ")
        .bind::<Text, _>("Ryan")
    )
    .get_results(connection);
let expected = vec!["Tess".to_string()];
assert_eq!(Ok(expected), query);