use crate::{Driver, Error, Executor, Result};
use anyhow::Context;
use convert_case::{Case, Casing};
use std::{
borrow::Cow,
future::{self, Future},
};
use url::Url;
pub trait Connection: Executor {
fn sanitize_url(driver: &Self::Driver, mut url: Cow<'static, str>) -> Result<Url>
where
Self: Sized,
{
let mut in_memory = false;
if let Some((scheme, host)) = url.split_once("://")
&& host.starts_with(":memory:")
{
url = format!("{scheme}://localhost{}", &host[8..]).into();
in_memory = true;
}
let context = || {
format!(
"While trying to connect to {}",
driver.name().to_case(Case::Title)
)
};
let mut result = Url::parse(&url).with_context(context)?;
if in_memory {
result.query_pairs_mut().append_pair("mode", "memory");
}
let names = <Self::Driver as Driver>::NAME;
'prefix: {
for name in names {
let prefix = format!("{}://", name);
if url.starts_with(&prefix) {
break 'prefix prefix;
}
}
let error = Error::msg(format!(
"Connection URL must start with: {}",
names.join(", ")
))
.context(context());
log::error!("{:#}", error);
return Err(error);
};
Ok(result)
}
fn connect(
driver: &Self::Driver,
url: Cow<'static, str>,
) -> impl Future<Output = Result<<Self::Driver as Driver>::Connection>> + Send
where
Self: Sized;
fn begin(&mut self) -> impl Future<Output = Result<<Self::Driver as Driver>::Transaction<'_>>>;
fn disconnect(self) -> impl Future<Output = Result<()>>
where
Self: Sized,
{
future::ready(Ok(()))
}
}
impl<S: Connection> Connection for &mut S {
fn connect(
driver: &Self::Driver,
url: Cow<'static, str>,
) -> impl Future<Output = Result<<Self::Driver as Driver>::Connection>> + Send
where
Self: Sized,
{
S::connect(driver, url)
}
fn begin(&mut self) -> impl Future<Output = Result<<Self::Driver as Driver>::Transaction<'_>>> {
(**self).begin()
}
fn disconnect(self) -> impl Future<Output = Result<()>>
where
Self: Sized,
{
future::ready(Err(Error::msg(
"Cannot disconnect using `&mut Connection`, use the actual object.",
)))
}
}