pub struct BatchExecutor<'a, C: Connection> { /* private fields */ }Expand description
v9.5.0 批量执行器:端到端批量 INSERT/UPSERT。
串联 QueryBuilder::build_batch_insert_with_params → Connection::execute_with_params,
含自动分片、空批边界。
§示例
use sz_orm_core::{Connection, DbType, Value};
use sz_orm_sqlx::BatchExecutor;
use std::collections::HashMap;
let mut executor = BatchExecutor::new(conn, DbType::MySQL);
let mut row = HashMap::new();
row.insert("name".to_string(), Value::String("alice".to_string()));
let affected = executor.batch_insert("users", &[row]).await.unwrap();Implementations§
Source§impl<'a, C: Connection> BatchExecutor<'a, C>
impl<'a, C: Connection> BatchExecutor<'a, C>
Sourcepub fn with_max_batch_rows(self, max: usize) -> Self
pub fn with_max_batch_rows(self, max: usize) -> Self
设置每批最大行数(自动分片阈值),最小为 1。
Sourcepub async fn batch_insert(
&mut self,
table: &str,
rows: &[HashMap<String, Value>],
) -> Result<u64, DbError>
pub async fn batch_insert( &mut self, table: &str, rows: &[HashMap<String, Value>], ) -> Result<u64, DbError>
批量 INSERT:生成多值 INSERT SQL 并执行。
- 空批返回
Ok(0),不执行 SQL(spec 5.1.1 规则 4) - 自动分片:
rows.len() > max_batch_rows时分多批执行并聚合影响行数 - 所有值参数化绑定,杜绝 SQL 注入(spec 5.1.1 规则 5)
Sourcepub async fn batch_upsert(
&mut self,
table: &str,
rows: &[HashMap<String, Value>],
conflict_cols: &[&str],
update_cols: &[&str],
) -> Result<u64, DbError>
pub async fn batch_upsert( &mut self, table: &str, rows: &[HashMap<String, Value>], conflict_cols: &[&str], update_cols: &[&str], ) -> Result<u64, DbError>
批量 UPSERT:生成 INSERT…ON DUPLICATE KEY UPDATE / ON CONFLICT UPDATE SQL 并执行。
conflict_cols:冲突检测列(主键/唯一键)update_cols:冲突时更新的列;空切片表示更新所有非冲突列- 空批返回
Err(InvalidInput)(与build_batch_upsert_with_params一致)
Sourcepub fn max_batch_rows(&self) -> usize
pub fn max_batch_rows(&self) -> usize
返回当前最大批次行数(用于测试断言)。
Sourcepub async fn batch_insert_parallel(
&mut self,
table: &str,
rows: &[HashMap<String, Value>],
shard_size: usize,
_parallelism: usize,
) -> Result<BatchParallelResult, DbError>
pub async fn batch_insert_parallel( &mut self, table: &str, rows: &[HashMap<String, Value>], shard_size: usize, _parallelism: usize, ) -> Result<BatchParallelResult, DbError>
v9.6.0 并行分片批量 INSERT:拆分行为多个分片,逐片执行并聚合结果。
当前使用 self.conn 串行执行各分片(单连接降级模式)。
真正的多连接并行需通过 batch_insert_parallel_with_pool 实现。
shard_size:每片行数(≤ 1 时按max_batch_rows分片)parallelism:期望并行度(当前降级为串行,参数保留兼容)- 某分片失败时其他分片继续(Continue 策略),已成功分片不回滚
Auto Trait Implementations§
impl<'a, C> !UnwindSafe for BatchExecutor<'a, C>
impl<'a, C> Freeze for BatchExecutor<'a, C>
impl<'a, C> RefUnwindSafe for BatchExecutor<'a, C>where
&'a mut C: RefUnwindSafe,
impl<'a, C> Send for BatchExecutor<'a, C>
impl<'a, C> Sync for BatchExecutor<'a, C>
impl<'a, C> Unpin for BatchExecutor<'a, C>
impl<'a, C> UnsafeUnpin for BatchExecutor<'a, C>where
&'a mut C: 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