use serde::Deserialize;
use crate::{Coin, Error};
#[cfg(feature = "mysql")]
use super::mysql::DbConfig as MySqlConfig;
#[cfg(feature = "postgres")]
use super::postgres::DbConfig as PostgresConfig;
#[cfg(feature = "sqlite")]
use super::sqlite::DbConfig as SqliteConfig;
use super::{Credentials, Database};
#[derive(Debug, PartialEq, Deserialize)]
#[serde(tag = "type")]
pub enum DbType {
#[cfg(feature = "mysql")]
#[cfg_attr(docsrs, doc(cfg(feature = "mysql")))]
#[serde(alias = "mysql", alias = "mariadb")]
MySql(MySqlConfig),
#[cfg(feature = "postgres")]
#[cfg_attr(docsrs, doc(cfg(feature = "postgres")))]
#[serde(alias = "postgres")]
Postgres(PostgresConfig),
#[cfg(feature = "sqlite")]
#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
#[serde(alias = "sqlite")]
Sqlite(SqliteConfig),
}
impl Database for DbType {
fn root_username(&self) -> Option<&str> {
match self {
#[cfg(feature = "mysql")]
Self::MySql(config) => config.root_username(),
#[cfg(feature = "sqlite")]
Self::Sqlite(config) => config.root_username(),
#[cfg(feature = "postgres")]
Self::Postgres(config) => config.root_username(),
}
}
fn requires_credentials(&self) -> bool {
match self {
#[cfg(feature = "mysql")]
Self::MySql(config) => config.requires_credentials(),
#[cfg(feature = "sqlite")]
Self::Sqlite(config) => config.requires_credentials(),
#[cfg(feature = "postgres")]
Self::Postgres(config) => config.requires_credentials(),
}
}
async fn init_schema(
&mut self,
creds: Option<Credentials>,
coins: &[Coin],
) -> Result<(), Error> {
match self {
#[cfg(feature = "mysql")]
Self::MySql(config) => config.init_schema(creds, coins).await,
#[cfg(feature = "sqlite")]
Self::Sqlite(config) => config.init_schema(creds, coins).await,
#[cfg(feature = "postgres")]
Self::Postgres(config) => config.init_schema(creds, coins).await,
}
}
async fn drop_schema(
&mut self,
creds: Option<Credentials>,
coins: Option<&[Coin]>,
) -> Result<(), Error> {
match self {
#[cfg(feature = "mysql")]
Self::MySql(config) => config.drop_schema(creds, coins).await,
#[cfg(feature = "sqlite")]
Self::Sqlite(config) => config.drop_schema(creds, coins).await,
#[cfg(feature = "postgres")]
Self::Postgres(config) => config.drop_schema(creds, coins).await,
}
}
}