Skip to content

Commit 2ecfa84

Browse files
committed
feat(query): Debug Query as SQL string.
Allow to print the current SQL query, whether it's bound or not. Useful for logging and diagnostics.
1 parent f6fbc6a commit 2ecfa84

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/query.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use hyper::{header::CONTENT_LENGTH, Method, Request};
22
use serde::Deserialize;
3+
use std::fmt;
34
use url::Url;
45

56
use crate::headers::with_request_headers;
@@ -22,6 +23,12 @@ pub struct Query {
2223
sql: SqlBuilder,
2324
}
2425

26+
impl fmt::Debug for Query {
27+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28+
write!(f, "{sql}", sql = self.sql)
29+
}
30+
}
31+
2532
impl Query {
2633
pub(crate) fn new(client: &Client, template: &str) -> Self {
2734
Self {

tests/it/query.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,14 @@ async fn overrides_client_options() {
214214
// should override the client options
215215
assert_eq!(value, override_value);
216216
}
217+
218+
#[tokio::test]
219+
async fn prints_query() {
220+
let client = prepare_database!();
221+
222+
let q = client.query("SELECT ?fields FROM test WHERE a = ? AND b < ?");
223+
assert_eq!(
224+
format!("{q:?}"),
225+
"SELECT ?fields FROM test WHERE a = ? AND b < ?"
226+
);
227+
}

0 commit comments

Comments
 (0)