1use crate::action::Action;
2use crate::{Tools, GLOBAL_DATA};
3use crate::PLUGIN_TOOLS;
4use json::{array, object, JsonValue};
5use std::any::type_name;
6#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
7use br_db::Db;
8
9pub trait Module: Send + Sync + 'static {
11 fn _name(&self) -> &'static str {
14 type_name::<Self>().split("::").collect::<Vec<&str>>()[3].to_lowercase().leak()
15 }
16 fn module_name(&self) -> &'static str {
19 let t = type_name::<Self>().split("::").collect::<Vec<&str>>();
20 format!("{}.{}", t[2], t[3]).leak()
21 }
22 fn title(&self) -> &'static str;
24 fn description(&self) -> &'static str {
26 ""
27 }
28 fn tags(&self) -> &'static [&'static str] {
30 &["admin", "org"]
31 }
32 fn icon(&self) -> &'static str {
35 ""
36 }
37 fn table(&self) -> bool {
40 false
41 }
42 fn _table_name(&self) -> &'static str {
44 let mut it = type_name::<Self>().rsplitn(4, "::");
45 let _ = it.next().unwrap_or(""); let t2 = it.next().unwrap_or("");
47 let t3 = it.next().unwrap_or("");
48 let s = format!("{t3}_{t2}").to_lowercase();
49 Box::leak(s.into_boxed_str())
50 }
51 fn table_key(&self) -> &'static str {
53 "id"
54 }
55 fn table_unique(&self) -> &'static [&'static str] {
57 &[]
58 }
59 fn table_index(&self) -> &'static [&'static [&'static str]] {
61 &[]
62 }
63 fn table_partition(&self) -> bool {
65 false
66 }
67 fn table_partition_columns(&self) -> JsonValue {
73 array![]
74 }
75 fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
79 fn fields(&mut self) -> JsonValue {
81 object! {}
82 }
83 fn init_data(&mut self) -> JsonValue {
85 array![]
86 }
87 fn tools(&mut self) -> Tools {
89 let tools = PLUGIN_TOOLS.lock().unwrap();
90 let tools = tools.get("tools").unwrap().clone();
91 tools
92 }
93 fn set_global_data(&mut self, key: &str, value: JsonValue) {
95 GLOBAL_DATA.with(|data| {
96 data.borrow_mut()[key] = value;
97 });
98 }
99 fn get_global_data(&mut self) -> JsonValue {
101 GLOBAL_DATA.with(|data| {
102 data.borrow().clone()
103 })
104 }
105 fn get_global_data_key(&mut self, key: &str) -> JsonValue {
107 GLOBAL_DATA.with(|data| {
108 data.borrow()[key].clone()
109 })
110 }
111 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
112 fn db_table(&mut self) -> Db {
113 let table = self._table_name();
114 self.tools().db.table(table).clone()
115 }
116 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
117 fn db_find(&mut self, id: &str) -> JsonValue {
118 self.db_table().where_and("id", "=", id.into()).find()
119 }
120 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
121 fn db_select(&mut self, ids: &str) -> JsonValue {
122 self.db_table().where_and("id", "in", ids.into()).select()
123 }
124 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
125 fn db_insert(&mut self, data: JsonValue) -> String {
126 let res = self.db_table().insert(data);
127 res.to_string()
128 }
129 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
130 fn db_delete(&mut self, id: &str) -> usize {
131 let delete = self.db_table().where_and("id", "=", id.into()).delete();
132 delete.as_usize().unwrap_or(0)
133 }
134 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
135 fn db_update(&mut self, id: &str, data: JsonValue) -> usize {
136 let update = self.db_table().where_and("id", "=", id.into()).update(data);
137 update.as_usize().unwrap_or(0)
138 }
139 fn handle(&mut self) {}
141}