Skip to main content

JsonQuery

Struct JsonQuery 

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

JSON 字段查询构造器

提供 think-orm 风格的链式 JSON 字段查询 API,支持 MySQL/PostgreSQL/SQLite 三种方言。

Implementations§

Source§

impl JsonQuery

Source

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

创建 JSON 查询构造器

  • db_type:目标数据库类型
  • column:JSON 列名
Source

pub fn path(self, path: impl Into<String>) -> Self

指定 JSON 路径(如 themea.b.c

Source

pub fn build_extract(&self) -> String

构建取字段表达式(左侧值,不含操作符与右侧值)

  • MySQL: col->'$.field'
  • PostgreSQL: col->>'field'
  • SQLite: json_extract(col, '$.field')

当路径为空时(未调用 path() 或传入空字符串),直接引用列自身, 避免生成 '$.'(MySQL 非法路径)或 '' 等非法语法。

Source

pub fn eq_string(self, value: &str) -> String

= 字符串值

Source

pub fn eq_i64(self, value: i64) -> String

= 整数值

Source

pub fn eq_f64(self, value: f64) -> String

= 浮点值

Source

pub fn ne_string(self, value: &str) -> String

!= 字符串值

Source

pub fn gt_string(self, value: &str) -> String

> 字符串值

Source

pub fn lt_string(self, value: &str) -> String

< 字符串值

Source

pub fn ge_string(self, value: &str) -> String

>= 字符串值

Source

pub fn le_string(self, value: &str) -> String

<= 字符串值

Source

pub fn ge_i64(self, value: i64) -> String

>= 整数值

Source

pub fn le_i64(self, value: i64) -> String

<= 整数值

Source

pub fn gt_i64(self, value: i64) -> String

> 整数值

Source

pub fn lt_i64(self, value: i64) -> String

< 整数值

Source

pub fn between_i64(self, low: i64, high: i64) -> String

BETWEEN 整数范围(包含两端)

Source

pub fn in_strs(self, values: &[&str]) -> String

IN (字符串列表)

Source

pub fn in_i64s(self, values: &[i64]) -> String

IN (整数列表)

Source

pub fn like(self, value: &str) -> String

LIKE 字符串值

Source

pub fn is_null(self) -> String

IS NULL

Source

pub fn is_not_null(self) -> String

IS NOT NULL

Source

pub fn has_key(self) -> String

键存在性检查(路径下有键)

  • MySQL: JSON_CONTAINS_PATH(col, 'one', '$.path')
  • PostgreSQL: col ? 'path'(顶层键)/ col #? '{path}'(路径)
  • SQLite: json_type(col, '$.path') IS NOT NULL
Source

pub fn json_type_eq(self, expected_type: &str) -> String

JSON 类型检查(判断 JSON 值类型)

  • MySQL: JSON_TYPE(col->'$.path') = 'INTEGER'
  • PostgreSQL: json_typeof(col#>>'{path}') = 'integer'
  • SQLite: json_type(col, '$.path') = 'integer'

expected_type 应为小写(‘integer’/‘string’/‘boolean’/‘array’/‘object’/‘null’)。 在 MySQL 中会被自动转为大写。

Source

pub fn contains(self, value: &str) -> String

数组包含某元素(JSON_CONTAINS / @> / json_extract LIKE)

  • MySQL: JSON_CONTAINS(col, '"v"', '$.path')
  • PostgreSQL: col @> '{"path":"v"}'(简化:用 path 拼接)
  • SQLite: EXISTS (SELECT 1 FROM json_each(json_extract(col, '$.path')) WHERE value = 'v')
Source

pub fn array_length_eq(self, length: i64) -> String

数组长度比较

  • MySQL: JSON_LENGTH(col->'$.path') = N
  • PG: jsonb_array_length(col#>>'{a,b}') = N
  • SQLite: json_array_length(json_extract(col, '$.path')) = N
Source

pub fn column(&self) -> &str

返回列名(不带方言处理)

Source

pub fn db_type(&self) -> DbType

返回数据库类型

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