mirror of
https://github.com/launchbadge/sqlx
synced 2024-11-10 14:34:19 +00:00
Allow chaining map / try_map on queries
To support `.map()` / `.try_map()` on `query!()` (and `query_as!()`).
This commit is contained in:
parent
9eca6413fe
commit
74835bfe58
1 changed files with 38 additions and 0 deletions
|
@ -260,6 +260,44 @@ where
|
|||
O: Send + Unpin,
|
||||
A: 'q + Send + IntoArguments<'q, DB>,
|
||||
{
|
||||
/// Map each row in the result to another type.
|
||||
///
|
||||
/// See [`try_map`](Map::try_map) for a fallible version of this method.
|
||||
///
|
||||
/// The [`query_as`](super::query_as::query_as) method will construct a mapped query using
|
||||
/// a [`FromRow`](super::from_row::FromRow) implementation.
|
||||
#[inline]
|
||||
pub fn map<G, P>(
|
||||
self,
|
||||
mut g: G,
|
||||
) -> Map<'q, DB, impl FnMut(DB::Row) -> Result<P, Error> + Send, A>
|
||||
where
|
||||
G: FnMut(O) -> P + Send,
|
||||
P: Unpin,
|
||||
{
|
||||
self.try_map(move |data| Ok(g(data)))
|
||||
}
|
||||
|
||||
/// Map each row in the result to another type.
|
||||
///
|
||||
/// The [`query_as`](super::query_as::query_as) method will construct a mapped query using
|
||||
/// a [`FromRow`](super::from_row::FromRow) implementation.
|
||||
#[inline]
|
||||
pub fn try_map<G, P>(
|
||||
self,
|
||||
mut g: G,
|
||||
) -> Map<'q, DB, impl FnMut(DB::Row) -> Result<P, Error> + Send, A>
|
||||
where
|
||||
G: FnMut(O) -> Result<P, Error> + Send,
|
||||
P: Unpin,
|
||||
{
|
||||
let mut f = self.mapper;
|
||||
Map {
|
||||
inner: self.inner,
|
||||
mapper: move |row| f(row).and_then(|o| g(o)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute the query and return the generated results as a stream.
|
||||
pub fn fetch<'e, 'c: 'e, E>(self, executor: E) -> BoxStream<'e, Result<O, Error>>
|
||||
where
|
||||
|
|
Loading…
Reference in a new issue