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