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()); // 关联表 SQLImplementations§
Source§impl<'a> WithRelation<'a>
impl<'a> WithRelation<'a>
Sourcepub fn new(
dialect: &'a dyn Dialect,
main_table: impl Into<String>,
) -> Result<Self, DbError>
pub fn new( dialect: &'a dyn Dialect, main_table: impl Into<String>, ) -> Result<Self, DbError>
创建关联加载器
Sourcepub fn with_has_many(
self,
related: &'a str,
foreign_key: impl Into<String>,
primary_key: impl Into<String>,
) -> Result<Self, DbError>
pub fn with_has_many( self, related: &'a str, foreign_key: impl Into<String>, primary_key: impl Into<String>, ) -> Result<Self, DbError>
添加 HasMany 关联
Sourcepub fn with_has_one(
self,
related: &'a str,
foreign_key: impl Into<String>,
primary_key: impl Into<String>,
) -> Result<Self, DbError>
pub fn with_has_one( self, related: &'a str, foreign_key: impl Into<String>, primary_key: impl Into<String>, ) -> Result<Self, DbError>
添加 HasOne 关联
Sourcepub fn with_belongs_to(
self,
related: &'a str,
foreign_key: impl Into<String>,
primary_key: impl Into<String>,
) -> Result<Self, DbError>
pub fn with_belongs_to( self, related: &'a str, foreign_key: impl Into<String>, primary_key: impl Into<String>, ) -> Result<Self, DbError>
添加 BelongsTo 关联
Sourcepub fn load_eager(self, main_where: Option<&str>) -> Result<Self, DbError>
pub fn load_eager(self, main_where: Option<&str>) -> Result<Self, DbError>
执行 eager load(生成主表 SQL + 各关联表 SQL)
main_where:主表 WHERE 条件(None 表示无 WHERE)
返回 self 后通过 main_sql 和
related_sql 获取生成的 SQL。
P2-7 循环引用检测:调用此方法前会自动检测重复关联名,
若发现重复则返回 DbError::InvalidInput。
Sourcepub fn load_join(&self, main_where: Option<&str>) -> Result<String, DbError>
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 循环引用检测:调用此方法前会自动检测重复关联名。
获取指定关联表的 SQL(默认占位符 ?)
获取指定关联表 SQL,使用具体的 ID 列表
接受任意可迭代且元素可 ToString 的输入(&[i64]、Vec<String>、["a", "b"] 等)。
§安全性(v0.2.2 修复 C-5)
每个 id 经 sql_safety::validate_id_value 严格校验,仅允许字母数字+下划线+减号,
杜绝通过 id 拼接 SQL 注入。
Sourcepub fn relation_names(&self) -> Vec<&str>
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> 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