Skip to main content

DbContext

Trait DbContext 

Source
pub trait DbContext: Sized {
    // Required methods
    fn from_shared_connection(connection: SharedConnection) -> Self;
    fn shared_connection(&self) -> SharedConnection;

    // Provided methods
    fn clear_tracker(&self) { ... }
    fn health_check(&self) -> impl Future<Output = Result<(), OrmError>> + Send { ... }
    fn raw<T>(&self, sql: impl Into<String>) -> RawQuery<T>
       where T: FromRow + Send { ... }
    fn raw_exec(&self, sql: impl Into<String>) -> RawCommand { ... }
    fn transaction<F, Fut, T>(
        &self,
        operation: F,
    ) -> impl Future<Output = Result<T, OrmError>> + Send
       where F: FnOnce(Self) -> Fut + Send,
             Fut: Future<Output = Result<T, OrmError>> + Send,
             T: Send { ... }
}
Expand description

Application database context contract.

#[derive(DbContext)] implements this trait for structs whose fields are DbSet<T>. The trait centralizes connection access, health checks, raw SQL, transactions, and save_changes() support while keeping SQL generation in sql-orm-sqlserver and execution in sql-orm-tiberius.

Required Methods§

Source

fn from_shared_connection(connection: SharedConnection) -> Self

Builds a context from an existing shared connection handle.

Source

fn shared_connection(&self) -> SharedConnection

Returns the shared connection handle used by this context.

Provided Methods§

Source

fn clear_tracker(&self)

Clears every tracking entry currently registered on this context.

This does not execute SQL. Pending inserts, updates and deletes are discarded from the unit of work represented by the current tracker.

Source

fn health_check(&self) -> impl Future<Output = Result<(), OrmError>> + Send

Executes the configured SQL Server health check through the current connection handle.

Source

fn raw<T>(&self, sql: impl Into<String>) -> RawQuery<T>
where T: FromRow + Send,

Creates a typed raw SQL query.

Raw SQL is executed exactly as written after ORM parameter handling; it does not automatically apply tenant or soft-delete filters.

Source

fn raw_exec(&self, sql: impl Into<String>) -> RawCommand

Creates a raw SQL command for statements that do not materialize rows.

Source

fn transaction<F, Fut, T>( &self, operation: F, ) -> impl Future<Output = Result<T, OrmError>> + Send
where F: FnOnce(Self) -> Fut + Send, Fut: Future<Output = Result<T, OrmError>> + Send, T: Send,

Executes an operation inside a transaction on a direct shared connection.

The closure receives a context bound to the same shared connection and runtime values. Returning Ok commits; returning Err rolls back. Contexts backed by a pool currently return an error because pooled transactions must pin one physical connection for the full closure.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§