#[non_exhaustive]pub enum Value {
}Expand description
数据库值类型
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
Null
Null 值
Bool(bool)
布尔值
I8(i8)
8 位有符号整数
I16(i16)
16 位有符号整数
I32(i32)
32 位有符号整数
I64(i64)
64 位有符号整数
U8(u8)
8 位无符号整数
U16(u16)
16 位无符号整数
U32(u32)
32 位无符号整数
U64(u64)
64 位无符号整数
F32(f32)
32 位浮点数
F64(f64)
64 位浮点数
String(String)
字符串值
Bytes(Vec<u8>)
字节值
Uuid(String)
UUID 值(以字符串形式存储)
Date(String)
日期值(ISO 8601 格式)
DateTime(String)
日期时间值(ISO 8601 格式)
Time(String)
时间值
Json(String)
JSON 值
Array(Vec<Value>)
值数组
Object(HashMap<String, Value>)
基于 HashMap 的对象值,用于存储关系数据
Implementations§
Source§impl Value
impl Value
Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
若可能,返回 i64 形式的值
支持 F32/F64 → i64 的有损转换(数据库 SUM/AVG 等聚合函数常返回浮点类型)
U64 → i64 使用 try_from,超过 i64::MAX 时返回 None(避免静默截断为负数)
Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
若可能,返回 bool 形式的值 支持整数(非 0 即真)、浮点(非 0.0 即真)、字符串(“1”/“true”/“yes”/“on” 为真)的转换
Sourcepub fn to_param(&self) -> Cow<'_, str>
pub fn to_param(&self) -> Cow<'_, str>
转换为 SQL 参数字符串(用于直接拼接 SQL 语句) 字符串类型会进行转义并加引号;字节类型转换为 X’..’ 形式
§安全性警告
本方法使用简单的 ' → '' 转义,对 PostgreSQL/SQLite 默认配置安全,
但对 MySQL 默认配置(backslash 是转义字符)不安全:含 \ 的字符串
可能被 MySQL 误解。生产环境请使用 Value::to_param_with_dialect
以获得方言感知的转义。
Sourcepub fn to_param_with_dialect(&self, dialect: &dyn Dialect) -> Cow<'_, str>
pub fn to_param_with_dialect(&self, dialect: &dyn Dialect) -> Cow<'_, str>
v0.2.2 修复 H-1:方言感知的 SQL 参数转换
与 to_param 的区别:字符串类型使用 dialect.escape_string()
而非简单的 ' → '' 转义,确保在所有方言下都安全:
- MySQL:转义
\、'、\0、\n、\r、\t、\x1a - PostgreSQL:仅转义
'(依赖standard_conforming_strings=on默认配置) - SQLite:仅转义
'
§推荐用法
ⓘ
use sz_orm_core::{DbType, get_dialect};
let dialect = get_dialect(DbType::MySQL)?;
let v = Value::String("hello\\nworld".to_string());
let param = v.to_param_with_dialect(&**dialect);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Value
impl<'de> Deserialize<'de> for Value
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Queryable for Value
单列查询结果(如 SELECT COUNT(*))
impl Queryable for Value
单列查询结果(如 SELECT COUNT(*))
Source§fn from_values(values: Vec<Value>) -> Result<Self, QueryError>
fn from_values(values: Vec<Value>) -> Result<Self, QueryError>
从按 SELECT 顺序排列的值列表构造实例
Source§fn from_values_with_desc(
values: Vec<Value>,
desc: &RowDesc,
) -> Result<Self, QueryError>
fn from_values_with_desc( values: Vec<Value>, desc: &RowDesc, ) -> Result<Self, QueryError>
从带行描述的值列表构造(默认实现忽略描述)
impl StructuralPartialEq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnsafeUnpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more