pub struct Query<'q, DB: Database, A> { /* private fields */ }Expand description
A single SQL query as a prepared statement. Returned by query().
Implementations§
Source§impl<DB: Database> Query<'_, DB, <DB as Database>::Arguments>
 
impl<DB: Database> Query<'_, DB, <DB as Database>::Arguments>
Sourcepub fn bind<'t, T: Encode<'t, DB> + Type<DB>>(self, value: T) -> Self
 
pub fn bind<'t, T: Encode<'t, DB> + Type<DB>>(self, value: T) -> Self
Bind a value for use with this SQL query.
If the number of times this is called does not match the number of bind parameters that
appear in the query (? for most SQL flavors, $1 .. $N for Postgres) then an error
will be returned when this query is executed.
There is no validation that the value is of the type expected by the query. Most SQL flavors will perform type coercion (Postgres will return a database error).
If encoding the value fails, the error is stored and later surfaced when executing the query.
Sourcepub fn try_bind<'t, T: Encode<'t, DB> + Type<DB>>(
    &mut self,
    value: T,
) -> Result<(), BoxDynError>
 
pub fn try_bind<'t, T: Encode<'t, DB> + Type<DB>>( &mut self, value: T, ) -> Result<(), BoxDynError>
Like Query::bind but immediately returns an error if encoding a value failed.
Source§impl<DB, A> Query<'_, DB, A>where
    DB: Database + HasStatementCache,
 
impl<DB, A> Query<'_, DB, A>where
    DB: Database + HasStatementCache,
Sourcepub fn persistent(self, value: bool) -> Self
 
pub fn persistent(self, value: bool) -> Self
If true, the statement will get prepared once and cached to the
connection’s statement cache.
If queried once with the flag set to true, all subsequent queries
matching the one with the flag will use the cached statement until the
cache is cleared.
If false, the prepared statement will be closed after execution.
Default: true.
Source§impl<'q, DB, A> Query<'q, DB, A>
 
impl<'q, DB, A> Query<'q, DB, A>
Sourcepub fn map<F, O>(
    self,
    f: F,
) -> Map<'q, DB, impl FnMut(DB::Row) -> Result<O, Error> + Send, A>
 
pub fn map<F, O>( self, f: F, ) -> Map<'q, DB, impl FnMut(DB::Row) -> Result<O, Error> + Send, A>
Sourcepub async fn execute<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<DB::QueryResult, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
 
pub async fn execute<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<DB::QueryResult, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
Execute the query and return the total number of rows affected.
Sourcepub async fn execute_many<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> BoxStream<'e, Result<DB::QueryResult, Error>>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
 👎Deprecated: Only the SQLite driver supports multiple statements in one prepared statement and that behavior is deprecated. Use sqlx::raw_sql() instead. See https://github.com/launchbadge/sqlx/issues/3108 for discussion.
pub async fn execute_many<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> BoxStream<'e, Result<DB::QueryResult, Error>>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
sqlx::raw_sql() instead. See https://github.com/launchbadge/sqlx/issues/3108 for discussion.Execute multiple queries and return the rows affected from each query, in a stream.
Sourcepub fn fetch<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> BoxStream<'e, Result<DB::Row, Error>>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
 
pub fn fetch<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> BoxStream<'e, Result<DB::Row, Error>>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
Execute the query and return the generated results as a stream.
Sourcepub fn fetch_many<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> BoxStream<'e, Result<Either<DB::QueryResult, DB::Row>, Error>>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
 👎Deprecated: Only the SQLite driver supports multiple statements in one prepared statement and that behavior is deprecated. Use sqlx::raw_sql() instead. See https://github.com/launchbadge/sqlx/issues/3108 for discussion.
pub fn fetch_many<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> BoxStream<'e, Result<Either<DB::QueryResult, DB::Row>, Error>>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
sqlx::raw_sql() instead. See https://github.com/launchbadge/sqlx/issues/3108 for discussion.Execute multiple queries and return the generated results as a stream.
For each query in the stream, any generated rows are returned first,
then the QueryResult with the number of rows affected.
Sourcepub async fn fetch_all<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<Vec<DB::Row>, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
 
pub async fn fetch_all<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<Vec<DB::Row>, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
Sourcepub async fn fetch_one<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<DB::Row, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
 
pub async fn fetch_one<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<DB::Row, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: '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.
Sourcepub async fn fetch_optional<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<Option<DB::Row>, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: 'e,
 
pub async fn fetch_optional<'e, 'c: 'e, E>(
    self,
    executor: E,
) -> Result<Option<DB::Row>, Error>where
    A: 'e,
    E: Executor<'c, Database = DB>,
    'q: '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§
Source§impl<'q, DB, A> Execute<'q, DB> for Query<'q, DB, A>
 
impl<'q, DB, A> Execute<'q, DB> for Query<'q, DB, A>
Source§fn statement(&self) -> Option<&DB::Statement>
 
fn statement(&self) -> Option<&DB::Statement>
Source§fn take_arguments(
    &mut self,
) -> Result<Option<<DB as Database>::Arguments>, BoxDynError>
 
fn take_arguments( &mut self, ) -> Result<Option<<DB as Database>::Arguments>, BoxDynError>
Source§fn persistent(&self) -> bool
 
fn persistent(&self) -> bool
true if the statement should be cached.Auto Trait Implementations§
impl<'q, DB, A> Freeze for Query<'q, DB, A>where
    A: Freeze,
impl<'q, DB, A> !RefUnwindSafe for Query<'q, DB, A>
impl<'q, DB, A> Send for Query<'q, DB, A>where
    A: Send,
impl<'q, DB, A> Sync for Query<'q, DB, A>
impl<'q, DB, A> Unpin for Query<'q, DB, A>
impl<'q, DB, A> !UnwindSafe for Query<'q, DB, 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
Source§impl<T> Instrument for T
 
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
 
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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