QueryBuilder

Struct QueryBuilder 

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

安全的查询构建器,使用绑定参数而非字符串拼接

Implementations§

Source§

impl QueryBuilder

Source

pub fn new(base_sql: impl Into<String>) -> Self

Source

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

替换基础 SQL(保留已有的条件、排序和绑定)

Source

pub fn limit(self, n: u64) -> Self

设置 LIMIT(链式调用),仅作用于 into_sql 生成的 SQL

Source

pub fn offset(self, n: u64) -> Self

设置 OFFSET(链式调用),仅作用于 into_sql 生成的 SQL

Source

pub fn group_by(self, field: &str) -> Self

添加 GROUP BY 字段(链式调用,可多次调用添加多个字段)

Source

pub fn having_eq(self, field: &str, value: impl Into<BindValue>) -> Self

HAVING 条件:等于

Source

pub fn having_ne(self, field: &str, value: impl Into<BindValue>) -> Self

HAVING 条件:不等于

Source

pub fn having_gt(self, field: &str, value: impl Into<BindValue>) -> Self

HAVING 条件:大于

Source

pub fn having_ge(self, field: &str, value: impl Into<BindValue>) -> Self

HAVING 条件:大于等于

Source

pub fn having_lt(self, field: &str, value: impl Into<BindValue>) -> Self

HAVING 条件:小于

Source

pub fn having_le(self, field: &str, value: impl Into<BindValue>) -> Self

HAVING 条件:小于等于

Source

pub fn and_eq(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn and_ne(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn and_gt(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn and_ge(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn and_lt(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn and_le(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn or_eq(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn or_ne(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn or_gt(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn or_ge(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn or_lt(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn or_le(self, field: &str, value: impl Into<BindValue>) -> Self

Source

pub fn and_like(self, field: &str, value: impl Into<String>) -> Self

Source

pub fn and_like_prefix(self, field: &str, value: impl Into<String>) -> Self

LIKE 前缀匹配(value%)

Source

pub fn and_like_suffix(self, field: &str, value: impl Into<String>) -> Self

LIKE 后缀匹配(%value)

Source

pub fn and_like_exact(self, field: &str, value: impl Into<String>) -> Self

LIKE 精确匹配(不添加 %)

Source

pub fn and_like_custom(self, field: &str, pattern: impl Into<String>) -> Self

LIKE 自定义模式匹配

Source

pub fn or_like(self, field: &str, value: impl Into<String>) -> Self

Source

pub fn and_regexp(self, field: &str, pattern: impl Into<String>) -> Self

正则表达式匹配(AND 条件) MySQL: field REGEXP pattern PostgreSQL: field ~ pattern SQLite: 不支持原生正则表达式(会抛出错误)

Source

pub fn or_regexp(self, field: &str, pattern: impl Into<String>) -> Self

正则表达式匹配(OR 条件) MySQL: field REGEXP pattern PostgreSQL: field ~ pattern SQLite: 不支持原生正则表达式(会抛出错误)

Source

pub fn and_in(self, field: &str, values: Vec<impl Into<BindValue>>) -> Self

Source

pub fn and_not_in(self, field: &str, values: Vec<impl Into<BindValue>>) -> Self

NOT IN 查询

Source

pub fn or_in(self, field: &str, values: Vec<impl Into<BindValue>>) -> Self

Source

pub fn and_is_null(self, field: &str) -> Self

IS NULL 查询

Source

pub fn and_is_not_null(self, field: &str) -> Self

IS NOT NULL 查询

Source

pub fn or_is_null(self, field: &str) -> Self

Source

pub fn or_is_not_null(self, field: &str) -> Self

Source

pub fn and_between( self, field: &str, min: impl Into<BindValue>, max: impl Into<BindValue>, ) -> Self

BETWEEN 范围查询

Source

pub fn or_between( self, field: &str, min: impl Into<BindValue>, max: impl Into<BindValue>, ) -> Self

Source

pub fn and_group<F>(self, f: F) -> Self

AND 条件分组:创建一个用 AND 连接的条件组 示例:builder.and_group(|b| b.and_eq("a", 1).and_eq("b", 2)) 生成:(a = ? AND b = ?)

Source

pub fn or_group<F>(self, f: F) -> Self

OR 条件分组:创建一个用 OR 连接的条件组 示例:builder.or_group(|b| b.and_eq("c", 3).and_eq("d", 4)) 生成:(c = ? OR d = ?)

Source

pub fn order_by(self, field: &str, ascending: bool) -> Self

Source

pub fn into_sql(&self, driver: DbDriver) -> String

Source

pub fn into_count_sql(&self, driver: DbDriver) -> String

Source

pub fn into_paginated_sql( &self, driver: DbDriver, limit: u64, offset: u64, ) -> String

Source

pub fn binds(&self) -> Vec<BindValue>

返回所有绑定值(WHERE 条件 + HAVING 条件)

Trait Implementations§

Source§

impl Clone for QueryBuilder

Source§

fn clone(&self) -> QueryBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryBuilder

Source§

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

Formats the value using the given formatter. Read more

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