Struct sqlx_core::query_scalar::QueryScalar
source · pub struct QueryScalar<'q, DB: Database, O, A> { /* private fields */ }
Expand description
A single SQL query as a prepared statement which extracts only the first column of each row.
Returned by query_scalar()
.
Implementations§
source§impl<'q, DB: Database, O> QueryScalar<'q, DB, O, <DB as HasArguments<'q>>::Arguments>
impl<'q, DB: Database, O> QueryScalar<'q, DB, O, <DB as HasArguments<'q>>::Arguments>
source§impl<'q, DB, O, A> QueryScalar<'q, DB, O, A>where
DB: Database + HasStatementCache,
impl<'q, DB, O, A> QueryScalar<'q, DB, O, 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, O, A> QueryScalar<'q, DB, O, A>
impl<'q, DB, O, A> QueryScalar<'q, DB, O, A>
sourcepub fn fetch<'e, 'c: 'e, E>(
self,
executor: E,
) -> BoxStream<'e, Result<O, Error>>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
A: 'e,
O: 'e,
'q: 'e,
pub fn fetch<'e, 'c: 'e, E>(
self,
executor: E,
) -> BoxStream<'e, Result<O, Error>>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
A: 'e,
O: 'e,
'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, O>, Error>>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
A: 'e,
O: 'e,
'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, O>, Error>>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
A: 'e,
O: 'e,
'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 from each query, in a stream.
sourcepub async fn fetch_one<'e, 'c: 'e, E>(self, executor: E) -> Result<O, Error>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
O: 'e,
A: 'e,
'q: 'e,
pub async fn fetch_one<'e, 'c: 'e, E>(self, executor: E) -> Result<O, Error>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
O: 'e,
A: 'e,
'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<O>, Error>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
O: 'e,
A: 'e,
'q: 'e,
pub async fn fetch_optional<'e, 'c: 'e, E>(
self,
executor: E,
) -> Result<Option<O>, Error>where
E: 'e + Executor<'c, Database = DB>,
DB: 'e,
O: 'e,
A: 'e,
'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: Database, O: Send, A> Execute<'q, DB> for QueryScalar<'q, DB, O, A>where
A: 'q + IntoArguments<'q, DB> + Send,
impl<'q, DB: Database, O: Send, A> Execute<'q, DB> for QueryScalar<'q, DB, O, A>where
A: 'q + IntoArguments<'q, DB> + Send,
source§fn statement(&self) -> Option<&<DB as HasStatement<'q>>::Statement>
fn statement(&self) -> Option<&<DB as HasStatement<'q>>::Statement>
source§fn take_arguments(&mut self) -> Option<<DB as HasArguments<'q>>::Arguments>
fn take_arguments(&mut self) -> Option<<DB as HasArguments<'q>>::Arguments>
source§fn persistent(&self) -> bool
fn persistent(&self) -> bool
true
if the statement should be cached.Auto Trait Implementations§
impl<'q, DB, O, A> Freeze for QueryScalar<'q, DB, O, A>where
A: Freeze,
impl<'q, DB, O, A> RefUnwindSafe for QueryScalar<'q, DB, O, A>where
A: RefUnwindSafe,
DB: RefUnwindSafe,
O: RefUnwindSafe,
<DB as HasStatement<'q>>::Statement: RefUnwindSafe,
impl<'q, DB, O, A> Send for QueryScalar<'q, DB, O, A>
impl<'q, DB, O, A> Sync for QueryScalar<'q, DB, O, A>
impl<'q, DB, O, A> Unpin for QueryScalar<'q, DB, O, A>
impl<'q, DB, O, A> UnwindSafe for QueryScalar<'q, DB, O, A>where
A: UnwindSafe,
DB: UnwindSafe,
O: UnwindSafe,
<DB as HasStatement<'q>>::Statement: RefUnwindSafe,
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