use crate::traits::from_iter::FromIter;
use crate::traits::model::Model;
use crate::traits::save_data::SaveData;
use crate::traits::save_data::ValidateSaveData;
use std::ops::Deref;
use thiserror::Error;
use crate::search::SearchQuery;
pub enum TransactionOptions {
Commit,
Rollback,
}
#[derive(Error, Debug)]
pub enum DatabaseStrategyError {
#[error("Failed to migrate model: {0}")]
MigrateModel(String),
#[error("Failed to setup migration table: {0}")]
MigrationTable(String),
#[error("Failed to create transaction: {0}")]
Transaction(String),
#[error("Error: {0}")]
Error(String),
#[error("Failed to save Model <{}>: {}", .model, .err)]
SaveModel { err: String, model: &'static str },
#[error("Failed to search Model: {0}")]
SearchModel(String),
#[error("Failed to delete Model: {0}")]
DeleteModel(String),
#[error("Failed to parse constraints: {0}")]
ParseConstraint(String),
}
pub trait DatabaseStrategy: Send + Sync {
type ConnectionType<'a>: Sized + Deref<Target = Self::FunctionConnType<'a>>
where
Self: 'a;
type FunctionConnType<'a>: Sized
where
Self: 'a;
type TransactionType<'a>: Deref<Target = Self::FunctionConnType<'a>>
where
Self: 'a;
fn get_connection(&self) -> Self::ConnectionType<'_>;
fn with_transaction<F, T>(&self, function: F) -> Result<T, DatabaseStrategyError>
where
F: FnOnce(Self::TransactionType<'_>) -> T;
fn table_exists(
&self,
conn: &Self::FunctionConnType<'_>,
table_name: &str,
) -> Result<bool, DatabaseStrategyError>;
fn migrate_model<M: Model>(&self) -> Result<(), DatabaseStrategyError>;
fn setup_migration_table(
&self,
conn: &Self::FunctionConnType<'_>,
) -> Result<(), DatabaseStrategyError>;
fn on_migration_run(
&self,
conn: &Self::FunctionConnType<'_>,
table_name: &str,
index: i64,
) -> Result<(), DatabaseStrategyError>;
fn get_last_migration(
&self,
conn: &Self::FunctionConnType<'_>,
table_name: &str,
) -> Result<Option<i64>, DatabaseStrategyError>;
fn save_model<T>(
&self,
conn: &Self::FunctionConnType<'_>,
model: &mut T,
) -> Result<(), DatabaseStrategyError>
where
T: SaveData + ValidateSaveData + Model + FromIter;
fn search_single_model<T>(
&self,
conn: &Self::FunctionConnType<'_>,
query: SearchQuery,
) -> Result<Option<T>, DatabaseStrategyError>
where
T: Model + FromIter;
fn search_multiple_model<T>(
&self,
conn: &Self::FunctionConnType<'_>,
query: SearchQuery,
) -> Result<Vec<T>, DatabaseStrategyError>
where
T: Model + FromIter;
fn remove_model<T: Model>(
&self,
conn: &Self::FunctionConnType<'_>,
query: SearchQuery,
) -> Result<(), DatabaseStrategyError>;
fn manage_transaction(
&self,
conn: Self::TransactionType<'_>,
options: TransactionOptions,
) -> Result<(), DatabaseStrategyError>;
}