use std::marker::PhantomData;
#[cfg(feature = "sqlite")]
use std::sync::Arc;
#[cfg(feature = "duckdb")]
use std::sync::Arc as DuckDbArc;
pub enum StreamConnection<'a> {
#[cfg(feature = "sqlite")]
Sqlite(Arc<turso::Connection>),
#[cfg(feature = "duckdb")]
DuckDB(DuckDbArc<super::super::duckdb_backend::duckcompat::Connection>),
#[cfg(feature = "postgresql")]
PostgreSQL(&'a tokio_postgres::Client),
#[doc(hidden)]
__Lifetime(PhantomData<&'a ()>),
}
impl<'a> StreamConnection<'a> {
#[cfg(feature = "sqlite")]
pub fn expect_sqlite(&self) -> crate::Result<&Arc<turso::Connection>> {
match self {
StreamConnection::Sqlite(conn) => Ok(conn),
_ => Err(crate::ormer_error!(
"expected a SQLite StreamConnection, got {:?}",
self.db_kind()
)),
}
}
#[cfg(feature = "postgresql")]
pub fn expect_postgresql(&self) -> crate::Result<&&'a tokio_postgres::Client> {
match self {
StreamConnection::PostgreSQL(client) => Ok(client),
_ => Err(crate::ormer_error!(
"expected a PostgreSQL StreamConnection, got {:?}",
self.db_kind()
)),
}
}
#[cfg(feature = "duckdb")]
pub fn expect_duckdb(
&self,
) -> crate::Result<&DuckDbArc<super::super::duckdb_backend::duckcompat::Connection>> {
match self {
StreamConnection::DuckDB(conn) => Ok(conn),
_ => Err(crate::ormer_error!(
"expected a DuckDB StreamConnection, got {:?}",
self.db_kind()
)),
}
}
#[cfg_attr(
not(any(feature = "postgresql", feature = "duckdb", feature = "sqlite")),
allow(dead_code)
)]
fn db_kind(&self) -> &'static str {
match self {
#[cfg(feature = "sqlite")]
StreamConnection::Sqlite(_) => "sqlite",
#[cfg(feature = "duckdb")]
StreamConnection::DuckDB(_) => "duckdb",
#[cfg(feature = "postgresql")]
StreamConnection::PostgreSQL(_) => "postgresql",
StreamConnection::__Lifetime(_) => "(lifetime placeholder)",
}
}
}