#![allow(dead_code)]
use crate::db::{Database, DatabaseConfig, DbDriver};
use crate::error::Result;
use async_trait::async_trait;
use once_cell::sync::OnceCell;
use parking_lot::RwLock;
use std::sync::Arc;
static DB_INSTANCE: OnceCell<Arc<RwLock<Option<DatabaseConnection>>>> = OnceCell::new();
pub struct DatabaseConnection {
config: DatabaseConfig,
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
sql_pool: Option<sqlx::AnyPool>,
#[cfg(feature = "mongodb")]
mongo_client: Option<mongodb::Client>,
}
impl DatabaseConnection {
pub async fn new(config: DatabaseConfig) -> Result<Self> {
let mut conn = Self {
config: config.clone(),
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
sql_pool: None,
#[cfg(feature = "mongodb")]
mongo_client: None,
};
conn.establish_connection().await?;
Ok(conn)
}
async fn establish_connection(&mut self) -> Result<()> {
match self.config.driver {
#[cfg(feature = "sqlite")]
DbDriver::SQLite => {
}
#[cfg(feature = "mysql")]
DbDriver::MySQL => {
}
#[cfg(feature = "postgres")]
DbDriver::PostgreSQL => {
}
#[cfg(feature = "mongodb")]
DbDriver::MongoDB => {
}
_ => {}
}
Ok(())
}
pub fn config(&self) -> &DatabaseConfig {
&self.config
}
}
#[async_trait]
impl Database for DatabaseConnection {
async fn connect(config: &DatabaseConfig) -> Result<Self> {
DatabaseConnection::new(config.clone()).await
}
async fn disconnect(&self) -> Result<()> {
Ok(())
}
async fn is_connected(&self) -> bool {
true
}
}
pub async fn init_db(config: DatabaseConfig) -> Result<()> {
let conn = DatabaseConnection::new(config).await?;
DB_INSTANCE.get_or_init(|| Arc::new(RwLock::new(Some(conn))));
Ok(())
}
pub fn get_db() -> Option<Arc<RwLock<Option<DatabaseConnection>>>> {
DB_INSTANCE.get().cloned()
}