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>,
}
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:") {
DatabaseKind::Mssql
} else {
DatabaseKind::Sqlite
};
let pool = 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
};
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 = "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()
}
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(),
}
}
}