Skip to main content

DatabaseBackend

Trait DatabaseBackend 

Source
pub trait DatabaseBackend:
    Send
    + Sync
    + 'static {
    // Required methods
    fn connect<'life0, 'async_trait>(
        uri: &'life0 str,
        pool_size: u32,
        acquire_timeout_secs: u64,
        max_lifetime_secs: u64,
        idle_timeout_secs: u64,
    ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
       where Self: Sized + 'async_trait,
             'life0: 'async_trait;
    fn version<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<DbVersion, Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn min_version(&self) -> (u32, u32);
    fn exec_raw<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
        params: &'life2 [SqlParam],
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn exec_statement<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
        params: &'life2 [SqlParam],
    ) -> Pin<Box<dyn Future<Output = Result<StatementResult, Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn exec_in_transaction<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
        &'life0 self,
        tx_vars: Option<&'life1 SqlBuilder>,
        pre_req: Option<&'life2 SqlBuilder>,
        mutation: Option<&'life3 SqlBuilder>,
        main: Option<&'life4 SqlBuilder>,
    ) -> Pin<Box<dyn Future<Output = Result<StatementResult, Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             'life4: 'async_trait;
    fn introspector(&self) -> Box<dyn DbIntrospector + '_>;
    fn start_listener<'life0, 'life1, 'async_trait>(
        &'life0 self,
        channel: &'life1 str,
        cancel: Receiver<bool>,
        on_event: Arc<dyn Fn(String) + Send + Sync>,
    ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn map_error(&self, err: Box<dyn Error + Send + Sync>) -> Error;

    // Provided method
    fn pool_status(&self) -> Option<PoolStatus> { ... }
}
Expand description

Core trait that a database backend must implement.

Covers connection management, query execution, introspection, version detection, error mapping, and change notification.

Required Methods§

Source

fn connect<'life0, 'async_trait>( uri: &'life0 str, pool_size: u32, acquire_timeout_secs: u64, max_lifetime_secs: u64, idle_timeout_secs: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, Error>> + Send + 'async_trait>>
where Self: Sized + 'async_trait, 'life0: 'async_trait,

Connect to the database and return a backend instance.

The implementation should create a connection pool internally.

Source

fn version<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<DbVersion, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Query the database server version.

Source

fn min_version(&self) -> (u32, u32)

Return the minimum supported version for this backend.

Source

fn exec_raw<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [SqlParam], ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Execute a raw SQL statement (no result set expected).

Used for session variable setup and pre-request function calls.

Source

fn exec_statement<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [SqlParam], ) -> Pin<Box<dyn Future<Output = Result<StatementResult, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Execute a CTE-wrapped statement and parse the standard result set.

The query is expected to return columns: total_result_set, page_total, body, response_headers, response_status

Source

fn exec_in_transaction<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>( &'life0 self, tx_vars: Option<&'life1 SqlBuilder>, pre_req: Option<&'life2 SqlBuilder>, mutation: Option<&'life3 SqlBuilder>, main: Option<&'life4 SqlBuilder>, ) -> Pin<Box<dyn Future<Output = Result<StatementResult, Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, 'life4: 'async_trait,

Begin a transaction and execute multiple statements within it.

Runs tx_vars, pre_req, mutation, and main query in order within a single transaction. Returns the result from the main query.

mutation is only set for backends that don’t support DML in CTEs. When set, the executor must run the mutation, capture RETURNING rows into a temp table _dbrst_mut, then run main which aggregates from it.

Source

fn introspector(&self) -> Box<dyn DbIntrospector + '_>

Get a database introspector for schema cache loading.

Source

fn start_listener<'life0, 'life1, 'async_trait>( &'life0 self, channel: &'life1 str, cancel: Receiver<bool>, on_event: Arc<dyn Fn(String) + Send + Sync>, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Start a background change listener (e.g. PostgreSQL NOTIFY).

Returns None if the backend does not support change notifications. The listener should call on_schema_reload when a schema reload is requested and on_config_reload when a config reload is requested.

Source

fn map_error(&self, err: Box<dyn Error + Send + Sync>) -> Error

Map a backend-specific database error into our Error type.

Provided Methods§

Source

fn pool_status(&self) -> Option<PoolStatus>

Return current connection pool status for metrics reporting.

Returns None if the backend does not track pool statistics.

Implementors§