pub struct SqlRepository<E: Model + ModelExt + Default + Clone + Send + Sync + 'static> { /* private fields */ }Expand description
Repository 的 SQL 后端实现。
重写 batch_create/batch_upsert/paginate_sql 为真正 SQL 实现,
复用 QueryBuilder + Connection,区别于 Repository trait 默认实现
(逐条 save / 内存切片)。
§泛型约束
E: Model + ModelExt— 获取表名/列名/列值E: Default— 从行数据还原实体(from_value需&mut self)E: Clone + Send + Sync + 'static— 异步安全
§示例
use sz_orm_core::{DbType, Pool};
use sz_orm_sqlx::SqlRepository;
let repo = SqlRepository::<E>::new(pool, DbType::MySQL);
let n = repo.batch_create(&entities).await.unwrap();Implementations§
Source§impl<E: Model + ModelExt + Default + Clone + Send + Sync + 'static> SqlRepository<E>
impl<E: Model + ModelExt + Default + Clone + Send + Sync + 'static> SqlRepository<E>
Sourcepub async fn find_all(&self) -> Result<Vec<E>, RepositoryError>
pub async fn find_all(&self) -> Result<Vec<E>, RepositoryError>
查询所有记录。
Sourcepub async fn find_by(
&self,
conditions: &[WhereCondition],
) -> Result<Vec<E>, RepositoryError>
pub async fn find_by( &self, conditions: &[WhereCondition], ) -> Result<Vec<E>, RepositoryError>
按条件查询。
Sourcepub async fn count(&self) -> Result<u64, RepositoryError>
pub async fn count(&self) -> Result<u64, RepositoryError>
总数。
Sourcepub async fn batch_create(&self, entities: &[E]) -> Result<u64, RepositoryError>
pub async fn batch_create(&self, entities: &[E]) -> Result<u64, RepositoryError>
v9.5.1 真正多值 INSERT,复用 QueryBuilder::build_batch_insert_with_params。
重写 Repository trait 默认实现(逐条 save),生成 1 条多值 INSERT SQL。
- 空批返回
Ok(0),不执行 SQL - 所有值参数化绑定,杜绝 SQL 注入
Sourcepub async fn batch_upsert(
&self,
entities: &[E],
conflict_cols: &[&str],
update_cols: &[&str],
) -> Result<u64, RepositoryError>
pub async fn batch_upsert( &self, entities: &[E], conflict_cols: &[&str], update_cols: &[&str], ) -> Result<u64, RepositoryError>
v9.5.1 真正 INSERT ON CONFLICT,复用 QueryBuilder::build_batch_upsert_with_params。
重写 Repository trait 默认实现,生成 1 条 UPSERT SQL。
conflict_cols:冲突检测列(主键/唯一键)update_cols:冲突时更新的列;空切片表示更新所有非冲突列- 空批返回
Err(InvalidParams)
Sourcepub async fn paginate_sql(
&self,
page: u64,
page_size: u64,
) -> Result<PageResult<E>, RepositoryError>
pub async fn paginate_sql( &self, page: u64, page_size: u64, ) -> Result<PageResult<E>, RepositoryError>
v9.5.1 真正 SQL LIMIT/OFFSET + COUNT,执行 2 条 SQL。
重写 Repository trait 默认实现(内存切片 paginate)。
page == 0 || page_size == 0返回Err(InvalidParams)- 执行
SELECT ... LIMIT ? OFFSET ?+SELECT COUNT(*) FROM table两条 SQL
Auto Trait Implementations§
impl<E> !Freeze for SqlRepository<E>
impl<E> !RefUnwindSafe for SqlRepository<E>
impl<E> !UnwindSafe for SqlRepository<E>
impl<E> Send for SqlRepository<E>where
PhantomData<E>: Send,
impl<E> Sync for SqlRepository<E>where
PhantomData<E>: Sync,
impl<E> Unpin for SqlRepository<E>where
PhantomData<E>: Unpin,
impl<E> UnsafeUnpin for SqlRepository<E>where
PhantomData<E>: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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