#[cfg(feature = "postgres")]
use deadpool_diesel::postgres::{Manager as PgManager, Pool as PgPool};
#[cfg(feature = "postgres")]
use diesel::PgConnection;
#[cfg(feature = "sqlite")]
use deadpool_diesel::sqlite::Pool as SqlitePool;
#[cfg(feature = "sqlite")]
use diesel::SqliteConnection;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackendType {
#[cfg(feature = "postgres")]
Postgres,
#[cfg(feature = "sqlite")]
Sqlite,
}
impl BackendType {
#[allow(unused_variables)]
pub fn from_url(url: &str) -> Self {
#[cfg(feature = "postgres")]
if url.starts_with("postgres://") || url.starts_with("postgresql://") {
return BackendType::Postgres;
}
#[cfg(feature = "sqlite")]
if url.starts_with("sqlite://")
|| url.starts_with("file:")
|| url.starts_with("/")
|| url.starts_with("./")
|| url.starts_with("../")
|| url == ":memory:"
|| url.ends_with(".db")
|| url.ends_with(".sqlite")
|| url.ends_with(".sqlite3")
{
return BackendType::Sqlite;
}
#[cfg(all(feature = "postgres", feature = "sqlite"))]
panic!(
"Unable to detect database backend from URL '{}'. \
Expected postgres://, postgresql://, sqlite://, or a file path.",
url
);
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
panic!(
"Unable to detect database backend from URL '{}'. \
Expected postgres:// or postgresql:// (sqlite feature not enabled).",
url
);
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
panic!(
"Unable to detect database backend from URL '{}'. \
Expected sqlite://, file path, or :memory: (postgres feature not enabled).",
url
);
#[cfg(not(any(feature = "postgres", feature = "sqlite")))]
panic!("No database backend enabled. Enable either 'postgres' or 'sqlite' feature.");
}
}
#[cfg(all(feature = "postgres", feature = "sqlite"))]
#[derive(diesel::MultiConnection)]
pub enum AnyConnection {
Postgres(PgConnection),
Sqlite(SqliteConnection),
}
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
pub type AnyConnection = PgConnection;
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
pub type AnyConnection = SqliteConnection;
#[cfg(all(feature = "postgres", feature = "sqlite"))]
#[derive(Clone)]
pub enum AnyPool {
Postgres(PgPool),
Sqlite(SqlitePool),
}
#[cfg(all(feature = "postgres", feature = "sqlite"))]
impl std::fmt::Debug for AnyPool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AnyPool::Postgres(_) => write!(f, "AnyPool::Postgres(...)"),
AnyPool::Sqlite(_) => write!(f, "AnyPool::Sqlite(...)"),
}
}
}
#[cfg(all(feature = "postgres", feature = "sqlite"))]
impl AnyPool {
pub fn as_postgres(&self) -> Option<&PgPool> {
match self {
AnyPool::Postgres(pool) => Some(pool),
_ => None,
}
}
pub fn as_sqlite(&self) -> Option<&SqlitePool> {
match self {
AnyPool::Sqlite(pool) => Some(pool),
_ => None,
}
}
pub fn expect_postgres(&self) -> &PgPool {
match self {
AnyPool::Postgres(pool) => pool,
_ => panic!("Expected PostgreSQL pool but got SQLite"),
}
}
pub fn expect_sqlite(&self) -> &SqlitePool {
match self {
AnyPool::Sqlite(pool) => pool,
_ => panic!("Expected SQLite pool but got PostgreSQL"),
}
}
pub fn close(&self) {
match self {
AnyPool::Postgres(pool) => pool.close(),
AnyPool::Sqlite(pool) => pool.close(),
}
}
}
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
pub type AnyPool = PgPool;
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
pub type AnyPool = SqlitePool;
#[cfg(feature = "postgres")]
pub type DbConnection = PgConnection;
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
pub type DbConnection = SqliteConnection;
#[cfg(feature = "postgres")]
pub type DbConnectionManager = PgManager;
#[cfg(feature = "postgres")]
pub type DbPool = PgPool;
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
pub type DbPool = SqlitePool;
#[macro_export]
macro_rules! dispatch_backend {
($backend:expr, $pg_branch:expr, $sqlite_branch:expr) => {{
#[cfg(all(feature = "postgres", feature = "sqlite"))]
{
match $backend {
$crate::database::BackendType::Postgres => $pg_branch,
$crate::database::BackendType::Sqlite => $sqlite_branch,
}
}
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
{
let _ = $backend; $pg_branch
}
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
{
let _ = $backend; $sqlite_branch
}
}};
}