Skip to main content

FromQueryResult

Trait FromQueryResult 

Source
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§

Source

fn from_value(_value: &Value) -> Result<Self, String>

从单个 Value 提取字段值

对结构体类型无意义(结构体由多列组成,不能从单个 Value 构造), 因此提供默认实现返回错误。仅基础标量类型(i64, String, bool 等) 需要真正重写此方法。

Source

fn from_row(row: &HashMap<String, Value>) -> Result<Self, String>

从一行数据(列名→值的 HashMap 表示)构建自身

Source

fn from_query_result(_row: &HashMap<String, Value>) -> Result<Self, String>

从一行数据构建自身(主入口,由 #[derive(FromQueryResult)] 自动生成)

Source

fn row_desc() -> Vec<&'static str>

返回该类型期望的列名列表(由 #[derive(FromQueryResult)] 自动生成)。

用于 query_as! 宏在 db-verify 模式下做编译期列名交叉验证: 确保 SQL 的 SELECT 列全部出现在结构体字段中。

Source

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".

Implementations on Foreign Types§

Source§

impl FromQueryResult for ()

Source§

fn from_value(_value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for String

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for bool

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for f32

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for f64

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for i8

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for i16

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for i32

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for i64

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for u8

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for u16

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for u32

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl FromQueryResult for u64

Source§

fn from_value(value: &Value) -> Result<Self, String>

Source§

impl<T: FromQueryResult> FromQueryResult for Option<T>

Source§

fn from_value(value: &Value) -> Result<Self, String>

Implementors§