use sqlx::mysql::MySqlPoolOptions;
use sqlx::Row;
use tiny_orm_macro_derive::{TinyOrm, TinyOrmQuery};
use crate::prelude::*;
use super::*;
#[derive(Debug, PartialEq, Eq)]
pub struct UserType {
pub id: Option<u32>,
pub name: String,
pub template: String,
}
impl UserType {
pub fn new(id: u32, name: &str, template: &str) -> Self {
UserType {
id: Some(id),
name: name.into(),
template: template.into(),
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub struct Organization {
pub id: String,
pub name: String,
}
#[allow(dead_code)]
#[derive(TinyOrm, TinyOrmQuery, Debug, PartialEq, Eq)]
#[orm_table_name = "user"]
#[orm_query(
name = "name_and_tel",
sql_where = "user.name = ? and mobile_phone = ?",
args = "name:&str,mobile_phone:&str",
doc = "根据姓名和手机号查询用户"
)]
#[orm_delete(
name = "name_and_tel",
sql_where = "user.name = ? and mobile_phone = ?",
args = "name:&str,mobile_phone:&str",
doc = "根据姓名和手机号删除用户"
)]
#[orm_exist(
name = "name",
sql_where = "user.name like ?",
args = "name:&str",
doc = "根据姓名查询用户是否存在"
)]
#[orm_update(
name = "name_and_tel",
sql_set = "name = ? , mobile_phone = ?",
sql_where = "id = ?",
args = "name:&str,mobile_phone:&str,id:u32",
doc = "根据id更新姓名和手机号"
)]
pub struct TestUser {
#[orm_pk(name = "id", auto = "true")]
pub id: Option<u32>,
#[orm_query]
pub name: String,
#[orm_pk]
pub mobile_phone: String,
#[orm_field(name = "password")]
#[orm_update]
password: String,
#[orm_join(
name="user_type_id", // 重命名表字段名称
select_field="user_type.name as user_type_name, user_type.template",
join="JOIN user_type ON user_type.id = user_type_id",
link_id="id",//update 时保存值使用的字段id,如:user_type.id
link_id_type="u32"
)]
#[allow(unused_variables)]
pub user_type: UserType,
#[orm_join(
name="org_id", // 重命名表字段名称
select_field="organization.name as org_name",
join="JOIN organization ON organization.id = org_id",
link_id="id",//update 时保存值使用的字段id,如:org.id
link_id_type="&str"
)]
pub org: Organization,
#[orm_ignore]
pub ignore_field: u32,
}
impl TestUser {
pub fn new(
id: u32,
name: &str,
mobile_phone: &str,
password: &str,
user_type: UserType,
org: Organization,
) -> Self {
Self {
id: Some(id),
name: name.into(),
mobile_phone: mobile_phone.into(),
password: password.into(),
user_type,
org,
ignore_field: 0,
}
}
pub fn new_no_id(
name: &str,
mobile_phone: &str,
password: &str,
user_type: UserType,
org: Organization,
) -> Self {
Self {
id: None,
name: name.into(),
mobile_phone: mobile_phone.into(),
password: password.into(),
user_type,
org,
ignore_field: 0,
}
}
}
impl TinyOrmData for TestUser {
fn orm_row_map(row: TinyOrmSqlRow) -> Self {
TestUser::new(
row.get::<u32, _>("id"),
row.get("name"),
row.get("mobile_phone"),
row.get("password"),
UserType::new(
row.get::<u32, _>("user_type_id"),
row.get("user_type_name"),
row.get("template"),
),
Organization {
id: row.get("org_id"),
name: row.get("org_name"),
},
)
}
}
async fn get_pool() -> AnyhowResult<TinyOrmDbPool> {
let user_name = "net_guard";
let password = "net_guard@20220806";
let ip = "localhost";
let port = 3306;
let db_name = "abc_net_guard";
let pool = MySqlPoolOptions::new()
.max_connections(1)
.connect(&format!(
"mysql://{}:{}@{}:{}/{}",
user_name, password, ip, port, db_name
))
.await?;
Ok(pool)
}
#[test]
fn test_user() {
println!("user sql : \n{}", TestUser::DB_META.select_sql);
}
#[test]
fn test_db_query() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let data = TestUser::orm_get_all(&pool).await.unwrap();
dbg!(data);
});
}
#[test]
fn test_orm_query_with_name_and_mobile() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let user = TestUser::orm_query_with_name_and_tel(&pool, "张三", "1850703xxxx")
.await
.unwrap();
dbg!(user);
});
}
#[test]
fn test_orm_get_with_pk() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let user = TestUser::orm_get_with_pk(&pool, 2, "13870381703")
.await
.unwrap();
dbg!(user);
});
}
#[test]
fn test_orm_delete() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let user = TestUser::orm_get_with_pk(&pool, 15, "18679301268")
.await
.unwrap();
user.orm_delete(&pool).await.unwrap();
dbg!(user);
});
}
#[test]
fn test_orm_delete_with_name_and_tel() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
TestUser::orm_delete_with_name_and_tel(&pool, "盛巧岚", "15070310588")
.await
.unwrap();
});
}
#[test]
fn test_orm_query_with_name() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let user = TestUser::orm_query_with_name(&pool, "张三").await.unwrap();
dbg!(user);
});
}
#[test]
fn test_orm_exist_with_pk() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let is_exist = TestUser::orm_exist_with_pk(&pool, 8, "18607037962")
.await
.unwrap();
dbg!(is_exist);
});
}
#[test]
fn test_orm_exist_with_name() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let is_exist = TestUser::orm_exist_with_name(&pool, "王佳慧")
.await
.unwrap();
dbg!(is_exist);
});
}
#[test]
fn test_orm_update_with_name_and_tel() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let user = TestUser::orm_update_with_name_and_tel(&pool, "张三", "18507032200", 4)
.await
.unwrap();
dbg!(user);
});
}
#[test]
fn test_db_filter() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let sql = TestUser::DB_META.build_select_sql("user.name like ? ");
println!("{sql}");
let key = String::from("%李%");
let data = TestUser::orm_filter_with_sql(&pool, &sql, &key)
.await
.unwrap();
dbg!(data);
});
}
#[test]
fn test_orm_query_join_with_user_type() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let user_type = UserType::new(1, "支行", "");
let mut data = TestUser::orm_query_join_with_user_type(&pool, user_type)
.await
.unwrap();
let mut ygl = data.pop().unwrap();
let user_type_2 = UserType::new(2, "", "");
ygl.user_type = user_type_2;
ygl.orm_update_join_with_user_type(&pool).await.unwrap();
dbg!(ygl);
});
}
#[test]
fn test_orm_query_join_with_org_id() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let data = TestUser::orm_query_join_with_org_id(&pool, "14H700")
.await
.unwrap();
dbg!(data);
});
}
#[test]
fn test_orm_delete_join_with_user_type() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let user_type = UserType::new(2, "支行", "");
TestUser::orm_delete_join_with_user_type(&pool, user_type)
.await
.unwrap();
});
}
#[test]
fn test_orm_update_all() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let mut user = TestUser::orm_get_with_pk(&pool, 2, "13870381703")
.await
.unwrap();
let user_type = UserType::new(2, "支行", "");
user.name = "王佳慧2".into();
user.user_type = user_type;
user.orm_update_all(&pool).await.unwrap();
dbg!(user);
});
}
#[test]
fn test_orm_insert() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let org = Organization {
id: "14H700".into(),
name: "上饶分行".into(),
};
let user_type = UserType::new(2, "管理员", "");
let mut user = TestUser::new_no_id("常建", "13707932010", "sss", user_type, org);
user.orm_insert(&pool).await.unwrap();
dbg!(user);
});
}
#[test]
fn test_orm_update_password() {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = get_pool().await.unwrap();
let mut user:TestUser = TestUser::orm_query_with_name(&pool, "张三").await.unwrap().pop().unwrap();
user.password="this is a pass".into();
user.orm_update_password(&pool).await.unwrap();
dbg!(user);
});
}