ood_persistence/src/bb8_postgres.rs

123 lines
3.4 KiB
Rust
Raw Permalink Normal View History

2021-10-17 15:08:46 +03:00
#[cfg(feature = "nightly")]
use crate::asyn::TransactionClient;
2021-10-12 18:14:02 +03:00
use crate::asyn::{ConnectionClient, PersistencePool};
pub use bb8::{Pool, PooledConnection};
pub use bb8_postgres::tokio_postgres;
pub use bb8_postgres::PostgresConnectionManager as Manager;
2021-10-17 23:12:55 +03:00
/// Inner connection of bb8 implementation.
2021-10-17 15:08:46 +03:00
pub type InnerConn<'p, M> = PooledConnection<'p, M>;
2021-10-17 23:12:55 +03:00
/// Inner connection of tokio postgres connection.
#[cfg(feature = "nightly")]
2021-10-17 15:08:46 +03:00
pub type InnerTrx<'p> = tokio_postgres::Transaction<'p>;
2021-10-17 23:12:55 +03:00
/// Alias for bb8 postgres no tls manager.
2021-10-12 18:14:02 +03:00
pub type NoTlsManager = Manager<tokio_postgres::NoTls>;
2021-10-17 23:12:55 +03:00
/// Alias for bb8 postgres no tls persistence.
2021-10-12 18:14:02 +03:00
pub type NoTlsPersistence<'p> = Persistence<'p, NoTlsManager>;
2021-10-17 23:12:55 +03:00
/// Alias for bb8 postgres no tls connection.
2021-10-12 18:14:02 +03:00
pub type NoTlsConnection<'p> = Connection<'p, NoTlsManager>;
2021-10-17 23:12:55 +03:00
/// Alias for bb8 postgres no tls inner connection.
2021-10-17 15:08:46 +03:00
pub type NoTlsInnerConn<'p> = InnerConn<'p, NoTlsManager>;
2021-10-17 23:12:55 +03:00
/// Alias for bb8 postgres no tls pool.
2021-10-12 18:14:02 +03:00
pub type NoTlsPool = Pool<NoTlsManager>;
2021-10-17 23:12:55 +03:00
/// It creates new persistence of bb8 postgres implementation.
2021-10-17 16:22:46 +03:00
#[must_use]
2021-10-12 18:14:02 +03:00
pub fn new<M>(pool: &Pool<M>) -> Persistence<M>
where
M: bb8::ManageConnection,
{
Persistence(pool)
}
2021-10-17 23:12:55 +03:00
/// Persistence wrap over bb8 pool.
2021-10-12 18:14:02 +03:00
#[derive(Clone)]
2021-10-17 15:08:46 +03:00
pub struct Persistence<'p, M>(&'p Pool<M>)
where
M: bb8::ManageConnection;
2021-10-12 18:14:02 +03:00
#[async_trait]
2021-10-17 15:08:46 +03:00
impl<'p> PersistencePool for NoTlsPersistence<'p> {
type Conn = NoTlsConnection<'p>;
2021-10-12 18:14:02 +03:00
async fn get_connection(&self) -> crate::Result<Self::Conn> {
2021-10-12 18:14:02 +03:00
self.0
.get()
.await
.map_err(|_| crate::Error::GetConnection)
2021-10-12 18:14:02 +03:00
.map(Connection)
}
}
2021-10-17 23:12:55 +03:00
/// Connection wrap over bb8 postgres inner connection.
2021-10-17 15:08:46 +03:00
pub struct Connection<'p, M>(InnerConn<'p, M>)
2021-10-12 18:14:02 +03:00
where
2021-10-17 15:08:46 +03:00
M: bb8::ManageConnection;
#[cfg_attr(feature = "nightly", async_trait)]
impl<'me> ConnectionClient for NoTlsConnection<'me> {
type InnerConn = NoTlsInnerConn<'me>;
#[cfg(feature = "nightly")]
type Trx<'t> = Transaction<'t>;
2021-10-12 18:14:02 +03:00
fn inner(&mut self) -> &mut Self::InnerConn {
&mut self.0
}
2021-10-17 15:08:46 +03:00
#[cfg(feature = "nightly")]
async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
2021-10-17 15:08:46 +03:00
self.0
.transaction()
.await
.map_err(|_| crate::Error::UpgradeToTransaction)
2021-10-17 15:08:46 +03:00
.map(Transaction)
}
}
/// Transaction wrap over `tokio_postgres` transaction.
2021-10-17 23:12:55 +03:00
///
/// **Note:** requires nightly rust channel and enabling the `nightly` feature.
2021-10-17 15:08:46 +03:00
#[cfg(feature = "nightly")]
pub struct Transaction<'p>(InnerTrx<'p>);
#[cfg(feature = "nightly")]
#[async_trait]
impl<'me> ConnectionClient for Transaction<'me> {
type InnerConn = InnerTrx<'me>;
type Trx<'t> = Transaction<'t>;
fn inner(&mut self) -> &mut Self::InnerConn {
&mut self.0
}
async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
2021-10-17 15:08:46 +03:00
self.0
.transaction()
.await
.map_err(|_| crate::Error::UpgradeToNestedTransaction)
2021-10-17 15:08:46 +03:00
.map(Transaction)
}
}
#[cfg(feature = "nightly")]
#[async_trait]
impl<'me> TransactionClient for Transaction<'me> {
async fn commit(self) -> crate::Result<()> {
2021-10-17 15:08:46 +03:00
self.0
.commit()
.await
.map_err(|_| crate::Error::CommitTransaction)
2021-10-17 15:08:46 +03:00
}
async fn rollback(self) -> crate::Result<()> {
2021-10-17 15:08:46 +03:00
self.0
.rollback()
.await
.map_err(|_| crate::Error::RollbackTransaction)
2021-10-17 15:08:46 +03:00
}
2021-10-12 18:14:02 +03:00
}