parsql_deadpool_postgres/
traits.rs

1use async_trait::async_trait;
2use postgres::types::FromSql;
3use std::fmt::Debug;
4use tokio_postgres::types::ToSql;
5use tokio_postgres::{Error, Row};
6
7/// Trait for generating SQL queries (for SELECT operations).
8/// This trait is implemented by the derive macro `Queryable`.
9pub trait SqlQuery<R> {
10    /// Returns the SQL query string.
11    fn query() -> String;
12}
13
14/// Trait for generating SQL commands (for INSERT/UPDATE/DELETE operations).
15/// This trait is implemented by the derive macros `Insertable`, `Updateable`, and `Deletable`.
16pub trait SqlCommand {
17    /// Returns the SQL command string.
18    fn query() -> String;
19}
20
21/// Trait for providing SQL parameters.
22/// This trait is implemented by the derive macro `SqlParams`.
23pub trait SqlParams {
24    /// Returns a vector of references to SQL parameters.
25    fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
26}
27
28/// Trait for providing UPDATE parameters.
29/// This trait is implemented by the derive macro `UpdateParams`.
30pub trait UpdateParams {
31    /// Returns a vector of references to SQL parameters for UPDATE operations.
32    fn params(&self) -> Vec<&(dyn ToSql + Sync)>;
33}
34
35/// Trait for converting database rows to Rust structs.
36/// This trait is implemented by the derive macro `FromRow`.
37pub trait FromRow {
38    /// Converts a database row to a Rust struct.
39    ///
40    /// # Arguments
41    /// * `row` - A reference to a database row
42    ///
43    /// # Returns
44    /// * `Result<Self, Error>` - The converted struct or an error
45    fn from_row(row: &Row) -> Result<Self, Error>
46    where
47        Self: Sized;
48}
49
50/// CrudOps trait'i, Pool nesnesi için CRUD işlemlerini extension method olarak sağlar.
51/// Bu trait, Pool üzerinde doğrudan CRUD işlemlerini çağırmayı mümkün kılar.
52#[async_trait]
53pub trait CrudOps {
54    /// Veritabanına yeni bir kayıt ekler.
55    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    /// Veritabanındaki mevcut bir kaydı günceller.
60    async fn update<T>(&self, entity: T) -> Result<u64, Error>
61    where
62        T: SqlCommand + UpdateParams + Send + Sync;
63
64    /// Veritabanından bir kaydı siler.
65    async fn delete<T>(&self, entity: T) -> Result<u64, Error>
66    where
67        T: SqlCommand + SqlParams + Send + Sync;
68
69    /// Belirtilen kriterlere uygun tek bir kaydı getirir.
70    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    /// Belirtilen kriterlere uygun tüm kayıtları getirir.
76    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    /// Belirtilen özel dönüşüm fonksiyonunu kullanarak tek bir kaydı getirir.
82    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    /// Belirtilen özel dönüşüm fonksiyonunu kullanarak tüm kayıtları getirir.
88    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/// TransactionOps trait, Transaction için CRUD işlemlerini extension method olarak sağlar
95/// Bu şekilde, herhangi bir Transaction nesnesi üzerinde doğrudan CRUD işlemleri yapılabilir
96#[async_trait]
97pub trait TransactionOps {
98    /// Insert method, yeni bir kayıt eklemek için kullanılır
99    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    /// Update method, mevcut bir kaydı güncellemek için kullanılır
105    async fn tx_update<T>(&self, entity: T) -> Result<bool, Error>
106    where
107        T: SqlCommand + UpdateParams + SqlParams + Debug + Send + 'static;
108
109    /// Delete method, bir kaydı silmek için kullanılır
110    async fn tx_delete<T>(&self, entity: T) -> Result<u64, Error>
111    where
112        T: SqlCommand + SqlParams + Debug + Send + 'static;
113
114    /// Fetch method, tek bir kayıt getirmek için kullanılır
115    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    /// Fetch All method, birden fazla kayıt getirmek için kullanılır
121    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    /// Select method, özel dönüşüm fonksiyonu ile tek bir kayıt getirmek için kullanılır
127    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    /// Select All method, özel dönüşüm fonksiyonu ile birden fazla kayıt getirmek için kullanılır
134    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 methods for backward compatibility
141    #[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}