optimize db pooling

main
Mathias Rothenhaeusler 2024-03-16 15:15:07 +01:00
parent 136039d1a1
commit 1e3900c93a
13 changed files with 121 additions and 128 deletions

View File

@ -1,7 +1,6 @@
use mysql::PooledConn;
use crate::{ use crate::{
container, container,
database::db::Db,
service::{ service::{
file_service::FileService, filter_service::FilterService, job_service::JobService, file_service::FileService, filter_service::FilterService, job_service::JobService,
merchant_service::MerchantService, schema_service::SchemaService, merchant_service::MerchantService, schema_service::SchemaService,
@ -10,14 +9,14 @@ use crate::{
use super::command::{Cli, Commands}; use super::command::{Cli, Commands};
pub fn process_args(args: Cli, conn: &mut PooledConn) { pub fn process_args(args: Cli, db: Db) {
match args.mode { match args.mode {
Commands::Merch { search } => { Commands::Merch { search } => {
let mut merchant_service: MerchantService = container::get_merchant_service(conn); let mut merchant_service: MerchantService = container::get_merchant_service(db);
merchant_service.get_merchant(&search.unwrap()); merchant_service.get_merchant(&search.unwrap());
} }
Commands::Schema { search } => { Commands::Schema { search } => {
let mut schema_service: SchemaService = container::get_schema_service(conn); let mut schema_service: SchemaService = container::get_schema_service(db);
schema_service.get_columns(&search.unwrap()) schema_service.get_columns(&search.unwrap())
} }
Commands::Filter { Commands::Filter {
@ -26,7 +25,7 @@ pub fn process_args(args: Cli, conn: &mut PooledConn) {
all, all,
log, log,
} => { } => {
let mut filter_service: FilterService = container::get_filter_service(conn); let mut filter_service: FilterService = container::get_filter_service(db);
if config { if config {
filter_service.get_filter_configs(&filter_id) filter_service.get_filter_configs(&filter_id)
} else if log { } else if log {
@ -36,11 +35,11 @@ pub fn process_args(args: Cli, conn: &mut PooledConn) {
} }
} }
Commands::Page { page_id } => { Commands::Page { page_id } => {
let mut file_service: FileService = container::get_page_service(conn); let mut file_service: FileService = container::get_page_service(db);
file_service.get_page(&page_id); file_service.get_page(&page_id);
} }
Commands::Job { job_id, log } => { Commands::Job { job_id, log } => {
let mut job_service: JobService = container::get_job_service(conn); let mut job_service: JobService = container::get_job_service(db);
if log { if log {
job_service.get_job_log(&job_id); job_service.get_job_log(&job_id);
} else { } else {

View File

@ -1,34 +1,36 @@
use mysql::PooledConn;
use crate::{ use crate::{
repository::{filter_repo::FilterRepo, merchant_repo::MerchantRepo, schema_repo::SchemaRepo, file_repo::FileRepo, job_repo::JobRepo}, database::db::Db,
repository::{
file_repo::FileRepo, filter_repo::FilterRepo, job_repo::JobRepo,
merchant_repo::MerchantRepo, schema_repo::SchemaRepo,
},
service::{ service::{
filter_service::FilterService, merchant_service::MerchantService, file_service::FileService, filter_service::FilterService, job_service::JobService,
schema_service::SchemaService, file_service::FileService, job_service::JobService, merchant_service::MerchantService, schema_service::SchemaService,
}, },
}; };
pub fn get_filter_service(pool: &mut PooledConn) -> FilterService { pub fn get_filter_service(pool: Db) -> FilterService {
let repo = FilterRepo::new(pool); let repo = FilterRepo::new(pool);
return FilterService::new(repo); return FilterService::new(repo);
} }
pub fn get_merchant_service(pool: &mut PooledConn) -> MerchantService { pub fn get_merchant_service(pool: Db) -> MerchantService {
let repo = MerchantRepo::new(pool); let repo = MerchantRepo::new(pool);
return MerchantService::new(repo); return MerchantService::new(repo);
} }
pub fn get_schema_service(pool: &mut PooledConn) -> SchemaService { pub fn get_schema_service(pool: Db) -> SchemaService {
let repo = SchemaRepo::new(pool); let repo = SchemaRepo::new(pool);
return SchemaService::new(repo); return SchemaService::new(repo);
} }
pub fn get_page_service(pool: &mut PooledConn) -> FileService { pub fn get_page_service(pool: Db) -> FileService {
let repo = FileRepo::new(pool); let repo = FileRepo::new(pool);
return FileService::new(repo); return FileService::new(repo);
} }
pub(crate) fn get_job_service(pool: &mut PooledConn) -> JobService { pub(crate) fn get_job_service(pool: Db) -> JobService {
let repo = JobRepo::new(pool); let repo = JobRepo::new(pool);
return JobService::new(repo); return JobService::new(repo);
} }

View File

@ -1,7 +1,6 @@
use clap::Parser; use clap::Parser;
use cli::{command::Cli, controller}; use cli::{command::Cli, controller};
use database::db::Db; use database::db::Db;
use mysql::PooledConn;
pub mod cli; pub mod cli;
pub mod container; pub mod container;
@ -15,14 +14,8 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let args: Cli = Cli::parse(); let args: Cli = Cli::parse();
let env: &String = &Cli::parse().env.unwrap_or("local".to_string()); let env: &String = &Cli::parse().env.unwrap_or("local".to_string());
let db: Db = Db::initialize(env.to_string())?; let db: Db = Db::initialize(env.to_string())?;
let conn_res: Result<PooledConn, mysql::Error> = db.get_connection();
match conn_res { controller::process_args(args, db);
Ok(mut conn) => controller::process_args(args, &mut conn),
Err(e) => {
println!("Error connecting DB: {e}");
}
}
Ok(()) Ok(())
} }

View File

@ -1,28 +1,31 @@
use mysql::{PooledConn, params, prelude::Queryable}; use mysql::{params, prelude::Queryable};
use crate::entity::file::Page; use crate::{database::db::Db, entity::file::Page};
#[derive(Debug)] #[derive(Debug)]
pub struct FileRepo<'a> { pub struct FileRepo {
db_pool: &'a mut PooledConn, db_pool: Db,
} }
impl<'a> FileRepo<'a> { impl FileRepo {
pub fn new(db_pool: &'a mut PooledConn) -> Self { pub fn new(db: Db) -> Self {
Self { db_pool } Self { db_pool: db }
} }
pub fn find_page(&mut self, file_id: &usize) -> Result<Vec<Page>, mysql::Error> { pub fn find_page(&mut self, file_id: &usize) -> Result<Vec<Page>, mysql::Error> {
let stat = self.db_pool let mut connection: mysql::PooledConn = self.db_pool.get_connection()?;
let stat = connection
.prep("SELECT m_id, description, file FROM global_data.effi_file_mapping WHERE file_id = :file_id") .prep("SELECT m_id, description, file FROM global_data.effi_file_mapping WHERE file_id = :file_id")
.unwrap(); .unwrap();
connection.exec_map(
self.db_pool.exec_map(
stat, stat,
params! {"file_id" => file_id}, params! {"file_id" => file_id},
|(m_id, description, file)| Page { m_id, title: description, file }, |(m_id, description, file)| Page {
m_id,
title: description,
file,
},
) )
} }
} }

View File

@ -1,20 +1,23 @@
use mysql::{params, prelude::Queryable, PooledConn}; use mysql::{params, prelude::Queryable, PooledConn};
use crate::entity::{filter::Filter, filter_config::FilterConfig, filter_log::FilterLog}; use crate::{
database::db::Db,
entity::{filter::Filter, filter_config::FilterConfig, filter_log::FilterLog},
};
#[derive(Debug)] #[derive(Debug)]
pub struct FilterRepo<'a> { pub struct FilterRepo {
db_pool: &'a mut PooledConn, db: Db,
} }
impl<'a> FilterRepo<'a> { impl FilterRepo {
pub fn new(db_pool: &'a mut PooledConn) -> Self { pub fn new(db: Db) -> Self {
Self { db_pool } Self { db }
} }
pub fn find_by_id(&mut self, filter_id: &usize) -> Result<Vec<Filter>, mysql::Error> { pub fn find_by_id(&mut self, filter_id: &usize) -> Result<Vec<Filter>, mysql::Error> {
let stat = self.db_pool let mut connection: PooledConn = self.db.get_connection()?;
.prep( let stat = connection.prep(
"SELECT f.filter_module_id, f.file_name, f.description, fm.filter_module_no, fm.filter_user "SELECT f.filter_module_id, f.file_name, f.description, fm.filter_module_no, fm.filter_user
FROM filter f FROM filter f
JOIN global_data.filter_modules fm ON fm.filter_module_id = f.filter_module_id JOIN global_data.filter_modules fm ON fm.filter_module_id = f.filter_module_id
@ -22,9 +25,10 @@ impl<'a> FilterRepo<'a> {
") ")
.unwrap(); .unwrap();
self.db_pool.exec_map( let params = params! {"filter_id" => filter_id};
connection.exec_map(
stat, stat,
params! {"filter_id" => filter_id}, params,
|(filter_module_id, file_name, description, filter_module_no, filter_user)| Filter { |(filter_module_id, file_name, description, filter_module_no, filter_user)| Filter {
filter_module_id, filter_module_id,
file_name, file_name,
@ -39,7 +43,8 @@ impl<'a> FilterRepo<'a> {
&mut self, &mut self,
filter_id: &usize, filter_id: &usize,
) -> Result<Vec<FilterConfig>, mysql::Error> { ) -> Result<Vec<FilterConfig>, mysql::Error> {
let stat = self.db_pool let mut connection: PooledConn = self.db.get_connection()?;
let stat = connection
.prep( .prep(
" "
SELECT t1.attribute, t1.value1, IF(t1.value2 is null or t1.value2 = '', 'EMPTY', t1.value2) as value2, t2.name, t1.upd_ts SELECT t1.attribute, t1.value1, IF(t1.value2 is null or t1.value2 = '', 'EMPTY', t1.value2) as value2, t2.name, t1.upd_ts
@ -50,7 +55,7 @@ impl<'a> FilterRepo<'a> {
) )
.unwrap(); .unwrap();
self.db_pool.exec_map( connection.exec_map(
stat, stat,
params! {"filter_id" => filter_id}, params! {"filter_id" => filter_id},
|(attribute, value1, value2, name, upd_ts)| FilterConfig { |(attribute, value1, value2, name, upd_ts)| FilterConfig {
@ -63,12 +68,9 @@ impl<'a> FilterRepo<'a> {
) )
} }
pub fn find_filter_log( pub fn find_filter_log(&mut self, filter_id: &usize) -> Result<Vec<FilterLog>, mysql::Error> {
&mut self, let mut connection: PooledConn = self.db.get_connection()?;
filter_id: &usize, let stat = connection
) -> Result<Vec<FilterLog>, mysql::Error> {
let stat = self
.db_pool
.prep( .prep(
" "
SELECT t1.run_ts, t1.error_code, t1.error_msg, t1.mysql_error SELECT t1.run_ts, t1.error_code, t1.error_msg, t1.mysql_error
@ -78,7 +80,7 @@ impl<'a> FilterRepo<'a> {
) )
.unwrap(); .unwrap();
self.db_pool.exec_map( connection.exec_map(
stat, stat,
params! {"filter_id" => filter_id}, params! {"filter_id" => filter_id},
|(run_ts, error_code, error_msg, mysql_error)| FilterLog { |(run_ts, error_code, error_msg, mysql_error)| FilterLog {

View File

@ -1,20 +1,23 @@
use mysql::{params, prelude::Queryable, PooledConn}; use mysql::{params, prelude::Queryable};
use crate::entity::job::{Job, JobLog}; use crate::{
database::db::Db,
entity::job::{Job, JobLog},
};
#[derive(Debug)] #[derive(Debug)]
pub struct JobRepo<'a> { pub struct JobRepo {
db_pool: &'a mut PooledConn, db: Db,
} }
impl<'a> JobRepo<'a> { impl JobRepo {
pub fn new(db_pool: &'a mut PooledConn) -> Self { pub fn new(db: Db) -> Self {
Self { db_pool } Self { db }
} }
pub fn find_by_id(&mut self, job_id: &usize) -> Result<Vec<Job>, mysql::Error> { pub fn find_by_id(&mut self, job_id: &usize) -> Result<Vec<Job>, mysql::Error> {
let stat = self let mut connection: mysql::PooledConn = self.db.get_connection()?;
.db_pool let stat = connection
.prep( .prep(
"SELECT "SELECT
jm.file_name, jm.description jm.file_name, jm.description
@ -26,7 +29,7 @@ impl<'a> JobRepo<'a> {
) )
.unwrap(); .unwrap();
self.db_pool.exec_map( connection.exec_map(
stat, stat,
params! {"job_id" => job_id}, params! {"job_id" => job_id},
|(file_name, description)| Job { |(file_name, description)| Job {
@ -37,8 +40,8 @@ impl<'a> JobRepo<'a> {
} }
pub fn find_log(&mut self, job_id: &usize) -> Result<Vec<JobLog>, mysql::Error> { pub fn find_log(&mut self, job_id: &usize) -> Result<Vec<JobLog>, mysql::Error> {
let stat = self let mut connection: mysql::PooledConn = self.db.get_connection()?;
.db_pool let stat = connection
.prep( .prep(
" "
SELECT jea.content, jea.upd_ts SELECT jea.content, jea.upd_ts
@ -51,10 +54,8 @@ impl<'a> JobRepo<'a> {
) )
.unwrap(); .unwrap();
self.db_pool.exec_map( connection.exec_map(stat, params! {"job_id" => job_id}, |(content, upd_ts)| {
stat, JobLog { content, upd_ts }
params! {"job_id" => job_id}, })
|( content, upd_ts )| JobLog { content, upd_ts },
)
} }
} }

View File

@ -1,30 +1,25 @@
use mysql::{params, prelude::Queryable, PooledConn}; use mysql::{params, prelude::Queryable};
use crate::entity::merchant::Merchant; use crate::{database::db::Db, entity::merchant::Merchant};
pub struct MerchantRepo<'a> { pub struct MerchantRepo {
db_pool: &'a mut PooledConn, db_pool: Db,
} }
impl<'a> MerchantRepo<'a> { impl MerchantRepo {
pub fn new(db_pool: &'a mut PooledConn) -> Self { pub fn new(db_pool: Db) -> Self {
Self { db_pool } Self { db_pool }
} }
pub fn find_by_name_or_id( pub fn find_by_name_or_id(&mut self, search: &str) -> Result<Vec<Merchant>, mysql::Error> {
&mut self, let mut connection = self.db_pool.get_connection()?;
search: &str, let stat = connection
) -> Result<Vec<Merchant>, mysql::Error> {
let stat = self.db_pool
.prep("SELECT m_name, m_id FROM global_data.merchant WHERE m_id = :search OR m_name LIKE :search2;") .prep("SELECT m_name, m_id FROM global_data.merchant WHERE m_id = :search OR m_name LIKE :search2;")
.unwrap(); .unwrap();
let search_like = format!("%{}%", search); let search_like = format!("%{}%", search);
self.db_pool.exec_map( let params = params! {"search" => search, "search2" => search_like};
stat, connection.exec_map(stat, params, |(m_name, m_id)| Merchant { m_name, m_id })
params! {"search" => search, "search2" => search_like},
|(m_name, m_id)| Merchant { m_name, m_id },
)
} }
} }

View File

@ -1,25 +1,23 @@
use mysql::{params, prelude::Queryable, PooledConn}; use mysql::{params, prelude::Queryable};
use crate::entity::schema::Schema; use crate::{database::db::Db, entity::schema::Schema};
pub struct SchemaRepo<'a> { pub struct SchemaRepo {
db_pool: &'a mut PooledConn, db: Db,
} }
impl<'a> SchemaRepo<'a> { impl SchemaRepo {
pub fn new(db_pool: &'a mut PooledConn) -> Self { pub fn new(db: Db) -> Self {
Self { db_pool } Self { db }
} }
pub fn find_by_column( pub fn find_by_column(&mut self, column_name: &str) -> Result<Vec<Schema>, mysql::Error> {
&mut self, let mut connection: mysql::PooledConn = self.db.get_connection()?;
column_name: &str, let stat = connection
) -> Result<Vec<Schema>, mysql::Error> {
let stat = self.db_pool
.prep("SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name LIKE :c_name") .prep("SELECT TABLE_NAME, COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name LIKE :c_name")
.unwrap(); .unwrap();
self.db_pool.exec_map( connection.exec_map(
stat, stat,
params! {"c_name" => column_name}, params! {"c_name" => column_name},
|(table_name, column_name, column_type)| Schema { |(table_name, column_name, column_type)| Schema {

View File

@ -2,14 +2,14 @@ use rcc::TerminalSize;
use crate::{entity::file::Page, repository::file_repo::FileRepo}; use crate::{entity::file::Page, repository::file_repo::FileRepo};
pub struct FileService<'a> { pub struct FileService {
repo: FileRepo<'a>, repo: FileRepo,
} }
impl TerminalSize for FileService<'_> {} impl TerminalSize for FileService {}
impl<'a> FileService<'a> { impl FileService {
pub fn new(repo: FileRepo<'a>) -> Self { pub fn new(repo: FileRepo) -> Self {
Self { repo } Self { repo }
} }

View File

@ -6,14 +6,14 @@ use crate::{
repository::filter_repo::FilterRepo, repository::filter_repo::FilterRepo,
}; };
pub struct FilterService<'a> { pub struct FilterService {
repo: FilterRepo<'a>, repo: FilterRepo,
} }
impl TerminalSize for FilterService<'_> {} impl TerminalSize for FilterService {}
impl<'a> FilterService<'a> { impl FilterService {
pub fn new(repo: FilterRepo<'a>) -> Self { pub fn new(repo: FilterRepo) -> Self {
Self { repo } Self { repo }
} }

View File

@ -5,14 +5,14 @@ use crate::{
repository::job_repo::JobRepo, repository::job_repo::JobRepo,
}; };
pub struct JobService<'a> { pub struct JobService {
pub repo: JobRepo<'a>, pub repo: JobRepo,
} }
impl TerminalSize for JobService<'_> {} impl TerminalSize for JobService {}
impl<'a> JobService<'a> { impl JobService {
pub fn new(repo: JobRepo<'a>) -> Self { pub fn new(repo: JobRepo) -> Self {
Self { repo } Self { repo }
} }

View File

@ -2,14 +2,14 @@ use rcc::TerminalSize;
use crate::repository::merchant_repo::MerchantRepo; use crate::repository::merchant_repo::MerchantRepo;
pub struct MerchantService<'a> { pub struct MerchantService {
repo: MerchantRepo<'a>, repo: MerchantRepo,
} }
impl TerminalSize for MerchantService<'_> {} impl TerminalSize for MerchantService {}
impl<'a> MerchantService<'a> { impl MerchantService {
pub fn new(repo: MerchantRepo<'a>) -> Self { pub fn new(repo: MerchantRepo) -> Self {
Self { repo } Self { repo }
} }

View File

@ -2,14 +2,14 @@ use rcc::TerminalSize;
use crate::repository::schema_repo::SchemaRepo; use crate::repository::schema_repo::SchemaRepo;
pub struct SchemaService<'a> { pub struct SchemaService {
repo: SchemaRepo<'a>, repo: SchemaRepo,
} }
impl TerminalSize for SchemaService<'_> {} impl TerminalSize for SchemaService {}
impl<'a> SchemaService<'a> { impl SchemaService {
pub fn new(repo: SchemaRepo<'a>) -> Self { pub fn new(repo: SchemaRepo) -> Self {
Self { repo } Self { repo }
} }
pub fn get_columns(&mut self, column: &str) { pub fn get_columns(&mut self, column: &str) {