Skip to main content

Module sql

Module sql 

Source
Expand description

The one place either driver is named, and the reason the SQL is written once.

Every statement in this crate is a runtime sqlx::query in a dialect both SQLite and PostgreSQL accept: epoch-second integers rather than dates, no strftime/julianday/CAST/||, ON CONFLICT … DO NOTHING rather than INSERT OR IGNORE, and RETURNING where a write has to read itself back. What is not shared is the parameter marker, the row type and the pool type, and this module is the whole of that seam: Query carries the SQL and its Values until an Exec says which driver is on the other end, Row hides which row came back, and Builder replaces sqlx::QueryBuilder for the paged listings.

§Placeholders

Statements are written with ?, as they always were, and to_dollar_placeholders rewrites them to $1…$n on the way to PostgreSQL. Writing $n in the source would have worked on both — sqlx’s SQLite driver parses a $N marker and binds argument N (sqlx-sqlite/src/arguments.rs) — but every number would then be a hand- maintained constant, and three things here build SQL by concatenation: the live_certificate! predicate spliced into the middle of three statements, the IN (?, ?, …) lists expanded per element, and the format!ed fragments in job::claim_next and job::settle. One rewrite at the edge is the same answer for all of them, and it cannot drift.

The rewrite deliberately skips '…' string literals. No statement in this crate holds a ? inside one today; a test pins that the rewriter would survive it if one arrived.

§Adding a bound type

Value has six variants because six are what the schema holds. A new one means a variant, a Bind impl and an arm in each of the two encoders — and, if it is read back, a Decode impl. Prefer reusing one: a JSON column is bound and read as Value::Text, and a status enum as its as_str.

Structs§

Builder
sqlx::QueryBuilder’s job, over Value rather than one driver.
Query
A statement and its parameters, before either driver has seen it.
QueryResult
What a write reports back. A thin wrapper so a call site reads the same whichever driver ran it.
Separated
A comma-separated run of bound values, handed out by Builder::separated.

Enums§

Dialect
Which dialect is on the other end of a connection.
Exec
Where a statement is about to run.
Idx
A column, named or positional.
NullKind
The type an absent value would have had. See Value::Null.
Row
One row, from whichever driver produced it.
Value
One bound parameter, in the only six shapes this schema stores.

Traits§

Bind
What Query::bind accepts.
Decode
What Row::try_get can read back.

Functions§

is_check_violation
Was this error a CHECK constraint refusing the write?
is_foreign_key_violation
Was this error a foreign key refusing the write? See is_check_violation.
is_unique_violation
Was this error any unique constraint refusing the write?
is_unique_violation_on
Was this error a unique-violation on one particular constraint?
query
Starts a statement. The SQL is written with ? markers.
to_dollar_placeholders
Rewrites ? markers to $1…$n, leaving '…' literals alone.