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§
Sourcefn database_type(&self) -> DatabaseType
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.
Sourcefn 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 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,
Sourcefn 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<'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,
Sourcefn 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 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,
Sourcefn ping<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = DMSCResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: '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,
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.
Sourcefn is_connected(&self) -> bool
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.
Sourcefn close<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = DMSCResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
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.
Sourcefn 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 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,
Sourcefn 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 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,
Sourcefn 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,
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§
Sourcefn 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_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.
Sourcefn 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,
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.