Skip to main content

ChangeTracker

Struct ChangeTracker 

Source
pub struct ChangeTracker { /* private fields */ }
Expand description

变更跟踪器

管理多个实体的状态,提供批量变更检测和变更集生成。

Implementations§

Source§

impl ChangeTracker

Source

pub fn new() -> Self

Source

pub fn track( &mut self, table: &str, id: &str, current: HashMap<String, Value>, state: EntityState, )

跟踪实体

如果状态为 Unchanged,会保存当前值作为原始值快照。 如果状态为 Modified,会保留之前的原始值快照。

Source

pub fn mark_added( &mut self, table: &str, id: &str, entity: HashMap<String, Value>, )

标记为新增

Source

pub fn mark_unchanged( &mut self, table: &str, id: &str, entity: HashMap<String, Value>, )

标记为未变更(从数据库加载后调用)

Source

pub fn update(&mut self, table: &str, id: &str, entity: HashMap<String, Value>)

标记为已修改

Source

pub fn mark_deleted(&mut self, table: &str, id: &str)

标记为已删除

Source

pub fn detach(&mut self, table: &str, id: &str)

分离实体(停止跟踪)

Source

pub fn detect_changes(&mut self)

自动检测变更

遍历所有 Unchanged 实体,如果 current 与 original 不同,自动转为 Modified

Source

pub fn get_pending_changes(&self) -> Vec<&EntityEntry>

获取所有待提交的变更

Source

pub fn get_pending_changes_by_table(&self) -> HashMap<String, Vec<&EntityEntry>>

按表分组获取待提交的变更

Source

pub fn entry(&self, table: &str, id: &str) -> Option<&EntityEntry>

获取条目

Source

pub fn count(&self) -> usize

跟踪的实体数量

Source

pub fn pending_count(&self) -> usize

待提交的变更数量

Source

pub fn accept_changes(&mut self)

清除所有跟踪状态(SaveChanges 成功后调用)

Source

pub fn build_sql_operations(&self) -> Vec<(String, Vec<Value>)>

将待提交的变更转换为 SQL 语句列表(生产接线点)

这是 ChangeTracker 与 SQL 生成的集成点。 生成的 SQL 使用参数化占位符 ?,参数通过返回值一并给出。

Trait Implementations§

Source§

impl Default for ChangeTracker

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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