Skip to main content

dbkit/
model.rs

1use crate::executor::BoxFuture;
2use crate::Executor;
3use crate::{ColumnRef, Error, Value};
4
5pub trait ModelValue {
6    fn column_value(&self, column: ColumnRef) -> Option<Value>;
7}
8
9pub trait JoinedModel: ModelValue + Sized {
10    fn joined_columns() -> &'static [ColumnRef];
11    fn joined_primary_keys() -> &'static [ColumnRef];
12
13    fn joined_from_row_prefixed(row: &sqlx::postgres::PgRow, prefix: &str) -> Result<Self, sqlx::Error>;
14
15    fn joined_row_has_pk(row: &sqlx::postgres::PgRow, prefix: &str) -> Result<bool, sqlx::Error>;
16}
17
18pub trait SetRelation<Rel, ValueOut> {
19    fn set_relation(&mut self, _rel: Rel, value: ValueOut) -> Result<(), Error> {
20        let _ = value;
21        Err(Error::RelationMismatch)
22    }
23}
24
25pub trait GetRelation<Rel, ValueOut> {
26    fn get_relation(&self, _rel: Rel) -> Option<&ValueOut> {
27        None
28    }
29
30    fn get_relation_mut(&mut self, _rel: Rel) -> Option<&mut ValueOut> {
31        None
32    }
33}
34
35pub trait ModelDelete: Sized {
36    fn delete<'e, E>(self, ex: &'e E) -> BoxFuture<'e, Result<u64, Error>>
37    where
38        E: Executor + Send + Sync + 'e;
39}
40
41pub trait LoadRelation<Rel>: Sized {
42    type Out;
43
44    fn load_relation<'e, E>(self, rel: Rel, ex: &'e E) -> BoxFuture<'e, Result<Self::Out, Error>>
45    where
46        E: Executor + Send + Sync + 'e;
47}