use sqlx::{AnyPool, any::install_default_drivers};
use std::sync::OnceLock;
pub use rust_eloquent_macros::Eloquent;
pub use async_trait::async_trait;
pub use sqlx;
pub use sqlx::FromRow;
static DB_POOL: OnceLock<AnyPool> = OnceLock::new();
static DB_DRIVER: OnceLock<String> = OnceLock::new();
#[derive(Clone, Debug)]
pub enum EloquentValue {
String(String),
Int(i32),
Float(f64),
Bool(bool),
}
impl From<&str> for EloquentValue {
fn from(s: &str) -> Self { EloquentValue::String(s.to_string()) }
}
impl From<String> for EloquentValue {
fn from(s: String) -> Self { EloquentValue::String(s) }
}
impl From<i32> for EloquentValue {
fn from(i: i32) -> Self { EloquentValue::Int(i) }
}
impl From<f64> for EloquentValue {
fn from(f: f64) -> Self { EloquentValue::Float(f) }
}
impl From<bool> for EloquentValue {
fn from(b: bool) -> Self { EloquentValue::Bool(b) }
}
pub struct Eloquent;
impl Eloquent {
pub async fn init(database_url: &str) -> Result<(), sqlx::Error> {
install_default_drivers();
let pool = AnyPool::connect(database_url).await?;
if DB_POOL.set(pool).is_err() {
panic!("Eloquent has already been initialized");
}
let driver = if database_url.starts_with("postgres") {
"postgres"
} else if database_url.starts_with("mysql") {
"mysql"
} else {
"sqlite"
};
let _ = DB_DRIVER.set(driver.to_string());
Ok(())
}
pub fn pool() -> &'static AnyPool {
DB_POOL.get().expect("Eloquent must be initialized before querying")
}
pub fn driver() -> &'static str {
DB_DRIVER.get().expect("Eloquent must be initialized before querying").as_str()
}
}
#[async_trait]
pub trait EloquentModel {
fn table_name() -> &'static str;
}