Skip to main content

Module raw

Module raw 

Source
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 UPDATE or DELETE, and returns the rows affected. Use insert on Query for a plain insert, which also returns the id.
fetch_all
Runs a SELECT and returns every row, decoded as T. A tuple of column types or a struct that implements FromRow both work.
fetch_optional
Runs a SELECT and returns its first row, decoded as T, or None when no row matches.
fetch_scalar
Runs a SELECT that returns one column, such as SELECT COUNT(*), and returns its first value as T. It is an error when no row comes back.