一个基于sqlx的极简orm,本项目将sql代码以derive属性方式和结构体进行关联,并自动生成相关CRUD相关API,可根据需要实现任意复杂度的数据获取。
本项目开发主要原因:
因此,是否可以将数据查询的sql和rust结合,由程序员自行控制sql的表现,这样在rust的高性能助力下,我们可以写成性能超级赞的数据库应用。
根据你的数据库类型选择不同特性,缺省为mysql
ignore
/// 根据姓名和手机号查询用户
pub async fn orm_query_by_name_and_tel(pool: &TinyOrmDbPool, name:&str,mobile_phone:&str) ->Result<Vec<Self>> {
let sql = Self::DB_META.build_select_sql("name = ? and mobile_phone = ?");
let query = Self::db_query(&sql)
.bind(name)
.bind(mobile_phone);
Self::db_fetch_all(pool,query,Self::orm_row_map).await
}
修改Cargo.toml,增加
``` [dependencies] async-trait = "^0.1" tinyormmacro_derive="^0.2"
[dependencies.tinyormcore] version = "^0.2" features = [ "mysql", ]
```
``` //! 数据库模型的测试 use sqlx::mysql::MySqlPoolOptions; use sqlx::Row; use tinyormcore::prelude::*;
use super::*;
pub struct UserType {
/// 类型编号
pub id: Option
/// 自动生成ormqueryby查询方法,生成后的函数定义如下
/// /// 根据姓名和手机号查询用户
/// pub async fn ormquerybynameandtel(pool: &TinyOrmDbPool, name:&str,mobilephone:&str) ->Result
name = "name_and_tel",
sql_where = "user.name = ? and mobile_phone = ?",
args = "name:&str,mobile_phone:&str",
doc = "根据姓名和手机号查询用户"
)]
name = "name",
sql_where = "user.name = ?",
args = "name:&str",
doc = "根据姓名查询用户"
)]
pub struct TestUser {
/// 类型编号
pub id: Option
impl TestUser {
/// 完整创建器
///
/// # 参数说明
///
/// * id 编号
/// * name 姓名
/// * mobilephone 手机
/// * password 密码
/// * usertype 用户类型
/// * org 对应机构
pub fn new(
id: u32,
name: &str,
mobilephone: &str,
password: &str,
usertype: UserType,
) -> Self {
Self {
id: Some(id),
name: name.into(),
mobilephone: mobilephone.into(),
password: password.into(),
usertype,
}
}
}
/// 实现数据获取接口
impl TinyOrmData for TestUser {
/// 将sql返回数据映射为TestUser
fn ormrowmap(row: TinyOrmSqlRow) -> Self {
TestUser::new(
row.get::
async fn getpool() -> Result
/// .测试SQL生成
fn testuser() { println!("user sql : \n{}", TestUser::DBMETA.select_sql); }
/// .测试SQL生成
fn testdbquery() { tokio::runtime::Builder::newcurrentthread() .enableall() .build() .unwrap() .blockon(async { let pool = getpool().await.unwrap(); let data = TestUser::ormget_all(&pool).await.unwrap(); dbg!(data); }); }
/// 测试根据id获取用户
fn testormgetbyid() { tokio::runtime::Builder::newcurrentthread() .enableall() .build() .unwrap() .blockon(async { let pool = getpool().await.unwrap(); let user = TestUser::ormgetbyid(&pool, 1).await.unwrap(); dbg!(user); }); } /// 测试根据姓名和手机获取用户
fn testormquerybynameandmobile() { tokio::runtime::Builder::newcurrentthread() .enableall() .build() .unwrap() .blockon(async { let pool = getpool().await.unwrap(); let user = TestUser::ormquerybynameandtel(&pool, "张三", "1850703xxxx") .await .unwrap(); dbg!(user); }); }
/// 测试根据姓名和手机获取用户
fn testormquerybyname() { tokio::runtime::Builder::newcurrentthread() .enableall() .build() .unwrap() .blockon(async { let pool = getpool().await.unwrap(); let user = TestUser::ormquerybyname(&pool, "张三") .await .unwrap(); dbg!(user); }); } /// 测试是否存在
fn testormexists() { tokio::runtime::Builder::newcurrentthread() .enableall() .build() .unwrap() .blockon(async { let pool = getpool().await.unwrap(); let isexist = TestUser::ormexists(&pool, 1).await.unwrap(); dbg!(isexist); }); }
/// 测试是否存在
fn testormdelete() { tokio::runtime::Builder::newcurrentthread() .enableall() .build() .unwrap() .blockon(async { let pool = getpool().await.unwrap(); TestUser::ormdelete(&pool, 6).await.unwrap(); }); }
/// 测试filter
fn testdbfilter() { tokio::runtime::Builder::newcurrentthread() .enableall() .build() .unwrap() .blockon(async { let pool = getpool().await.unwrap(); let sql = TestUser::DBMETA.buildselectsql("name like ? "); println!("{sql}"); let key = String::from("%黄%"); let data = TestUser::ormfilterwith_sql(&pool, &sql, &key) .await .unwrap(); dbg!(data); }); }
```