Skip to content

Commit

Permalink
switch model and relationship away from async_trait
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Dec 13, 2023
1 parent da5e553 commit f2952a5
Show file tree
Hide file tree
Showing 30 changed files with 4,783 additions and 4,768 deletions.
116 changes: 58 additions & 58 deletions ensemble/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ static DB_POOL: OnceLock<RBatis> = OnceLock::new();

#[derive(Debug, thiserror::Error)]
pub enum SetupError {
#[error("The provided database URL is invalid.")]
UrlError(#[from] rbatis::Error),
#[error("The provided database URL is invalid.")]
UrlError(#[from] rbatis::Error),

#[cfg(any(feature = "mysql", feature = "postgres"))]
#[error("The database pool has already been initialized.")]
AlreadyInitialized,
#[cfg(any(feature = "mysql", feature = "postgres"))]
#[error("The database pool has already been initialized.")]
AlreadyInitialized,
}

/// Sets up the database pool.
Expand All @@ -28,44 +28,44 @@ pub enum SetupError {
/// Returns an error if the database pool has already been initialized, or if the provided database URL is invalid.
#[cfg(any(feature = "mysql", feature = "postgres"))]
pub fn setup(database_url: &str) -> Result<(), SetupError> {
let rb = RBatis::new();

#[cfg(feature = "mysql")]
tracing::info!(
database_url = database_url,
"Setting up MySQL database pool..."
);
#[cfg(feature = "postgres")]
tracing::info!(
database_url = database_url,
"Setting up PostgreSQL database pool..."
);

#[cfg(feature = "mysql")]
rb.init_option::<MysqlDriver, MySqlConnectOptions, DefaultPool>(
MysqlDriver {},
MySqlConnectOptions::from_str(database_url)?,
)?;
#[cfg(feature = "postgres")]
rb.init_option::<PgDriver, PgConnectOptions, DefaultPool>(
PgDriver {},
PgConnectOptions::from_str(database_url)?,
)?;

DB_POOL
.set(rb)
.map_err(|_| SetupError::AlreadyInitialized)?;

Ok(())
let rb = RBatis::new();

#[cfg(feature = "mysql")]
tracing::info!(
database_url = database_url,
"Setting up MySQL database pool..."
);
#[cfg(feature = "postgres")]
tracing::info!(
database_url = database_url,
"Setting up PostgreSQL database pool..."
);

#[cfg(feature = "mysql")]
rb.init_option::<MysqlDriver, MySqlConnectOptions, DefaultPool>(
MysqlDriver {},
MySqlConnectOptions::from_str(database_url)?,
)?;
#[cfg(feature = "postgres")]
rb.init_option::<PgDriver, PgConnectOptions, DefaultPool>(
PgDriver {},
PgConnectOptions::from_str(database_url)?,
)?;

DB_POOL
.set(rb)
.map_err(|_| SetupError::AlreadyInitialized)?;

Ok(())
}

#[derive(Debug, thiserror::Error)]
pub enum ConnectError {
#[error("The database pool has not been initialized.")]
NotInitialized,
#[error("The database pool has not been initialized.")]
NotInitialized,

#[error("An error occurred while connecting to the database.")]
Connection(#[from] rbatis::Error),
#[error("An error occurred while connecting to the database.")]
Connection(#[from] rbatis::Error),
}

/// Returns a connection to the database. Used internally by `ensemble` models.
Expand All @@ -74,37 +74,37 @@ pub enum ConnectError {
///
/// Returns an error if the database pool has not been initialized, or if an error occurs while connecting to the database.
pub async fn get() -> Result<Connection, ConnectError> {
match DB_POOL.get() {
None => Err(ConnectError::NotInitialized),
Some(rb) => Ok(rb.get_pool()?.get().await?),
}
match DB_POOL.get() {
None => Err(ConnectError::NotInitialized),
Some(rb) => Ok(rb.get_pool()?.get().await?),
}
}

#[cfg(any(feature = "mysql", feature = "postgres"))]
pub enum Database {
MySQL,
PostgreSQL,
MySQL,
PostgreSQL,
}

#[cfg(any(feature = "mysql", feature = "postgres"))]
impl Database {
pub const fn is_mysql(&self) -> bool {
matches!(self, Self::MySQL)
}
pub const fn is_mysql(&self) -> bool {
matches!(self, Self::MySQL)
}

pub const fn is_postgres(&self) -> bool {
matches!(self, Self::PostgreSQL)
}
pub const fn is_postgres(&self) -> bool {
matches!(self, Self::PostgreSQL)
}
}

#[cfg(any(feature = "mysql", feature = "postgres"))]
pub const fn which_db() -> Database {
#[cfg(all(feature = "mysql", feature = "postgres"))]
panic!("Both the `mysql` and `postgres` features are enabled. Please enable only one of them.");

if cfg!(feature = "mysql") {
Database::MySQL
} else {
Database::PostgreSQL
}
#[cfg(all(feature = "mysql", feature = "postgres"))]
panic!("Both the `mysql` and `postgres` features are enabled. Please enable only one of them.");

if cfg!(feature = "mysql") {
Database::MySQL
} else {
Database::PostgreSQL
}
}
Loading

0 comments on commit f2952a5

Please sign in to comment.