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>
impl<'a> RawQuery<'a>
Sourcepub fn new(database: &'a Database, sql: impl Into<String>) -> Self
pub fn new(database: &'a Database, sql: impl Into<String>) -> Self
Start from sql. Prefer Database::query.
Sourcepub fn bind(self, value: impl Into<Value>) -> Self
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.
Sourcepub fn bind_all(
self,
values: impl IntoIterator<Item = impl Into<Value>>,
) -> Self
pub fn bind_all( self, values: impl IntoIterator<Item = impl Into<Value>>, ) -> Self
Bind several, in order.
Sourcepub fn route_by(self, key: impl Into<Value>) -> Self
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.
Sourcepub fn on_shard_key(self, key: u64) -> Self
pub fn on_shard_key(self, key: u64) -> Self
Send this to the shard holding key, already resolved.
Sourcepub fn raw_placeholders(self) -> Self
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.
Sourcepub fn prepared(self) -> Prepared
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.
Sourcepub async fn execute(self) -> Result<ExecOutcome>
pub async fn execute(self) -> Result<ExecOutcome>
Run it as a write, and report what happened.
Sourcepub async fn fetch(self, columns: Vec<ColumnRequest>) -> Result<Vec<OwnedRow>>
pub async fn fetch(self, columns: Vec<ColumnRequest>) -> Result<Vec<OwnedRow>>
Run it and read back the named columns.
Sourcepub async fn fetch_all<E: Entity>(self) -> Result<Vec<E>>
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.
Sourcepub async fn fetch_one<E: Entity>(self) -> Result<Option<E>>
pub async fn fetch_one<E: Entity>(self) -> Result<Option<E>>
Run it and decode the first row into E.
Sourcepub async fn scalar_i64(self, column: &str) -> Result<Option<i64>>
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.