Struct Map
pub struct Map<'q, DB, F, A>where
DB: Database,{
inner: Query<'q, DB, A>,
mapper: F,
}
Expand description
A single SQL query that will map its results to an owned Rust type.
Executes as a prepared statement.
Returned by Query::try_map
, query!()
, etc. Has most of the same methods as Query
but
the return types are changed to reflect the mapping. However, there is no equivalent of
Query::execute
as it doesn’t make sense to map the result type and then ignore it.
Query::bind
is also omitted; stylistically we recommend placing your .bind()
calls
before .try_map()
. This is also to prevent adding superfluous binds to the result of
query!()
et al.
Fields§
§inner: Query<'q, DB, A>
§mapper: F
Implementations§
§impl<'q, DB, F, O, A> Map<'q, DB, F, A>
impl<'q, DB, F, O, A> Map<'q, DB, F, A>
pub fn fetch<'e, 'c, E>(
self,
executor: E,
) -> Pin<Box<dyn Stream<Item = Result<O, Error>> + Send + 'e>>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
pub fn fetch<'e, 'c, E>(
self,
executor: E,
) -> Pin<Box<dyn Stream<Item = Result<O, Error>> + Send + 'e>>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
Execute the query and return the generated results as a stream.
pub fn fetch_many<'e, 'c, E>(
self,
executor: E,
) -> Pin<Box<dyn Stream<Item = Result<Either<<DB as Database>::QueryResult, O>, Error>> + Send + 'e>>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
👎Deprecated: Only the SQLite driver supports multiple statements in one prepared statement and that behavior is deprecated. Use sqlx::raw_sql()
instead.
pub fn fetch_many<'e, 'c, E>(
self,
executor: E,
) -> Pin<Box<dyn Stream<Item = Result<Either<<DB as Database>::QueryResult, O>, Error>> + Send + 'e>>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
sqlx::raw_sql()
instead.Execute multiple queries and return the generated results as a stream from each query, in a stream.
pub async fn fetch_all<'e, 'c, E>(self, executor: E) -> Result<Vec<O>, Error>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
pub async fn fetch_all<'e, 'c, E>(self, executor: E) -> Result<Vec<O>, Error>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
pub async fn fetch_one<'e, 'c, E>(self, executor: E) -> Result<O, Error>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
pub async fn fetch_one<'e, 'c, E>(self, executor: E) -> Result<O, Error>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
Execute the query, returning the first row or [Error::RowNotFound
] otherwise.
§Note: for best performance, ensure the query returns at most one row.
Depending on the driver implementation, if your query can return more than one row, it may lead to wasted CPU time and bandwidth on the database server.
Even when the driver implementation takes this into account, ensuring the query returns at most one row can result in a more optimal query plan.
If your query has a WHERE
clause filtering a unique column by a single value, you’re good.
Otherwise, you might want to add LIMIT 1
to your query.
pub async fn fetch_optional<'e, 'c, E>(
self,
executor: E,
) -> Result<Option<O>, Error>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
pub async fn fetch_optional<'e, 'c, E>(
self,
executor: E,
) -> Result<Option<O>, Error>where
'c: 'e,
'q: 'e,
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
F: 'e,
O: 'e,
Execute the query, returning the first row or None
otherwise.
§Note: for best performance, ensure the query returns at most one row.
Depending on the driver implementation, if your query can return more than one row, it may lead to wasted CPU time and bandwidth on the database server.
Even when the driver implementation takes this into account, ensuring the query returns at most one row can result in a more optimal query plan.
If your query has a WHERE
clause filtering a unique column by a single value, you’re good.
Otherwise, you might want to add LIMIT 1
to your query.
Trait Implementations§
§impl<'q, DB, F, A> Execute<'q, DB> for Map<'q, DB, F, A>
impl<'q, DB, F, A> Execute<'q, DB> for Map<'q, DB, F, A>
§fn statement(&self) -> Option<&<DB as Database>::Statement<'q>>
fn statement(&self) -> Option<&<DB as Database>::Statement<'q>>
§fn take_arguments(
&mut self,
) -> Result<Option<<DB as Database>::Arguments<'q>>, Box<dyn Error + Sync + Send>>
fn take_arguments( &mut self, ) -> Result<Option<<DB as Database>::Arguments<'q>>, Box<dyn Error + Sync + Send>>
§fn persistent(&self) -> bool
fn persistent(&self) -> bool
true
if the statement should be cached.Auto Trait Implementations§
impl<'q, DB, F, A> Freeze for Map<'q, DB, F, A>
impl<'q, DB, F, A> !RefUnwindSafe for Map<'q, DB, F, A>
impl<'q, DB, F, A> Send for Map<'q, DB, F, A>
impl<'q, DB, F, A> Sync for Map<'q, DB, F, A>
impl<'q, DB, F, A> Unpin for Map<'q, DB, F, A>
impl<'q, DB, F, A> !UnwindSafe for Map<'q, DB, F, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more