Skip to main content

BatchExecutor

Struct BatchExecutor 

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

Source

pub fn new(conn: &'a mut C, dialect: DbType) -> Self

创建批量执行器,默认 max_batch_rows = 1000。

Source

pub fn with_max_batch_rows(self, max: usize) -> Self

设置每批最大行数(自动分片阈值),最小为 1。

Source

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

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

pub fn max_batch_rows(&self) -> usize

返回当前最大批次行数(用于测试断言)。

Source

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> Forget for BatchExecutor<'a, C>

§

impl<'a, C> Freeze for BatchExecutor<'a, C>

§

impl<'a, C> RefUnwindSafe for BatchExecutor<'a, C>

§

impl<'a, C> Send for BatchExecutor<'a, C>
where &'a mut C: Send,

§

impl<'a, C> Sync for BatchExecutor<'a, C>
where &'a mut C: Sync,

§

impl<'a, C> Unpin for BatchExecutor<'a, C>

§

impl<'a, C> UnsafeUnpin for BatchExecutor<'a, C>

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