pub struct LambdaWrapper<M> { /* private fields */ }Expand description
Lambda 类型安全查询构造器
泛型参数 M 是 Model 类型(仅用于类型隔离,不实际存储实例)。
§字段引用方式
与原始 QueryBuilder 使用 &str 字段名不同,LambdaWrapper 接受 Column<M> 实例,
从而在编译期检查字段拼写错误。
§示例
use sz_orm_query::lambda::{LambdaWrapper, Column};
use sz_orm_query::define_columns;
use sz_orm_model::Value;
struct User;
define_columns! {
UserColumns for User table = "users" {
Id => "id",
Name => "name",
}
}
let mut w = LambdaWrapper::<User>::new("users");
w.eq(UserColumns::Id, Value::I64(1));Implementations§
Source§impl<M> LambdaWrapper<M>
impl<M> LambdaWrapper<M>
Sourcepub fn new(table: impl Into<String>) -> LambdaWrapper<M>
pub fn new(table: impl Into<String>) -> LambdaWrapper<M>
创建 LambdaWrapper,默认使用 MySQL 方言
Sourcepub fn with_dialect(
table: impl Into<String>,
dialect: Box<dyn Dialect>,
) -> LambdaWrapper<M>
pub fn with_dialect( table: impl Into<String>, dialect: Box<dyn Dialect>, ) -> LambdaWrapper<M>
创建 LambdaWrapper 并指定方言
Sourcepub fn with_soft_delete(self, config: SoftDeleteConfig) -> LambdaWrapper<M>
pub fn with_soft_delete(self, config: SoftDeleteConfig) -> LambdaWrapper<M>
启用软删除支持
启用后:
build_select/build_count/build_exists:自动追加column = not_deleted_valuebuild_delete:改为UPDATE ... SET column = deleted_value
§示例
use sz_orm_query::lambda::{LambdaWrapper, SoftDeleteConfig};
use sz_orm_model::Value;
struct User;
let mut w = LambdaWrapper::<User>::new("users")
.with_soft_delete(SoftDeleteConfig {
column: "deleted".to_string(),
not_deleted_value: Value::I64(0),
deleted_value: Value::I64(1),
});Sourcepub fn with_tenant(self, config: TenantConfig) -> LambdaWrapper<M>
pub fn with_tenant(self, config: TenantConfig) -> LambdaWrapper<M>
启用多租户过滤
启用后:
build_select/build_count/build_exists/build_delete: 自动追加column = tenant_id
§示例
use sz_orm_query::lambda::{LambdaWrapper, TenantConfig};
use sz_orm_model::Value;
struct User;
let mut w = LambdaWrapper::<User>::new("users")
.with_tenant(TenantConfig {
column: "tenant_id".to_string(),
tenant_id: Value::I64(42),
});Sourcepub fn select<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn select<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
添加 SELECT 字段(类型安全)
M-4 修复:对列名进行 validate_identifier 校验,防止恶意实现 Column trait
注入非法标识符。
Sourcepub fn select_many<C>(&mut self, cols: &[C]) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn select_many<C>(&mut self, cols: &[C]) -> &mut LambdaWrapper<M>where
C: Column<M>,
批量添加 SELECT 字段
M-4 修复:同 select,对每个列名校验。
Sourcepub fn select_all(&mut self) -> &mut LambdaWrapper<M>
pub fn select_all(&mut self) -> &mut LambdaWrapper<M>
SELECT *(清空已有字段选择)
Sourcepub fn eq<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn eq<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
col = value
Sourcepub fn ne<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn ne<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
col != value
Sourcepub fn gt<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn gt<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
col > value
Sourcepub fn ge<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn ge<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
col >= value
Sourcepub fn lt<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn lt<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
col < value
Sourcepub fn le<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn le<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
col <= value
Sourcepub fn like<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn like<C>(&mut self, col: C, value: Value) -> &mut LambdaWrapper<M>where
C: Column<M>,
col LIKE value
Sourcepub fn is_null<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn is_null<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
col IS NULL
Sourcepub fn is_not_null<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn is_not_null<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
col IS NOT NULL
Sourcepub fn in<C>(&mut self, col: C, values: Vec<Value>) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn in<C>(&mut self, col: C, values: Vec<Value>) -> &mut LambdaWrapper<M>where
C: Column<M>,
col IN (v1, v2, ...)
Sourcepub fn not_in<C>(&mut self, col: C, values: Vec<Value>) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn not_in<C>(&mut self, col: C, values: Vec<Value>) -> &mut LambdaWrapper<M>where
C: Column<M>,
col NOT IN (v1, v2, ...)
Sourcepub fn between<C>(
&mut self,
col: C,
a: Value,
b: Value,
) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn between<C>(
&mut self,
col: C,
a: Value,
b: Value,
) -> &mut LambdaWrapper<M>where
C: Column<M>,
col BETWEEN a AND b
Sourcepub fn raw_where(&mut self, sql: impl Into<String>) -> &mut LambdaWrapper<M>
pub fn raw_where(&mut self, sql: impl Into<String>) -> &mut LambdaWrapper<M>
追加原始 SQL WHERE 条件(用于 OR 等复杂场景)
§安全警告
此方法是 escape hatch,传入的 SQL 会原样拼接到最终 SQL 中。
严禁将用户输入直接拼接到 sql 参数中(会引入 SQL 注入风险)。
若需使用用户输入,请改用 eq / ne / lt 等参数化方法。
Sourcepub fn order_by_asc<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn order_by_asc<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
添加升序排序
Sourcepub fn order_by_desc<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
pub fn order_by_desc<C>(&mut self, col: C) -> &mut LambdaWrapper<M>where
C: Column<M>,
添加降序排序
Sourcepub fn limit(&mut self, n: u64) -> &mut LambdaWrapper<M>
pub fn limit(&mut self, n: u64) -> &mut LambdaWrapper<M>
设置 LIMIT
Sourcepub fn offset(&mut self, n: u64) -> &mut LambdaWrapper<M>
pub fn offset(&mut self, n: u64) -> &mut LambdaWrapper<M>
设置 OFFSET
Sourcepub fn page(&mut self, page: u64, page_size: u64) -> &mut LambdaWrapper<M>
pub fn page(&mut self, page: u64, page_size: u64) -> &mut LambdaWrapper<M>
分页(设置 LIMIT + OFFSET)
page 从 1 开始计数,page=1 时无 OFFSET(从第 0 条开始)。
Sourcepub fn build_select(&self) -> String
pub fn build_select(&self) -> String
生成 SELECT SQL
P3-2 修复:若启用了软删除/多租户,自动追加隐式 WHERE 条件
Sourcepub fn build_count(&self) -> String
pub fn build_count(&self) -> String
生成 COUNT SQL(SELECT COUNT(*) FROM … WHERE …)
P3-2 修复:若启用了软删除/多租户,自动追加隐式 WHERE 条件
Sourcepub fn build_exists(&self) -> String
pub fn build_exists(&self) -> String
生成 EXISTS SQL(SELECT EXISTS(SELECT 1 FROM … WHERE …) AS exists_flag)
P3-2 修复:若启用了软删除/多租户,自动追加隐式 WHERE 条件
Sourcepub fn build_delete(&self) -> String
pub fn build_delete(&self) -> String
生成 DELETE SQL
P3-2 修复:
- 若启用软删除,则改为
UPDATE ... SET column = deleted_value(即软删除:仅标记,不实际删除行) - 若启用多租户,自动追加
column = tenant_id条件,防止跨租户删除
Sourcepub fn where_count(&self) -> usize
pub fn where_count(&self) -> usize
返回当前 WHERE 条件数量
Sourcepub fn select_count(&self) -> usize
pub fn select_count(&self) -> usize
返回当前 SELECT 字段数量
Sourcepub fn reset(&mut self) -> &mut LambdaWrapper<M>
pub fn reset(&mut self) -> &mut LambdaWrapper<M>
重置所有条件(保留表名和方言)
Auto Trait Implementations§
impl<M> !RefUnwindSafe for LambdaWrapper<M>
impl<M> !UnwindSafe for LambdaWrapper<M>
impl<M> Freeze for LambdaWrapper<M>
impl<M> Send for LambdaWrapper<M>where
M: Send,
impl<M> Sync for LambdaWrapper<M>where
M: Sync,
impl<M> Unpin for LambdaWrapper<M>where
M: Unpin,
impl<M> UnsafeUnpin for LambdaWrapper<M>
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
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>
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>
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