use crate::sql::reusable_executor::ReusableExecutor;
use sqlx::{Database, Executor};
pub trait HasColumns {
fn columns() -> Vec<&'static str>;
}
pub trait IntoRow<DB: Database>: HasColumns {
fn bind_arguments(self, arguments: &mut DB::Arguments) -> sqlx::Result<()>;
}
pub trait HasId {
type Id: Clone + Send + Sync;
fn id_column() -> &'static str;
fn id(&self) -> Self::Id;
}
pub trait Schema: Send {
fn table_name() -> &'static str;
fn columns() -> Vec<&'static str>;
}
pub trait DBAssignedId: HasId {}
pub trait ExternallyAssignedId: HasId {}
pub trait NoId: Send {}
pub trait CrudlyDefault {}
pub trait SelectAll<DB: Database>: Sized {
fn select_all<'c, E>(executor: E) -> impl Future<Output = sqlx::Result<Vec<Self>>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait DeleteAll<DB: Database>: Sized {
fn delete_all<'c, E>(executor: E) -> impl Future<Output = sqlx::Result<()>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait SelectById<DB: Database>: Sized + HasId {
fn select_by_id<'c, E>(
id: &Self::Id,
executor: E,
) -> impl Future<Output = sqlx::Result<Option<Self>>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait SelectByIds<DB: Database>: Sized + HasId {
fn select_by_ids<'c, E>(
ids: Vec<Self::Id>,
batch_size: usize,
executor: E,
) -> impl Future<Output = sqlx::Result<Vec<Self>>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait IdExists<DB: Database>: Sized + HasId {
fn id_exists<'c, E>(
id: &Self::Id,
executor: E,
) -> impl Future<Output = sqlx::Result<bool>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait UpdateById<DB: Database>: Sized + HasId {
fn update_by_id<'c, E>(self, executor: E) -> impl Future<Output = sqlx::Result<bool>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait DeleteById<DB: Database>: Sized + HasId {
fn delete_by_id<'c, E>(
id: &Self::Id,
executor: E,
) -> impl Future<Output = sqlx::Result<bool>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait InsertWithoutId<DB: Database>: Sized {
fn insert<'c, E>(self, executor: E) -> impl Future<Output = sqlx::Result<i64>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait InsertManyWithoutIds<DB: Database>: Sized {
fn insert_many<E>(
entities: Vec<Self>,
batch_size: usize,
executor: E,
) -> impl Future<Output = sqlx::Result<()>> + Send
where
E: ReusableExecutor<DB> + Send;
}
pub trait Insert<DB: Database>: Sized {
fn insert<'c, E>(self, executor: E) -> impl Future<Output = sqlx::Result<()>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait InsertMany<DB: Database>: Sized {
fn insert_many<E>(
entities: Vec<Self>,
batch_size: usize,
executor: E,
) -> impl Future<Output = sqlx::Result<()>>
where
E: ReusableExecutor<DB> + Send;
}
pub trait SelectAllNoId<DB: Database>: Sized {
fn select_all<'c, E>(executor: E) -> impl Future<Output = sqlx::Result<Vec<Self>>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait InsertNoId<DB: Database>: Sized {
fn insert<'c, E>(self, executor: E) -> impl Future<Output = sqlx::Result<()>> + Send
where
E: Executor<'c, Database = DB>;
}
pub trait InsertManyNoId<DB: Database>: Sized {
fn insert_many<E>(
entities: Vec<Self>,
batch_size: usize,
executor: E,
) -> impl Future<Output = sqlx::Result<()>> + Send
where
E: ReusableExecutor<DB> + Send;
}