br_addon/
module.rs

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
9/// 模型配置
10pub trait Module: Send + Sync + 'static {
11    /// =================基础配置=============================
12    /// 插件名称
13    fn _name(&self) -> &'static str {
14        type_name::<Self>().split("::").collect::<Vec<&str>>()[3].to_lowercase().leak()
15    }
16    /// 模型名称
17    /// xxx.xxx
18    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    /// 模型标题
23    fn title(&self) -> &'static str;
24    /// 模型描述
25    fn description(&self) -> &'static str {
26        ""
27    }
28    /// =================视图配置=============================
29    /// 模型icon
30    fn icon(&self) -> &'static str {
31        ""
32    }
33    /// =================数据库配置=============================
34    /// 是否数据表
35    fn table(&self) -> bool {
36        false
37    }
38    /// 数据库表名称
39    fn _table_name(&self) -> &'static str {
40        let mut it = type_name::<Self>().rsplitn(4, "::");
41        let _ = it.next().unwrap_or(""); // 最末段
42        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    /// 主键
48    fn table_key(&self) -> &'static str {
49        "id"
50    }
51    /// 唯一索引
52    fn table_unique(&self) -> &'static [&'static str] {
53        &[]
54    }
55    /// 查询索引
56    fn table_index(&self) -> &'static [&'static [&'static str]] {
57        &[]
58    }
59    /// 是否分区
60    fn table_partition(&self) -> bool {
61        false
62    }
63    /// 分区配置
64    /// *列名
65    /// *分区key
66    /// *实例 array![str, array![range1, range2, range3...]]
67    /// *如果在已有分区的情况下想要修改分区的参照列,先要将当前分区代码删除后刷库,然后写新的分区规则再刷库
68    fn table_partition_columns(&self) -> JsonValue {
69        array![]
70    }
71    /// =================功能配置=============================
72    /// 加载功能
73    /// * name 功能名称
74    fn action(&mut self, name: &str) -> Result<Box<dyn Action>, String>;
75    /// 模型字段
76    fn fields(&mut self) -> JsonValue {
77        object! {}
78    }
79    /// 使用工具
80    fn tools(&mut self) -> Tools {
81        let tools = PLUGIN_TOOLS.lock().unwrap();
82        let tools = tools.get("tools").unwrap().clone();
83        tools
84    }
85    /// 设置全局变量
86    fn set_global_data(&mut self, key: &str, value: JsonValue) {
87        GLOBAL_DATA.with(|data| {
88            data.borrow_mut()[key] = value;
89        });
90    }
91    /// 获取全局变量数据
92    fn get_global_data(&mut self) -> JsonValue {
93        GLOBAL_DATA.with(|data| {
94            data.borrow().clone()
95        })
96    }
97    /// 获取全局变量指定字段数据
98    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    /// 监听
132    fn handle(&mut self) {}
133}