use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread;
use json::{JsonValue};
use crate::config::Config;
use crate::connect::Connect;
mod comm;
mod packet;
mod config;
mod server;
mod character_set;
mod client;
mod response;
pub mod connect;
#[derive(Clone, Debug)]
pub struct Mysql {
pub config: Config,
pub pool: HashMap<String, Connect>,
}
impl Mysql {
pub fn new(config: JsonValue) -> Result<Self, String> {
let config = match Config::new(config) {
Ok(e) => e,
Err(e) => return Err(format!("配置文件错误: {e}")),
};
Ok(Self { config, pool: HashMap::new() })
}
pub fn get_conn(&mut self, thread_id: &str) -> Result<&mut Connect, String> {
let thread_id = if thread_id.is_empty() {
format!("{:?}", thread::current().id())
} else {
thread_id.to_string()
};
if self.pool.get_mut(&*thread_id).is_none() {
let conn = match Connect::connect(self.config.clone()) {
Ok(e) => e,
Err(e) => return Err(e)
};
self.pool.insert(thread_id.clone(), conn);
}
let connect = self.pool.get_mut(&*thread_id).unwrap();
Ok(connect)
}
}