1use crate::Result;
11use crate::SelectQueryBuilder;
12use futures::future::BoxFuture;
13
14pub trait Insert<DB>
29where
30 Self: Sized + Send + Sync,
31 DB: sqlx::Database,
32{
33 type Model;
34 fn insert<'e, A>(self, conn: A) -> BoxFuture<'e, Result<Self::Model>>
35 where
36 A: 'e + Send + sqlx::Acquire<'e, Database = DB>;
37}
38
39pub trait ModelBuilder<'a, DB>
41where
42 Self: Sized + Send + Sync,
43 DB: sqlx::Database,
44{
45 type Model;
46
47 fn insert<'e: 'a, E>(self, db: E) -> BoxFuture<'a, Result<Self::Model>>
48 where
49 E: 'e + sqlx::Executor<'e, Database = DB>;
50
51 fn update<'e: 'a, E>(self, db: E) -> BoxFuture<'a, Result<Self::Model>>
52 where
53 E: 'e + sqlx::Executor<'e, Database = DB>;
54
55 fn modified_fields(&self) -> Vec<&'static str>;
57
58 fn build(self) -> Self::Model;
60}
61
62pub trait Model<DB>
64where
65 DB: sqlx::Database,
66 Self: Sized + TableMeta,
67{
68 type ModelBuilder<'a>: ModelBuilder<'a, DB>
69 where
70 Self: 'a;
71
72 fn insert<'a, A>(self, conn: A) -> crate::insert::Insertion<'a, A, Self, DB>
74 where
75 A: 'a + Send + sqlx::Acquire<'a, Database = DB>,
76 Self: Send;
77
78 fn insert_many<'e, E>(values: Vec<Self>, db: E) -> BoxFuture<'e, Result<Vec<Self>>>
79 where
80 E: 'e + sqlx::Executor<'e, Database = DB>;
81
82 fn update_all_fields<'e, E>(self, db: E) -> BoxFuture<'e, Result<Self>>
85 where
86 E: 'e + Send + sqlx::Executor<'e, Database = DB>;
87
88 fn delete<'e, E>(self, db: E) -> BoxFuture<'e, Result<()>>
89 where
90 E: 'e + sqlx::Executor<'e, Database = DB>;
91
92 fn fetch_one<'e, 'a, Arg, E>(id: Arg, db: E) -> BoxFuture<'e, Result<Self>>
94 where
95 'a: 'e,
96 E: 'e + sqlx::Executor<'e, Database = DB>,
97 Arg: 'a + Send + sqlx::Encode<'a, DB> + sqlx::Type<DB>;
98
99 fn query(query: &str) -> sqlx::query::QueryAs<DB, Self, DB::Arguments<'_>>;
101
102 fn select<'args>() -> SelectQueryBuilder<'args, DB, Self>;
104
105 fn update_partial(&self) -> Self::ModelBuilder<'_>;
108
109 fn builder() -> Self::ModelBuilder<'static>;
110}
111
112pub trait TableMeta {
113 fn table_name() -> &'static str;
114 fn table_columns() -> &'static [&'static str];
115 fn primary_key() -> Option<&'static str>;
116 fn primary_key_placeholder_idx() -> Option<usize> {
117 let col = Self::primary_key()?;
118 Self::table_columns().iter().position(|&c| c == col).map(|i| i + 1)
119 }
120}