mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-20 22:24:54 +01:00
Introduce cdk-sql-common
The primary purpose of this new crate is to have a common and shared codebase for all SQL storage systems. It would force us to write standard SQL using best practices for all databases. This crate has been extracted from #878
This commit is contained in:
53
crates/cdk-sql-common/src/database.rs
Normal file
53
crates/cdk-sql-common/src/database.rs
Normal file
@@ -0,0 +1,53 @@
|
||||
//! Database traits definition
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use cdk_common::database::Error;
|
||||
|
||||
use crate::stmt::{Column, Statement};
|
||||
|
||||
/// Database Executor
|
||||
///
|
||||
/// This trait defines the expectations of a database execution
|
||||
#[async_trait::async_trait]
|
||||
pub trait DatabaseExecutor: Debug + Sync + Send {
|
||||
/// Database driver name
|
||||
fn name() -> &'static str;
|
||||
|
||||
/// Executes a query and returns the affected rows
|
||||
async fn execute(&self, statement: Statement) -> Result<usize, Error>;
|
||||
|
||||
/// Runs the query and returns the first row or None
|
||||
async fn fetch_one(&self, statement: Statement) -> Result<Option<Vec<Column>>, Error>;
|
||||
|
||||
/// Runs the query and returns the first row or None
|
||||
async fn fetch_all(&self, statement: Statement) -> Result<Vec<Vec<Column>>, Error>;
|
||||
|
||||
/// Fetches the first row and column from a query
|
||||
async fn pluck(&self, statement: Statement) -> Result<Option<Column>, Error>;
|
||||
|
||||
/// Batch execution
|
||||
async fn batch(&self, statement: Statement) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
/// Database transaction trait
|
||||
#[async_trait::async_trait]
|
||||
pub trait DatabaseTransaction<'a>: Debug + DatabaseExecutor + Send + Sync {
|
||||
/// Consumes the current transaction committing the changes
|
||||
async fn commit(self) -> Result<(), Error>;
|
||||
|
||||
/// Consumes the transaction rolling back all changes
|
||||
async fn rollback(self) -> Result<(), Error>;
|
||||
}
|
||||
|
||||
/// Database connector
|
||||
#[async_trait::async_trait]
|
||||
pub trait DatabaseConnector: Debug + DatabaseExecutor + Send + Sync {
|
||||
/// Transaction type for this database connection
|
||||
type Transaction<'a>: DatabaseTransaction<'a>
|
||||
where
|
||||
Self: 'a;
|
||||
|
||||
/// Begin a new transaction
|
||||
async fn begin(&self) -> Result<Self::Transaction<'_>, Error>;
|
||||
}
|
||||
Reference in New Issue
Block a user