mod builder;
mod connect;
mod connection;
mod executor;
mod pool;
mod tx;
pub use builder::Builder;
pub use connect::Connect;
pub use connection::Connection;
pub use executor::Executor;
pub use pool::{Pool, PoolConfig, PoolStatus, Timeouts};
pub use toasty_core::driver::{Capability, Driver};
pub use tx::{Transaction, TransactionBuilder};
pub use toasty_core::driver::ExecResponse;
pub(crate) use pool::ConnectionOperation;
pub(crate) use tx::ConnRef;
use crate::{Result, engine::Engine};
use async_trait::async_trait;
use toasty_core::{Schema, stmt};
use std::sync::Arc;
pub(crate) struct Shared {
pub(crate) engine: Engine,
pub(crate) pool: Pool,
}
pub struct Db {
shared: Arc<Shared>,
}
impl Clone for Db {
fn clone(&self) -> Self {
Db {
shared: self.shared.clone(),
}
}
}
impl Db {
pub fn builder() -> Builder {
Builder::default()
}
pub async fn connection(&self) -> Result<Connection> {
self.shared.pool.get(self.shared.clone()).await
}
pub(crate) async fn exec_stmt(
&self,
stmt: stmt::Statement,
in_transaction: bool,
) -> Result<ExecResponse> {
let conn = self.connection().await?;
conn.exec_stmt(stmt, in_transaction).await
}
pub async fn push_schema(&self) -> Result<()> {
let conn = self.connection().await?;
conn.push_schema().await
}
pub async fn reset_db(&self) -> Result<()> {
self.shared.pool.driver().reset_db().await
}
pub fn driver(&self) -> &dyn Driver {
self.shared.pool.driver()
}
pub fn schema(&self) -> &Arc<Schema> {
&self.shared.engine.schema
}
pub fn capability(&self) -> &Capability {
self.shared.engine.capability()
}
pub async fn transaction(&mut self) -> Result<Transaction<'_>> {
<Self as Executor>::transaction(self).await
}
pub fn transaction_builder(&mut self) -> TransactionBuilder<'_> {
TransactionBuilder::new(tx::TxSource::Db(self))
}
#[doc(hidden)]
pub fn pool(&self) -> &Pool {
&self.shared.pool
}
}
impl std::fmt::Debug for Db {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Db")
.field("engine", &self.shared.engine)
.finish()
}
}
#[async_trait]
impl Executor for Db {
async fn transaction(&mut self) -> Result<Transaction<'_>> {
let conn = self.connection().await?;
Transaction::begin(ConnRef::owned(conn)).await
}
async fn exec_untyped(&mut self, stmt: stmt::Statement) -> Result<ExecResponse> {
self.exec_stmt(stmt, false).await
}
fn schema(&mut self) -> &Arc<Schema> {
Db::schema(self)
}
}