6 个版本 (破坏性更新)
0.6.0 | 2023 年 12 月 14 日 |
---|---|
0.5.0 | 2023 年 10 月 2 日 |
0.4.0 | 2023 年 10 月 2 日 |
0.3.0 | 2023 年 10 月 2 日 |
0.1.0 | 2023 年 10 月 1 日 |
#4 在 #tiberius
每月 78 次下载
用于 eventful-sql-server
5KB
tiberius-mappers
为 Tiberius SQL Server 驱动程序提供行映射器。
- 允许您将 tiberius 行映射到结构体
- 为
tiberius::Row
定义了一个TryFromRow
trait - 支持通过 tiberius-mappers-derive crate 推导
TryFromRow
trait - 要求 SQL 查询中的列顺序与结构体字段顺序相同
- 处理映射到结构体中 Option 字段的空值
- 目前通过名称在 FromRowBorrowed 中映射,通过索引在 FromRowOwned 中映射
现有的 tiberius-derive crate 目前提供了更多映射选项,但似乎未得到维护,并且不适用于 Tiberius 的新版本。我一直在维护此 crate 的分支,以支持内部构建中的 Tiberius 的新版本,但我希望从头开始使用更简单的实现。请注意,此实现基于原始的 tiberius-derive crate,因此感谢原始作者的想法和一些代码。
用法
这是一个正在进行中的项目。目前,已实现了 TryFromRow
映射器。
use tiberius_mappers::TryFromRow;
#[derive(TryFromRow)] // Derive the FromRow trait on our struct
pub struct Customer {
pub id: i32,
pub first_name: String,
pub last_name: String,
pub description: Option<String>,
}
pub async fn print_customers(rows: Vec<tiberius::Row>) -> Result<(), Box<dyn std::error::Error>> {
// Now we can call the try_from_row method on each row to get a Customer struct
let customers: Vec<Customer> = rows
.into_iter()
.map(Customer::try_from_row).collect::<Result<Vec<Customer>, _>>()?;
for customer in customers {
println!("Customer: {} - {:?} - {:?}", customer.id, customer.first_name, customer.last_name);
}
Ok(())
}
待办事项
- 添加更多测试(进程宏不容易测试!)
- 添加一个选项,以安全地验证返回的查询结果集中的行名称是否与结构体字段匹配
- 改进错误消息
- 可能支持重命名字段(也许,不确定这是否是一个好主意)。这将需要与上述提到的行名称验证选项交互。
依赖关系
~5.5MB
~98K SLoC