daoyi_cloud_common/db/
mod.rs

1use std::sync::OnceLock;
2
3use rbatis::RBatis;
4
5use crate::config::DbConfig;
6
7pub static RBATIS_ENGINE: OnceLock<RBatis> = OnceLock::new();
8
9pub async fn init(config: &DbConfig) {
10    let rb = RBatis::new();
11    rb.init(rbdc_mysql::driver::MysqlDriver {}, &config.url)
12        .unwrap();
13    let sql_file = "./data/init.sql";
14
15    if sql_file != "" {
16        let sql = std::fs::read_to_string(sql_file).unwrap();
17        let _ = rb.exec(&sql, vec![]).await;
18    }
19    RBATIS_ENGINE.set(rb).expect("rbatis should be set");
20}
21
22pub fn engine() -> &'static RBatis {
23    RBATIS_ENGINE.get().expect("rbatis should be initialized")
24}