| 1 | use crate::database::Database; |
| 2 | use crate::describe::Describe; |
| 3 | use crate::error::{BoxDynError, Error}; |
| 4 | |
| 5 | use either::Either; |
| 6 | use futures_core::future::BoxFuture; |
| 7 | use futures_core::stream::BoxStream; |
| 8 | use futures_util::{future, FutureExt, StreamExt, TryFutureExt, TryStreamExt}; |
| 9 | use std::fmt::Debug; |
| 10 | |
| 11 | /// A type that contains or can provide a database |
| 12 | /// connection to use for executing queries against the database. |
| 13 | /// |
| 14 | /// No guarantees are provided that successive queries run on the same |
| 15 | /// physical database connection. |
| 16 | /// |
| 17 | /// A [`Connection`](crate::connection::Connection) is an `Executor` that guarantees that |
| 18 | /// successive queries are ran on the same physical database connection. |
| 19 | /// |
| 20 | /// Implemented for the following: |
| 21 | /// |
| 22 | /// * [`&Pool`](super::pool::Pool) |
| 23 | /// * [`&mut Connection`](super::connection::Connection) |
| 24 | /// |
| 25 | /// The [`Executor`] impls for [`Transaction`](crate::transaction::Transaction) |
| 26 | /// and [`PoolConnection`](crate::pool::PoolConnection) have been deleted because they |
| 27 | /// cannot exist in the new crate architecture without rewriting the Executor trait entirely. |
| 28 | /// To fix this breakage, simply add a dereference where an impl [`Executor`] is expected, as |
| 29 | /// they both dereference to the inner connection type which will still implement it: |
| 30 | /// * `&mut transaction` -> `&mut *transaction` |
| 31 | /// * `&mut connection` -> `&mut *connection` |
| 32 | /// |
| 33 | pub trait Executor<'c>: Send + Debug + Sized { |
| 34 | type Database: Database; |
| 35 | |
| 36 | /// Execute the query and return the total number of rows affected. |
| 37 | fn execute<'e, 'q: 'e, E>( |
| 38 | self, |
| 39 | query: E, |
| 40 | ) -> BoxFuture<'e, Result<<Self::Database as Database>::QueryResult, Error>> |
| 41 | where |
| 42 | 'c: 'e, |
| 43 | E: 'q + Execute<'q, Self::Database>, |
| 44 | { |
| 45 | self.execute_many(query).try_collect().boxed() |
| 46 | } |
| 47 | |
| 48 | /// Execute multiple queries and return the rows affected from each query, in a stream. |
| 49 | fn execute_many<'e, 'q: 'e, E>( |
| 50 | self, |
| 51 | query: E, |
| 52 | ) -> BoxStream<'e, Result<<Self::Database as Database>::QueryResult, Error>> |
| 53 | where |
| 54 | 'c: 'e, |
| 55 | E: 'q + Execute<'q, Self::Database>, |
| 56 | { |
| 57 | self.fetch_many(query) |
| 58 | .try_filter_map(|step| async move { |
| 59 | Ok(match step { |
| 60 | Either::Left(rows) => Some(rows), |
| 61 | Either::Right(_) => None, |
| 62 | }) |
| 63 | }) |
| 64 | .boxed() |
| 65 | } |
| 66 | |
| 67 | /// Execute the query and return the generated results as a stream. |
| 68 | fn fetch<'e, 'q: 'e, E>( |
| 69 | self, |
| 70 | query: E, |
| 71 | ) -> BoxStream<'e, Result<<Self::Database as Database>::Row, Error>> |
| 72 | where |
| 73 | 'c: 'e, |
| 74 | E: 'q + Execute<'q, Self::Database>, |
| 75 | { |
| 76 | self.fetch_many(query) |
| 77 | .try_filter_map(|step| async move { |
| 78 | Ok(match step { |
| 79 | Either::Left(_) => None, |
| 80 | Either::Right(row) => Some(row), |
| 81 | }) |
| 82 | }) |
| 83 | .boxed() |
| 84 | } |
| 85 | |
| 86 | /// Execute multiple queries and return the generated results as a stream |
| 87 | /// from each query, in a stream. |
| 88 | fn fetch_many<'e, 'q: 'e, E>( |
| 89 | self, |
| 90 | query: E, |
| 91 | ) -> BoxStream< |
| 92 | 'e, |
| 93 | Result< |
| 94 | Either<<Self::Database as Database>::QueryResult, <Self::Database as Database>::Row>, |
| 95 | Error, |
| 96 | >, |
| 97 | > |
| 98 | where |
| 99 | 'c: 'e, |
| 100 | E: 'q + Execute<'q, Self::Database>; |
| 101 | |
| 102 | /// Execute the query and return all the generated results, collected into a [`Vec`]. |
| 103 | fn fetch_all<'e, 'q: 'e, E>( |
| 104 | self, |
| 105 | query: E, |
| 106 | ) -> BoxFuture<'e, Result<Vec<<Self::Database as Database>::Row>, Error>> |
| 107 | where |
| 108 | 'c: 'e, |
| 109 | E: 'q + Execute<'q, Self::Database>, |
| 110 | { |
| 111 | self.fetch(query).try_collect().boxed() |
| 112 | } |
| 113 | |
| 114 | /// Execute the query and returns exactly one row. |
| 115 | fn fetch_one<'e, 'q: 'e, E>( |
| 116 | self, |
| 117 | query: E, |
| 118 | ) -> BoxFuture<'e, Result<<Self::Database as Database>::Row, Error>> |
| 119 | where |
| 120 | 'c: 'e, |
| 121 | E: 'q + Execute<'q, Self::Database>, |
| 122 | { |
| 123 | self.fetch_optional(query) |
| 124 | .and_then(|row| match row { |
| 125 | Some(row) => future::ok(row), |
| 126 | None => future::err(Error::RowNotFound), |
| 127 | }) |
| 128 | .boxed() |
| 129 | } |
| 130 | |
| 131 | /// Execute the query and returns at most one row. |
| 132 | fn fetch_optional<'e, 'q: 'e, E>( |
| 133 | self, |
| 134 | query: E, |
| 135 | ) -> BoxFuture<'e, Result<Option<<Self::Database as Database>::Row>, Error>> |
| 136 | where |
| 137 | 'c: 'e, |
| 138 | E: 'q + Execute<'q, Self::Database>; |
| 139 | |
| 140 | /// Prepare the SQL query to inspect the type information of its parameters |
| 141 | /// and results. |
| 142 | /// |
| 143 | /// Be advised that when using the `query`, `query_as`, or `query_scalar` functions, the query |
| 144 | /// is transparently prepared and executed. |
| 145 | /// |
| 146 | /// This explicit API is provided to allow access to the statement metadata available after |
| 147 | /// it prepared but before the first row is returned. |
| 148 | #[inline] |
| 149 | fn prepare<'e, 'q: 'e>( |
| 150 | self, |
| 151 | query: &'q str, |
| 152 | ) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement<'q>, Error>> |
| 153 | where |
| 154 | 'c: 'e, |
| 155 | { |
| 156 | self.prepare_with(query, &[]) |
| 157 | } |
| 158 | |
| 159 | /// Prepare the SQL query, with parameter type information, to inspect the |
| 160 | /// type information about its parameters and results. |
| 161 | /// |
| 162 | /// Only some database drivers (PostgreSQL, MSSQL) can take advantage of |
| 163 | /// this extra information to influence parameter type inference. |
| 164 | fn prepare_with<'e, 'q: 'e>( |
| 165 | self, |
| 166 | sql: &'q str, |
| 167 | parameters: &'e [<Self::Database as Database>::TypeInfo], |
| 168 | ) -> BoxFuture<'e, Result<<Self::Database as Database>::Statement<'q>, Error>> |
| 169 | where |
| 170 | 'c: 'e; |
| 171 | |
| 172 | /// Describe the SQL query and return type information about its parameters |
| 173 | /// and results. |
| 174 | /// |
| 175 | /// This is used by compile-time verification in the query macros to |
| 176 | /// power their type inference. |
| 177 | #[doc(hidden)] |
| 178 | fn describe<'e, 'q: 'e>( |
| 179 | self, |
| 180 | sql: &'q str, |
| 181 | ) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>> |
| 182 | where |
| 183 | 'c: 'e; |
| 184 | } |
| 185 | |
| 186 | /// A type that may be executed against a database connection. |
| 187 | /// |
| 188 | /// Implemented for the following: |
| 189 | /// |
| 190 | /// * [`&str`](std::str) |
| 191 | /// * [`Query`](super::query::Query) |
| 192 | /// |
| 193 | pub trait Execute<'q, DB: Database>: Send + Sized { |
| 194 | /// Gets the SQL that will be executed. |
| 195 | fn sql(&self) -> &'q str; |
| 196 | |
| 197 | /// Gets the previously cached statement, if available. |
| 198 | fn statement(&self) -> Option<&DB::Statement<'q>>; |
| 199 | |
| 200 | /// Returns the arguments to be bound against the query string. |
| 201 | /// |
| 202 | /// Returning `Ok(None)` for `Arguments` indicates to use a "simple" query protocol and to not |
| 203 | /// prepare the query. Returning `Ok(Some(Default::default()))` is an empty arguments object that |
| 204 | /// will be prepared (and cached) before execution. |
| 205 | /// |
| 206 | /// Returns `Err` if encoding any of the arguments failed. |
| 207 | fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError>; |
| 208 | |
| 209 | /// Returns `true` if the statement should be cached. |
| 210 | fn persistent(&self) -> bool; |
| 211 | } |
| 212 | |
| 213 | // NOTE: `Execute` is explicitly not implemented for String and &String to make it slightly more |
| 214 | // involved to write `conn.execute(format!("SELECT {val}"))` |
| 215 | impl<'q, DB: Database> Execute<'q, DB> for &'q str { |
| 216 | #[inline] |
| 217 | fn sql(&self) -> &'q str { |
| 218 | self |
| 219 | } |
| 220 | |
| 221 | #[inline] |
| 222 | fn statement(&self) -> Option<&DB::Statement<'q>> { |
| 223 | None |
| 224 | } |
| 225 | |
| 226 | #[inline] |
| 227 | fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> { |
| 228 | Ok(None) |
| 229 | } |
| 230 | |
| 231 | #[inline] |
| 232 | fn persistent(&self) -> bool { |
| 233 | true |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | impl<'q, DB: Database> Execute<'q, DB> for (&'q str, Option<<DB as Database>::Arguments<'q>>) { |
| 238 | #[inline] |
| 239 | fn sql(&self) -> &'q str { |
| 240 | self.0 |
| 241 | } |
| 242 | |
| 243 | #[inline] |
| 244 | fn statement(&self) -> Option<&DB::Statement<'q>> { |
| 245 | None |
| 246 | } |
| 247 | |
| 248 | #[inline] |
| 249 | fn take_arguments(&mut self) -> Result<Option<<DB as Database>::Arguments<'q>>, BoxDynError> { |
| 250 | Ok(self.1.take()) |
| 251 | } |
| 252 | |
| 253 | #[inline] |
| 254 | fn persistent(&self) -> bool { |
| 255 | true |
| 256 | } |
| 257 | } |