Skip to main content

SqlRepository

Struct SqlRepository 

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

Source

pub fn new(pool: Pool, dialect: DbType) -> Self

构造 SqlRepository,须传入已初始化的连接池 + 数据库方言。

Source

pub async fn find_all(&self) -> Result<Vec<E>, RepositoryError>

查询所有记录。

Source

pub async fn find_by( &self, conditions: &[WhereCondition], ) -> Result<Vec<E>, RepositoryError>

按条件查询。

Source

pub async fn save(&self, entity: &mut E) -> Result<u64, RepositoryError>

保存(INSERT)。

Source

pub async fn count(&self) -> Result<u64, RepositoryError>

总数。

Source

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

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

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>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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