Skip to main content

Module sql

Module sql 

Source
Expand description

Raw SQL execution helpers. Raw SQL execution helpers for SQL backends.

Use [statement] for SQL that returns an affected-row count and query for SQL that returns rows. Both builders execute through Executor, so they work with Db, Connection, and Transaction.

Raw SQL is backend SQL. Toasty does not rewrite placeholders, quote identifiers, or hydrate models from raw query results. Use the placeholder syntax of the active driver. The syntax is exposed at runtime through Capability::sql_placeholder:

BackendSqlPlaceholder
SQLiteNumberedQuestionMark (?1, ?2, …)
TursoNumberedQuestionMark (?1, ?2, …)
PostgreSQLDollarNumber ($1, $2, …)
MySQLQuestionMark (?, ?, …)

§Statements

ⓘ
let updated = toasty::sql::statement(
    "UPDATE users SET name = ?1 WHERE id = ?2",
)
.bind("Alice")
.bind(1_i64)
.exec(&mut db)
.await?;

assert_eq!(updated, 1);

§Queries

Queries return dynamic [Value] rows. Each row is a [Value::Record] with fields in selected-column order.

ⓘ
let rows = toasty::sql::query(
    "SELECT id, name FROM users WHERE active = ?1",
)
.bind(true)
.exec(&mut db)
.await?;

The driver infers result value types from database metadata by default. Use [Query::column_types] when a result column is ambiguous, such as a SQLite boolean stored as an integer or a UUID stored as bytes.

Structs§

Query
A raw SQL query that returns dynamic rows.
Statement
A raw SQL statement that returns an affected-row count.

Functions§

query
Create a raw SQL query.
statement
Create a raw SQL statement.