use crate::database::{Database, HasStatementCache};
use crate::error::Error;
use crate::transaction::Transaction;
use futures_core::future::BoxFuture;
use futures_core::Future;
use log::LevelFilter;
use std::fmt::Debug;
use std::str::FromStr;
use std::time::Duration;
pub trait Connection: Send {
type Database: Database;
type Options: ConnectOptions<Connection = Self>;
fn close(self) -> BoxFuture<'static, Result<(), Error>>;
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
fn begin(&mut self) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
where
Self: Sized;
fn transaction<'c: 'f, 'f, T, E, F, Fut>(&'c mut self, f: F) -> BoxFuture<'f, Result<T, E>>
where
Self: Sized,
T: Send,
F: FnOnce(&mut <Self::Database as Database>::Connection) -> Fut + Send + 'f,
E: From<Error> + Send,
Fut: Future<Output = Result<T, E>> + Send,
{
Box::pin(async move {
let mut tx = self.begin().await?;
match f(&mut tx).await {
Ok(r) => {
tx.commit().await?;
Ok(r)
}
Err(e) => {
tx.rollback().await?;
Err(e)
}
}
})
}
fn cached_statements_size(&self) -> usize
where
Self::Database: HasStatementCache,
{
0
}
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
where
Self::Database: HasStatementCache,
{
Box::pin(async move { Ok(()) })
}
#[doc(hidden)]
fn flush(&mut self) -> BoxFuture<'_, Result<(), Error>>;
#[doc(hidden)]
fn should_flush(&self) -> bool;
#[inline]
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
where
Self: Sized,
{
let options = url.parse();
Box::pin(async move { Ok(Self::connect_with(&options?).await?) })
}
fn connect_with(options: &Self::Options) -> BoxFuture<'_, Result<Self, Error>>
where
Self: Sized,
{
options.connect()
}
}
#[derive(Clone, Debug)]
pub(crate) struct LogSettings {
pub(crate) statements_level: LevelFilter,
pub(crate) slow_statements_level: LevelFilter,
pub(crate) slow_statements_duration: Duration,
}
impl Default for LogSettings {
fn default() -> Self {
LogSettings {
statements_level: LevelFilter::Info,
slow_statements_level: LevelFilter::Warn,
slow_statements_duration: Duration::from_secs(1),
}
}
}
impl LogSettings {
pub(crate) fn log_statements(&mut self, level: LevelFilter) {
self.statements_level = level;
}
pub(crate) fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) {
self.slow_statements_level = level;
self.slow_statements_duration = duration;
}
}
pub trait ConnectOptions: 'static + Send + Sync + FromStr<Err = Error> + Debug {
type Connection: Connection + ?Sized;
fn connect(&self) -> BoxFuture<'_, Result<Self::Connection, Error>>
where
Self::Connection: Sized;
fn log_statements(&mut self, level: LevelFilter) -> &mut Self;
fn log_slow_statements(&mut self, level: LevelFilter, duration: Duration) -> &mut Self;
}