pub struct QueryBuilder { /* private fields */ }Expand description
安全的查询构建器,使用绑定参数而非字符串拼接
Implementations§
Source§impl QueryBuilder
impl QueryBuilder
pub fn new(base_sql: impl Into<String>) -> Self
Sourcepub fn with_base_sql(self, base_sql: impl Into<String>) -> Self
pub fn with_base_sql(self, base_sql: impl Into<String>) -> Self
替换基础 SQL(保留已有的条件、排序和绑定)
pub fn and_eq(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn and_ne(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn and_gt(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn and_ge(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn and_lt(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn and_le(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn or_eq(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn or_ne(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn or_gt(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn or_ge(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn or_lt(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn or_le(self, field: &str, value: impl Into<BindValue>) -> Self
pub fn and_like(self, field: &str, value: impl Into<String>) -> Self
Sourcepub fn and_like_prefix(self, field: &str, value: impl Into<String>) -> Self
pub fn and_like_prefix(self, field: &str, value: impl Into<String>) -> Self
LIKE 前缀匹配(value%)
Sourcepub fn and_like_suffix(self, field: &str, value: impl Into<String>) -> Self
pub fn and_like_suffix(self, field: &str, value: impl Into<String>) -> Self
LIKE 后缀匹配(%value)
Sourcepub fn and_like_exact(self, field: &str, value: impl Into<String>) -> Self
pub fn and_like_exact(self, field: &str, value: impl Into<String>) -> Self
LIKE 精确匹配(不添加 %)
Sourcepub fn and_like_custom(self, field: &str, pattern: impl Into<String>) -> Self
pub fn and_like_custom(self, field: &str, pattern: impl Into<String>) -> Self
LIKE 自定义模式匹配
pub fn or_like(self, field: &str, value: impl Into<String>) -> Self
Sourcepub fn and_regexp(self, field: &str, pattern: impl Into<String>) -> Self
pub fn and_regexp(self, field: &str, pattern: impl Into<String>) -> Self
正则表达式匹配(AND 条件) MySQL: field REGEXP pattern PostgreSQL: field ~ pattern SQLite: 不支持原生正则表达式(会抛出错误)
Sourcepub fn or_regexp(self, field: &str, pattern: impl Into<String>) -> Self
pub fn or_regexp(self, field: &str, pattern: impl Into<String>) -> Self
正则表达式匹配(OR 条件) MySQL: field REGEXP pattern PostgreSQL: field ~ pattern SQLite: 不支持原生正则表达式(会抛出错误)
pub fn and_in(self, field: &str, values: Vec<impl Into<BindValue>>) -> Self
pub fn or_in(self, field: &str, values: Vec<impl Into<BindValue>>) -> Self
Sourcepub fn and_is_null(self, field: &str) -> Self
pub fn and_is_null(self, field: &str) -> Self
IS NULL 查询
Sourcepub fn and_is_not_null(self, field: &str) -> Self
pub fn and_is_not_null(self, field: &str) -> Self
IS NOT NULL 查询
pub fn or_is_null(self, field: &str) -> Self
pub fn or_is_not_null(self, field: &str) -> Self
Sourcepub fn and_between(
self,
field: &str,
min: impl Into<BindValue>,
max: impl Into<BindValue>,
) -> Self
pub fn and_between( self, field: &str, min: impl Into<BindValue>, max: impl Into<BindValue>, ) -> Self
BETWEEN 范围查询
pub fn or_between( self, field: &str, min: impl Into<BindValue>, max: impl Into<BindValue>, ) -> Self
Sourcepub fn and_group<F>(self, f: F) -> Self
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 = ?)
Sourcepub fn or_group<F>(self, f: F) -> Self
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 = ?)
pub fn order_by(self, field: &str, ascending: bool) -> Self
pub fn into_sql(&self, driver: DbDriver) -> String
pub fn into_count_sql(&self, driver: DbDriver) -> String
pub fn into_paginated_sql( &self, driver: DbDriver, limit: u64, offset: u64, ) -> String
Trait Implementations§
Source§impl Clone for QueryBuilder
impl Clone for QueryBuilder
Source§fn clone(&self) -> QueryBuilder
fn clone(&self) -> QueryBuilder
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for QueryBuilder
impl RefUnwindSafe for QueryBuilder
impl Send for QueryBuilder
impl Sync for QueryBuilder
impl Unpin for QueryBuilder
impl UnwindSafe for QueryBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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