pub trait FromQueryResult: Sized {
// Provided methods
fn from_value(_value: &Value) -> Result<Self, String> { ... }
fn from_row(row: &HashMap<String, Value>) -> Result<Self, String> { ... }
fn from_query_result(_row: &HashMap<String, Value>) -> Result<Self, String> { ... }
fn row_desc() -> Vec<&'static str> { ... }
fn column_types() -> &'static [(&'static str, &'static str)] { ... }
}Expand description
从 Value 反序列化查询结果行的字段值。
由 #[derive(FromQueryResult)] 自动为结构体生成实现,
也可手动为自定义类型实现。
§示例
ⓘ
use sz_orm_core::value::{Value, FromQueryResult};
#[derive(FromQueryResult)]
struct User {
id: i64,
name: String,
}
let row = vec![
("id".to_string(), Value::from(1i64)),
("name".to_string(), Value::from("Alice")),
];
let user = User::from_row(&row).unwrap();
assert_eq!(user.id, 1);Provided Methods§
Sourcefn from_value(_value: &Value) -> Result<Self, String>
fn from_value(_value: &Value) -> Result<Self, String>
从单个 Value 提取字段值
对结构体类型无意义(结构体由多列组成,不能从单个 Value 构造), 因此提供默认实现返回错误。仅基础标量类型(i64, String, bool 等) 需要真正重写此方法。
Sourcefn from_query_result(_row: &HashMap<String, Value>) -> Result<Self, String>
fn from_query_result(_row: &HashMap<String, Value>) -> Result<Self, String>
从一行数据构建自身(主入口,由 #[derive(FromQueryResult)] 自动生成)
Sourcefn row_desc() -> Vec<&'static str>
fn row_desc() -> Vec<&'static str>
返回该类型期望的列名列表(由 #[derive(FromQueryResult)] 自动生成)。
用于 query_as! 宏在 db-verify 模式下做编译期列名交叉验证:
确保 SQL 的 SELECT 列全部出现在结构体字段中。
Sourcefn column_types() -> &'static [(&'static str, &'static str)]
fn column_types() -> &'static [(&'static str, &'static str)]
返回该类型期望的列名 + SQL 类型列表(由 #[derive(FromQueryResult)] 自动生成)。
用于 query_as! 宏在 db-verify 模式下做编译期列类型匹配验证:
确保 SQL 的 SELECT 列类型与结构体字段类型兼容。
返回格式:&[(&'static str, &'static str)],例如 [("id", "bigint"), ("name", "varchar")]。
SQL 类型名使用 INFORMATION_SCHEMA.DATA_TYPE(MySQL)或 udt_name(PostgreSQL)
的规范化小写形式。
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".