From c1decf6644ac0aeab6f19eba279453759b75763c Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Sat, 18 Apr 2020 19:50:40 -0400 Subject: [PATCH] Format SQL queries when printing them to the logs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Cargo.lock | 18 ++++++++++++++++++ sqlx-core/Cargo.toml | 1 + sqlx-core/src/logging.rs | 16 ++++++++++++---- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1508f4a88c..996658a00f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -990,6 +990,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "maplit" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" + [[package]] name = "matches" version = "0.1.8" @@ -1723,6 +1729,17 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "sqlx" version = "0.3.4" @@ -1777,6 +1794,7 @@ dependencies = [ "serde_json", "sha-1", "sha2", + "sqlformat", "time 0.2.9", "tokio 0.2.13", "url 2.1.1", diff --git a/sqlx-core/Cargo.toml b/sqlx-core/Cargo.toml index 3215cb3202..d75e45f837 100644 --- a/sqlx-core/Cargo.toml +++ b/sqlx-core/Cargo.toml @@ -66,6 +66,7 @@ uuid = { version = "0.8.1", default-features = false, optional = true, features serde = { version = "1.0", features = [ "derive" ], optional = true } time = { version = "0.2.7", optional = true } serde_json = { version = "1.0", features = [ "raw_value" ], optional = true } +sqlformat = "0.1.0" # [dependencies.libsqlite3-sys] diff --git a/sqlx-core/src/logging.rs b/sqlx-core/src/logging.rs index a817ddb864..ef364bbe6b 100644 --- a/sqlx-core/src/logging.rs +++ b/sqlx-core/src/logging.rs @@ -8,17 +8,25 @@ macro_rules! log_execution { let elapsed = timer.elapsed(); if elapsed >= std::time::Duration::from_secs(1) { log::warn!( - "{} ..., elapsed: {:.3?}\n\n {}\n", + "{} ..., elapsed: {:.3?}\n\n{}\n", crate::logging::parse_query_summary(query_string), elapsed, - query_string + sqlformat::format( + query_string, + &sqlformat::QueryParams::None, + sqlformat::FormatOptions::default() + ) ); } else { log::debug!( - "{} ..., elapsed: {:.3?}\n\n {}\n", + "{} ..., elapsed: {:.3?}\n\n{}\n", crate::logging::parse_query_summary(query_string), elapsed, - query_string + sqlformat::format( + query_string, + &sqlformat::QueryParams::None, + sqlformat::FormatOptions::default() + ) ); } result