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 icon(&self) -> &'static str {
31 ""
32 }
33 fn table(&self) -> bool {
36 false
37 }
38 fn _table_name(&self) -> &'static str {
40 let mut it = type_name::<Self>().rsplitn(4, "::");
41 let _ = it.next().unwrap_or(""); let t2 = it.next().unwrap_or("");
43 let t3 = it.next().unwrap_or("");
44 let s = format!("{t3}_{t2}").to_lowercase();
45 Box::leak(s.into_boxed_str())
46 }
47 fn table_key(&self) -> &'static str {
49 "id"
50 }
51 fn table_unique(&self) -> &'static [&'static str] {
53 &[]
54 }
55 fn table_index(&self) -> &'static [&'static [&'static str]] {
57 &[]
58 }
59 fn table_partition(&self) -> bool {
61 false
62 }
63 fn table_partition_columns(&self) -> JsonValue {
69 array![]
70 }
71 fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
75 fn fields(&mut self) -> JsonValue {
77 object! {}
78 }
79 fn tools(&mut self) -> Tools {
81 let tools = PLUGIN_TOOLS.lock().unwrap();
82 let tools = tools.get("tools").unwrap().clone();
83 tools
84 }
85 fn set_global_data(&mut self, key: &str, value: JsonValue) {
87 GLOBAL_DATA.with(|data| {
88 data.borrow_mut()[key] = value;
89 });
90 }
91 fn get_global_data(&mut self) -> JsonValue {
93 GLOBAL_DATA.with(|data| {
94 data.borrow().clone()
95 })
96 }
97 fn get_global_data_key(&mut self, key: &str) -> JsonValue {
99 GLOBAL_DATA.with(|data| {
100 data.borrow()[key].clone()
101 })
102 }
103 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
104 fn db_table(&mut self) -> Db {
105 let table = self._table_name();
106 self.tools().db.table(table).clone()
107 }
108 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
109 fn db_find(&mut self, id: &str) -> JsonValue {
110 self.db_table().where_and("id", "=", id.into()).find()
111 }
112 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
113 fn db_select(&mut self, ids: &str) -> JsonValue {
114 self.db_table().where_and("id", "in", ids.into()).select()
115 }
116 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
117 fn db_insert(&mut self, data: JsonValue) -> String {
118 let res = self.db_table().insert(data);
119 res.to_string()
120 }
121 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
122 fn db_delete(&mut self, id: &str) -> usize {
123 let delete = self.db_table().where_and("id", "=", id.into()).delete();
124 delete.as_usize().unwrap_or(0)
125 }
126 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
127 fn db_update(&mut self, id: &str, data: JsonValue) -> usize {
128 let update = self.db_table().where_and("id", "=", id.into()).update(data);
129 update.as_usize().unwrap_or(0)
130 }
131 fn handle(&mut self) {}
133}