I have a String
called main_sql
and a Vec<String>
called main_params
.
main_sql
looks something like: INSERT INTO "foo" ("col1", "col2", "col3", "col4") VALUES (?, ?, NULL, ?)
main_params
looks like ["some string", "another string", "yet another string"]
I then call the following code to execute the insert statement against my database:
use sqlx::{
...
query as sqlx_query, ...
};
...
let mut main_query = sqlx_query(&main_sql);
for param in &main_params {
main_query = main_query.bind(param);
}
let main_result = main_query.execute(pool).await;
...
This all works fine as far as inserting the data to the database goes. However I would like to be able to print out the literal query that is executed, including the bound parameters. (The main use case is that I would like to have the literal insert statements saved in a file so that I can easily repopulate the db afterwards.) So, in the above example, that means I would like to be able to print out:
INSERT INTO "foo" ("col1", "col2", "col3", "col4") VALUES ('some string', 'another string', NULL, 'yet another string')
One might think that the sqlx::query::Query::sql method would do the trick. However that does not print the string with bound variables, but still includes the ?
in the output. That is, in the above example, this will just print:
INSERT INTO "foo" ("col1", "col2", "col3", "col4") VALUES (?, ?, NULL, ?)
Is there a way to print out generated SQL including bound variables using sqlx
?