Define database driver struct on project config file

Welcome to the rbatis first wiki!
today we see some java's database config
look like this:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://192.168.85.111:3306/demo?serverTimezone=UTC&useUnicode=true@characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver

oh,We can see that Java can specify the path of the class, which makes it very convenient to replace the database data source and is also very conducive to CI construction. So how can we implement it in Rust?

see abs_admin
use RBatis-ORM

  • 0 create your config file application.json5
{
  //choose database
  //"sqlite://target/sqlite.db"
  //"mysql://root:123456@localhost:3306/test"
  //"postgres://postgres:123456@localhost:5432/postgres"
  //"mssql://SA:TestPass!123456@localhost:1433/test"
  database_url: "sqlite://target/sqlite.db",
  //choose driver struct(Cargo.toml must add like 'rbdc-*** = { version = "4.4" }')
  //"rbdc_sqlite::Driver{}"
  //"rbdc_mysql::Driver{}"
  //"rbdc_mssql::Driver{}"
  //"rbdc_pg::Driver{}"
  database_struct: "rbdc_sqlite::Driver{}",
}
  • 1 add [build-dependencies] (Cargo.toml)
[dependencies]
rbs = { version = "4.4" }
rbatis = { version = "4.4", features = [] }
rbdc-sqlite = { version = "4.4" }

[build-dependencies]
json5 = "0.4"
serde = { version = "1.0", features = ["derive"] }
  • 2 define your build.rs and read config file(this is application.json5)
use std::fs::File;
use std::io::Write;

#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ApplicationConfig{
   pub database_struct: String,
}
fn main(){
   println!("start build");
   let js_data = include_str!("application.json5");
   let result: ApplicationConfig = json5::from_str(js_data).expect("load config file fail");
   println!("result={:?}",result);
   let mut f=File::create("target/driver.rs").unwrap();
   f.write_all(result.database_struct.as_ref()).unwrap();
   f.flush().unwrap();
   drop(f);
}
  • 3 use include! import code
 //
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ApplicationConfig{
   pub database_struct: String,
}
 let driver = include!("../../target/driver.rs");
 let rb = RBatis::new();
 let js_data = include_str!("application.json5");
 let config: ApplicationConfig = json5::from_str(js_data).expect("load config file fail");
 rb.init(driver, &config.database_url).expect("[abs_admin] rbatis pool init fail!");
 rb.acquire().await.expect("rbatis connect database fail");

To create a configuration file (application.json5) for your project using b2b data enrichment tools, follow these steps:

  1. Create Config File (application.json5):
{
  "database_url": "sqlite://target/sqlite.db",
  "database_struct": "rbdc_sqlite::Driver{}",
  "enrichment_tool": "b2b data enrichment tools"
}

Define build.rs to Read Config File:

use std::fs::File;
use std::io::Write;

#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub struct ApplicationConfig {
    pub database_struct: String,
    pub enrichment_tool: String,
}

fn main() {
    println!("start build");
    let js_data = include_str!("application.json5");
    let result: ApplicationConfig = json5::from_str(js_data).expect("load config file fail");
    println!("result={:?}", result);
    let mut f = File::create("target/driver.rs").unwrap();
    f.write_all(result.database_struct.as_ref()).unwrap();
    f.flush().unwrap();
    drop(f);
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.