Skip to main content

Value

Enum Value 

Source
#[non_exhaustive]
pub enum Value {
Show 22 variants Null, Bool(bool), I8(i8), I16(i16), I32(i32), I64(i64), U8(u8), U16(u16), U32(u32), U64(u64), F32(f32), F64(f64), Decimal(String), String(String), Bytes(Vec<u8>), Uuid(String), Date(String), DateTime(String), Time(String), Json(String), Array(Vec<Value>), Object(HashMap<String, 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 位浮点数

§

Decimal(String)

高精度十进制数(NUMERIC/DECIMAL),以字符串形式存储避免 f64 精度丢失

§

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

Source

pub fn is_null(&self) -> bool

判断是否为 null

Source

pub fn is_bool(&self) -> bool

判断是否为布尔值

Source

pub fn is_i64(&self) -> bool

判断是否为整数

Source

pub fn is_f64(&self) -> bool

判断是否为浮点数

Source

pub fn is_string(&self) -> bool

判断是否为字符串

Source

pub fn is_bytes(&self) -> bool

判断是否为字节

Source

pub fn is_object(&self) -> bool

判断是否为对象

Source

pub fn from_map(map: HashMap<String, Value>) -> Self

从 HashMap 构造 Value

Source

pub fn as_str(&self) -> Option<&str>

若可能,返回 &str 形式的值

Source

pub fn as_i64(&self) -> Option<i64>

若可能,返回 i64 形式的值 支持 F32/F64 → i64 的有损转换(数据库 SUM/AVG 等聚合函数常返回浮点类型) U64 → i64 使用 try_from,超过 i64::MAX 时返回 None(避免静默截断为负数)

Source

pub fn as_f64(&self) -> Option<f64>

若可能,返回 f64 形式的值 支持整数类型 → f64 的转换

Source

pub fn as_bool(&self) -> Option<bool>

若可能,返回 bool 形式的值 支持整数(非 0 即真)、浮点(非 0.0 即真)、字符串(“1”/“true”/“yes”/“on” 为真)的转换

Source

pub fn as_bytes(&self) -> Option<&[u8]>

若可能,返回字节切片形式(&u8)的值 字符串类型会返回其 UTF-8 字节

Source

pub fn to_param(&self) -> Cow<'_, str>

转换为 SQL 参数字符串(用于直接拼接 SQL 语句) 字符串类型会进行转义并加引号;字节类型转换为 X’..’ 形式

§安全性警告

本方法使用简单的 ''' 转义,对 PostgreSQL/SQLite 默认配置安全, 但对 MySQL 默认配置(backslash 是转义字符)不安全:含 \ 的字符串 可能被 MySQL 误解。生产环境请使用 Value::to_param_with_dialect 以获得方言感知的转义。

Source

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_model::{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);
Source

pub fn from<T: Into<Value>>(v: T) -> Self

从任何实现了 Into<Value> 的类型构造 Value

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Value

Source§

fn default() -> Value

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Value

Source§

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 Display for Value

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&[u8]> for Value

Source§

fn from(v: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for Value

Source§

fn from(v: &str) -> Self

Converts to this type from the input type.
Source§

impl From<()> for Value

Source§

fn from(_: ()) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(v: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<Value>> for Value

Source§

fn from(v: Vec<Value>) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Value

Source§

fn from(v: Vec<u8>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(v: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(v: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(v: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(v: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(v: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(v: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(v: i64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(v: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(v: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(v: u64) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Value) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Value

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more