mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-12-27 22:18:51 +00:00
Can log select queries
This commit is contained in:
parent
11051b28a0
commit
df1e2ffd7b
@ -26,4 +26,7 @@ database:
|
||||
host: localhost
|
||||
name: comunic
|
||||
username: pierre
|
||||
password: pierre
|
||||
password: pierre
|
||||
|
||||
# If set to true, every requests made on the database will be shown on the terminal
|
||||
log_all_queries: true
|
@ -11,6 +11,7 @@ pub struct DatabaseConfig {
|
||||
pub name: String,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub log_all_queries: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@ -59,6 +60,7 @@ impl Config {
|
||||
name: Config::yaml_str(parsed_db, "name"),
|
||||
username: Config::yaml_str(parsed_db, "username"),
|
||||
password: Config::yaml_str(parsed_db, "password"),
|
||||
log_all_queries: Config::yaml_bool(parsed_db, "log_all_queries"),
|
||||
};
|
||||
|
||||
let proxy = Config::yaml_str(parsed, "proxy");
|
||||
|
@ -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!()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user