use sqlx::{any::AnyPoolOptions, Any, Pool};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DatabaseKind {
Sqlite,
Postgres,
MySql,
Mssql,
}
#[derive(Clone)]
pub struct DatabasePool {
pub pool: Pool<Any>,
pub kind: DatabaseKind,
#[cfg(feature = "postgres")]
pub pg_pool: Option<sqlx::PgPool>,
#[cfg(feature = "sqlite")]
pub sqlite_pool: Option<sqlx::SqlitePool>,
#[cfg(feature = "mysql")]
pub mysql_pool: Option<sqlx::MySqlPool>,
#[cfg(feature = "mssql")]
pub mssql_pool: Option<bb8::Pool<bb8_tiberius::ConnectionManager>>,
}
pub type Db = DatabasePool;
impl DatabasePool {
pub async fn connect(database_url: &str) -> Result<Self, sqlx::Error> {
sqlx::any::install_default_drivers();
let kind = if database_url.starts_with("sqlite:") {
DatabaseKind::Sqlite
} else if database_url.starts_with("postgres:") || database_url.starts_with("postgresql:") {
DatabaseKind::Postgres
} else if database_url.starts_with("mysql:") {
DatabaseKind::MySql
} else if database_url.starts_with("mssql:")
|| database_url.starts_with("sqlserver:")
|| database_url.starts_with("jdbc:sqlserver:")
|| database_url.to_ascii_lowercase().starts_with("server=")
{
DatabaseKind::Mssql
} else {
DatabaseKind::Sqlite
};
let pool = if kind == DatabaseKind::Mssql {
AnyPoolOptions::new()
.max_connections(1)
.connect("sqlite::memory:")
.await
.unwrap_or_else(|_| AnyPoolOptions::new().connect_lazy("sqlite::memory:").unwrap())
} else {
AnyPoolOptions::new()
.max_connections(5)
.connect(database_url)
.await?
};
#[cfg(feature = "postgres")]
let pg_pool = if kind == DatabaseKind::Postgres {
sqlx::PgPool::connect(database_url).await.ok()
} else {
None
};
#[cfg(feature = "sqlite")]
let sqlite_pool = if kind == DatabaseKind::Sqlite {
sqlx::SqlitePool::connect(database_url).await.ok()
} else {
None
};
#[cfg(feature = "mysql")]
let mysql_pool = if kind == DatabaseKind::MySql {
sqlx::MySqlPool::connect(database_url).await.ok()
} else {
None
};
#[cfg(feature = "mssql")]
let mssql_pool = if kind == DatabaseKind::Mssql {
match Self::connect_mssql(database_url).await {
Ok(p) => {
println!(" 🚀 MSSQL Connection Pool (Tiberius TDS) connected successfully!");
Some(p)
}
Err(e) => {
eprintln!(" ⚠️ Gagal menghubungkan MSSQL Pool: {}", e);
None
}
}
} else {
None
};
println!("📦 Database connected: {:?}", kind);
Ok(Self {
pool,
kind,
#[cfg(feature = "postgres")]
pg_pool,
#[cfg(feature = "sqlite")]
sqlite_pool,
#[cfg(feature = "mysql")]
mysql_pool,
#[cfg(feature = "mssql")]
mssql_pool,
})
}
#[cfg(feature = "postgres")]
pub fn pg(&self) -> Option<&sqlx::PgPool> {
self.pg_pool.as_ref()
}
#[cfg(feature = "sqlite")]
pub fn sqlite(&self) -> Option<&sqlx::SqlitePool> {
self.sqlite_pool.as_ref()
}
#[cfg(feature = "mysql")]
pub fn mysql(&self) -> Option<&sqlx::MySqlPool> {
self.mysql_pool.as_ref()
}
#[cfg(feature = "mssql")]
pub fn mssql(&self) -> Option<&bb8::Pool<bb8_tiberius::ConnectionManager>> {
self.mssql_pool.as_ref()
}
#[cfg(feature = "mssql")]
pub async fn query_mssql_simple(&self, sql: &str) -> Result<Vec<tiberius::Row>, Box<dyn std::error::Error + Send + Sync>> {
if let Some(pool) = &self.mssql_pool {
let mut conn = pool.get().await?;
let stream = conn.simple_query(sql).await?;
let rows = stream.into_first_result().await?;
Ok(rows)
} else {
Err("MSSQL Pool belum diinisialisasi atau fitur mssql tidak aktif".into())
}
}
#[cfg(feature = "mssql")]
pub async fn connect_mssql(database_url: &str) -> Result<bb8::Pool<bb8_tiberius::ConnectionManager>, Box<dyn std::error::Error + Send + Sync>> {
let config = if database_url.starts_with("sqlserver://")
|| database_url.starts_with("mssql://")
|| database_url.starts_with("jdbc:sqlserver://")
{
let jdbc_str = if let Some(stripped) = database_url.strip_prefix("mssql://") {
format!("jdbc:sqlserver://{}", stripped)
} else if let Some(stripped) = database_url.strip_prefix("sqlserver://") {
format!("jdbc:sqlserver://{}", stripped)
} else {
database_url.to_string()
};
tiberius::Config::from_jdbc_string(&jdbc_str)
.or_else(|_| tiberius::Config::from_ado_string(database_url))?
} else {
tiberius::Config::from_ado_string(database_url)?
};
let mgr = bb8_tiberius::ConnectionManager::new(config);
let pool = bb8::Pool::builder()
.max_size(10)
.build(mgr)
.await?;
Ok(pool)
}
pub fn get_pool(&self) -> &Pool<Any> {
&self.pool
}
pub fn prepare_sql(&self, sql: &str) -> String {
match self.kind {
DatabaseKind::Postgres => {
let mut result = String::with_capacity(sql.len() + 16);
let mut idx = 1;
for ch in sql.chars() {
if ch == '?' {
result.push('$');
result.push_str(&idx.to_string());
idx += 1;
} else {
result.push(ch);
}
}
result
}
DatabaseKind::Mssql => {
let mut result = String::with_capacity(sql.len() + 16);
let mut idx = 1;
for ch in sql.chars() {
if ch == '?' {
result.push_str("@p");
result.push_str(&idx.to_string());
idx += 1;
} else {
result.push(ch);
}
}
result
}
_ => sql.to_string(),
}
}
}