Skip to main content

DbSet

Struct DbSet 

Source
pub struct DbSet<E: Entity> { /* private fields */ }
Expand description

Typed entry point for querying and persisting one entity type.

DbSet<E> is normally declared as a field on a derived DbContext. It builds query ASTs, applies runtime policies such as tenant and soft-delete visibility, compiles through the SQL Server crate, and executes through the shared Tiberius connection handle.

Implementations§

Source§

impl<E: Entity> DbSet<E>

Source

pub fn new(connection: SharedConnection) -> Self

Creates a set backed by the given shared connection.

Source

pub fn entity_metadata(&self) -> &'static EntityMetadata

Returns the static metadata generated for entity E.

Source

pub fn query(&self) -> DbSetQuery<E>

Starts a query for the full entity.

Tenant and soft-delete visibility are materialized when the query is compiled or executed, so callers cannot bypass those policies through the public query surface.

Source

pub fn query_with(&self, select_query: SelectQuery) -> DbSetQuery<E>

Starts a query from a caller-provided SelectQuery.

This is useful for advanced composition while still routing execution through DbSetQuery, so mandatory tenant and soft-delete behavior can be applied before SQL compilation.

Source

pub async fn find<K>(&self, key: K) -> Result<Option<E>, OrmError>

Finds one entity by its single-column primary key.

Composite primary keys are rejected in this stage. Tenant and soft-delete policies are applied through the normal query path.

Source

pub async fn find_tracked<K>( &self, key: K, ) -> Result<Option<Tracked<E>>, OrmError>

Loads an entity by its single-column primary key and wraps it in the experimental snapshot-based tracking container.

The loaded row is registered in this context’s tracker using entity type, schema, table and primary key value. Tracking the same persisted identity twice in one context returns OrmError instead of creating duplicate entries. Composite primary keys are rejected with a stable tracking error in the first stable cut. Included navigation graphs are not registered automatically; use explicit tracking entry points for every entity that should participate in save_changes().

Source

pub fn add_tracked(&self, entity: E) -> Tracked<E>
where E: Clone,

Registers a new in-memory entity as experimentally tracked in Added state so a later save_changes() can persist it via insert.

Added entries use a temporary identity until persistence. Entities with composite primary keys can be held in memory, but save_changes() rejects them before executing SQL in the first stable cut. A successful tracked insert replaces the temporary identity with the persisted single-column primary key returned by SQL Server. Dropping the returned wrapper still detaches the pending insert in this experimental wrapper-backed slice.

Source

pub fn remove_tracked(&self, tracked: &mut Tracked<E>)

Marks a tracked entity for deletion so a later save_changes() can persist it through the regular delete pipeline.

Calling this on an Added wrapper cancels the pending insert locally: the wrapper becomes Deleted and is detached from the tracker, so no database delete is issued by a later save_changes(). Calling this on a loaded or modified wrapper marks only that wrapper; relationship wrappers are not interpreted as cascade instructions.

Source

pub fn detach_tracked(&self, tracked: &mut Tracked<E>)

Detaches a tracked wrapper from this context’s experimental tracker.

Detach does not execute SQL and does not reset the wrapper state. It only removes the entry from the context unit of work so later save_changes() calls ignore it.

Source

pub async fn insert<I>(&self, insertable: I) -> Result<E, OrmError>

Inserts a new row and materializes the inserted entity.

The insert path applies tenant insert fill/validation and audit runtime values for entities that opt into those policies.

Source

pub async fn update<K, C>( &self, key: K, changeset: C, ) -> Result<Option<E>, OrmError>

Updates one row by single-column primary key and materializes the updated entity when a row matched.

Rowversion mismatches are surfaced as OrmError::ConcurrencyConflict when the entity still exists. Tenant and audit policies are applied by the shared update pipeline.

Source

pub async fn delete<K>(&self, key: K) -> Result<bool, OrmError>

Deletes one row by single-column primary key.

Entities with soft_delete emit an UPDATE through the soft-delete pipeline; other entities emit a physical DELETE. The return value is true when a row was affected.

Source

pub fn shared_connection(&self) -> SharedConnection

Returns the shared connection handle backing this set.

Source

pub async fn load_collection<J>( &self, entity: &mut E, navigation: &'static str, ) -> Result<(), OrmError>
where E: EntityPrimaryKey + IncludeCollection<J>, J: FromRow + Send + SoftDeleteEntity + TenantScopedEntity,

Explicitly loads a has_many collection navigation into an already materialized entity.

This performs I/O only at this call site. It does not install lazy loading behavior on the entity or navigation field.

Source

pub async fn load_collection_tracked<J>( &self, tracked: &mut Tracked<E>, navigation: &'static str, ) -> Result<(), OrmError>
where E: EntityPrimaryKey + IncludeCollection<J>, J: FromRow + Send + SoftDeleteEntity + TenantScopedEntity,

Explicitly loads a has_many collection navigation into a tracked entity without marking it as modified.

Trait Implementations§

Source§

impl<E: Clone + Entity> Clone for DbSet<E>

Source§

fn clone(&self) -> DbSet<E>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Entity> Debug for DbSet<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<E> Freeze for DbSet<E>

§

impl<E> !RefUnwindSafe for DbSet<E>

§

impl<E> Send for DbSet<E>

§

impl<E> Sync for DbSet<E>

§

impl<E> Unpin for DbSet<E>

§

impl<E> UnsafeUnpin for DbSet<E>

§

impl<E> !UnwindSafe for DbSet<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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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