Skip to main content

LambdaWrapper

Struct LambdaWrapper 

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

Source

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

创建 LambdaWrapper,默认使用 MySQL 方言

Source

pub fn with_dialect(table: impl Into<String>, dialect: Box<dyn Dialect>) -> Self

创建 LambdaWrapper 并指定方言

Source

pub fn with_soft_delete(self, config: SoftDeleteConfig) -> Self

启用软删除支持

启用后:

  • build_select / build_count / build_exists:自动追加 column = not_deleted_value
  • build_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),
    });
Source

pub fn with_tenant(self, config: TenantConfig) -> Self

启用多租户过滤

启用后:

  • 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),
    });
Source

pub fn select<C: Column<M>>(&mut self, col: C) -> &mut Self

添加 SELECT 字段(类型安全)

M-4 修复:对列名进行 validate_identifier 校验,防止恶意实现 Column trait 注入非法标识符。

Source

pub fn select_many<C: Column<M>>(&mut self, cols: &[C]) -> &mut Self

批量添加 SELECT 字段

M-4 修复:同 select,对每个列名校验。

Source

pub fn select_all(&mut self) -> &mut Self

SELECT *(清空已有字段选择)

Source

pub fn eq<C: Column<M>>(&mut self, col: C, value: Value) -> &mut Self

col = value

Source

pub fn ne<C: Column<M>>(&mut self, col: C, value: Value) -> &mut Self

col != value

Source

pub fn gt<C: Column<M>>(&mut self, col: C, value: Value) -> &mut Self

col > value

Source

pub fn ge<C: Column<M>>(&mut self, col: C, value: Value) -> &mut Self

col >= value

Source

pub fn lt<C: Column<M>>(&mut self, col: C, value: Value) -> &mut Self

col < value

Source

pub fn le<C: Column<M>>(&mut self, col: C, value: Value) -> &mut Self

col <= value

Source

pub fn like<C: Column<M>>(&mut self, col: C, value: Value) -> &mut Self

col LIKE value

Source

pub fn is_null<C: Column<M>>(&mut self, col: C) -> &mut Self

col IS NULL

Source

pub fn is_not_null<C: Column<M>>(&mut self, col: C) -> &mut Self

col IS NOT NULL

Source

pub fn in<C: Column<M>>(&mut self, col: C, values: Vec<Value>) -> &mut Self

col IN (v1, v2, ...)

Source

pub fn not_in<C: Column<M>>(&mut self, col: C, values: Vec<Value>) -> &mut Self

col NOT IN (v1, v2, ...)

Source

pub fn between<C: Column<M>>(&mut self, col: C, a: Value, b: Value) -> &mut Self

col BETWEEN a AND b

Source

pub fn raw_where(&mut self, sql: impl Into<String>) -> &mut Self

追加原始 SQL WHERE 条件(用于 OR 等复杂场景)

§安全警告

此方法是 escape hatch,传入的 SQL 会原样拼接到最终 SQL 中。 严禁将用户输入直接拼接sql 参数中(会引入 SQL 注入风险)。 若需使用用户输入,请改用 eq / ne / lt 等参数化方法。

Source

pub fn order_by_asc<C: Column<M>>(&mut self, col: C) -> &mut Self

添加升序排序

Source

pub fn order_by_desc<C: Column<M>>(&mut self, col: C) -> &mut Self

添加降序排序

Source

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

设置 LIMIT

Source

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

设置 OFFSET

Source

pub fn page(&mut self, page: u64, page_size: u64) -> &mut Self

分页(设置 LIMIT + OFFSET)

page 从 1 开始计数,page=1 时无 OFFSET(从第 0 条开始)。

Source

pub fn build_select(&self) -> String

生成 SELECT SQL

P3-2 修复:若启用了软删除/多租户,自动追加隐式 WHERE 条件

Source

pub fn build_count(&self) -> String

生成 COUNT SQL(SELECT COUNT(*) FROM … WHERE …)

P3-2 修复:若启用了软删除/多租户,自动追加隐式 WHERE 条件

Source

pub fn build_exists(&self) -> String

生成 EXISTS SQL(SELECT EXISTS(SELECT 1 FROM … WHERE …) AS exists_flag)

P3-2 修复:若启用了软删除/多租户,自动追加隐式 WHERE 条件

Source

pub fn build_delete(&self) -> String

生成 DELETE SQL

P3-2 修复:

  • 若启用软删除,则改为 UPDATE ... SET column = deleted_value (即软删除:仅标记,不实际删除行)
  • 若启用多租户,自动追加 column = tenant_id 条件,防止跨租户删除
Source

pub fn where_count(&self) -> usize

返回当前 WHERE 条件数量

Source

pub fn select_count(&self) -> usize

返回当前 SELECT 字段数量

Source

pub fn table(&self) -> &str

返回表名

Source

pub fn reset(&mut self) -> &mut Self

重置所有条件(保留表名和方言)

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