mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-11-01 14:02:46 +00:00
Format SQL queries when printing them to the logs
Before, the query would be formatted equivalent to the input string:
```
[2020-04-18T23:47:32Z DEBUG sqlx_core::postgres::executor] SELECT id, queue, ..., elapsed: 2.320µs
SELECT id, queue, payload, status, priority, created_at, updated_at
FROM jobs
WHERE status = $1
ORDER BY priority ASC, created_at ASC
```
After, the query is formatted cleanly and consistently:
```
[2020-04-19T00:30:18Z DEBUG sqlx_core::postgres::executor] SELECT id, queue, ..., elapsed: 2.280µs
SELECT
id,
queue,
payload,
status,
priority,
created_at,
updated_at
FROM
jobs
WHERE
status = $1
ORDER BY
priority ASC,
created_at ASC
```
This uses the `sqlformat` crate, which was ported from the
Javascript `sql-formatter-plus` library specifically for this purpose.
This commit is contained in:
parent
1cdfb8507c
commit
f73149a90f
18
Cargo.lock
generated
18
Cargo.lock
generated
@ -991,6 +991,12 @@ dependencies = [
|
|||||||
"cfg-if",
|
"cfg-if",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "maplit"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matches"
|
name = "matches"
|
||||||
version = "0.1.8"
|
version = "0.1.8"
|
||||||
@ -1724,6 +1730,17 @@ version = "0.5.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "sqlformat"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5ce64a4576e1720a2e511bf3ccdb8c0f6cfed0fc265bcbaa0bd369485e02c631"
|
||||||
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
|
"maplit",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "sqlx"
|
name = "sqlx"
|
||||||
version = "0.3.4"
|
version = "0.3.4"
|
||||||
@ -1778,6 +1795,7 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
"sha-1",
|
"sha-1",
|
||||||
"sha2",
|
"sha2",
|
||||||
|
"sqlformat",
|
||||||
"time 0.2.9",
|
"time 0.2.9",
|
||||||
"tokio 0.2.13",
|
"tokio 0.2.13",
|
||||||
"url 2.1.1",
|
"url 2.1.1",
|
||||||
|
|||||||
@ -66,6 +66,7 @@ uuid = { version = "0.8.1", default-features = false, optional = true, features
|
|||||||
serde = { version = "1.0", features = [ "derive" ], optional = true }
|
serde = { version = "1.0", features = [ "derive" ], optional = true }
|
||||||
time = { version = "0.2.7", optional = true }
|
time = { version = "0.2.7", optional = true }
|
||||||
serde_json = { version = "1.0", features = [ "raw_value" ], optional = true }
|
serde_json = { version = "1.0", features = [ "raw_value" ], optional = true }
|
||||||
|
sqlformat = "0.1.0"
|
||||||
|
|
||||||
# <https://github.com/jgallagher/rusqlite/tree/master/libsqlite3-sys>
|
# <https://github.com/jgallagher/rusqlite/tree/master/libsqlite3-sys>
|
||||||
[dependencies.libsqlite3-sys]
|
[dependencies.libsqlite3-sys]
|
||||||
|
|||||||
@ -8,17 +8,25 @@ macro_rules! log_execution {
|
|||||||
let elapsed = timer.elapsed();
|
let elapsed = timer.elapsed();
|
||||||
if elapsed >= std::time::Duration::from_secs(1) {
|
if elapsed >= std::time::Duration::from_secs(1) {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"{} ..., elapsed: {:.3?}\n\n {}\n",
|
"{} ..., elapsed: {:.3?}\n\n{}\n",
|
||||||
crate::logging::parse_query_summary(query_string),
|
crate::logging::parse_query_summary(query_string),
|
||||||
elapsed,
|
elapsed,
|
||||||
query_string
|
sqlformat::format(
|
||||||
|
query_string,
|
||||||
|
&sqlformat::QueryParams::None,
|
||||||
|
sqlformat::FormatOptions::default()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"{} ..., elapsed: {:.3?}\n\n {}\n",
|
"{} ..., elapsed: {:.3?}\n\n{}\n",
|
||||||
crate::logging::parse_query_summary(query_string),
|
crate::logging::parse_query_summary(query_string),
|
||||||
elapsed,
|
elapsed,
|
||||||
query_string
|
sqlformat::format(
|
||||||
|
query_string,
|
||||||
|
&sqlformat::QueryParams::None,
|
||||||
|
sqlformat::FormatOptions::default()
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user