Function diesel::sql_query[][src]

pub fn sql_query<T: Into<String>>(query: T) -> SqlQuery
Expand description

Construct a full SQL query using raw SQL.

This function exists for cases where a query needs to be written that is not supported by the query builder. Unlike most queries in Diesel, sql_query will deserialize its data by name, not by index. That means that you cannot deserialize into a tuple, and structs which you deserialize from this function will need to have #[derive(QueryableByName)]

Safety

The implementation of QueryableByName will assume that columns with a given name will have a certain type. The compiler will be unable to verify that the given type is correct. If your query returns a column of an unexpected type, the result may have the wrong value, or return an error.

Example

let users = sql_query("SELECT * FROM users ORDER BY id")
    .load(&connection);
let expected_users = vec![
    User { id: 1, name: "Sean".into() },
    User { id: 2, name: "Tess".into() },
];
assert_eq!(Ok(expected_users), users);