Skip to main content

WithRelation

Struct WithRelation 

Source
pub struct WithRelation<'a> { /* private fields */ }
Expand description

SeaORM find_with_related 风格的关联加载器

§用法

use sz_orm_query::find_with_related::WithRelation;
use sz_orm_model::dialect::get_dialect;
use sz_orm_model::DbType;

let dialect = get_dialect(DbType::MySQL).unwrap();
let loader = WithRelation::new(&*dialect, "users")
    .with_has_many("orders", "user_id", "id")
    .with_has_one("profiles", "user_id", "id")
    .load_eager(Some("users.id IN (1, 2, 3)"));

println!("{}", loader.main_sql());        // 主表 SQL
println!("{}", loader.related_sql("orders").unwrap());  // 关联表 SQL

Implementations§

Source§

impl<'a> WithRelation<'a>

Source

pub fn new( dialect: &'a dyn Dialect, main_table: impl Into<String>, ) -> Result<Self, DbError>

创建关联加载器

Source

pub fn with_has_many( self, related: &'a str, foreign_key: impl Into<String>, primary_key: impl Into<String>, ) -> Result<Self, DbError>

添加 HasMany 关联

Source

pub fn with_has_one( self, related: &'a str, foreign_key: impl Into<String>, primary_key: impl Into<String>, ) -> Result<Self, DbError>

添加 HasOne 关联

Source

pub fn with_belongs_to( self, related: &'a str, foreign_key: impl Into<String>, primary_key: impl Into<String>, ) -> Result<Self, DbError>

添加 BelongsTo 关联

Source

pub fn load_eager(self, main_where: Option<&str>) -> Result<Self, DbError>

执行 eager load(生成主表 SQL + 各关联表 SQL)

  • main_where:主表 WHERE 条件(None 表示无 WHERE)

返回 self 后通过 main_sqlrelated_sql 获取生成的 SQL。

P2-7 循环引用检测:调用此方法前会自动检测重复关联名, 若发现重复则返回 DbError::InvalidInput

Source

pub fn load_join(&self, main_where: Option<&str>) -> Result<String, DbError>

执行 JOIN load(生成单条 JOIN SQL)

HasMany / HasOne → LEFT JOIN BelongsTo → INNER JOIN

P2-7 循环引用检测:调用此方法前会自动检测重复关联名。

Source

pub fn main_sql(&self) -> String

获取主表 SQL

Source

pub fn related_sql(&self, name: &str) -> Option<String>

获取指定关联表的 SQL(默认占位符 ?

Source

pub fn related_sql_with_ids( &self, name: &str, ids: impl IntoIterator<Item = impl ToString>, ) -> Result<Option<String>, DbError>

获取指定关联表 SQL,使用具体的 ID 列表

接受任意可迭代且元素可 ToString 的输入(&[i64]Vec<String>["a", "b"] 等)。

§安全性(v0.2.2 修复 C-5)

每个 id 经 sql_safety::validate_id_value 严格校验,仅允许字母数字+下划线+减号, 杜绝通过 id 拼接 SQL 注入。

Source

pub fn relation_names(&self) -> Vec<&str>

获取所有已注册的关联名

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for WithRelation<'a>

§

impl<'a> !UnwindSafe for WithRelation<'a>

§

impl<'a> Freeze for WithRelation<'a>

§

impl<'a> Send for WithRelation<'a>

§

impl<'a> Sync for WithRelation<'a>

§

impl<'a> Unpin for WithRelation<'a>

§

impl<'a> UnsafeUnpin for WithRelation<'a>

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