1use crate::action::Action;
2use crate::PLUGIN_TOOLS;
3use crate::{Tools, GLOBAL_DATA};
4#[cfg(any(
5 feature = "mysql",
6 feature = "sqlite",
7 feature = "mssql",
8 feature = "pgsql"
9))]
10use br_db::Db;
11use json::{array, object, JsonValue};
12use std::any::type_name;
13
14pub trait Module: Send + Sync + 'static {
16 fn _name(&self) -> &'static str {
20 let name = type_name::<Self>()
21 .split("::")
22 .nth(3)
23 .unwrap_or_default()
24 .to_lowercase();
25 Box::leak(name.into_boxed_str())
26 }
27 fn module_name(&self) -> &'static str {
31 let t = type_name::<Self>().split("::").collect::<Vec<&str>>();
32 let name = if t.len() > 3 {
33 format!("{}.{}", t[2], t[3])
34 } else {
35 String::new()
36 };
37 Box::leak(name.into_boxed_str())
38 }
39 fn title(&self) -> &'static str;
41 fn description(&self) -> &'static str {
43 ""
44 }
45 fn tags(&self) -> &'static [&'static str] {
47 &["admin", "org"]
48 }
49 fn icon(&self) -> &'static str {
52 ""
53 }
54 fn table(&self) -> bool {
57 false
58 }
59 fn _table_name(&self) -> &'static str {
61 let mut it = type_name::<Self>().rsplitn(4, "::");
62 let _ = it.next().unwrap_or(""); let t2 = it.next().unwrap_or("");
64 let t3 = it.next().unwrap_or("");
65 let s = format!("{t3}_{t2}").to_lowercase();
66 Box::leak(s.into_boxed_str())
67 }
68 fn table_key(&self) -> &'static str {
70 "id"
71 }
72 fn table_unique(&self) -> &'static [&'static str] {
74 &[]
75 }
76 fn table_index(&self) -> &'static [&'static [&'static str]] {
78 &[]
79 }
80 fn table_partition(&self) -> bool {
82 false
83 }
84 fn table_partition_columns(&self) -> JsonValue {
90 array![]
91 }
92 fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
96 fn fields(&mut self) -> JsonValue {
98 object! {}
99 }
100 fn init_data(&mut self) -> JsonValue {
102 array![]
103 }
104 fn tools(&mut self) -> Tools {
106 let tools = PLUGIN_TOOLS.lock().expect("PLUGIN_TOOLS lock failed");
107 tools.get("tools").expect("tools not initialized").clone()
108 }
109 fn set_global_data(&mut self, key: &str, value: JsonValue) {
111 GLOBAL_DATA.with(|data| {
112 data.borrow_mut()[key] = value;
113 });
114 }
115 fn get_global_data(&mut self) -> JsonValue {
117 GLOBAL_DATA.with(|data| data.borrow().clone())
118 }
119 fn get_global_data_key(&mut self, key: &str) -> JsonValue {
121 GLOBAL_DATA.with(|data| data.borrow()[key].clone())
122 }
123 #[cfg(any(
124 feature = "mysql",
125 feature = "sqlite",
126 feature = "mssql",
127 feature = "pgsql"
128 ))]
129 fn db_table(&mut self) -> Db {
130 let table = self._table_name();
131 self.tools().db.table(table).clone()
132 }
133 #[cfg(any(
134 feature = "mysql",
135 feature = "sqlite",
136 feature = "mssql",
137 feature = "pgsql"
138 ))]
139 fn db_find(&mut self, id: &str) -> JsonValue {
140 self.db_table().where_and("id", "=", id.into()).find()
141 }
142 #[cfg(any(
143 feature = "mysql",
144 feature = "sqlite",
145 feature = "mssql",
146 feature = "pgsql"
147 ))]
148 fn db_select(&mut self, ids: &str) -> JsonValue {
149 self.db_table().where_and("id", "in", ids.into()).select()
150 }
151 #[cfg(any(
152 feature = "mysql",
153 feature = "sqlite",
154 feature = "mssql",
155 feature = "pgsql"
156 ))]
157 fn db_insert(&mut self, data: JsonValue) -> String {
158 let res = self.db_table().insert(data);
159 res.to_string()
160 }
161 #[cfg(any(
162 feature = "mysql",
163 feature = "sqlite",
164 feature = "mssql",
165 feature = "pgsql"
166 ))]
167 fn db_delete(&mut self, id: &str) -> usize {
168 let delete = self.db_table().where_and("id", "=", id.into()).delete();
169 delete.as_usize().unwrap_or(0)
170 }
171 #[cfg(any(
172 feature = "mysql",
173 feature = "sqlite",
174 feature = "mssql",
175 feature = "pgsql"
176 ))]
177 fn db_update(&mut self, id: &str, data: JsonValue) -> usize {
178 let update = self.db_table().where_and("id", "=", id.into()).update(data);
179 update.as_usize().unwrap_or(0)
180 }
181 fn handle(&mut self) {}
183}