Simplify database query response object

This commit is contained in:
Tobie Morgan Hitchcock 2022-05-02 00:35:01 +01:00
parent a4e07f693b
commit 0cbcb3b374
3 changed files with 96 additions and 59 deletions

View file

@ -1,5 +1,5 @@
use crate::ctx::Context; use crate::ctx::Context;
use crate::dbs::response::{Response, Responses, Status}; use crate::dbs::response::{Response, Responses};
use crate::dbs::Auth; use crate::dbs::Auth;
use crate::dbs::Level; use crate::dbs::Level;
use crate::dbs::Options; use crate::dbs::Options;
@ -97,9 +97,7 @@ impl Executor {
Response { Response {
sql: v.sql, sql: v.sql,
time: v.time, time: v.time,
status: Status::Err, result: Err(Error::QueryCancelled),
detail: Some(format!("{}", Error::QueryCancelled)),
result: None,
} }
} }
@ -108,12 +106,10 @@ impl Executor {
true => Response { true => Response {
sql: v.sql, sql: v.sql,
time: v.time, time: v.time,
status: Status::Err, result: match v.result {
detail: match v.status { Ok(_) => Err(Error::QueryNotExecuted),
Status::Ok => Some(format!("{}", Error::QueryNotExecuted)), Err(e) => Err(e),
Status::Err => v.detail,
}, },
result: None,
}, },
_ => v, _ => v,
} }
@ -274,10 +270,8 @@ impl Executor {
true => Some(format!("{}", stm)), true => Some(format!("{}", stm)),
false => None, false => None,
}, },
time: format!("{:?}", dur), time: dur,
status: Status::Ok, result: Ok(v),
detail: None,
result: v.output(),
}, },
Err(e) => { Err(e) => {
// Produce the response // Produce the response
@ -286,10 +280,8 @@ impl Executor {
true => Some(format!("{}", stm)), true => Some(format!("{}", stm)),
false => None, false => None,
}, },
time: format!("{:?}", dur), time: dur,
status: Status::Err, result: Err(e),
detail: Some(format!("{}", e)),
result: None,
}; };
// Mark the error // Mark the error
self.err = true; self.err = true;
@ -310,6 +302,6 @@ impl Executor {
} }
} }
// Return responses // Return responses
Ok(Responses(out)) Ok(out)
} }
} }

View file

@ -1,54 +1,89 @@
use crate::err::Error;
use crate::sql::value::Value; use crate::sql::value::Value;
use serde::{Deserialize, Serialize}; use serde::ser::SerializeStruct;
use serde::Serialize;
use std::time::Duration;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] pub type Responses = Vec<Response>;
#[serde(rename_all = "UPPERCASE")]
pub enum Status {
Ok,
Err,
}
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] #[derive(Debug)]
pub struct Responses(pub Vec<Response>);
impl Responses {
pub fn first(mut self) -> Response {
self.0.remove(0)
}
}
#[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 sql: Option<String>,
pub time: String, pub time: Duration,
pub status: Status, pub result: Result<Value, Error>,
#[serde(skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub result: Option<Value>,
} }
impl Response { impl Response {
// Check if response succeeded // Return the transaction speed
pub fn is_ok(&self) -> bool { pub fn speed(&self) -> String {
match self.status { format!("{:?}", self.time)
Status::Ok => true,
Status::Err => false,
}
}
// Check if response failed
pub fn is_err(&self) -> bool {
match self.status {
Status::Ok => false,
Status::Err => true,
}
} }
// Retrieve the response as a result // Retrieve the response as a result
pub fn output(self) -> Result<Value, String> { pub fn output(&self) -> Result<&Value, &Error> {
match self.status { match &self.result {
Status::Ok => Ok(self.result.unwrap()), Ok(v) => Ok(v),
Status::Err => Err(self.detail.unwrap()), Err(e) => Err(e),
}
}
}
impl Serialize for Response {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match &self.result {
Ok(v) => match v {
Value::None => match &self.sql {
Some(s) => {
let mut val = serializer.serialize_struct("Response", 3)?;
val.serialize_field("sql", s.as_str())?;
val.serialize_field("time", self.speed().as_str())?;
val.serialize_field("status", "OK")?;
val.end()
}
None => {
let mut val = serializer.serialize_struct("Response", 2)?;
val.serialize_field("time", self.speed().as_str())?;
val.serialize_field("status", "OK")?;
val.end()
}
},
v => match &self.sql {
Some(s) => {
let mut val = serializer.serialize_struct("Response", 4)?;
val.serialize_field("sql", s.as_str())?;
val.serialize_field("time", self.speed().as_str())?;
val.serialize_field("status", "OK")?;
val.serialize_field("result", v)?;
val.end()
}
None => {
let mut val = serializer.serialize_struct("Response", 3)?;
val.serialize_field("time", self.speed().as_str())?;
val.serialize_field("status", "OK")?;
val.serialize_field("result", v)?;
val.end()
}
},
},
Err(e) => match &self.sql {
Some(s) => {
let mut val = serializer.serialize_struct("Response", 4)?;
val.serialize_field("sql", s.as_str())?;
val.serialize_field("time", self.speed().as_str())?;
val.serialize_field("status", "ERR")?;
val.serialize_field("detail", e)?;
val.end()
}
None => {
let mut val = serializer.serialize_struct("Response", 3)?;
val.serialize_field("time", self.speed().as_str())?;
val.serialize_field("status", "ERR")?;
val.serialize_field("detail", e)?;
val.end()
}
},
} }
} }
} }

View file

@ -2,6 +2,7 @@ use crate::sql::idiom::Idiom;
use crate::sql::thing::Thing; use crate::sql::thing::Thing;
use crate::sql::value::Value; use crate::sql::value::Value;
use msgpack::encode::Error as SerdeError; use msgpack::encode::Error as SerdeError;
use serde::Serialize;
use std::time::Duration; use std::time::Duration;
use storekey::decode::Error as DecodeError; use storekey::decode::Error as DecodeError;
use storekey::encode::Error as EncodeError; use storekey::encode::Error as EncodeError;
@ -206,3 +207,12 @@ pub enum Error {
#[error("Tokio Error: {0}")] #[error("Tokio Error: {0}")]
Tokio(#[from] TokioError<(Option<Thing>, Value)>), Tokio(#[from] TokioError<(Option<Thing>, Value)>),
} }
impl Serialize for Error {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.to_string().as_str())
}
}