use edm_core::Uuid;
use std::time::Instant;
#[derive(Debug, Clone)]
pub struct QueryContext {
start_time: Instant,
query_id: Uuid,
query: String,
arguments: Vec<String>,
last_insert_id: Option<i64>,
rows_affected: Option<u64>,
success: bool,
}
impl QueryContext {
#[inline]
pub fn new() -> Self {
Self {
start_time: Instant::now(),
query_id: Uuid::new_v4(),
query: String::new(),
arguments: Vec::new(),
last_insert_id: None,
rows_affected: None,
success: false,
}
}
#[inline]
pub fn set_query(&mut self, query: impl ToString) {
self.query = query.to_string();
}
#[inline]
pub fn add_argument(&mut self, arg: impl ToString) {
self.arguments.push(arg.to_string());
}
#[inline]
pub fn append_arguments(&mut self, arguments: &mut Vec<String>) {
self.arguments.append(arguments);
}
#[inline]
pub fn set_last_insert_id(&mut self, id: i64) {
self.last_insert_id = Some(id);
}
#[inline]
pub fn set_query_result(&mut self, rows_affected: Option<u64>, success: bool) {
self.rows_affected = rows_affected;
self.success = success;
}
#[inline]
pub fn get_start_time(&self) -> Instant {
self.start_time
}
#[inline]
pub fn get_query_id(&self) -> Uuid {
self.query_id
}
pub fn get_query(&self) -> &str {
&self.query
}
pub fn get_arguments(&self) -> &[String] {
&self.arguments
}
pub fn get_last_insert_id(&self) -> Option<i64> {
self.last_insert_id
}
pub fn get_rows_affected(&self) -> Option<u64> {
self.rows_affected
}
pub fn is_success(&self) -> bool {
self.success
}
#[inline]
pub fn format_arguments(&self) -> String {
self.arguments.join(", ")
}
}
impl Default for QueryContext {
#[inline]
fn default() -> Self {
Self::new()
}
}