From ff5a5fd34618f2dd6543c40c2322e570da775081 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Thu, 27 Jan 2022 08:21:04 +0000 Subject: [PATCH] Implement SQL query debugging in response output --- src/dbs/executor.rs | 11 +++++++++++ src/dbs/options.rs | 10 ++++++++++ src/dbs/response.rs | 2 ++ 3 files changed, 23 insertions(+) diff --git a/src/dbs/executor.rs b/src/dbs/executor.rs index e7221e59..39963ecc 100644 --- a/src/dbs/executor.rs +++ b/src/dbs/executor.rs @@ -109,6 +109,7 @@ impl<'a> Executor<'a> { pub fn buf_cancel(&self, v: Response) -> Response { Response { + sql: v.sql, time: v.time, status: Status::Err, detail: Some(format!("Transaction cancelled")), @@ -119,6 +120,7 @@ impl<'a> Executor<'a> { pub fn buf_commit(&self, v: Response) -> Response { match &self.err { Some(_) => Response { + sql: v.sql, time: v.time, status: Status::Err, detail: match v.status { @@ -157,6 +159,7 @@ impl<'a> Executor<'a> { "EVENT_QUERIES" => opt = opt.events(stm.what), "TABLE_QUERIES" => opt = opt.tables(stm.what), "IMPORT" => opt = opt.import(stm.what), + "DEBUG" => opt = opt.debug(stm.what), _ => break, } continue; @@ -249,6 +252,10 @@ impl<'a> Executor<'a> { // Produce the response let res = match res { Ok(v) => Response { + sql: match opt.debug { + true => Some(format!("{}", stm)), + false => None, + }, time: format!("{:?}", dur), status: Status::Ok, detail: None, @@ -257,6 +264,10 @@ impl<'a> Executor<'a> { Err(e) => { // Produce the response let res = Response { + sql: match opt.debug { + true => Some(format!("{}", stm)), + false => None, + }, time: format!("{:?}", dur), status: Status::Err, detail: Some(format!("{}", e)), diff --git a/src/dbs/options.rs b/src/dbs/options.rs index 6498cbe5..311d5e91 100644 --- a/src/dbs/options.rs +++ b/src/dbs/options.rs @@ -14,6 +14,7 @@ use crate::sql::version::Version; pub struct Options<'a> { pub auth: &'a Auth, pub dive: usize, // How many subqueries have we gone into? + pub debug: bool, // Should we debug query response SQL? pub force: bool, // Should we force tables/events to re-run? pub fields: bool, // Should we process field queries? pub events: bool, // Should we process event queries? @@ -34,6 +35,7 @@ impl<'a> Options<'a> { Options { auth, dive: 0, + debug: false, force: false, fields: true, events: true, @@ -57,6 +59,14 @@ impl<'a> Options<'a> { } } + // Create a new Options object for a subquery + pub fn debug(&self, v: bool) -> Options<'a> { + Options { + debug: v, + ..*self + } + } + // Create a new Options object for a subquery pub fn force(&self, v: bool) -> Options<'a> { Options { diff --git a/src/dbs/response.rs b/src/dbs/response.rs index b198f877..aa6f014e 100644 --- a/src/dbs/response.rs +++ b/src/dbs/response.rs @@ -13,6 +13,8 @@ pub struct Responses(pub Vec); #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Response { + #[serde(skip_serializing_if = "Option::is_none")] + pub sql: Option, pub time: String, pub status: Status, #[serde(skip_serializing_if = "Option::is_none")]