Skip to main content

Record

Struct Record 

Source
pub struct Record { /* private fields */ }
Expand description

动态数据库记录

以键值对形式存储一行数据库查询结果,支持任意表结构。 字段名为键,字段值为 JSON 值。

§字段

  • columns:列名到 JSON 值的映射

§示例

ⓘ
use yang_base::table::Record;
use serde_json::json;

let row = Record::new()
    .set("id", json!(1))
    .set("name", json!("Alice"));

Implementations§

Source§

impl Record

Source

pub fn new() -> Self

创建空记录。

Source

pub fn is_sql_null(&self, key: &str) -> bool

该列是否为 SQL NULL(区别于 JSON 字面量 null)。

列不存在时返回 false。

Source

pub fn is_json_null(&self, key: &str) -> bool

该列是否为 PostgreSQL json/jsonb 的 JSON 字面量 null。

Source

pub fn set(self, key: impl Into<String>, value: impl Into<Value>) -> Self

链式写入字段值。

Source

pub fn insert( &mut self, key: impl Into<String>, value: impl Into<Value>, ) -> Option<Value>

写入字段值并返回被替换的旧值。

Source

pub fn remove_column(&mut self, key: &str) -> Option<Value>

移除指定列并返回其原值。

同时从 JSON null sidecar 中移除该列,避免列已不存在却仍被 Record::is_json_null 判定为 JSON null。

§返回
  • Some(Value):列存在时返回被移除的值
  • None:列不存在
Source

pub fn retain_columns(&mut self, keys: &[&str])

只保留指定列,其余列及其 JSON null 登记一并移除。

用于多列投影:投影结果必须保留 Record::is_json_null 的区分能力, 不能经 into_map() + Record::from 重建(那会丢失 sidecar)。

Source

pub fn get(&self, key: &str) -> Option<&Value>

获取指定列的原始 JSON 值。

§参数
  • key:列名
§返回
  • Some(&Value):列存在时返回值的引用
  • None:列不存在
Source

pub fn require<T>(&self, key: &str) -> Result<T, BaseError>

读取必需字段并转换为目标类型。

Source

pub fn optional<T>(&self, key: &str) -> Result<Option<T>, BaseError>

读取可选字段;字段不存在或为 null 时返回 None。

Source

pub fn as_map(&self) -> &Map<String, Value>

返回字段映射的只读引用。

Source

pub fn into_map(self) -> Map<String, Value>

消费记录并返回字段映射。

Trait Implementations§

Source§

impl Clone for Record

Source§

fn clone(&self) -> Self

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 Record

Source§

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

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

impl Default for Record

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Record

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 From<Map<String, Value>> for Record

Source§

fn from(columns: Map<String, Value>) -> Self

Converts to this type from the input type.
Source§

impl From<Record> for Value

将 Record 转换为 serde_json::Value(Object 类型)。

Source§

fn from(row: Record) -> Self

Converts to this type from the input type.
Source§

impl<'r> FromRow<'r, MySqlRow> for Record

Available on crate feature mysql only.

实现 sqlx::FromRow,支持从 MySQL 查询结果动态解码

按 MySQL 列类型将值映射到对应的 JSON 类型:

  • INT/BIGINT/MEDIUMINT/SMALLINT → i64
  • TINYINT → i8
  • TINYINT UNSIGNED → u8
  • FLOAT/DOUBLE → f64
  • DECIMAL/NUMERIC → String(保留精度)
  • VARCHAR/TEXT/CHAR → String
  • BOOLEAN(即 TINYINT(1))→ Bool
  • DATE/DATETIME/TIMESTAMP → ISO 8601 字符串
  • NULL → Null
  • BLOB/BINARY → Base64 字符串
  • JSON → Object/Array
Source§

fn from_row(row: &'r MySqlRow) -> Result<Self, Error>

Source§

impl<'r> FromRow<'r, PgRow> for Record

Available on crate feature postgres only.

实现 sqlx::FromRow,支持从 PostgreSQL 查询结果动态解码。

与 MySQL 实现并存,按 feature 分别编译。类型分派基于 OID / PgTypeKind, 不依赖可能被用户自定义类型复用的类型名。

Source§

fn from_row(row: &'r PgRow) -> Result<Self, Error>

Source§

impl JsonSchema for Record

Source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
Source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

impl PartialEq for Record

Source§

fn eq(&self, other: &Self) -> 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 Record

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 Record

Auto Trait Implementations§

§

impl Forget for Record

§

impl Freeze for Record

§

impl Move for Record

§

impl RefUnwindSafe for Record

§

impl Send for Record

§

impl Sync for Record

§

impl Unpin for Record

§

impl UnsafeUnpin for Record

§

impl UnwindSafe for Record

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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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