pub struct Statement<T> { /* private fields */ }Expand description
A typed wrapper around an untyped stmt::Statement.
The type parameter T is the returning type — the type produced when the
statement is executed — not the model the statement operates on. For example,
a query that selects User records returns List<User>, so its statement
type is Statement<List<User>>, not Statement<User>.
Common returning types:
| Statement kind | T | exec() produces |
|---|---|---|
| Multi-row query | List<M> | Vec<M> |
Single-row query (.one()) | M | M |
Optional query (.first()) | Option<M> | Option<M> |
| Insert (create) | M | M |
| Delete | () | () |
| Update | () | () |
You rarely construct Statement directly. Instead, use the From
implementations to convert from Query, Insert, Update, or
Delete, or call IntoStatement::into_statement on a query builder.
Implementations§
Source§impl<T> Statement<T>
impl<T> Statement<T>
Sourcepub fn from_untyped_stmt(untyped: Statement) -> Statement<T>
pub fn from_untyped_stmt(untyped: Statement) -> Statement<T>
Wrap a raw untyped stmt::Statement,
tagging it with returning type T.
§Examples
let raw: core_stmt::Statement = core_stmt::Query::unit().into();
let _typed: Statement<()> = Statement::from_untyped_stmt(raw);Sourcepub fn into_untyped(self) -> Statement
pub fn into_untyped(self) -> Statement
Consume this wrapper and return the underlying untyped statement.
This is the inverse of from_untyped_stmt.
§Examples
let raw: core_stmt::Statement = core_stmt::Query::unit().into();
let typed: Statement<()> = Statement::from_untyped_stmt(raw.clone());
let back: core_stmt::Statement = typed.into_untyped();
assert_eq!(raw, back);Sourcepub fn into_query(self) -> Option<Query<T>>
pub fn into_query(self) -> Option<Query<T>>
Try to extract the inner Query from this statement.
Returns Some(query) if the statement is a query, or None for
inserts, updates, and deletes.
§Examples
let query_stmt: Statement<List<()>> = Statement::from_untyped_stmt(
core_stmt::Query::unit().into(),
);
assert!(query_stmt.into_query().is_some());Source§impl<T> Statement<T>where
T: Load,
impl<T> Statement<T>where
T: Load,
Sourcepub async fn exec(
self,
executor: &mut dyn Executor,
) -> Result<<T as Load>::Output, Error>
pub async fn exec( self, executor: &mut dyn Executor, ) -> Result<<T as Load>::Output, Error>
Execute this statement against the given executor and return the deserialized result.
This is a convenience wrapper around
Executor::exec.
§Examples
use toasty::stmt::{IntoStatement, List, Query};
let stmt = Query::<List<User>>::all().into_statement();
let users: Vec<User> = stmt.exec(&mut db).await.unwrap();