Implement SQL query debugging in response output

This commit is contained in:
Tobie Morgan Hitchcock 2022-01-27 08:21:04 +00:00
parent 92e24e2201
commit ff5a5fd346
3 changed files with 23 additions and 0 deletions

View file

@ -109,6 +109,7 @@ impl<'a> Executor<'a> {
pub fn buf_cancel(&self, v: Response) -> Response { pub fn buf_cancel(&self, v: Response) -> Response {
Response { Response {
sql: v.sql,
time: v.time, time: v.time,
status: Status::Err, status: Status::Err,
detail: Some(format!("Transaction cancelled")), detail: Some(format!("Transaction cancelled")),
@ -119,6 +120,7 @@ impl<'a> Executor<'a> {
pub fn buf_commit(&self, v: Response) -> Response { pub fn buf_commit(&self, v: Response) -> Response {
match &self.err { match &self.err {
Some(_) => Response { Some(_) => Response {
sql: v.sql,
time: v.time, time: v.time,
status: Status::Err, status: Status::Err,
detail: match v.status { detail: match v.status {
@ -157,6 +159,7 @@ impl<'a> Executor<'a> {
"EVENT_QUERIES" => opt = opt.events(stm.what), "EVENT_QUERIES" => opt = opt.events(stm.what),
"TABLE_QUERIES" => opt = opt.tables(stm.what), "TABLE_QUERIES" => opt = opt.tables(stm.what),
"IMPORT" => opt = opt.import(stm.what), "IMPORT" => opt = opt.import(stm.what),
"DEBUG" => opt = opt.debug(stm.what),
_ => break, _ => break,
} }
continue; continue;
@ -249,6 +252,10 @@ impl<'a> Executor<'a> {
// Produce the response // Produce the response
let res = match res { let res = match res {
Ok(v) => Response { Ok(v) => Response {
sql: match opt.debug {
true => Some(format!("{}", stm)),
false => None,
},
time: format!("{:?}", dur), time: format!("{:?}", dur),
status: Status::Ok, status: Status::Ok,
detail: None, detail: None,
@ -257,6 +264,10 @@ impl<'a> Executor<'a> {
Err(e) => { Err(e) => {
// Produce the response // Produce the response
let res = Response { let res = Response {
sql: match opt.debug {
true => Some(format!("{}", stm)),
false => None,
},
time: format!("{:?}", dur), time: format!("{:?}", dur),
status: Status::Err, status: Status::Err,
detail: Some(format!("{}", e)), detail: Some(format!("{}", e)),

View file

@ -14,6 +14,7 @@ use crate::sql::version::Version;
pub struct Options<'a> { pub struct Options<'a> {
pub auth: &'a Auth, pub auth: &'a Auth,
pub dive: usize, // How many subqueries have we gone into? 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 force: bool, // Should we force tables/events to re-run?
pub fields: bool, // Should we process field queries? pub fields: bool, // Should we process field queries?
pub events: bool, // Should we process event queries? pub events: bool, // Should we process event queries?
@ -34,6 +35,7 @@ impl<'a> Options<'a> {
Options { Options {
auth, auth,
dive: 0, dive: 0,
debug: false,
force: false, force: false,
fields: true, fields: true,
events: 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 // Create a new Options object for a subquery
pub fn force(&self, v: bool) -> Options<'a> { pub fn force(&self, v: bool) -> Options<'a> {
Options { Options {

View file

@ -13,6 +13,8 @@ pub struct Responses(pub Vec<Response>);
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Response { pub struct Response {
#[serde(skip_serializing_if = "Option::is_none")]
pub sql: Option<String>,
pub time: String, pub time: String,
pub status: Status, pub status: Status,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]