Struct sqlx::query::QueryScalar

source ·
pub struct QueryScalar<'q, DB, O, A>
where DB: Database,
{ /* 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, O> QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>
where DB: Database,

source

pub fn bind<T>( self, value: T, ) -> QueryScalar<'q, DB, O, <DB as Database>::Arguments<'q>>
where T: 'q + Encode<'q, DB> + Type<DB>,

Bind a value for use with this SQL query.

See Query::bind.

source§

impl<'q, DB, O, A> QueryScalar<'q, DB, O, A>

source

pub fn persistent(self, value: bool) -> QueryScalar<'q, DB, O, A>

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>
where DB: Database, O: Send + Unpin, A: 'q + IntoArguments<'q, DB>, (O,): Send + Unpin + for<'r> FromRow<'r, <DB as Database>::Row>,

source

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, A: 'e, O: 'e,

Execute the query and return the generated results as a stream.

source

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, A: '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. 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.

source

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, (O,): 'e, A: 'e,

Execute the query and return all the resulting rows collected into a Vec.

§Note: beware result set size.

This will attempt to collect the full result set of the query into memory.

To avoid exhausting available memory, ensure the result set has a known upper bound, e.g. using LIMIT.

source

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, O: 'e, A: '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.

source

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, O: 'e, A: '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, O, A> Execute<'q, DB> for QueryScalar<'q, DB, O, A>
where DB: Database, O: Send, A: Send + 'q + IntoArguments<'q, DB>,

source§

fn sql(&self) -> &'q str

Gets the SQL that will be executed.
source§

fn statement(&self) -> Option<&<DB as Database>::Statement<'q>>

Gets the previously cached statement, if available.
source§

fn take_arguments( &mut self, ) -> Result<Option<<DB as Database>::Arguments<'q>>, Box<dyn Error + Sync + Send>>

Returns the arguments to be bound against the query string. Read more
source§

fn persistent(&self) -> bool

Returns 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>

§

impl<'q, DB, O, A> Send for QueryScalar<'q, DB, O, A>
where O: Send, A: Send,

§

impl<'q, DB, O, A> Sync for QueryScalar<'q, DB, O, A>
where DB: Sync, O: Sync, A: Sync,

§

impl<'q, DB, O, A> Unpin for QueryScalar<'q, DB, O, A>
where DB: Unpin, O: Unpin, A: Unpin,

§

impl<'q, DB, O, A> !UnwindSafe for QueryScalar<'q, DB, O, A>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more