pub struct UnitOfWork { /* private fields */ }Expand description
Tracks and manages all pending changes in a session.
The Unit of Work is responsible for:
- Maintaining the set of new, dirty, and deleted objects
- Computing the correct flush order based on FK dependencies
- Detecting dependency cycles before flush
Implementations§
Source§impl UnitOfWork
impl UnitOfWork
Sourcepub fn register_model<T: Model>(&mut self)
pub fn register_model<T: Model>(&mut self)
Register a model type for dependency tracking.
This extracts foreign key relationships from the model’s metadata and registers them for flush ordering.
Sourcepub fn track_new<T: Model + Serialize>(&mut self, model: &T, key: ObjectKey)
pub fn track_new<T: Model + Serialize>(&mut self, model: &T, key: ObjectKey)
Track a new object for insertion.
The object will be INSERTed during flush.
Sourcepub fn track_dirty<T: Model + Serialize>(
&mut self,
model: &T,
key: ObjectKey,
changed_columns: Vec<&'static str>,
)
pub fn track_dirty<T: Model + Serialize>( &mut self, model: &T, key: ObjectKey, changed_columns: Vec<&'static str>, )
Track a dirty object for update.
The object will be UPDATEd during flush (only changed columns).
Sourcepub fn track_dirty_auto<T: Model + Serialize>(
&mut self,
model: &T,
key: ObjectKey,
)
pub fn track_dirty_auto<T: Model + Serialize>( &mut self, model: &T, key: ObjectKey, )
Track a dirty object for update (auto-detect changed fields).
Uses the change tracker to determine which fields changed.
Sourcepub fn track_deleted<T: Model>(&mut self, model: &T, key: ObjectKey)
pub fn track_deleted<T: Model>(&mut self, model: &T, key: ObjectKey)
Track an object for deletion.
The object will be DELETEd during flush.
Sourcepub fn snapshot<T: Model + Serialize>(&mut self, key: ObjectKey, model: &T)
pub fn snapshot<T: Model + Serialize>(&mut self, key: ObjectKey, model: &T)
Take a snapshot of an object for later dirty detection.
Sourcepub fn is_dirty<T: Model + Serialize>(&self, key: &ObjectKey, model: &T) -> bool
pub fn is_dirty<T: Model + Serialize>(&self, key: &ObjectKey, model: &T) -> bool
Check if an object is dirty (has changed since snapshot).
Sourcepub fn changed_fields<T: Model + Serialize>(
&self,
key: &ObjectKey,
model: &T,
) -> Vec<&'static str>
pub fn changed_fields<T: Model + Serialize>( &self, key: &ObjectKey, model: &T, ) -> Vec<&'static str>
Get the changed fields for an object.
Sourcepub fn check_cycles(&self) -> Result<(), UowError>
pub fn check_cycles(&self) -> Result<(), UowError>
Check for dependency cycles in the registered tables.
Returns Err(UowError::CycleDetected) if a cycle is found.
Sourcepub fn compute_flush_plan(&self) -> Result<FlushPlan, UowError>
pub fn compute_flush_plan(&self) -> Result<FlushPlan, UowError>
Compute the flush plan.
This checks for cycles and orders operations by dependencies.
§Errors
Returns Err if a dependency cycle is detected.
Sourcepub fn has_changes(&self) -> bool
pub fn has_changes(&self) -> bool
Check if there are any pending changes.
Sourcepub fn pending_count(&self) -> PendingCounts
pub fn pending_count(&self) -> PendingCounts
Get the count of pending operations.
Sourcepub fn change_tracker(&self) -> &ChangeTracker
pub fn change_tracker(&self) -> &ChangeTracker
Get a reference to the change tracker.
Sourcepub fn change_tracker_mut(&mut self) -> &mut ChangeTracker
pub fn change_tracker_mut(&mut self) -> &mut ChangeTracker
Get a mutable reference to the change tracker.