parsql_deadpool_postgres/
traits.rs1use postgres::types::FromSql;
2use tokio_postgres::{Error, Row};
3use tokio_postgres::types::ToSql;
4use std::fmt::Debug;
5use async_trait::async_trait;
6
7pub trait SqlQuery {
10 fn query() -> String;
12}
13
14pub trait SqlParams {
17 fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
19}
20
21pub trait UpdateParams {
24 fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
26}
27
28pub trait FromRow {
31 fn from_row(row: &Row) -> Result<Self, Error>
39 where
40 Self: Sized;
41}
42
43#[async_trait]
46pub trait CrudOps {
47 async fn insert<T, P:for<'a> FromSql<'a> + Send + Sync>(&self, entity: T) -> Result<P, Error>
49 where
50 T: SqlQuery + SqlParams + Send + Sync;
51
52 async fn update<T>(&self, entity: T) -> Result<u64, Error>
54 where
55 T: SqlQuery + UpdateParams + Send + Sync;
56
57 async fn delete<T>(&self, entity: T) -> Result<u64, Error>
59 where
60 T: SqlQuery + SqlParams + Send + Sync;
61
62 async fn fetch<T>(&self, params: &T) -> Result<T, Error>
64 where
65 T: SqlQuery + FromRow + SqlParams + Send + Sync;
66
67 async fn fetch_all<T>(&self, params: &T) -> Result<Vec<T>, Error>
69 where
70 T: SqlQuery + FromRow + SqlParams + Send + Sync;
71
72 async fn select<T, R, F>(&self, entity: T, to_model: F) -> Result<R, Error>
74 where
75 T: SqlQuery + SqlParams + Send + Sync,
76 F: FnOnce(&Row) -> Result<R, Error> + Send + Sync;
77
78 async fn select_all<T, R, F>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
80 where
81 T: SqlQuery + SqlParams + Send + Sync,
82 F: Fn(&Row) -> R + Send + Sync;
83}
84
85#[async_trait]
88pub trait TransactionOps {
89 async fn insert<T>(&self, entity: T) -> Result<u64, Error>
91 where
92 T: SqlQuery + SqlParams + Debug + Send + 'static;
93
94 async fn update<T>(&self, entity: T) -> Result<u64, Error>
96 where
97 T: SqlQuery + UpdateParams + SqlParams + Debug + Send + 'static;
98
99 async fn delete<T>(&self, entity: T) -> Result<u64, Error>
101 where
102 T: SqlQuery + SqlParams + Debug + Send + 'static;
103
104 async fn get<T>(&self, params: &T) -> Result<T, Error>
106 where
107 T: SqlQuery + FromRow + SqlParams + Debug + Send + Sync + Clone + 'static;
108
109 async fn get_all<T>(&self, params: &T) -> Result<Vec<T>, Error>
111 where
112 T: SqlQuery + FromRow + SqlParams + Debug + Send + Sync + Clone + 'static;
113
114 async fn select<T, R, F>(&self, entity: T, to_model: F) -> Result<R, Error>
116 where
117 T: SqlQuery + SqlParams + Debug + Send + 'static,
118 F: FnOnce(&Row) -> Result<R, Error> + Send + Sync + 'static,
119 R: Send + 'static;
120
121 async fn select_all<T, R, F>(&self, entity: T, to_model: F) -> Result<Vec<R>, Error>
123 where
124 T: SqlQuery + SqlParams + Debug + Send + 'static,
125 F: Fn(&Row) -> R + Send + Sync + 'static,
126 R: Send + 'static;
127}