#profiles #有关mysql的集成简化工具 #内容为:

yanked db_utils

有关mysql的集成简化工具

0.1.5 Mar 3, 2023
0.1.4 Mar 3, 2023

#22 in #profiles

23 downloads per month

GPL-3.0 license

15KB

db_utils

主要是为mysql做的一些简化curd操作,集成了许多工具的功能

db的连接在对应配置里面

需要在项目路径下添加配置 application.toml

内容:

[profiles]
db = "test" // 使用哪个db文件配置 test为db_test.toml

static_resource = "./" // 静态文件路径  可忽略

limit = 100 // 默认分页数量 (没有用)可忽略

application.toml为第一层的配置文件

dev为生产环境文件,test为测试环境文件 里面集成了db连接方式 需要自己添加

内容为:

## 数据库测试分支

## mysql数据库
[mysql]
## mysql数据库地址
ip = "127.0.0.1"
## 数据库端口
port = 3306
## 数据库账号
username = "root"
## 数据库密码
password = "yannuo"
## 数据库库名
db_name = "test"

[redis]
## redis地址
ip = "127.0.0.1"
## 端口
port = 6379
## token超时时间
token_timeout = 86400
## 普通超时时间
timeout = 86400

最重要的是macro_builder中的过程宏,在quote!块里面修改或新增功能,并且在CommonDbImpl trait模块里面添加对应函数

例子:

   use macro_builder::CommonDbMacro;
   use serde_derive::{Serialize, Deserialize};

   #[derive(Debug, Serialize, Deserialize, CommonDbMacro)]
   pub struct User {
       pub id: u32,
       pub name: String,
       pub address: String,
       pub phone: u32
   }

   impl User {
       pub fn new(id: u32, name: String, address: String, phone: u32) -> Self {
           Self {id, name, address, phone,}
       }
   }

   pub trait DbImpl {
       fn get(table_name: &str) -> usize {
           let mut conn = db_tool::get_db_conn();
           conn.query_first(format!("select count(*) from {}", table_name)).unwrap().unwrap()
       }
   }

   impl DbImpl for User {

   }

   #[test]
   fn test() {
       // 获取表数据的数量
       let number = User::get_count("table");
       println!("number: {}", number);
       let number2 = User::get("table");
       println!("number2: {}", number2);
       // 获取表数据
       let user_list = User::batch_select("table", 1, 100);
       for item in user_list {
           println!("user: {:?}", item);
       }
       let user = User::new(1, "tester".to_owned(), "earth".to_owned(), 10086);
       // 插入
       user.insert("table");
       // 更新
       user.update("table");
       // 删除
       User::delete("table", 1);
   }

Dependencies

~22–37MB
~597K SLoC