1use std::collections::HashMap;
2use std::thread;
3use json::{JsonValue};
4use crate::config::Config;
5use crate::connect::Connect;
6
7mod comm;
8mod packet;
9mod config;
10mod server;
11mod character_set;
12mod client;
13mod response;
14pub mod connect;
15
16#[derive(Clone, Debug)]
17pub struct Mysql {
18 pub config: Config,
20 pub pool: HashMap<String, Connect>,
21}
22
23impl Mysql {
24 pub fn new(config: JsonValue) -> Result<Self, String> {
25 let config = match Config::new(config) {
27 Ok(e) => e,
28 Err(e) => return Err(format!("配置文件错误: {e}")),
29 };
30 Ok(Self { config, pool: HashMap::new() })
31 }
32 pub fn get_conn(&mut self, thread_id: &str) -> Result<&mut Connect, String> {
34 let thread_id = if thread_id.is_empty() {
35 format!("{:?}", thread::current().id())
36 } else {
37 thread_id.to_string()
38 };
39 if self.pool.get_mut(&*thread_id).is_none() {
40 let conn = match Connect::connect(self.config.clone()) {
41 Ok(e) => e,
42 Err(e) => return Err(e)
43 };
44 self.pool.insert(thread_id.clone(), conn);
45 }
46 let connect = self.pool.get_mut(&*thread_id).unwrap();
47 Ok(connect)
48 }
49}