Skip to main content

JsonUpdate

Struct JsonUpdate 

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

JSON 字段更新构造器

提供 think-orm 风格的 JSON 字段 SET 子句构造。

  • MySQL: JSON_SET(col, '$.key', 'value')
  • PG: jsonb_set(col, '{key}', '"value"')
  • SQLite: json_set(col, '$.key', 'value')

Implementations§

Source§

impl JsonUpdate

Source

pub fn new(db_type: DbType, column: impl Into<String>) -> JsonUpdate

创建 JSON 更新构造器

Source

pub fn set_str(self, key: impl Into<String>, value: &str) -> JsonUpdate

添加一个 SET 项(key → value,value 为字符串)

Source

pub fn set_i64(self, key: impl Into<String>, value: i64) -> JsonUpdate

添加一个 SET 项(key → value,value 为 i64)

Source

pub fn set_bool(self, key: impl Into<String>, value: bool) -> JsonUpdate

添加一个 SET 项(key → value,value 为 bool)

Source

pub fn array_append_str(self, key: impl Into<String>, value: &str) -> JsonUpdate

数组追加元素(字符串):将 value 追加到 col.path 指向的数组末尾

  • MySQL: col = JSON_ARRAY_APPEND(col, '$.path', 'v')
  • PG: col = jsonb_set(col, '{path}', (col#>'{path}') || to_jsonb('v'::text))
  • SQLite: col = json_set(col, '$.path', json_insert(col->'$.path', '$[#]', 'v'))

注:追加多个元素请多次调用。

Source

pub fn array_append_i64(self, key: impl Into<String>, value: i64) -> JsonUpdate

数组追加元素(整数)

Source

pub fn remove_key(self, key: impl Into<String>) -> JsonUpdate

删除指定 JSON 路径的字段

  • MySQL: col = JSON_REMOVE(col, '$.key')
  • PG: col = col - 'key'
  • SQLite: col = json_remove(col, '$.key')
Source

pub fn build_set(&self) -> String

构建 SET 子句片段(不含 SET 关键字)

返回可直接拼到 UPDATE ... SET <此处> 的字符串。 若同时存在 set / array_append / remove 操作,将按 SET → APPEND → REMOVE 顺序合并到同一列。

Auto Trait Implementations§

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> 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> 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, 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