| 1 | use crate::any::{Any, AnyConnection, AnyQueryResult, AnyRow, AnyStatement, AnyTypeInfo}; |
| 2 | use crate::describe::Describe; |
| 3 | use crate::error::Error; |
| 4 | use crate::executor::{Execute, Executor}; |
| 5 | use either::Either; |
| 6 | use futures_core::future::BoxFuture; |
| 7 | use futures_core::stream::BoxStream; |
| 8 | use futures_util::{stream, FutureExt, StreamExt}; |
| 9 | use std::future; |
| 10 | |
| 11 | impl<'c> Executor<'c> for &'c mut AnyConnection { |
| 12 | type Database = Any; |
| 13 | |
| 14 | fn fetch_many<'e, 'q: 'e, E>( |
| 15 | self, |
| 16 | mut query: E, |
| 17 | ) -> BoxStream<'e, Result<Either<AnyQueryResult, AnyRow>, Error>> |
| 18 | where |
| 19 | 'c: 'e, |
| 20 | E: 'q + Execute<'q, Any>, |
| 21 | { |
| 22 | let arguments = match query.take_arguments().map_err(Error::Encode) { |
| 23 | Ok(arguments) => arguments, |
| 24 | Err(error) => return stream::once(future::ready(Err(error))).boxed(), |
| 25 | }; |
| 26 | self.backend |
| 27 | .fetch_many(query.sql(), query.persistent(), arguments) |
| 28 | } |
| 29 | |
| 30 | fn fetch_optional<'e, 'q: 'e, E>( |
| 31 | self, |
| 32 | mut query: E, |
| 33 | ) -> BoxFuture<'e, Result<Option<AnyRow>, Error>> |
| 34 | where |
| 35 | 'c: 'e, |
| 36 | E: 'q + Execute<'q, Self::Database>, |
| 37 | { |
| 38 | let arguments = match query.take_arguments().map_err(Error::Encode) { |
| 39 | Ok(arguments) => arguments, |
| 40 | Err(error) => return future::ready(Err(error)).boxed(), |
| 41 | }; |
| 42 | self.backend |
| 43 | .fetch_optional(query.sql(), query.persistent(), arguments) |
| 44 | } |
| 45 | |
| 46 | fn prepare_with<'e, 'q: 'e>( |
| 47 | self, |
| 48 | sql: &'q str, |
| 49 | parameters: &[AnyTypeInfo], |
| 50 | ) -> BoxFuture<'e, Result<AnyStatement<'q>, Error>> |
| 51 | where |
| 52 | 'c: 'e, |
| 53 | { |
| 54 | self.backend.prepare_with(sql, parameters) |
| 55 | } |
| 56 | |
| 57 | fn describe<'e, 'q: 'e>( |
| 58 | self, |
| 59 | sql: &'q str, |
| 60 | ) -> BoxFuture<'e, Result<Describe<Self::Database>, Error>> |
| 61 | where |
| 62 | 'c: 'e, |
| 63 | { |
| 64 | self.backend.describe(sql) |
| 65 | } |
| 66 | } |