mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-03 17:14:03 +00:00 
			
		
		
		
	Can log select queries
This commit is contained in:
		@@ -27,3 +27,6 @@ database:
 | 
			
		||||
  name: comunic
 | 
			
		||||
  username: 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| {
 | 
			
		||||
@@ -860,3 +865,35 @@ 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