use sqlx::{Database, Executor};
pub trait HasColumns {
fn columns() -> Vec<&'static str>;
}
pub trait IntoRow<DB: Database>: HasColumns {
fn bind_arguments<'q>(self, arguments: &mut DB::Arguments<'q>) -> sqlx::Result<()>;
}
pub trait Schema<DB: Database>: HasColumns + Send {
type Id: Clone + Send + Sync;
fn table_name() -> &'static str;
fn id_column() -> &'static str;
fn id(&self) -> Self::Id;
}
pub trait BindRow<DB: Database>: Schema<DB> + IntoRow<DB> {}
impl<T, DB: Database> BindRow<DB> for T where T: Schema<DB> + IntoRow<DB> {}
pub trait DBAssignedId {}
pub trait ExternallyAssignedId {}
pub trait Crudly<DB: Database>: Sized {
type Id: Clone + Send + Sync;
type UpdateByIdResult;
type DeleteByIdResult;
fn find_all(
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = sqlx::Result<Vec<Self>>>;
fn select_by_id(
id: &Self::Id,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = sqlx::Result<Option<Self>>>;
fn id_exists(
id: &Self::Id,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = sqlx::Result<bool>>;
fn update_by_id(
entity: Self,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = Self::UpdateByIdResult>;
fn delete_by_id(
id: &Self::Id,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = Self::DeleteByIdResult>;
}
pub trait InsertReturningId<DB: Database> {
fn insert_returning_id(
self,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = sqlx::Result<i64>>;
}
pub trait InsertWithId<DB: Database> {
type Result;
fn insert_with_id(
self,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = Self::Result>;
}