parsql_deadpool_postgres/
traits.rs1use async_trait::async_trait;
2use postgres::types::FromSql;
3use std::fmt::Debug;
4use tokio_postgres::types::ToSql;
5use tokio_postgres::{Error, Row};
6
7pub trait SqlQuery<R> {
10 fn query() -> String;
12}
13
14pub trait SqlCommand {
17 fn query() -> String;
19}
20
21pub trait SqlParams {
24 fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
26}
27
28pub trait UpdateParams {
31 fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
33}
34
35pub trait FromRow {
38 fn from_row(row: &Row) -> Result<Self, Error>
46 where
47 Self: Sized;
48}
49
50#[async_trait]
53pub trait CrudOps {
54 async fn insert<T, P: for<'a> FromSql<'a> + Send + Sync>(&self, entity: T) -> Result<P, Error>
56 where
57 T: SqlCommand + SqlParams + Send + Sync;
58
59 async fn update<T>(&self, entity: T) -> Result<u64, Error>
61 where
62 T: SqlCommand + UpdateParams + Send + Sync;
63
64 async fn delete<T>(&self, entity: T) -> Result<u64, Error>
66 where
67 T: SqlCommand + SqlParams + Send + Sync;
68
69 async fn fetch<P, R>(&self, params: &P) -> Result<R, Error>
71 where
72 P: SqlQuery<R> + SqlParams + Send + Sync,
73 R: FromRow + Send + Sync;
74
75 async fn fetch_all<P, R>(&self, params: &P) -> Result<Vec<R>, Error>
77 where
78 P: SqlQuery<R> + SqlParams + Send + Sync,
79 R: FromRow + Send + Sync;
80
81 async fn select<T, R, F>(&self, entity: T, to_model: F) -> Result<R, Error>
83 where
84 T: SqlQuery<T> + SqlParams + Send + Sync,
85 F: FnOnce(&Row) -> Result<R, Error> + Send + Sync;
86
87 async fn select_all<T, R, F>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
89 where
90 T: SqlQuery<T> + SqlParams + Send + Sync,
91 F: Fn(&Row) -> R + Send + Sync;
92}
93
94#[async_trait]
97pub trait TransactionOps {
98 async fn tx_insert<T, P>(&self, entity: T) -> Result<P, Error>
100 where
101 T: SqlCommand + SqlParams + Debug + Send + 'static,
102 P: for<'a> tokio_postgres::types::FromSql<'a> + Send + Sync;
103
104 async fn tx_update<T>(&self, entity: T) -> Result<bool, Error>
106 where
107 T: SqlCommand + UpdateParams + SqlParams + Debug + Send + 'static;
108
109 async fn tx_delete<T>(&self, entity: T) -> Result<u64, Error>
111 where
112 T: SqlCommand + SqlParams + Debug + Send + 'static;
113
114 async fn tx_fetch<P, R>(&self, params: &P) -> Result<R, Error>
116 where
117 P: SqlQuery<R> + SqlParams + Debug + Send + Sync + Clone + 'static,
118 R: FromRow + Debug + Send + Sync + Clone + 'static;
119
120 async fn tx_fetch_all<P, R>(&self, params: &P) -> Result<Vec<R>, Error>
122 where
123 P: SqlQuery<R> + SqlParams + Debug + Send + Sync + Clone + 'static,
124 R: FromRow + Debug + Send + Sync + Clone + 'static;
125
126 async fn tx_select<T, F, R>(&self, entity: T, to_model: F) -> Result<R, Error>
128 where
129 T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
130 F: Fn(&Row) -> Result<R, Error> + Send + Sync + 'static,
131 R: Send + 'static;
132
133 async fn tx_select_all<T, F, R>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
135 where
136 T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
137 F: Fn(&Row) -> R + Send + Sync + 'static,
138 R: Send + 'static;
139
140 #[deprecated(
142 since = "0.2.0",
143 note = "Renamed to `tx_insert`. Please use `tx_insert` function instead."
144 )]
145 async fn insert<T>(&self, entity: T) -> Result<u64, Error>
146 where
147 T: SqlCommand + SqlParams + Debug + Send + 'static;
148
149 #[deprecated(
150 since = "0.2.0",
151 note = "Renamed to `tx_update`. Please use `tx_update` function instead."
152 )]
153 async fn update<T>(&self, entity: T) -> Result<u64, Error>
154 where
155 T: SqlCommand + UpdateParams + SqlParams + Debug + Send + 'static;
156
157 #[deprecated(
158 since = "0.2.0",
159 note = "Renamed to `tx_delete`. Please use `tx_delete` function instead."
160 )]
161 async fn delete<T>(&self, entity: T) -> Result<u64, Error>
162 where
163 T: SqlCommand + SqlParams + Debug + Send + 'static;
164
165 #[deprecated(
166 since = "0.2.0",
167 note = "Renamed to `tx_fetch`. Please use `tx_fetch` function instead."
168 )]
169 async fn get<T>(&self, params: &T) -> Result<T, Error>
170 where
171 T: SqlQuery<T> + FromRow + SqlParams + Debug + Send + Sync + Clone + 'static;
172
173 #[deprecated(
174 since = "0.2.0",
175 note = "Renamed to `tx_fetch_all`. Please use `tx_fetch_all` function instead."
176 )]
177 async fn get_all<T>(&self, params: &T) -> Result<Vec<T>, Error>
178 where
179 T: SqlQuery<T> + FromRow + SqlParams + Debug + Send + Sync + Clone + 'static;
180
181 #[deprecated(
182 since = "0.2.0",
183 note = "Renamed to `tx_select`. Please use `tx_select` function instead."
184 )]
185 async fn select<T, R, F>(&self, entity: T, to_model: F) -> Result<R, Error>
186 where
187 T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
188 F: for<'a> Fn(&'a Row) -> Result<R, Error> + Send + Sync + 'static,
189 R: Send + 'static;
190
191 #[deprecated(
192 since = "0.2.0",
193 note = "Renamed to `tx_select_all`. Please use `tx_select_all` function instead."
194 )]
195 async fn select_all<T, R, F>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
196 where
197 T: SqlQuery<T> + SqlParams + Debug + Send + 'static,
198 F: Fn(&Row) -> R + Send + Sync + 'static,
199 R: Send + 'static;
200}