DMSCDatabase

Trait DMSCDatabase 

Source
pub trait DMSCDatabase: Send + Sync {
    // Required methods
    fn database_type(&self) -> DatabaseType;
    fn execute<'life0, 'life1, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<u64>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn query<'life0, 'life1, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<DMSCDBResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn query_one<'life0, 'life1, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<Option<DMSCDBRow>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
    fn ping<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<bool>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn is_connected(&self) -> bool;
    fn close<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
    fn execute_with_params<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
        params: &'life2 [Value],
    ) -> Pin<Box<dyn Future<Output = DMSCResult<u64>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn query_with_params<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
        params: &'life2 [Value],
    ) -> Pin<Box<dyn Future<Output = DMSCResult<DMSCDBResult>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait;
    fn transaction<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = DMSCResult<Box<dyn DMSCDatabaseTransaction>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn batch_execute<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
        params: &'life2 [Vec<Value>],
    ) -> Pin<Box<dyn Future<Output = DMSCResult<Vec<u64>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
    fn batch_query<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        sql: &'life1 str,
        params: &'life2 [Vec<Value>],
    ) -> Pin<Box<dyn Future<Output = DMSCResult<Vec<DMSCDBResult>>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait { ... }
}
Expand description

Trait defining the database interface for all supported databases.

This trait provides a unified API for database operations across different database backends. Implementations handle backend-specific details while presenting a consistent interface to callers.

§Supported Operations

  • Execution: Execute SQL statements without returning results
  • Querying: Execute SELECT statements and iterate over results
  • Parameter Binding: Safe SQL parameter binding to prevent injection
  • Batch Operations: Efficient bulk execution and querying
  • Transactions: ACID-compliant transaction support

§Example

use dmsc::database::{DMSCDatabase, DMSCDatabasePool, DMSCDatabaseConfig};

#[tokio::main]
async fn main() -> DMSCResult<()> {
    let config = DMSCDatabaseConfig::postgres()
        .host("localhost")
        .database("mydb")
        .build();

    let pool = DMSCDatabasePool::new(config).await?;
    let db = pool.get().await?;

    // Execute a statement
    db.execute("CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name TEXT)").await?;

    // Query with parameters
    let rows = db.query_with_params(
        "SELECT * FROM users WHERE name = $1",
        &[serde_json::json!("alice")]
    ).await?;

    Ok(())
}

Required Methods§

Source

fn database_type(&self) -> DatabaseType

Returns the type of database this instance connects to.

This can be used to identify which database backend is in use and potentially branch behavior based on database type.

Source

fn execute<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = DMSCResult<u64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Executes a SQL statement without returning results.

This method is suitable for INSERT, UPDATE, DELETE, and DDL statements. Returns the number of rows affected.

§Parameters
  • sql: The SQL statement to execute
§Returns

The number of rows affected, or an error if execution fails.

Source

fn query<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = DMSCResult<DMSCDBResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Executes a SQL query and returns all matching rows.

This method is suitable for SELECT statements. The returned result can be iterated over to access individual rows.

§Parameters
  • sql: The SQL query to execute
§Returns

A result set containing all matching rows, or an error if the query fails.

Source

fn query_one<'life0, 'life1, 'async_trait>( &'life0 self, sql: &'life1 str, ) -> Pin<Box<dyn Future<Output = DMSCResult<Option<DMSCDBRow>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Executes a SQL query and returns at most one row.

This method is equivalent to adding “LIMIT 1” to the query. Returns None if no rows match.

§Parameters
  • sql: The SQL query to execute
§Returns

Some(row) if a row was found, None if no rows match, or an error.

Source

fn ping<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = DMSCResult<bool>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Checks if the database connection is alive.

This method can be used to verify that the connection is still responsive before executing important operations.

§Returns

true if the connection is healthy, false otherwise.

Source

fn is_connected(&self) -> bool

Checks if the database connection is currently connected.

Unlike ping(), this method does not perform any network operation.

§Returns

true if connected, false otherwise.

Source

fn close<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = DMSCResult<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Closes the database connection.

This method should be called when the connection is no longer needed to release resources. After calling this method, the connection should not be used.

Source

fn execute_with_params<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [Value], ) -> Pin<Box<dyn Future<Output = DMSCResult<u64>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Executes a SQL statement with parameters.

Parameters are bound using placeholder syntax ($1, $2, etc.) to prevent SQL injection attacks.

§Parameters
  • sql: The SQL statement with placeholders
  • params: The parameter values to bind
§Returns

The number of rows affected.

Source

fn query_with_params<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [Value], ) -> Pin<Box<dyn Future<Output = DMSCResult<DMSCDBResult>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Executes a SQL query with parameters.

Parameters are bound using placeholder syntax ($1, $2, etc.) to prevent SQL injection attacks.

§Parameters
  • sql: The SQL query with placeholders
  • params: The parameter values to bind
§Returns

A result set containing all matching rows.

Source

fn transaction<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = DMSCResult<Box<dyn DMSCDatabaseTransaction>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Starts a new database transaction.

Transactions allow multiple operations to be grouped together with atomic commit/rollback semantics.

§Returns

A transaction handle that can be used to execute operations within the transaction.

Provided Methods§

Source

fn batch_execute<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [Vec<Value>], ) -> Pin<Box<dyn Future<Output = DMSCResult<Vec<u64>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Executes the same SQL statement multiple times with different parameters.

This is more efficient than executing statements individually when performing bulk operations.

§Parameters
  • sql: The SQL statement with placeholders ($1, $2, etc.)
  • params: A slice of parameter sets, one for each execution
§Returns

A vector of affected row counts, one for each execution.

Source

fn batch_query<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, sql: &'life1 str, params: &'life2 [Vec<Value>], ) -> Pin<Box<dyn Future<Output = DMSCResult<Vec<DMSCDBResult>>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Executes the same query multiple times with different parameters.

This is more efficient than executing queries individually when performing bulk reads.

§Parameters
  • sql: The SQL query with placeholders ($1, $2, etc.)
  • params: A slice of parameter sets, one for each query
§Returns

A vector of result sets, one for each query.

Implementors§