use crate::action::Action;
use crate::{Tools, GLOBAL_DATA};
use crate::PLUGIN_TOOLS;
use json::{array, object, JsonValue};
use std::any::type_name;
#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
use br_db::Db;
pub trait Module: Send + Sync + 'static {
fn _name(&self) -> &'static str {
type_name::<Self>().split("::").collect::<Vec<&str>>()[3].to_lowercase().leak()
}
fn module_name(&self) -> &'static str {
let t = type_name::<Self>().split("::").collect::<Vec<&str>>();
format!("{}.{}", t[2], t[3]).leak()
}
fn title(&self) -> &'static str;
fn description(&self) -> &'static str {
""
}
fn tags(&self) -> &'static [&'static str] {
&["admin", "org"]
}
fn icon(&self) -> &'static str {
""
}
fn table(&self) -> bool {
false
}
fn _table_name(&self) -> &'static str {
let mut it = type_name::<Self>().rsplitn(4, "::");
let _ = it.next().unwrap_or(""); let t2 = it.next().unwrap_or("");
let t3 = it.next().unwrap_or("");
let s = format!("{t3}_{t2}").to_lowercase();
Box::leak(s.into_boxed_str())
}
fn table_key(&self) -> &'static str {
"id"
}
fn table_unique(&self) -> &'static [&'static str] {
&[]
}
fn table_index(&self) -> &'static [&'static [&'static str]] {
&[]
}
fn table_partition(&self) -> bool {
false
}
fn table_partition_columns(&self) -> JsonValue {
array![]
}
fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
fn fields(&mut self) -> JsonValue {
object! {}
}
fn init_data(&mut self) -> JsonValue {
array![]
}
fn tools(&mut self) -> Tools {
let tools = PLUGIN_TOOLS.lock().unwrap();
let tools = tools.get("tools").unwrap().clone();
tools
}
fn set_global_data(&mut self, key: &str, value: JsonValue) {
GLOBAL_DATA.with(|data| {
data.borrow_mut()[key] = value;
});
}
fn get_global_data(&mut self) -> JsonValue {
GLOBAL_DATA.with(|data| {
data.borrow().clone()
})
}
fn get_global_data_key(&mut self, key: &str) -> JsonValue {
GLOBAL_DATA.with(|data| {
data.borrow()[key].clone()
})
}
#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
fn db_table(&mut self) -> Db {
let table = self._table_name();
self.tools().db.table(table).clone()
}
#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
fn db_find(&mut self, id: &str) -> JsonValue {
self.db_table().where_and("id", "=", id.into()).find()
}
#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
fn db_select(&mut self, ids: &str) -> JsonValue {
self.db_table().where_and("id", "in", ids.into()).select()
}
#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
fn db_insert(&mut self, data: JsonValue) -> String {
let res = self.db_table().insert(data);
res.to_string()
}
#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
fn db_delete(&mut self, id: &str) -> usize {
let delete = self.db_table().where_and("id", "=", id.into()).delete();
delete.as_usize().unwrap_or(0)
}
#[cfg(any(feature = "mysql", feature = "sqlite", feature = "mssql", feature = "pgsql"))]
fn db_update(&mut self, id: &str, data: JsonValue) -> usize {
let update = self.db_table().where_and("id", "=", id.into()).update(data);
update.as_usize().unwrap_or(0)
}
fn handle(&mut self) {}
}