added schema service

main
Mathias Rothenhaeusler 2023-04-21 18:42:22 +02:00
parent 3fd8b22e18
commit fce81a0809
5 changed files with 72 additions and 40 deletions

View File

@ -1,7 +1,11 @@
use mysql::PooledConn;
use crate::{
repository::{filter_repo::FilterRepo, merchant_repo::MerchantRepo}, service::{filter_service::FilterService, merchant_service::MerchantService}
repository::{filter_repo::FilterRepo, merchant_repo::MerchantRepo, schema_repo::SchemaRepo},
service::{
filter_service::FilterService, merchant_service::MerchantService,
schema_service::SchemaService,
},
};
pub fn get_filter_service(pool: &mut PooledConn) -> FilterService {
@ -12,5 +16,9 @@ pub fn get_filter_service(pool: &mut PooledConn) -> FilterService {
pub fn get_merchant_service(pool: &mut PooledConn) -> MerchantService {
let repo = MerchantRepo::new(pool);
return MerchantService::new(repo);
}
pub fn get_schema_service(pool: &mut PooledConn) -> SchemaService {
let repo = SchemaRepo::new(pool);
return SchemaService::new(repo);
}

View File

@ -2,14 +2,16 @@ use clap::Parser;
use cli::cli::{Cli, Commands};
use database::db::Db;
use mysql::PooledConn;
use service::{schema_service, filter_service::FilterService};
use service::{
filter_service::FilterService, merchant_service::MerchantService, schema_service::SchemaService,
};
pub mod cli;
pub mod container;
pub mod database;
pub mod entity;
pub mod repository;
pub mod service;
pub mod container;
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let args = Cli::parse();
@ -18,20 +20,25 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
match args.mode {
Commands::Merch { search } => {
let merchant_service = container::get_merchant_service(&mut conn);
let merchant_service: MerchantService = container::get_merchant_service(&mut conn);
merchant_service.get_merchant(&search.unwrap());
}
Commands::Schema { search } => {
schema_service::get_columns(&search.unwrap(), &mut conn)
},
Commands::Filter { filter_id, config, all } => {
let schema_service: SchemaService = container::get_schema_service(&mut conn);
schema_service.get_columns(&search.unwrap())
}
Commands::Filter {
filter_id,
config,
all,
} => {
let mut filter_service: FilterService = container::get_filter_service(&mut conn);
if config {
filter_service.get_filter_configs(&filter_id)
} else {
filter_service.get_filter(&filter_id, all)
}
},
}
}
Ok(())

View File

@ -2,21 +2,31 @@ use mysql::{params, prelude::Queryable, PooledConn};
use crate::entity::schema::Schema;
pub fn find_by_column(
column_name: &str,
conn: &mut PooledConn,
) -> Result<Vec<Schema>, mysql::Error> {
let stat = conn
pub struct SchemaRepo<'a> {
db_pool: &'a mut PooledConn,
}
impl<'a> SchemaRepo<'a> {
pub fn new(db_pool: &'a mut PooledConn) -> Self {
Self { db_pool }
}
pub fn find_by_column(
self,
column_name: &str,
) -> 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")
.unwrap();
conn.exec_map(
stat,
params! {"c_name" => column_name},
|(table_name, column_name, column_type)| Schema {
table_name,
column_name,
column_type,
},
)
self.db_pool.exec_map(
stat,
params! {"c_name" => column_name},
|(table_name, column_name, column_type)| Schema {
table_name,
column_name,
column_type,
},
)
}
}

View File

@ -20,7 +20,7 @@ impl<'a> MerchantService<'a> {
merchants.into_iter().for_each(|merchant| {
println!("{}", "-".repeat(150));
println!("Merchant: {}", merchant.m_name);
println!("M-ID: {}", merchant.m_id);
println!("M-ID: {}", merchant.m_id);
});
println!("{}", "-".repeat(150));

View File

@ -1,24 +1,31 @@
use mysql::PooledConn;
use crate::repository::schema_repo::SchemaRepo;
use crate::repository::schema_repo;
pub struct SchemaService<'a> {
repo: SchemaRepo<'a>,
}
pub fn get_columns(column: &str, conn: &mut PooledConn) {
let columns = schema_repo::find_by_column(column, conn);
impl<'a> SchemaService<'a> {
pub fn new(repo: SchemaRepo<'a>) -> Self {
Self { repo }
}
pub fn get_columns(self, column: &str) {
let columns = self.repo.find_by_column(column);
match columns {
Ok(columns) => {
if columns.is_empty() {
println!("No column found!");
} else {
columns.into_iter().for_each(|column| {
match columns {
Ok(columns) => {
if columns.is_empty() {
println!("No column found!");
} else {
columns.into_iter().for_each(|column| {
println!("{}", "-".repeat(100));
println!("Table name: {}", column.table_name);
println!("Column name: {}", column.column_name);
println!("Type: {}", column.column_type);
});
println!("{}", "-".repeat(100));
println!("Table name: {}", column.table_name);
println!("Column name: {}", column.column_name);
println!("Type: {}", column.column_type);
});
println!("{}", "-".repeat(100));
}
}
Err(err) => panic!("{err}"),
}
Err(err) => panic!("{err}"),
}
}