Expand description
Raw SQL, for statements that the query builder does not cover. Each function takes a
&Database or a &mut Transaction, as the query builder’s terminal methods do.
The SQL string is not escaped and is not checked. It must not contain user input. Put every
value in binds, in the order of the ? placeholders, and Orbit sends each one as a bind
parameter:
ⓘ
let title: String = orbit_orm::raw::fetch_scalar(
&db,
"SELECT title FROM posts WHERE slug = ?",
&[slug.into()],
)
.await?;Write ? on every database. On PostgreSQL Orbit numbers the placeholders ($1, $2, …)
itself, and a NULL bind is written into the SQL as NULL, so it needs no type. A ? inside
a quoted string is not a placeholder.
Functions§
- execute
- Runs a statement that changes rows, such as
UPDATEorDELETE, and returns the rows affected. UseinsertonQueryfor a plain insert, which also returns the id. - fetch_
all - Runs a
SELECTand returns every row, decoded asT. A tuple of column types or a struct that implementsFromRowboth work. - fetch_
optional - Runs a
SELECTand returns its first row, decoded asT, orNonewhen no row matches. - fetch_
scalar - Runs a
SELECTthat returns one column, such asSELECT COUNT(*), and returns its first value asT. It is an error when no row comes back.