1 个不稳定版本
0.3.1 | 2022年12月18日 |
---|
#163 在 #sqlx
在 sqlx-meta 中使用
10KB
206 行
Sqlx Meta
作为 sqlx-crud 的精简版本派生出的库,提供标记实体的元数据,设计用于仅提供构建块以供您构建自己的查询。
入门
添加到依赖项
sqlx-meta = "0.1.0"
并用 SqlxMeta
标记您的实体
#[derive(Default, Debug, Clone, sqlx::FromRow, SqlxMeta)]
pub struct File {
pub id: Option<i32>,
pub entry_time: String,
pub abs_path: String,
pub path: String,
pub ext: Option<String>,
pub mode: Option<String>,
// ..
}
构建自己的查询(当它实际上不改变时,首选 lazy_static
)
lazy_static! {
static ref INSERT_SQL: String = {
let cols = &File::columns()[1..];
let holders = (0..cols.len()).map(|_| "?").collect::<Vec<_>>().join(", ");
let excludes = cols
.iter()
.map(|c| format!("'{}'=excluded.'{}'", c, c))
.collect::<Vec<_>>()
.join(",");
// ..
// ..
使用 update_binds
或 insert_binds
方法以节省打字
use sqlx_meta::{Binds, Schema};
f.update_binds(q).fetch_optional(&mut conn).await?;
依赖项
~1.5MB
~38K SLoC