|
| 1 | +// TODO: remove once Rust's async lifetime in trait story got improved |
| 2 | +#![allow(clippy::manual_async_fn)] |
| 3 | + |
| 4 | +use std::future::Future; |
| 5 | + |
| 6 | +use deadpool_postgres::GenericClient; |
| 7 | +use tokio_postgres::types::ToSql; |
| 8 | +use tokio_postgres::Row; |
| 9 | + |
| 10 | +use crate::Error; |
| 11 | + |
| 12 | +pub trait Connection: Send + Sync { |
| 13 | + fn query_one<'a>( |
| 14 | + &'a self, |
| 15 | + query: &'a str, |
| 16 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 17 | + ) -> impl Future<Output = Result<Row, Error>> + Send + 'a; |
| 18 | + |
| 19 | + fn query_opt<'a>( |
| 20 | + &'a self, |
| 21 | + query: &'a str, |
| 22 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 23 | + ) -> impl Future<Output = Result<Option<Row>, Error>> + Send + 'a; |
| 24 | + |
| 25 | + fn query<'a>( |
| 26 | + &'a self, |
| 27 | + query: &'a str, |
| 28 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 29 | + ) -> impl Future<Output = Result<Vec<Row>, Error>> + Send + 'a; |
| 30 | + |
| 31 | + fn execute<'a>( |
| 32 | + &'a self, |
| 33 | + query: &'a str, |
| 34 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 35 | + ) -> impl Future<Output = Result<(), Error>> + Send + 'a; |
| 36 | +} |
| 37 | + |
| 38 | +impl Connection for deadpool_postgres::Client { |
| 39 | + fn query_one<'a>( |
| 40 | + &'a self, |
| 41 | + query: &'a str, |
| 42 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 43 | + ) -> impl Future<Output = Result<Row, Error>> + Send + 'a { |
| 44 | + async move { |
| 45 | + let stmt = self.prepare_cached(query).await?; |
| 46 | + Ok(tokio_postgres::Client::query_one(self, &stmt, parameters).await?) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + fn query_opt<'a>( |
| 51 | + &'a self, |
| 52 | + query: &'a str, |
| 53 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 54 | + ) -> impl Future<Output = Result<Option<Row>, Error>> + Send + 'a { |
| 55 | + async move { |
| 56 | + let stmt = self.prepare_cached(query).await?; |
| 57 | + Ok(tokio_postgres::Client::query_opt(self, &stmt, parameters).await?) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + fn query<'a>( |
| 62 | + &'a self, |
| 63 | + query: &'a str, |
| 64 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 65 | + ) -> impl Future<Output = Result<Vec<Row>, Error>> + Send + 'a { |
| 66 | + async move { |
| 67 | + let stmt = self.prepare_cached(query).await?; |
| 68 | + Ok(tokio_postgres::Client::query(self, &stmt, parameters).await?) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + fn execute<'a>( |
| 73 | + &'a self, |
| 74 | + query: &'a str, |
| 75 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 76 | + ) -> impl Future<Output = Result<(), Error>> + Send + 'a { |
| 77 | + async move { |
| 78 | + let stmt = self.prepare_cached(query).await?; |
| 79 | + tokio_postgres::Client::execute(self, &stmt, parameters).await?; |
| 80 | + Ok(()) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<'t> Connection for deadpool_postgres::Transaction<'t> { |
| 86 | + fn query_one<'a>( |
| 87 | + &'a self, |
| 88 | + query: &'a str, |
| 89 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 90 | + ) -> impl Future<Output = Result<Row, Error>> + Send + 'a { |
| 91 | + async move { |
| 92 | + let stmt = self.prepare_cached(query).await?; |
| 93 | + Ok(tokio_postgres::Transaction::query_one(self, &stmt, parameters).await?) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fn query_opt<'a>( |
| 98 | + &'a self, |
| 99 | + query: &'a str, |
| 100 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 101 | + ) -> impl Future<Output = Result<Option<Row>, Error>> + Send + 'a { |
| 102 | + async move { |
| 103 | + let stmt = self.prepare_cached(query).await?; |
| 104 | + Ok(tokio_postgres::Transaction::query_opt(self, &stmt, parameters).await?) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + fn query<'a>( |
| 109 | + &'a self, |
| 110 | + query: &'a str, |
| 111 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 112 | + ) -> impl Future<Output = Result<Vec<Row>, Error>> + Send + 'a { |
| 113 | + async move { |
| 114 | + let stmt = self.prepare_cached(query).await?; |
| 115 | + Ok(tokio_postgres::Transaction::query(self, &stmt, parameters).await?) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + fn execute<'a>( |
| 120 | + &'a self, |
| 121 | + query: &'a str, |
| 122 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 123 | + ) -> impl Future<Output = Result<(), Error>> + Send + 'a { |
| 124 | + async move { |
| 125 | + let stmt = self.prepare_cached(query).await?; |
| 126 | + tokio_postgres::Transaction::execute(self, &stmt, parameters).await?; |
| 127 | + Ok(()) |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl<'b, C> Connection for &'b C |
| 133 | +where |
| 134 | + C: Connection, |
| 135 | +{ |
| 136 | + fn query_one<'a>( |
| 137 | + &'a self, |
| 138 | + query: &'a str, |
| 139 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 140 | + ) -> impl Future<Output = Result<Row, Error>> + Send + 'a { |
| 141 | + (*self).query_one(query, parameters) |
| 142 | + } |
| 143 | + |
| 144 | + fn query_opt<'a>( |
| 145 | + &'a self, |
| 146 | + query: &'a str, |
| 147 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 148 | + ) -> impl Future<Output = Result<Option<Row>, Error>> + Send + 'a { |
| 149 | + (*self).query_opt(query, parameters) |
| 150 | + } |
| 151 | + |
| 152 | + fn query<'a>( |
| 153 | + &'a self, |
| 154 | + query: &'a str, |
| 155 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 156 | + ) -> impl Future<Output = Result<Vec<Row>, Error>> + Send + 'a { |
| 157 | + (*self).query(query, parameters) |
| 158 | + } |
| 159 | + |
| 160 | + fn execute<'a>( |
| 161 | + &'a self, |
| 162 | + query: &'a str, |
| 163 | + parameters: &'a [&'a (dyn ToSql + Sync)], |
| 164 | + ) -> impl Future<Output = Result<(), Error>> + Send + 'a { |
| 165 | + (*self).execute(query, parameters) |
| 166 | + } |
| 167 | +} |
0 commit comments