2021-10-12 18:14:02 +03:00
|
|
|
use crate::error;
|
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// The pool is one of the main features to be realized in persistence.
|
|
|
|
///
|
|
|
|
/// Any implementation (database, file storage, memory or others) must be able to get a connection
|
|
|
|
/// to manipulate it afterwards.
|
2021-10-12 18:14:02 +03:00
|
|
|
#[async_trait]
|
|
|
|
pub trait PersistencePool: Send + Sync {
|
2021-10-17 23:12:55 +03:00
|
|
|
/// ConnectionClient implementation that persistence can can return.
|
2021-10-12 18:14:02 +03:00
|
|
|
type Conn: ConnectionClient;
|
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Returns new connection.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns PersistenceError if pool cannot get a connection.
|
2021-10-12 18:14:02 +03:00
|
|
|
async fn get_connection(&self) -> error::Result<Self::Conn>;
|
|
|
|
}
|
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Connection client knows about the inner connection, and also knows how to create transactions.
|
2021-10-17 15:08:46 +03:00
|
|
|
#[cfg_attr(feature = "nightly", async_trait)]
|
2021-10-12 18:14:02 +03:00
|
|
|
pub trait ConnectionClient {
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Inner connection
|
2021-10-12 18:14:02 +03:00
|
|
|
type InnerConn;
|
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// TransactionClient implementation in which the connection can be updated.
|
|
|
|
///
|
|
|
|
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
|
2021-10-17 15:08:46 +03:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
type Trx<'t>: TransactionClient;
|
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Returns inner connection
|
2021-10-12 18:14:02 +03:00
|
|
|
fn inner(&mut self) -> &mut Self::InnerConn;
|
2021-10-17 15:08:46 +03:00
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Updates connection to transaction.
|
|
|
|
///
|
|
|
|
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns PersistenceError if connection cannot update to transaction.
|
2021-10-17 15:08:46 +03:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
async fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>>;
|
|
|
|
}
|
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Transaction client is updated connection client that can additionally commit and rollback data
|
|
|
|
/// in transactions.
|
|
|
|
///
|
|
|
|
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
|
2021-10-17 15:08:46 +03:00
|
|
|
#[cfg(feature = "nightly")]
|
|
|
|
#[async_trait]
|
|
|
|
pub trait TransactionClient: ConnectionClient {
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Consumes the transaction, committing all changes made within it.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns PersistenceError if transaction cannot commit
|
2021-10-17 15:08:46 +03:00
|
|
|
async fn commit(self) -> error::Result<()>;
|
|
|
|
|
2021-10-17 23:12:55 +03:00
|
|
|
/// Rolls the transaction back, discarding all changes made within it.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
///
|
|
|
|
/// Returns PersistenceError if transaction cannot rolls back.
|
2021-10-17 15:08:46 +03:00
|
|
|
async fn rollback(self) -> error::Result<()>;
|
2021-10-12 18:14:02 +03:00
|
|
|
}
|