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>
impl<E: Entity> DbSet<E>
Sourcepub fn new(connection: SharedConnection) -> Self
pub fn new(connection: SharedConnection) -> Self
Creates a set backed by the given shared connection.
Sourcepub fn entity_metadata(&self) -> &'static EntityMetadata
pub fn entity_metadata(&self) -> &'static EntityMetadata
Returns the static metadata generated for entity E.
Sourcepub fn query(&self) -> DbSetQuery<E>
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.
Sourcepub fn query_with(&self, select_query: SelectQuery) -> DbSetQuery<E>
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.
Sourcepub async fn find<K>(&self, key: K) -> Result<Option<E>, OrmError>
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.
Sourcepub async fn find_tracked<K>(
&self,
key: K,
) -> Result<Option<Tracked<E>>, OrmError>
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().
Sourcepub fn add_tracked(&self, entity: E) -> Tracked<E>where
E: Clone,
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.
Sourcepub fn remove_tracked(&self, tracked: &mut Tracked<E>)
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.
Sourcepub fn detach_tracked(&self, tracked: &mut Tracked<E>)
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.
Sourcepub async fn insert<I>(&self, insertable: I) -> Result<E, OrmError>
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.
Sourcepub async fn update<K, C>(
&self,
key: K,
changeset: C,
) -> Result<Option<E>, OrmError>where
E: AuditEntity + FromRow + Send + SoftDeleteEntity + TenantScopedEntity,
K: SqlTypeMapping,
C: Changeset<E>,
pub async fn update<K, C>(
&self,
key: K,
changeset: C,
) -> Result<Option<E>, OrmError>where
E: AuditEntity + FromRow + Send + SoftDeleteEntity + TenantScopedEntity,
K: SqlTypeMapping,
C: Changeset<E>,
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.
Sourcepub async fn delete<K>(&self, key: K) -> Result<bool, OrmError>
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.
Returns the shared connection handle backing this set.
Sourcepub async fn load_collection<J>(
&self,
entity: &mut E,
navigation: &'static str,
) -> Result<(), OrmError>where
E: EntityPrimaryKey + IncludeCollection<J>,
J: FromRow + Send + SoftDeleteEntity + TenantScopedEntity,
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.
Sourcepub 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,
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.