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 select_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(
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 InsertWithoutId<DB: Database>: Sized {
type InsertManyResult;
fn insert(
self,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = sqlx::Result<i64>>;
fn insert_many(
entities: Vec<Self>,
batch_size: usize,
executor: impl for<'e> Executor<'e, Database = DB> + Clone,
) -> impl Future<Output = Self::InsertManyResult>;
}
pub trait InsertWithId<DB: Database>: Sized {
type InsertResult;
fn insert(
self,
executor: impl for<'e> Executor<'e, Database = DB>,
) -> impl Future<Output = Self::InsertResult>;
fn insert_many(
entities: Vec<Self>,
batch_size: usize,
executor: impl for<'e> Executor<'e, Database = DB> + Clone,
) -> impl Future<Output = sqlx::Result<()>>;
}