Skip to main content

RawQuery

Struct RawQuery 

Source
pub struct RawQuery<'a> { /* private fields */ }
Expand description

A statement being built: the SQL, its bindings, and where to run it.

Nothing happens until one of the terminal methods is awaited.

Implementations§

Source§

impl<'a> RawQuery<'a>

Source

pub fn new(database: &'a Database, sql: impl Into<String>) -> Self

Start from sql. Prefer Database::query.

Source

pub fn bind(self, value: impl Into<Value>) -> Self

Bind the next ?.

Order matters and nothing checks the count until the driver does — an over- or under-bound statement is an error from the database, not from here, because only it knows how many placeholders it found.

Source

pub fn bind_all( self, values: impl IntoIterator<Item = impl Into<Value>>, ) -> Self

Bind several, in order.

Source

pub fn route_by(self, key: impl Into<Value>) -> Self

Send this to the shard that owns key.

Only meaningful on a sharded connection; ignored by everything else. Without it a query goes to the global route, which for a sharded deployment means “the one that is not shard-specific” — so a query touching a sharded table must say which shard, or it will look in the wrong place and quietly find nothing.

Takes the same values the ORM routes by: a shard-encoded id as-is, a string key hashed the same stable way, so the same key lands on the same shard from any process.

Source

pub fn on_shard_key(self, key: u64) -> Self

Send this to the shard holding key, already resolved.

Source

pub fn raw_placeholders(self) -> Self

Leave the placeholders exactly as written.

For Postgres’s JSON ? operators, or for SQL that is already written in $n form. The statement is then dialect-specific, which is the trade.

Source

pub fn prepared(self) -> Prepared

The statement as it will be sent — SQL, bindings and route.

For logging it, asserting on it in a test, or handing it to Database::fetch directly.

Source

pub async fn execute(self) -> Result<ExecOutcome>

Run it as a write, and report what happened.

Source

pub async fn fetch(self, columns: Vec<ColumnRequest>) -> Result<Vec<OwnedRow>>

Run it and read back the named columns.

Source

pub async fn fetch_all<E: Entity>(self) -> Result<Vec<E>>

Run it and decode every row into E.

The statement has to return E’s columns by their own names — SELECT *, or an explicit list with matching aliases.

Source

pub async fn fetch_one<E: Entity>(self) -> Result<Option<E>>

Run it and decode the first row into E.

Source

pub async fn scalar_i64(self, column: &str) -> Result<Option<i64>>

Read one integer out of the first row — a COUNT, a SUM, a MAX.

None when there was no row, or the value was NULL. Those are different from 0, which is why this does not flatten them: SUM over no rows is NULL, not zero, and rounding that to zero is how a total silently becomes wrong.

Source

pub async fn scalar_string(self, column: &str) -> Result<Option<String>>

Read one text column out of the first row.

Source

pub async fn column(self, column: &str) -> Result<Vec<String>>

Read one text column out of every row.

A NULL is skipped rather than becoming an empty string — the two mean different things and only one of them is a name.

Trait Implementations§

Source§

impl<'a> Debug for RawQuery<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for RawQuery<'a>

§

impl<'a> !UnwindSafe for RawQuery<'a>

§

impl<'a> Freeze for RawQuery<'a>

§

impl<'a> Send for RawQuery<'a>

§

impl<'a> Sync for RawQuery<'a>

§

impl<'a> Unpin for RawQuery<'a>

§

impl<'a> UnsafeUnpin for RawQuery<'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> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
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, 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<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