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 {
11
12 fn _name(&self) -> &'static str {
15 type_name::<Self>().split("::").collect::<Vec<&str>>()[3].to_lowercase().leak()
16 }
17 fn title(&self) -> &'static str;
19 fn description(&self) -> &'static str {
21 ""
22 }
23 fn icon(&self) -> &'static str {
26 ""
27 }
28 fn table(&self) -> bool {
31 false
32 }
33 fn _table_name(&self) -> &'static str {
35 let mut it = type_name::<Self>().rsplitn(4, "::");
36 let _ = it.next().unwrap_or(""); let t2 = it.next().unwrap_or("");
38 let t3 = it.next().unwrap_or("");
39 let s = format!("{t3}_{t2}").to_lowercase();
40 Box::leak(s.into_boxed_str())
41 }
42 fn table_key(&self) -> &'static str {
44 "id"
45 }
46 fn table_unique(&self) -> &'static [&'static str] {
48 &[]
49 }
50 fn table_index(&self) -> &'static [&'static [&'static str]] {
52 &[]
53 }
54 fn table_partition(&self) -> bool {
56 false
57 }
58 fn table_partition_columns(&self) -> JsonValue {
64 array![]
65 }
66 fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
70 fn fields(&mut self) -> JsonValue {
72 object! {}
73 }
74 fn tools(&mut self) -> Tools {
76 let tools = PLUGIN_TOOLS.lock().unwrap();
77 let tools = tools.get("tools").unwrap().clone();
78 tools
79 }
80 fn set_global_data(&mut self, key: &str, value: JsonValue) {
82 GLOBAL_DATA.with(|data| {
83 data.borrow_mut()[key] = value;
84 });
85 }
86 fn get_global_data(&mut self) -> JsonValue {
88 GLOBAL_DATA.with(|data| {
89 data.borrow().clone()
90 })
91 }
92 fn get_global_data_key(&mut self, key: &str) -> JsonValue {
94 GLOBAL_DATA.with(|data| {
95 data.borrow()[key].clone()
96 })
97 }
98 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
99 fn db_table(&mut self) -> Db {
100 let table = self._table_name();
101 self.tools().db.table(table).clone()
102 }
103 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
104 fn db_find(&mut self, id: &str) -> JsonValue {
105 self.db_table().where_and("id", "=", id.into()).find()
106 }
107 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
108 fn db_select(&mut self, ids: &str) -> JsonValue {
109 self.db_table().where_and("id", "in", ids.into()).select()
110 }
111 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
112 fn db_insert(&mut self, data: JsonValue) -> String {
113 let res = self.db_table().insert(data);
114 res.to_string()
115 }
116 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
117 fn db_delete(&mut self, id: &str) -> usize {
118 let delete = self.db_table().where_and("id", "=", id.into()).delete();
119 delete.as_usize().unwrap_or(0)
120 }
121 #[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
122 fn db_update(&mut self, id: &str, data: JsonValue) -> usize {
123 let update = self.db_table().where_and("id", "=", id.into()).update(data);
124 update.as_usize().unwrap_or(0)
125 }
126}