mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Can log select queries
This commit is contained in:
@ -8,7 +8,7 @@ use chrono::{TimeZone, Utc};
|
||||
use mysql::{Binary, Pool, ResultSet, Value};
|
||||
use mysql::prelude::Queryable;
|
||||
|
||||
use crate::data::config::DatabaseConfig;
|
||||
use crate::data::config::{conf, DatabaseConfig};
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
use crate::data::group_id::GroupID;
|
||||
use crate::data::user::UserID;
|
||||
@ -477,6 +477,11 @@ pub fn query<E, F: Fn(&RowResult) -> ProcessRowResult<E>>(info: QueryInfo, proce
|
||||
query = query.add(&format!(" LIMIT {}", info.limit));
|
||||
}
|
||||
|
||||
// Log query, if required
|
||||
if conf().database.log_all_queries {
|
||||
watcher::ExecDatabaseQuery::new(&query, ¶ms).display()
|
||||
}
|
||||
|
||||
// Execute query
|
||||
let mut con = get_connection()?;
|
||||
let stmt = con.prep(&query).or_else(|err| {
|
||||
@ -859,4 +864,36 @@ pub fn update(u: UpdateInfo) -> ResultBoxError<()> {
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
/// Private module used to watch what is going on the database
|
||||
mod watcher {
|
||||
/// Database logging & optimisation
|
||||
pub struct ExecDatabaseQuery {
|
||||
query: String,
|
||||
values: Vec<mysql::Value>,
|
||||
}
|
||||
|
||||
impl ExecDatabaseQuery {
|
||||
/// Construct a new instance of this structure
|
||||
pub fn new(query: &str, values: &Vec<mysql::Value>) -> ExecDatabaseQuery {
|
||||
ExecDatabaseQuery {
|
||||
query: query.to_string(),
|
||||
values: values.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Display the values stored inside this query
|
||||
pub fn display(&self) {
|
||||
println!("==================");
|
||||
println!("DB: {}", self.query);
|
||||
println!("Arguments:");
|
||||
|
||||
for arg in &self.values {
|
||||
println!("* {:?} ", arg);
|
||||
}
|
||||
println!()
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user