Skip to main content

BuiltQuery

Struct BuiltQuery 

Source
#[non_exhaustive]
pub struct BuiltQuery { pub sql: String, pub params: Params, pub cacheable: bool, }
Expand description

Rendered SQL plus its bind parameters.

Builders expose convenience fetch_* methods that call build() each time. Keep a BuiltQuery when you want to inspect SQL, log the generated shape, or execute the same validated query more than once with the same binds.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§sql: String

Rendered Postgres SQL using $N placeholders.

§params: Params

Bind parameters in placeholder order.

§cacheable: bool

Whether this query is safe to reuse as a stable prepared statement shape.

rqb passes this to sqlx .persistent(cacheable). SQLx keeps a bounded per-connection prepared-statement cache for Postgres, with a default capacity of 100 entries. Raw SQL fragments make the query non-cacheable because rqb cannot prove that their text is a stable statement shape.

High-cardinality typed query shapes, such as many different IN list lengths or optional-filter combinations, stay memory-bounded by sqlx’s LRU cache but can still churn prepared statements. Tune sqlx with PgConnectOptions::statement_cache_capacity or the statement-cache-capacity URL parameter when an application needs a different cache size.

Implementations§

Source§

impl BuiltQuery

Source

pub fn arguments(&self) -> Result<PgArguments>

Converts stored parameters into sqlx Postgres arguments for one execution.

sqlx argument buffers are consumed by execution, so each execute/fetch path creates a fresh PgArguments value from the stored params.

Source

pub fn summary(&self) -> String

Returns debug-friendly query output.

The output includes pretty-printed rendered SQL, bind parameter count, Rust type names, and cacheability. It never interpolates bind values into SQL.

Source

pub fn pretty_sql(&self) -> String

Returns only the formatted SQL text for human-readable logs.

This does not change BuiltQuery::sql, which remains the exact SQL text used for execution.

Source§

impl BuiltQuery

Source

pub async fn execute<'e>(&self, executor: impl PgExecutor<'e>) -> Result<u64>

Executes the query and returns affected row count.

Source

pub async fn fetch_all<'e>( &self, executor: impl PgExecutor<'e>, ) -> Result<Vec<PgRow>>

Fetches all rows as raw sqlx PgRow values.

Source

pub fn fetch_stream<'q, 'e>( &'q self, executor: impl PgExecutor<'e> + 'q, ) -> Result<BoxStream<'q, Result<PgRow>>>
where 'e: 'q,

Streams raw sqlx PgRow values.

The returned stream borrows this BuiltQuery, so keep the built query value alive until the stream is fully consumed.

Source

pub fn fetch_stream_pool( self, pool: PgPool, ) -> Result<BoxStream<'static, Result<PgRow>>>

Streams raw sqlx PgRow values from an owned pool-backed query.

The returned stream owns this built query and a cloneable PgPool handle, so it can outlive the call frame that created it.

Source

pub async fn fetch_one<'e>( &self, executor: impl PgExecutor<'e>, ) -> Result<PgRow>

Fetches exactly one raw sqlx PgRow.

Source

pub async fn fetch_optional<'e>( &self, executor: impl PgExecutor<'e>, ) -> Result<Option<PgRow>>

Fetches zero or one raw sqlx PgRow.

Source

pub async fn fetch_all_as<'e, T>( &self, executor: impl PgExecutor<'e>, ) -> Result<Vec<T>>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin,

Fetches all rows into a sqlx::FromRow type.

Source

pub fn fetch_stream_as<'q, 'e, T>( &'q self, executor: impl PgExecutor<'e> + 'q, ) -> Result<BoxStream<'q, Result<T>>>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin + 'q, 'e: 'q,

Streams rows into a sqlx::FromRow type.

Streaming avoids materializing large exports in memory. Keep the BuiltQuery value alive while consuming the stream.

Source

pub fn fetch_stream_pool_as<T>( self, pool: PgPool, ) -> Result<BoxStream<'static, Result<T>>>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin + 'static,

Streams rows into a sqlx::FromRow type from an owned pool-backed query.

The returned stream owns this built query and a cloneable PgPool handle, so it can outlive the call frame that created it.

Source

pub async fn fetch_one_as<'e, T>( &self, executor: impl PgExecutor<'e>, ) -> Result<T>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin,

Fetches exactly one row into a sqlx::FromRow type.

Source

pub async fn fetch_optional_as<'e, T>( &self, executor: impl PgExecutor<'e>, ) -> Result<Option<T>>
where T: for<'r> FromRow<'r, PgRow> + Send + Unpin,

Fetches zero or one row into a sqlx::FromRow type.

Source

pub async fn fetch_scalar<'e, T>( &self, executor: impl PgExecutor<'e>, ) -> Result<Vec<T>>
where T: ScalarValue,

Fetches all rows as a single decoded scalar column.

Source

pub fn fetch_stream_scalar<'q, 'e, T>( &'q self, executor: impl PgExecutor<'e> + 'q, ) -> Result<BoxStream<'q, Result<T>>>
where T: ScalarValue + 'q, 'e: 'q,

Streams rows as a single decoded scalar column.

Keep the BuiltQuery value alive while consuming the stream.

Source

pub fn fetch_stream_pool_scalar<T>( self, pool: PgPool, ) -> Result<BoxStream<'static, Result<T>>>
where T: ScalarValue + 'static,

Streams rows as a single decoded scalar column from an owned pool-backed query.

The returned stream owns this built query and a cloneable PgPool handle, so it can outlive the call frame that created it.

Source

pub async fn fetch_one_scalar<'e, T>( &self, executor: impl PgExecutor<'e>, ) -> Result<T>
where T: ScalarValue,

Fetches exactly one decoded scalar value.

Source

pub async fn fetch_optional_scalar<'e, T>( &self, executor: impl PgExecutor<'e>, ) -> Result<Option<T>>
where T: ScalarValue,

Fetches zero or one decoded scalar value.

Trait Implementations§

Source§

impl Clone for BuiltQuery

Source§

fn clone(&self) -> BuiltQuery

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BuiltQuery

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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