diesel::query_builder

Function debug_query

Source
pub fn debug_query<DB, T>(query: &T) -> DebugQuery<'_, T, DB>
Expand description

Takes a query QueryFragment expression as an argument and returns a type that implements fmt::Display and fmt::Debug to show the query.

The Display implementation will show the exact query being sent to the server, with a comment showing the values of the bind parameters. The Debug implementation will include the same information in a more structured form, and respects pretty printing.

§Example

§Returning SQL from a count statement:

let sql = debug_query::<DB, _>(&users.count()).to_string();
assert_eq!(sql, "SELECT COUNT(*) FROM `users` -- binds: []");

let query = users.find(1);
let debug = debug_query::<DB, _>(&query);
assert_eq!(debug.to_string(), "SELECT `users`.`id`, `users`.`name` FROM `users` \
    WHERE (`users`.`id` = ?) -- binds: [1]");

let debug = format!("{:?}", debug);
let expected = "Query { \
    sql: \"SELECT `users`.`id`, `users`.`name` FROM `users` WHERE \
        (`users`.`id` = ?)\", \
    binds: [1] \
}";
assert_eq!(debug, expected);