rnacos/config/
config_db.rs1use crate::common::AppSysConfig;
2use chrono::Local;
3use rusqlite::Connection;
4use std::rc::Rc;
5
6use super::{
7 core::{ConfigKey, ConfigValue},
8 dal::{
9 ConfigDO, ConfigDao, ConfigHistoryDO, ConfigHistoryDao, ConfigHistoryParam, ConfigParam,
10 },
11};
12
13pub struct ConfigDB {
14 config_dao: ConfigDao,
15 config_history_dao: ConfigHistoryDao,
16}
17
18impl Default for ConfigDB {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl ConfigDB {
25 pub fn new() -> Self {
26 let sys_config = AppSysConfig::init_from_env();
27 let config_db = sys_config.config_db_file;
28 let conn = Connection::open(config_db).unwrap();
29 Self::init(&conn);
30 let conn = Rc::new(conn);
31 let config_dao = ConfigDao::new(conn.clone());
32 let config_history_dao = ConfigHistoryDao::new(conn);
33 Self {
34 config_dao,
35 config_history_dao,
36 }
37 }
38
39 fn init(conn: &Connection) {
40 let create_table_sql = r"
41create table if not exists tb_config(
42 id integer primary key autoincrement,
43 data_id varchar(255),
44 `group` varchar(255),
45 tenant varchar(255),
46 content text,
47 content_md5 varchar(36),
48 last_time long
49);
50create index if not exists tb_config_key_idx on tb_config(data_id,`group`,tenant);
51
52create table if not exists tb_config_history(
53 id integer primary key autoincrement,
54 data_id varchar(255),
55 `group` varchar(255),
56 tenant varchar(255),
57 content text,
58 last_time long
59);
60create index if not exists tb_config_history_key_idx on tb_config_history(data_id,`group`,tenant);
61 ";
62 conn.execute_batch(create_table_sql).unwrap();
63 }
64
65 fn convert_to_config_do(key: &ConfigKey, val: &ConfigValue) -> ConfigDO {
66 let current_time = Local::now().timestamp_millis();
67 ConfigDO {
68 tenant: Some(key.tenant.as_ref().to_owned()),
69 group: Some(key.group.as_ref().to_owned()),
70 data_id: Some(key.data_id.as_ref().to_owned()),
71 content: Some(val.content.as_ref().to_owned()),
72 content_md5: Some(val.md5.as_ref().to_owned()),
73 last_time: Some(current_time),
74 ..Default::default()
75 }
76 }
77
78 fn convert_to_config_history_do(key: &ConfigKey, val: &ConfigValue) -> ConfigHistoryDO {
79 let current_time = Local::now().timestamp_millis();
80 ConfigHistoryDO {
81 tenant: Some(key.tenant.as_ref().to_owned()),
82 group: Some(key.group.as_ref().to_owned()),
83 data_id: Some(key.data_id.as_ref().to_owned()),
84 content: Some(val.content.as_ref().to_owned()),
85 last_time: Some(current_time),
86 ..Default::default()
87 }
88 }
89
90 fn convert_to_config_param(key: &ConfigKey) -> ConfigParam {
91 ConfigParam {
92 tenant: Some(key.tenant.as_ref().to_owned()),
93 group: Some(key.group.as_ref().to_owned()),
94 data_id: Some(key.data_id.as_ref().to_owned()),
95 ..Default::default()
96 }
97 }
98
99 fn convert_to_config_history_param(key: &ConfigKey) -> ConfigHistoryParam {
100 ConfigHistoryParam {
101 tenant: Some(key.tenant.as_ref().to_owned()),
102 group: Some(key.group.as_ref().to_owned()),
103 data_id: Some(key.data_id.as_ref().to_owned()),
104 ..Default::default()
105 }
106 }
107
108 pub fn update_config(&self, key: &ConfigKey, val: &ConfigValue) {
109 let config = Self::convert_to_config_do(key, val);
110 let config_history = Self::convert_to_config_history_do(key, val);
111 let config_param = Self::convert_to_config_param(key);
112 let is_update = match self.config_dao.update(&config, &config_param) {
113 Ok(size) => size > 0,
114 _ => false,
115 };
116 if !is_update {
117 self.config_dao.insert(&config).unwrap();
118 }
119 self.config_history_dao.insert(&config_history).unwrap();
120 }
121
122 pub fn del_config(&self, key: &ConfigKey) {
123 let config_param = Self::convert_to_config_param(key);
124 let config_history_param = Self::convert_to_config_history_param(key);
125 self.config_dao.delete(&config_param).unwrap();
126 self.config_history_dao
127 .delete(&config_history_param)
128 .unwrap();
129 }
130
131 pub fn query_config_list(&self) -> Vec<ConfigDO> {
132 let param = ConfigParam::default();
133 self.config_dao.query(¶m).unwrap()
134 }
135
136 pub fn query_config_history_page(
137 &self,
138 param: &ConfigHistoryParam,
139 ) -> (usize, Vec<ConfigHistoryDO>) {
140 let size = self
141 .config_history_dao
142 .query_count(param)
143 .unwrap_or_default();
144 let list = self.config_history_dao.query(param).unwrap_or_default();
145 (size as usize, list)
146 }
147}